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);
});
}
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);
}
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"));
}
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);
}
}
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");
}
Aggregations