use of dev.morphia.test.models.User in project morphia by mongodb.
the class AggregationTest method testDateToString.
@Test
public void testDateToString() {
LocalDate joined = LocalDate.parse("2016-05-01 UTC", DateTimeFormatter.ofPattern("yyyy-MM-dd z"));
getDs().save(new User("John Doe", joined));
Aggregation<User> pipeline = getDs().aggregate(User.class).project(project().include("string", dateToString().format("%Y-%m-%d").date(field("joined"))));
for (Iterator<StringDates> it = pipeline.execute(StringDates.class); it.hasNext(); ) {
Assert.assertEquals(it.next().string, "2016-05-01");
}
}
use of dev.morphia.test.models.User in project morphia by mongodb.
the class AggregationTest method testUnwind.
@Test
public void testUnwind() {
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
getDs().save(asList(new User("jane", LocalDate.parse("2011-03-02", format), "golf", "racquetball"), new User("joe", LocalDate.parse("2012-07-02", format), "tennis", "golf", "swimming"), new User("john", LocalDate.parse("2012-07-02", format))));
Iterator<User> aggregate = getDs().aggregate(User.class).project(project().include("name").include("joined").include("likes")).unwind(unwind("likes")).execute(User.class);
int count = 0;
while (aggregate.hasNext()) {
User user = aggregate.next();
switch(count) {
case 0:
Assert.assertEquals(user.name, "jane");
Assert.assertEquals(user.likes.get(0), "golf");
break;
case 1:
Assert.assertEquals(user.name, "jane");
Assert.assertEquals(user.likes.get(0), "racquetball");
break;
case 2:
Assert.assertEquals(user.name, "joe");
Assert.assertEquals(user.likes.get(0), "tennis");
break;
case 3:
Assert.assertEquals(user.name, "joe");
Assert.assertEquals(user.likes.get(0), "golf");
break;
case 4:
Assert.assertEquals(user.name, "joe");
Assert.assertEquals(user.likes.get(0), "swimming");
break;
default:
Assert.fail("Should only find 5 elements");
}
count++;
}
aggregate = getDs().aggregate(User.class).project(project().include("name").include("joined").include("likes")).unwind(unwind("likes").preserveNullAndEmptyArrays(true)).execute(User.class);
count = 0;
while (aggregate.hasNext()) {
User user = aggregate.next();
switch(count) {
case 0:
Assert.assertEquals(user.name, "jane");
Assert.assertEquals(user.likes.get(0), "golf");
break;
case 1:
Assert.assertEquals(user.name, "jane");
Assert.assertEquals(user.likes.get(0), "racquetball");
break;
case 2:
Assert.assertEquals(user.name, "joe");
Assert.assertEquals(user.likes.get(0), "tennis");
break;
case 3:
Assert.assertEquals(user.name, "joe");
Assert.assertEquals(user.likes.get(0), "golf");
break;
case 4:
Assert.assertEquals(user.name, "joe");
Assert.assertEquals(user.likes.get(0), "swimming");
break;
case 5:
Assert.assertEquals(user.name, "john");
Assert.assertNull(user.likes);
break;
default:
Assert.fail("Should only find 6 elements");
}
count++;
}
}
use of dev.morphia.test.models.User in project morphia by mongodb.
the class TestTransactions method manual.
@Test
public void manual() {
boolean success = false;
int count = 0;
while (!success && count < 5) {
try (MorphiaSession session = getDs().startSession()) {
session.startTransaction();
Rectangle rectangle = new Rectangle(1, 1);
session.save(rectangle);
session.save(new User("transactions", LocalDate.now()));
assertNull(getDs().find(Rectangle.class).first());
assertNull(getDs().find(User.class).first());
assertNotNull(session.find(Rectangle.class).first());
assertNotNull(session.find(User.class).first());
session.commitTransaction();
success = true;
} catch (MongoQueryException e) {
if (e.getErrorCode() == 251 && e.getErrorMessage().contains("has been aborted")) {
count++;
} else {
throw e;
}
} catch (RuntimeException e) {
throw e;
}
}
assertNotNull(getDs().find(Rectangle.class).first());
assertNotNull(getDs().find(User.class).first());
}
use of dev.morphia.test.models.User in project morphia by mongodb.
the class TestUpdateOperations method testMinWithDates.
@Test
public void testMinWithDates() {
List<User> entities = List.of(new User("User 1", LocalDate.of(2003, 7, 13)), new User("User 2", LocalDate.of(2009, 12, 1)), new User("User 3", LocalDate.of(2015, 8, 19)));
getDs().save(entities);
UpdateResult updated = getDs().find(User.class).update(min("joined", LocalDate.of(1985, 10, 12))).execute(new UpdateOptions().multi(true));
assertEquals(updated.getModifiedCount(), 3);
getDs().find(User.class).delete();
getDs().save(entities);
updated = getDs().find(User.class).update(min("joined", Instant.now().minus(5000, DAYS))).execute(new UpdateOptions().multi(true));
assertEquals(updated.getModifiedCount(), 2);
getDs().find(User.class).delete();
getDs().save(entities);
Calendar instance = Calendar.getInstance();
instance.set(86, Calendar.MAY, 13);
Date date = instance.getTime();
updated = getDs().find(User.class).update(min("joined", date)).execute(new UpdateOptions().multi(true));
assertEquals(updated.getModifiedCount(), 3);
}
use of dev.morphia.test.models.User 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);
}
Aggregations