use of dev.morphia.test.models.Rectangle in project morphia by mongodb.
the class TestTransactions method save.
@Test
public void save() {
Rectangle rectangle = new Rectangle(1, 1);
getDs().withTransaction((session) -> {
session.save(rectangle);
assertNull(getDs().find(Rectangle.class).first());
assertNotNull(session.find(Rectangle.class).first());
rectangle.setWidth(42);
session.save(rectangle);
assertNull(getDs().find(Rectangle.class).first());
assertEquals(session.find(Rectangle.class).first().getWidth(), 42, 0.5);
return null;
});
assertNotNull(getDs().find(Rectangle.class).first());
}
use of dev.morphia.test.models.Rectangle in project morphia by mongodb.
the class TestTransactions method merge.
@Test
public void merge() {
Rectangle rectangle = new Rectangle(1, 1);
getDs().save(rectangle);
assertEquals(getDs().find(Rectangle.class).count(), 1);
getDs().withTransaction((session) -> {
assertEquals(getDs().find(Rectangle.class).first(), new Rectangle(1, 1));
assertEquals(session.find(Rectangle.class).first(), new Rectangle(1, 1));
rectangle.setWidth(20);
session.merge(rectangle);
assertEquals(getDs().find(Rectangle.class).first().getWidth(), 1, 0.5);
assertEquals(session.find(Rectangle.class).first().getWidth(), 20, 0.5);
return null;
});
assertEquals(getDs().find(Rectangle.class).first().getWidth(), 20, 0.5);
}
use of dev.morphia.test.models.Rectangle in project morphia by mongodb.
the class TestTransactions method insertList.
@Test
public void insertList() {
List<Rectangle> rectangles = List.of(new Rectangle(5, 7), new Rectangle(1, 1));
getDs().withTransaction((session) -> {
session.insert(rectangles);
assertNull(getDs().find(Rectangle.class).first());
assertEquals(session.find(Rectangle.class).iterator().toList(), rectangles);
return null;
});
assertEquals(getDs().find(Rectangle.class).count(), 2);
}
use of dev.morphia.test.models.Rectangle in project morphia by mongodb.
the class TestTransactions method remove.
@Test
public void remove() {
Rectangle rectangle = new Rectangle(1, 1);
getDs().save(rectangle);
getDs().withTransaction((session) -> {
assertNotNull(getDs().find(Rectangle.class).first());
assertNotNull(session.find(Rectangle.class).first());
session.find(Rectangle.class).delete();
assertNotNull(getDs().find(Rectangle.class).first());
assertNull(session.find(Rectangle.class).first());
return null;
});
assertNull(getDs().find(Rectangle.class).first());
}
use of dev.morphia.test.models.Rectangle in project morphia by mongodb.
the class TestQuery method testAliasedFieldSort.
@Test
public void testAliasedFieldSort() {
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("w")).limit(1)).tryNext();
assertNotNull(r1);
assertEquals(r1.getWidth(), 1, 0);
r1 = getDs().find(Rectangle.class).iterator(new FindOptions().sort(descending("w")).limit(1)).tryNext();
assertNotNull(r1);
assertEquals(r1.getWidth(), 10, 0);
}
Aggregations