use of dev.morphia.test.models.Circle in project morphia by mongodb.
the class TestUpdateOperations method testExistingUpdates.
@Test
public void testExistingUpdates() {
getDs().save(new Circle(100D));
getDs().save(new Circle(12D));
Query<Circle> circle = getDs().find(Circle.class);
assertUpdated(circle.update(inc("radius", 1D)).execute(), 1);
assertUpdated(circle.update(inc("radius")).execute(new UpdateOptions().multi(true)), 2);
// test possible data type change.
final Circle updatedCircle = circle.filter(eq("radius", 13)).iterator(new FindOptions().limit(1)).next();
assertThat(updatedCircle, is(notNullValue()));
MatcherAssert.assertThat(updatedCircle.getRadius(), is(13D));
}
use of dev.morphia.test.models.Circle in project morphia by mongodb.
the class TestUpdateOperations method testMaxKeepsCurrentDocumentValueWhenThisIsLargerThanSuppliedValue.
@Test
public void testMaxKeepsCurrentDocumentValueWhenThisIsLargerThanSuppliedValue() {
final ObjectId id = new ObjectId();
final double originalValue = 2D;
Datastore ds = getDs();
Query<Circle> query = ds.find(Circle.class).filter(eq("id", id));
assertInserted(query.update(setOnInsert(Map.of("radius", originalValue))).execute(new UpdateOptions().upsert(true)));
Assert.assertEquals(query.update(max("radius", 1D)).execute(new UpdateOptions().upsert(true)).getMatchedCount(), 1);
MatcherAssert.assertThat(ds.find(Circle.class).filter(eq("_id", id)).first().getRadius(), is(originalValue));
}
use of dev.morphia.test.models.Circle in project morphia by mongodb.
the class TestUpdateOperations method testSetOnInsertWhenUpdating.
@Test
public void testSetOnInsertWhenUpdating() {
ObjectId id = new ObjectId();
Query<Circle> query = getDs().find(Circle.class).filter(eq("id", id));
assertInserted(query.update(setOnInsert(Map.of("radius", 1D))).execute(new UpdateOptions().upsert(true)));
Assert.assertEquals(query.update(setOnInsert(Map.of("radius", 2D))).execute(new UpdateOptions().upsert(true)).getMatchedCount(), 1);
final Circle updatedCircle = getDs().find(Circle.class).filter(eq("_id", id)).first();
assertNotNull(updatedCircle);
Assert.assertEquals(updatedCircle.getRadius(), 1D, 0.1);
}
use of dev.morphia.test.models.Circle in project morphia by mongodb.
the class TestUpdateOperations method testSetOnInsertWhenInserting.
@Test
public void testSetOnInsertWhenInserting() {
ObjectId id = new ObjectId();
Query<Circle> query = getDs().find(Circle.class).filter(eq("id", id));
assertInserted(query.update(setOnInsert(Map.of("radius", 2D))).execute(new UpdateOptions().upsert(true)));
final Circle updatedCircle = getDs().find(Circle.class).filter(eq("_id", id)).first();
assertThat(updatedCircle, is(notNullValue()));
MatcherAssert.assertThat(updatedCircle.getRadius(), is(2D));
}
use of dev.morphia.test.models.Circle in project morphia by mongodb.
the class TestUpdateOperations method testSetUnset.
@Test
public void testSetUnset() {
Datastore ds = getDs();
final ObjectId key = ds.save(new Circle(1)).getId();
Query<Circle> circle = ds.find(Circle.class).filter(eq("radius", 1D));
assertUpdated(circle.update(set("radius", 2D)).execute(), 1);
Query<Circle> idQuery = ds.find(Circle.class).filter(eq("_id", key));
MatcherAssert.assertThat(idQuery.first().getRadius(), is(2D));
circle = ds.find(Circle.class).filter(eq("radius", 2D));
assertUpdated(circle.update(unset("radius")).execute(new UpdateOptions().multi(false)), 1);
MatcherAssert.assertThat(idQuery.first().getRadius(), is(0D));
Book article = new Book();
ds.save(article);
Query<Book> query = ds.find(Book.class);
query.update(set("title", "Some Title")).execute();
query.update(unset("title")).execute();
}
Aggregations