Search in sources :

Example 1 with Circle

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));
}
Also used : Circle(dev.morphia.test.models.Circle) FindOptions(dev.morphia.query.FindOptions) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 2 with Circle

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));
}
Also used : Circle(dev.morphia.test.models.Circle) Datastore(dev.morphia.Datastore) ObjectId(org.bson.types.ObjectId) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 3 with Circle

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);
}
Also used : Circle(dev.morphia.test.models.Circle) ObjectId(org.bson.types.ObjectId) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 4 with Circle

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));
}
Also used : Circle(dev.morphia.test.models.Circle) ObjectId(org.bson.types.ObjectId) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 5 with Circle

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();
}
Also used : Circle(dev.morphia.test.models.Circle) Datastore(dev.morphia.Datastore) ObjectId(org.bson.types.ObjectId) Book(dev.morphia.test.models.Book) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Aggregations

UpdateOptions (dev.morphia.UpdateOptions)5 Circle (dev.morphia.test.models.Circle)5 Test (org.testng.annotations.Test)5 ObjectId (org.bson.types.ObjectId)4 Datastore (dev.morphia.Datastore)2 FindOptions (dev.morphia.query.FindOptions)1 Book (dev.morphia.test.models.Book)1