Search in sources :

Example 1 with DeleteOptions

use of dev.morphia.DeleteOptions in project morphia by mongodb.

the class TestVersioning method testThrowsExceptionWhenTryingToSaveAnOldVersion.

@Test
public void testThrowsExceptionWhenTryingToSaveAnOldVersion() {
    assertThrows(VersionMismatchException.class, () -> {
        getDs().find(Versioned.class).delete(new DeleteOptions().multi(true));
        // given
        final Versioned version1 = new Versioned();
        getDs().save(version1);
        final Versioned version2 = getDs().find(Versioned.class).filter(eq("_id", version1.getId())).first();
        getDs().save(version2);
        // when
        getDs().save(version1);
    });
}
Also used : DeleteOptions(dev.morphia.DeleteOptions) Versioned(dev.morphia.test.models.versioned.Versioned) Test(org.testng.annotations.Test)

Example 2 with DeleteOptions

use of dev.morphia.DeleteOptions in project morphia by mongodb.

the class TestQuery method testNegativeBatchSize.

@Test
public void testNegativeBatchSize() {
    getDs().find(PhotoWithKeywords.class).delete(new DeleteOptions().multi(true));
    getDs().save(asList(new PhotoWithKeywords("scott", "hernandez"), new PhotoWithKeywords("scott", "hernandez"), new PhotoWithKeywords("scott", "hernandez"), new PhotoWithKeywords("1", "2"), new PhotoWithKeywords("3", "4"), new PhotoWithKeywords("5", "6")));
    assertEquals(getDs().find(PhotoWithKeywords.class).iterator(new FindOptions().batchSize(-2)).toList().size(), 2);
}
Also used : FindOptions(dev.morphia.query.FindOptions) DeleteOptions(dev.morphia.DeleteOptions) Test(org.testng.annotations.Test)

Example 3 with DeleteOptions

use of dev.morphia.DeleteOptions in project morphia by mongodb.

the class TestUpdateOperations method testInsertWithRef.

@Test
public void testInsertWithRef() {
    final Pic pic = new Pic();
    pic.setName("fist");
    final ObjectId picKey = getDs().save(pic).getId();
    Query<ContainsPic> query = getDs().find(ContainsPic.class).filter(eq("name", "first"), eq("pic", picKey));
    assertInserted(query.update(set("name", "A")).execute(new UpdateOptions().upsert(true)));
    MatcherAssert.assertThat(getDs().find(ContainsPic.class).count(), is(1L));
    getDs().find(ContainsPic.class).delete(new DeleteOptions().multi(true));
    query = getDs().find(ContainsPic.class).filter(eq("name", "first"), eq("pic", pic));
    assertInserted(query.update(set("name", "second")).execute(new UpdateOptions().upsert(true)));
    MatcherAssert.assertThat(getDs().find(ContainsPic.class).count(), is(1L));
    // test reading the object.
    final ContainsPic cp = getDs().find(ContainsPic.class).iterator(new FindOptions().limit(1)).next();
    assertThat(cp, is(notNullValue()));
    MatcherAssert.assertThat(cp.getName(), is("second"));
    MatcherAssert.assertThat(cp.getPic(), is(notNullValue()));
    MatcherAssert.assertThat(cp.getPic().getName(), is(notNullValue()));
    MatcherAssert.assertThat(cp.getPic().getName(), is("fist"));
}
Also used : FindOptions(dev.morphia.query.FindOptions) DeleteOptions(dev.morphia.DeleteOptions) ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic) ObjectId(org.bson.types.ObjectId) ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic) Pic(dev.morphia.test.query.TestQuery.Pic) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 4 with DeleteOptions

use of dev.morphia.DeleteOptions in project morphia by mongodb.

the class TestUpdateOperations method testUpdateFirstNoCreate.

@Test
public void testUpdateFirstNoCreate() {
    getDs().find(LogHolder.class).delete(new DeleteOptions().multi(true));
    List<LogHolder> logs = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        logs.add(createEntryLogs("logs" + i));
    }
    LogHolder logs1 = logs.get(0);
    Query<LogHolder> query = getDs().find(LogHolder.class);
    Document object = new Document("new", "value");
    query.update(set("raw", object)).execute();
    List<LogHolder> list = getDs().find(LogHolder.class).iterator().toList();
    for (int i = 0; i < list.size(); i++) {
        final LogHolder logHolder = list.get(i);
        Assert.assertEquals(logHolder.id.equals(logs1.id) ? object : logs.get(i).raw, logHolder.raw);
    }
}
Also used : DeleteOptions(dev.morphia.DeleteOptions) ArrayList(java.util.ArrayList) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 5 with DeleteOptions

use of dev.morphia.DeleteOptions in project morphia by mongodb.

the class TestDatastore method testDeletes.

@Test
public void testDeletes() {
    for (int i = 0; i < 100; i++) {
        getDs().save(new City());
    }
    DeleteResult delete = getDs().find(City.class).delete();
    assertEquals(delete.getDeletedCount(), 1, "Should only delete 1");
    City first = getDs().find(City.class).first();
    delete = getDs().delete(first);
    assertEquals(delete.getDeletedCount(), 1, "Should only delete 1");
    first = getDs().find(City.class).first();
    delete = getDs().delete(first, new DeleteOptions().multi(true));
    assertEquals(delete.getDeletedCount(), 1, "Should only delete 1");
    delete = getDs().find(City.class).delete(new DeleteOptions().multi(true));
    assertTrue(delete.getDeletedCount() > 1, "Should the rest");
}
Also used : DeleteOptions(dev.morphia.DeleteOptions) FindAndDeleteOptions(dev.morphia.query.FindAndDeleteOptions) City(dev.morphia.test.models.City) DeleteResult(com.mongodb.client.result.DeleteResult) Test(org.testng.annotations.Test)

Aggregations

DeleteOptions (dev.morphia.DeleteOptions)6 Test (org.testng.annotations.Test)6 FindOptions (dev.morphia.query.FindOptions)3 UpdateOptions (dev.morphia.UpdateOptions)2 Versioned (dev.morphia.test.models.versioned.Versioned)2 DeleteResult (com.mongodb.client.result.DeleteResult)1 Datastore (dev.morphia.Datastore)1 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)1 FindAndDeleteOptions (dev.morphia.query.FindAndDeleteOptions)1 City (dev.morphia.test.models.City)1 ContainsPic (dev.morphia.test.query.TestQuery.ContainsPic)1 Pic (dev.morphia.test.query.TestQuery.Pic)1 ArrayList (java.util.ArrayList)1 Document (org.bson.Document)1 ObjectId (org.bson.types.ObjectId)1