Search in sources :

Example 21 with Rectangle

use of dev.morphia.test.models.Rectangle in project morphia by mongodb.

the class TestLegacyQuery method testCombinationQuery.

@Test
public void testCombinationQuery() {
    getDs().save(asList(new Rectangle(1, 10), new Rectangle(4, 2), new Rectangle(6, 10), new Rectangle(8, 5), new Rectangle(10, 4)));
    Query<Rectangle> q = getDs().find(Rectangle.class);
    q.and(q.criteria("width").equal(10), q.criteria("height").equal(1));
    List<Rectangle> list = q.execute(new FindOptions().logQuery()).toList();
    assertEquals(q.count(), 1);
    q = getDs().find(Rectangle.class);
    q.or(q.criteria("width").equal(10), q.criteria("height").equal(10));
    assertEquals(q.count(), 3);
    q = getDs().find(Rectangle.class);
    q.or(q.criteria("width").equal(10), q.and(q.criteria("width").equal(5), q.criteria("height").equal(8)));
    q.execute(new FindOptions().logQuery()).toList();
    assertEquals(q.count(), 3, q.getLoggedQuery());
}
Also used : FindOptions(dev.morphia.query.FindOptions) Rectangle(dev.morphia.test.models.Rectangle) Test(org.testng.annotations.Test)

Example 22 with Rectangle

use of dev.morphia.test.models.Rectangle in project morphia by mongodb.

the class TestLegacyQuery method testCompoundSort.

@Test
public void testCompoundSort() {
    getDs().save(asList(new Rectangle(1, 10), new Rectangle(3, 8), new Rectangle(6, 10), new Rectangle(10, 10), new Rectangle(10, 1)));
    Rectangle r1 = getDs().find(Rectangle.class).execute(new FindOptions().sort(ascending("width"), descending("height")).limit(1)).tryNext();
    assertNotNull(r1);
    assertEquals(r1.getWidth(), 1, 0);
    assertEquals(r1.getHeight(), 10, 0);
    r1 = getDs().find(Rectangle.class).execute(new FindOptions().sort(descending("height"), descending("width")).limit(1)).tryNext();
    assertNotNull(r1);
    assertEquals(r1.getWidth(), 10, 0);
    assertEquals(r1.getHeight(), 10, 0);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Rectangle(dev.morphia.test.models.Rectangle) Test(org.testng.annotations.Test)

Example 23 with Rectangle

use of dev.morphia.test.models.Rectangle in project morphia by mongodb.

the class TestQuery method testKeyList.

@Test
public void testKeyList() {
    final Rectangle rect = new Rectangle(1000, 1);
    Rectangle rectangle = getDs().save(rect);
    assertEquals(rectangle.getId(), rect.getId());
    final FacebookUser fbUser1 = new FacebookUser(1, "scott");
    final FacebookUser fbUser2 = new FacebookUser(2, "tom");
    final FacebookUser fbUser3 = new FacebookUser(3, "oli");
    final FacebookUser fbUser4 = new FacebookUser(4, "frank");
    final List<FacebookUser> users = getDs().save(asList(fbUser1, fbUser2, fbUser3, fbUser4));
    assertEquals(fbUser1.getId(), 1);
    final List<Key<FacebookUser>> fbUserKeys = new ArrayList<>();
    for (FacebookUser user : users) {
        fbUserKeys.add(getMapper().getKey(user));
    }
    assertEquals(fbUserKeys.get(0).getId(), fbUser1.getId());
    assertEquals(fbUserKeys.get(1).getId(), fbUser2.getId());
    assertEquals(fbUserKeys.get(2).getId(), fbUser3.getId());
    assertEquals(fbUserKeys.get(3).getId(), fbUser4.getId());
    final Keys k1 = new Keys(getMapper().getKey(rectangle), fbUserKeys);
    final Keys keys = getDs().save(k1);
    assertEquals(k1.getId(), keys.getId());
    final Datastore datastore = getDs();
    final Keys k1Loaded = datastore.find(Keys.class).filter(eq("_id", k1.getId())).first();
    for (Key<FacebookUser> key : k1Loaded.getUsers()) {
        assertNotNull(key.getId());
    }
    assertNotNull(k1Loaded.getRect().getId());
}
Also used : Datastore(dev.morphia.Datastore) Keys(dev.morphia.test.models.Keys) Rectangle(dev.morphia.test.models.Rectangle) ArrayList(java.util.ArrayList) Key(dev.morphia.Key) FacebookUser(dev.morphia.test.models.FacebookUser) Test(org.testng.annotations.Test)

Example 24 with Rectangle

use of dev.morphia.test.models.Rectangle in project morphia by mongodb.

the class TestQuery method testNaturalSortDescending.

@Test
public void testNaturalSortDescending() {
    getDs().save(asList(new Rectangle(6, 10), new Rectangle(3, 8), new Rectangle(10, 10), new Rectangle(10, 1)));
    List<Rectangle> results = getDs().find(Rectangle.class).iterator(new FindOptions().sort(naturalDescending())).toList();
    assertEquals(results.size(), 4);
    Rectangle r;
    r = results.get(0);
    assertNotNull(r);
    assertEquals(r.getHeight(), 10, 0);
    assertEquals(r.getWidth(), 1, 0);
    r = results.get(1);
    assertNotNull(r);
    assertEquals(r.getHeight(), 10, 0);
    assertEquals(r.getWidth(), 10, 0);
    r = results.get(2);
    assertNotNull(r);
    assertEquals(r.getHeight(), 3, 0);
    assertEquals(r.getWidth(), 8, 0);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Rectangle(dev.morphia.test.models.Rectangle) Test(org.testng.annotations.Test)

Example 25 with Rectangle

use of dev.morphia.test.models.Rectangle in project morphia by mongodb.

the class TestQuery method testCompoundSort.

@Test
public void testCompoundSort() {
    getDs().save(asList(new Rectangle(1, 10), new Rectangle(3, 8), new Rectangle(6, 10), new Rectangle(10, 10), new Rectangle(10, 1)));
    Rectangle r1 = getDs().find(Rectangle.class).iterator(new FindOptions().sort(ascending("width"), descending("height")).limit(1)).tryNext();
    assertNotNull(r1);
    assertEquals(r1.getWidth(), 1, 0);
    assertEquals(r1.getHeight(), 10, 0);
    r1 = getDs().find(Rectangle.class).iterator(new FindOptions().sort(descending("height"), descending("width")).limit(1)).tryNext();
    assertNotNull(r1);
    assertEquals(r1.getWidth(), 10, 0);
    assertEquals(r1.getHeight(), 10, 0);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Rectangle(dev.morphia.test.models.Rectangle) Test(org.testng.annotations.Test)

Aggregations

Rectangle (dev.morphia.test.models.Rectangle)26 Test (org.testng.annotations.Test)25 FindOptions (dev.morphia.query.FindOptions)12 Datastore (dev.morphia.Datastore)2 Key (dev.morphia.Key)2 User (dev.morphia.test.models.User)2 ArrayList (java.util.ArrayList)2 MongoQueryException (com.mongodb.MongoQueryException)1 UpdateResult (com.mongodb.client.result.UpdateResult)1 UpdateOptions (dev.morphia.UpdateOptions)1 MorphiaSession (dev.morphia.experimental.MorphiaSession)1 FacebookUser (dev.morphia.test.models.FacebookUser)1 Keys (dev.morphia.test.models.Keys)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BeforeMethod (org.testng.annotations.BeforeMethod)1