use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestMapping method testEmbeddedDocument.
@Test
public void testEmbeddedDocument() {
getMapper().map(ContainsDocument.class);
getDs().save(new ContainsDocument());
assertNotNull(getDs().find(ContainsDocument.class).iterator(new FindOptions().limit(1)).next());
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestDatastore method testCappedEntity.
@Test
public void testCappedEntity() {
// given
getMapper().map(CurrentStatus.class);
getDs().ensureCaps();
assertCapped(CurrentStatus.class, 1, 1048576);
// when-then
Query<CurrentStatus> query = getDs().find(CurrentStatus.class);
getDs().save(new CurrentStatus("All Good"));
assertEquals(query.count(), 1);
getDs().save(new CurrentStatus("Kinda Bad"));
assertEquals(query.count(), 1);
assertTrue(query.iterator(new FindOptions().limit(1)).next().message.contains("Bad"));
getDs().save(new CurrentStatus("Kinda Bad2"));
assertEquals(query.count(), 1);
getDs().save(new CurrentStatus("Kinda Bad3"));
assertEquals(query.count(), 1);
getDs().save(new CurrentStatus("Kinda Bad4"));
assertEquals(query.count(), 1);
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestDocumentValidation method save.
@Test
public void save() {
getMapper().map(DocumentValidation.class);
getDs().enableDocumentValidation();
try {
getDs().save(new DocumentValidation("Harold", 8, new Date()));
fail("Document validation should have complained");
} catch (MongoWriteException e) {
// expected
}
getDs().save(new DocumentValidation("Harold", 8, new Date()), new InsertOneOptions().bypassDocumentValidation(true));
Query<DocumentValidation> query = getDs().find(DocumentValidation.class).filter(eq("number", 8));
Assert.assertNotNull(query.iterator(new FindOptions().limit(1)).tryNext());
List<DocumentValidation> list = asList(new DocumentValidation("Harold", 8, new Date()), new DocumentValidation("Harold", 8, new Date()), new DocumentValidation("Harold", 8, new Date()), new DocumentValidation("Harold", 8, new Date()), new DocumentValidation("Harold", 8, new Date()));
try {
getDs().save(list);
fail("Document validation should have complained");
} catch (MongoBulkWriteException e) {
// expected
}
getDs().save(list, new InsertManyOptions().bypassDocumentValidation(true));
assertTrue(query.filter(eq("number", 8)).iterator().hasNext());
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestDocumentValidation method update.
@Test
@SuppressWarnings("rawtypes")
public void update() {
getMapper().map(DocumentValidation.class);
getDs().enableDocumentValidation();
getDs().save(new DocumentValidation("Harold", 100, new Date()));
Query<DocumentValidation> query = getDs().find(DocumentValidation.class);
UpdateOptions options = new UpdateOptions().bypassDocumentValidation(false);
Update<DocumentValidation> update = query.update(set("number", 5));
try {
update.execute(options);
fail("Document validation should have complained");
} catch (MongoWriteException e) {
// expected
}
options.bypassDocumentValidation(true);
update.execute(options);
Assert.assertNotNull(query.filter(eq("number", 5)).iterator(new FindOptions().limit(1)).tryNext());
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestVersioning method testVersionedUpsert.
@Test
public void testVersionedUpsert() {
final Datastore datastore = getDs();
datastore.find(Versioned.class).delete(new DeleteOptions().multi(true));
Versioned entity = new Versioned();
entity.setName("Value 1");
Query<Versioned> query = datastore.find(Versioned.class);
query.filter(eq("name", "Value 1"));
query.update(set("name", "Value 3")).execute(new UpdateOptions().upsert(true));
entity = datastore.find(Versioned.class).iterator(new FindOptions().limit(1)).tryNext();
assertEquals(entity.getName(), "Value 3");
assertEquals(entity.getVersion().longValue(), 1);
}
Aggregations