@Test public void testUserData() { getMorphia().map(MapOfListString.class); MapOfListString ud = new MapOfListString(); ud.id = "123"; ArrayList<String> d = new ArrayList<String>(); d.add("1"); d.add("2"); d.add("3"); ud.data.put("123123", d); getDs().save(ud); }
@Test public final void testGetByKeys() { final A a1 = new A(); final A a2 = new A(); final Iterable<Key<A>> keys = getDs().save(asList(a1, a2)); final List<A> reloaded = getDs().getByKeys(keys); final Iterator<A> i = reloaded.iterator(); Assert.assertNotNull(i.next()); Assert.assertNotNull(i.next()); Assert.assertFalse(i.hasNext()); }
@Test(expected = ConcurrentModificationException.class) public void testConcurrentModDetectionLongWithMerge() throws Exception { final ALong a = new ALong(); Assert.assertEquals(null, a.v); getDs().save(a); a.text = " foosdfds "; final ALong a2 = getDs().get(a); getDs().save(a2); getDs().merge(a); }
@Test public void testMapAsId() { getMorphia().map(MapAsId.class); final MapAsId mai = new MapAsId(); mai.id.put("test", "string"); final Key<MapAsId> maiKey = getDs().save(mai); final MapAsId maiLoaded = getDs().get(MapAsId.class, new BasicDBObject("test", "string")); assertNotNull(maiLoaded); assertNotNull(maiKey); }
@Test public void testTimestampMapping() { getMorphia().map(ContainsTimestamp.class); final ContainsTimestamp cts = new ContainsTimestamp(); final Key<ContainsTimestamp> savedKey = getDs().save(cts); final ContainsTimestamp loaded = getDs().get(ContainsTimestamp.class, savedKey.getId()); assertNotNull(loaded.ts); assertEquals(loaded.ts.getTime(), cts.ts.getTime()); }
@Test public void singleLookup() throws Exception { A.loadCount = 0; final A a = new A(); HoldsMultipleA holder = new HoldsMultipleA(); holder.a1 = a; holder.a2 = a; getDs().save(asList(a, holder)); holder = getDs().get(HoldsMultipleA.class, holder.id); Assert.assertEquals(1, A.loadCount); Assert.assertTrue(holder.a1 == holder.a2); }
@Test public void testDelete() { final CompoundIdEntity entity = new CompoundIdEntity(); entity.id = new CompoundId("test"); getDs().save(entity); getDs().delete(CompoundIdEntity.class, entity.id); }
@Test(expected = DuplicateKeyException.class) public void testUniqueIndexedEntity() throws Exception { getDs().ensureIndexes(); assertThat(getDs().getCollection(UniqueIndexOnValue.class).getIndexInfo(), hasIndexNamed("l_ascending")); getDs().save(new UniqueIndexOnValue("a")); // this should throw... getDs().save(new UniqueIndexOnValue("v")); } @Test(expected = MappingException.class)
@Test public void shouldRetrieveGeoJsonPoint() { // given City city = new City("New City", point(3.0, 7.0)); getDs().save(city); // when City found = getDs().find(City.class).field("name").equal("New City").find(new FindOptions().limit(1)).tryNext(); // then assertThat(found, is(notNullValue())); assertThat(found, is(city)); }
@Test public void testListOfListOfPerson() { getMorphia().map(ListListPerson.class); ListListPerson list = new ListListPerson(); list.list.add(asList(new Person("Peter"), new Person("Paul"), new Person("Mary"))); list.list.add(asList(new Person("Crosby"), new Person("Stills"), new Person("Nash"))); getDs().save(list); ListListPerson result = getDs().find(ListListPerson.class).find(new FindOptions().limit(1)).tryNext(); Assert.assertEquals(list, result); }
/** * Real test */ @Test public void testSavingBook() throws Exception { // Test saving getDs().save(newBook()); assertEquals(1, getDs().getCollection(Book.class).count()); }
@Test public void testEnum() { final EnumEntity ee = new EnumEntity(); getDs().save(ee); final DBObject dbObj = getDs().getCollection(EnumEntity.class).findOne(); Assert.assertEquals(1, dbObj.get("val")); }
@Test public void basicReference() { final Author author = new Author("Jane Austen"); getDs().save(author); final Book book = new Book("Pride and Prejudice"); book.setAuthor(author); getDs().save(book); final Book loaded = getDs().find(Book.class).filter("_id", book.id).first(); Assert.assertFalse(loaded.author.isResolved()); Assert.assertEquals(author, loaded.author.get()); assertTrue(loaded.author.isResolved()); }
@Test(expected = MappingException.class) public void testMissingRef() { final Source source = new Source(); source.setTarget(new Target()); getDs().save(source); // does not fail due to pre-initialized Ids toList(getDs().find(Source.class).find()); }
@Test @Ignore("does not work since the EnumConverter stores as a single string value -- no type info") public void testEnumBehindAnInterfacePersistence() throws Exception { getMorphia().map(ContainerEntity.class); ContainerEntity n = new ContainerEntity(); getDs().save(n); n = getDs().get(n); Assert.assertSame(EnumBehindAnInterface.A, n.foo); }
@Test public void example() throws Exception { ChildEntity entity = new ChildEntity(); entity.setEmbeddedList(asList(new ChildEmbedded("first"), new ChildEmbedded("second"), new AnotherChildEmbedded("third"))); getDs().save(entity); ChildEntity childEntity = getDs().find(ChildEntity.class) .find(new FindOptions().limit(1)) .next(); Assert.assertEquals(entity, childEntity); } }
@Test public void testMissingRefLazyIgnoreMissing() { final Source e = new Source(); e.setIgnoreMissing(new Target()); getDs().save(e); // does not fail due to pre-initialized Ids try { getDs().find(Source.class).find(new FindOptions().limit(1)).tryNext().getIgnoreMissing().foo(); } catch (RuntimeException re) { Assert.assertEquals("Cannot dispatch method foo", re.getMessage()); } }
@Test public void testSuperclassEntity() { final Car c = new Car(); getDs().save(c); assertNotNull(c.getId()); assertEquals(1, getDs().getCount(Car.class)); assertEquals(1, getDs().getCount(AbstractVehicle.class)); }
@Test public void testEmptyEmbeddedNotNullAfterReload() { A a = new A(); a.b = new B(); getDs().save(a); Assert.assertNotNull(a.b); a = getDs().find(A.class).filter("_id", a.getId()) .find(new FindOptions().limit(1)) .tryNext(); Assert.assertNull(a.b); }
@Test public void testExistsWithEntity() { final FacebookUser facebookUser = new FacebookUser(1, "user one"); getDs().save(facebookUser); assertEquals(1, getDs().getCount(FacebookUser.class)); assertNotNull(getDs().get(FacebookUser.class, 1)); assertNotNull(getDs().exists(facebookUser)); getDs().delete(getDs().find(FacebookUser.class)); assertEquals(0, getDs().getCount(FacebookUser.class)); assertNull(getDs().exists(facebookUser)); }