use of dev.morphia.InsertOneOptions in project morphia by mongodb.
the class TestDatastore method testInsert.
@Test
public void testInsert() {
MongoCollection collection = getDs().getCollection(TestEntity.class);
this.getDs().insert(new TestEntity());
assertEquals(collection.countDocuments(), 1);
this.getDs().insert(new TestEntity(), new InsertOneOptions().writeConcern(WriteConcern.ACKNOWLEDGED));
assertEquals(collection.countDocuments(), 2);
}
use of dev.morphia.InsertOneOptions in project morphia by mongodb.
the class TestDocumentValidation method testBypassDocumentValidation.
@Test
public void testBypassDocumentValidation() {
getMapper().map(User.class);
getDs().enableDocumentValidation();
final User user = new User("Jim Halpert", LocalDate.now());
user.age = 5;
try {
getDs().save(user);
fail("Document validation should have rejected the document");
} catch (MongoWriteException ignored) {
}
getDs().save(user, new InsertOneOptions().bypassDocumentValidation(true));
assertEquals(getDs().find(User.class).count(), 1);
}
use of dev.morphia.InsertOneOptions in project morphia by mongodb.
the class TestDocumentValidation method insert.
@Test
public void insert() {
getMapper().map(DocumentValidation.class);
getDs().enableDocumentValidation();
try {
getDs().insert(new DocumentValidation("Harold", 8, new Date()));
fail("Document validation should have complained");
} catch (MongoWriteException e) {
// expected
}
getDs().insert(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("John", 8, new Date()), new DocumentValidation("Sarah", 8, new Date()), new DocumentValidation("Amy", 8, new Date()), new DocumentValidation("James", 8, new Date()));
try {
getDs().insert(list);
fail("Document validation should have complained");
} catch (MongoBulkWriteException e) {
// expected
}
getDs().insert(list, new InsertManyOptions().bypassDocumentValidation(true));
assertTrue(query.filter(eq("number", 8)).iterator().hasNext());
}
use of dev.morphia.InsertOneOptions 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());
}
Aggregations