querydsl-mongodb: add failing unit tests to illustrate issue

New junits on polymorphic collection; "as" method not working as expected
This commit is contained in:
Joel Takvorian 2015-07-03 17:29:30 +02:00
parent d17b422212
commit a187caa58e
4 changed files with 178 additions and 0 deletions

View File

@ -0,0 +1,89 @@
package com.querydsl.mongodb;
import static org.junit.Assert.assertEquals;
import java.net.UnknownHostException;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.querydsl.core.testutil.ExternalDB;
import com.querydsl.core.types.Predicate;
import com.querydsl.mongodb.domain.Fish;
import com.querydsl.mongodb.domain.Food;
import com.querydsl.mongodb.domain.Chips;
import com.querydsl.mongodb.domain.QFish;
import com.querydsl.mongodb.domain.QFood;
import com.querydsl.mongodb.morphia.MorphiaQuery;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
@Category(ExternalDB.class)
public class PolymorphicCollectionTest {
private final Morphia morphia;
private final Datastore ds;
private final Fish f1 = new Fish("f1");
private final Fish f2 = new Fish("f2");
private final Chips c1 = new Chips("c1");
public PolymorphicCollectionTest() throws UnknownHostException, MongoException {
final Mongo mongo = new Mongo();
morphia = new Morphia().map(Food.class);
ds = morphia.createDatastore(mongo, "testdb");
}
@Before
public void before() throws UnknownHostException, MongoException {
ds.delete(ds.createQuery(Food.class));
ds.save(f1, f2, c1);
}
@Test
public void basicCount() {
assertEquals(where().fetchCount(), 3);
}
@Test
public void countFishFromName() {
assertEquals(where(QFood.food.name.eq("f1")).fetchCount(), 1);
}
@Test
public void countFishFromNameAndBreed() {
assertEquals(where(QFood.food.name.eq("f1")
.and(QFish.fish.breed.eq("unknown"))).fetchCount(), 1);
}
@Test
public void countFishFromNameAndBreedWithCast() {
// Fails because generated query is { "name" : "f1" , "food.breed" : "unknown"}
// instead of { "name" : "f1" , "breed" : "unknown"}
assertEquals(where(QFood.food.name.eq("f1")
.and(QFood.food.as(QFish.class).breed.eq("unknown"))).fetchCount(), 1);
}
@Test
public void countFishes() {
assertEquals(where(isFish()).fetchCount(), 2);
}
@Test
public void countFishesWithInstanceOf() {
assertEquals(where(QFood.food.instanceOf(Fish.class)).fetchCount(), 2);
}
private Predicate isFish() {
return QFood.food.name.startsWith("f");
}
private MorphiaQuery<Food> query() {
return new MorphiaQuery<Food>(morphia, ds, QFood.food);
}
private MorphiaQuery<Food> where(final Predicate... e) {
return query().where(e);
}
}

View File

@ -0,0 +1,24 @@
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.querydsl.mongodb.domain;
import org.mongodb.morphia.annotations.Entity;
@Entity("food")
public class Chips extends Food {
public Chips(final String name) {
super(name);
}
}

View File

@ -0,0 +1,36 @@
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.querydsl.mongodb.domain;
import org.mongodb.morphia.annotations.Entity;
@Entity("food")
public class Fish extends Food {
private final String breed;
public Fish(final String name) {
super(name);
this.breed = "unknown";
}
public Fish(final String name, final String breed) {
super(name);
this.breed = breed;
}
public String getBreed() {
return breed;
}
}

View File

@ -0,0 +1,29 @@
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.querydsl.mongodb.domain;
import org.mongodb.morphia.annotations.Entity;
@Entity("food")
public class Food extends AbstractEntity {
private final String name;
public Food(final String name) {
this.name = name;
}
public String getName() {
return name;
}
}