use of org.mongodb.morphia.testmodel.Article in project morphia by mongodb.
the class TestMapping method testMaps.
@Test
public void testMaps() throws Exception {
final DBCollection articles = getDb().getCollection("articles");
getMorphia().map(Article.class).map(Translation.class).map(Circle.class);
final Article related = new Article();
final BasicDBObject relatedDbObj = (BasicDBObject) getMorphia().toDBObject(related);
articles.save(relatedDbObj);
final Article relatedLoaded = getMorphia().fromDBObject(getDs(), Article.class, articles.findOne(new BasicDBObject(Mapper.ID_KEY, relatedDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
final Article article = new Article();
article.setTranslation("en", new Translation("Hello World", "Just a test"));
article.setTranslation("is", new Translation("Halló heimur", "Bara að prófa"));
article.setAttribute("myDate", new Date());
article.setAttribute("myString", "Test");
article.setAttribute("myInt", 123);
article.putRelated("test", relatedLoaded);
final BasicDBObject articleDbObj = (BasicDBObject) getMorphia().toDBObject(article);
articles.save(articleDbObj);
final Article articleLoaded = getMorphia().fromDBObject(getDs(), Article.class, articles.findOne(new BasicDBObject(Mapper.ID_KEY, articleDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
assertEquals(article.getTranslations().size(), articleLoaded.getTranslations().size());
assertEquals(article.getTranslation("en").getTitle(), articleLoaded.getTranslation("en").getTitle());
assertEquals(article.getTranslation("is").getBody(), articleLoaded.getTranslation("is").getBody());
assertEquals(article.getAttributes().size(), articleLoaded.getAttributes().size());
assertEquals(article.getAttribute("myDate"), articleLoaded.getAttribute("myDate"));
assertEquals(article.getAttribute("myString"), articleLoaded.getAttribute("myString"));
assertEquals(article.getAttribute("myInt"), articleLoaded.getAttribute("myInt"));
assertEquals(article.getRelated().size(), articleLoaded.getRelated().size());
assertEquals(article.getRelated("test").getId(), articleLoaded.getRelated("test").getId());
}
use of org.mongodb.morphia.testmodel.Article in project morphia by mongodb.
the class TestUpdateOps method testSetUnset.
@Test
public void testSetUnset() throws Exception {
Datastore ds = getDs();
final Key<Circle> key = ds.save(new Circle(1));
assertUpdated(ds.update(ds.find(Circle.class).filter("radius", 1D), ds.createUpdateOperations(Circle.class).set("radius", 2D), new UpdateOptions().multi(false)), 1);
assertThat(ds.getByKey(Circle.class, key).getRadius(), is(2D));
assertUpdated(ds.update(ds.find(Circle.class).filter("radius", 2D), ds.createUpdateOperations(Circle.class).unset("radius"), new UpdateOptions().multi(false)), 1);
assertThat(ds.getByKey(Circle.class, key).getRadius(), is(0D));
Article article = new Article();
ds.save(article);
ds.update(ds.find(Article.class), ds.createUpdateOperations(Article.class).set("translations", new HashMap<String, Translation>()));
ds.update(ds.find(Article.class), ds.createUpdateOperations(Article.class).unset("translations"));
}
Aggregations