@Test public void testListOfListMapping() throws Exception { getMorphia().map(ContainsListOfList.class); getDs().delete(getDs().find(ContainsListOfList.class)); final ContainsListOfList entity = new ContainsListOfList(); entity.strings = new ArrayList<List<String>>(); entity.strings.add(Arrays.asList("element1", "element2")); entity.strings.add(Collections.singletonList("element3")); entity.integers = new ArrayList<List<Integer>>(); entity.integers.add(Arrays.asList(1, 2)); entity.integers.add(Collections.singletonList(3)); getDs().save(entity); final ContainsListOfList loaded = getDs().get(entity); Assert.assertNotNull(loaded.strings); Assert.assertEquals(entity.strings, loaded.strings); Assert.assertEquals(entity.strings.get(0), loaded.strings.get(0)); Assert.assertEquals(entity.strings.get(0).get(0), loaded.strings.get(0).get(0)); Assert.assertNotNull(loaded.integers); Assert.assertEquals(entity.integers, loaded.integers); Assert.assertEquals(entity.integers.get(0), loaded.integers.get(0)); Assert.assertEquals(entity.integers.get(0).get(0), loaded.integers.get(0).get(0)); Assert.assertNotNull(loaded.id); }
private void empties() { Datastore ds = getDs(); ds.delete(ds.find(Employee.class)); Employee employee = new Employee(); HashMap<String, Byte> byteMap = new HashMap<String, Byte>(); byteMap.put("b", (byte) 1); employee.byteMap = byteMap; ds.save(employee); Employee loaded = ds.find(Employee.class) .find(new FindOptions().limit(1)) .next(); assertEquals(Byte.valueOf((byte) 1), loaded.byteMap.get("b")); assertNull(loaded.floatMap); }
@Test public void testDelete() { final CompoundIdEntity entity = new CompoundIdEntity(); entity.id = new CompoundId("test"); getDs().save(entity); getDs().delete(CompoundIdEntity.class, entity.id); }
@Test public void testDelete() throws Exception { final E e = new E(); e.id = new CId("test"); final Key<E> key = getDs().save(e); getDs().delete(E.class, e.id); }
@Test public void testUpdateFirstNoCreate() { getDs().delete(getDs().find(EntityLogs.class)); List<EntityLogs> logs = new ArrayList<EntityLogs>(); for (int i = 0; i < 100; i++) { logs.add(createEntryLogs("name", "logs" + i)); } EntityLogs logs1 = logs.get(0); Query<EntityLogs> query = getDs().find(EntityLogs.class); UpdateOperations<EntityLogs> updateOperations = getDs().createUpdateOperations(EntityLogs.class); BasicDBObject object = new BasicDBObject("new", "value"); updateOperations.set("raw", object); getDs().update(query, updateOperations, new UpdateOptions()); List<EntityLogs> list = toList(getDs().find(EntityLogs.class).find()); for (int i = 0; i < list.size(); i++) { final EntityLogs entityLogs = list.get(i); assertEquals(entityLogs.id.equals(logs1.id) ? object : logs.get(i).raw, entityLogs.raw); } }
@Test public void testDoesNotExistAfterDelete() { // given long id = System.currentTimeMillis(); final Key<FacebookUser> key = getDs().save(new FacebookUser(id, "user 1")); // when getDs().delete(getDs().find(FacebookUser.class)); // then assertNull("Shouldn't exist after delete", getDs().exists(key)); }
private void empties() { Datastore ds = getDs(); ds.delete(ds.find(Employee.class)); Employee employee = new Employee(); employee.byteList = asList((byte) 1, (byte) 2); ds.save(employee); Employee loaded = ds.find(Employee.class) .find(new FindOptions().limit(1)) .next(); assertEquals(employee.byteList, loaded.byteList); assertNull(loaded.floatList); }
@Test public void testNonSnapshottedQuery() { Assume.assumeTrue(serverIsAtMostVersion(3.6)); getDs().delete(getDs().find(PhotoWithKeywords.class)); getDs().save(asList(new PhotoWithKeywords("scott", "hernandez"), new PhotoWithKeywords("scott", "hernandez"), new PhotoWithKeywords("scott", "hernandez"))); final Iterator<PhotoWithKeywords> it = getDs().find(PhotoWithKeywords.class) .find(new FindOptions() .modifier("$snapshot", true) .batchSize(2) ); getDs().save(asList(new PhotoWithKeywords("1", "2"), new PhotoWithKeywords("3", "4"), new PhotoWithKeywords("5", "6"))); assertNotNull(it.next()); assertNotNull(it.next()); //okay, now we should getMore... assertTrue(it.hasNext()); assertNotNull(it.next()); assertTrue(it.hasNext()); assertNotNull(it.next()); }
@Test public void testSnapshottedQuery() { Assume.assumeTrue(serverIsAtMostVersion(3.6)); getDs().delete(getDs().find(PhotoWithKeywords.class)); getDs().save(asList(new PhotoWithKeywords("scott", "hernandez"), new PhotoWithKeywords("scott", "hernandez"), new PhotoWithKeywords("scott", "hernandez"))); final Iterator<PhotoWithKeywords> it = getDs().find(PhotoWithKeywords.class) .filter("keywords.keyword", "scott") .find(new FindOptions() .modifier("$snapshot", true) .batchSize(2)); getDs().save(asList(new PhotoWithKeywords("1", "2"), new PhotoWithKeywords("3", "4"), new PhotoWithKeywords("5", "6"))); assertNotNull(it.next()); assertNotNull(it.next()); //okay, now we should getMore... assertTrue(it.hasNext()); assertNotNull(it.next()); assertTrue(!it.hasNext()); }
@Test @SuppressWarnings("deprecation") public void testNegativeBatchSizeOld() { getDs().delete(getDs().find(PhotoWithKeywords.class)); getDs().save(asList(new PhotoWithKeywords("scott", "hernandez"), new PhotoWithKeywords("scott", "hernandez"), new PhotoWithKeywords("scott", "hernandez"), new PhotoWithKeywords("1", "2"), new PhotoWithKeywords("3", "4"), new PhotoWithKeywords("5", "6"))); assertEquals(2, toList(getDs().find(PhotoWithKeywords.class) .batchSize(-2) .find()) .size()); }
@Test public void testNegativeBatchSize() { getDs().delete(getDs().find(PhotoWithKeywords.class)); getDs().save(asList(new PhotoWithKeywords("scott", "hernandez"), new PhotoWithKeywords("scott", "hernandez"), new PhotoWithKeywords("scott", "hernandez"), new PhotoWithKeywords("1", "2"), new PhotoWithKeywords("3", "4"), new PhotoWithKeywords("5", "6"))); assertEquals(2, toList(getDs().find(PhotoWithKeywords.class) .find(new FindOptions() .batchSize(-2))) .size()); }
@Test public void testDeleteWithCollation() { checkMinServerVersion(3.4); getDs().getCollection(FacebookUser.class).drop(); getDs().save(asList(new FacebookUser(1, "John Doe"), new FacebookUser(2, "john doe"))); Query<FacebookUser> query = getDs().find(FacebookUser.class) .field("username").equal("john doe"); assertEquals(1, getDs().delete(query).getN()); assertEquals(1, getDs().delete(query, new DeleteOptions() .collation(Collation.builder() .locale("en") .collationStrength(CollationStrength.SECONDARY) .build())) .getN()); }
@Test public void testDeleteQuery() { getDs().save(asList(new Rectangle(1, 10), new Rectangle(1, 10), new Rectangle(1, 10), new Rectangle(10, 10), new Rectangle(10, 10))); assertEquals(5, getDs().getCount(Rectangle.class)); getDs().delete(getDs().find(Rectangle.class).filter("height", 1D)); assertEquals(2, getDs().getCount(Rectangle.class)); }
@Test(expected = MappingException.class) public void testMissingReferences() { final ContainsPic cpk = new ContainsPic(); final Pic p = new Pic(); cpk.setPic(p); getDs().save(p); getDs().save(cpk); getDs().delete(p); toList(getDs().find(ContainsPic.class).find()); }
@Test public void testInsertWithRef() { final Pic pic = new Pic(); pic.setName("fist"); final Key<Pic> picKey = getDs().save(pic); assertInserted(getDs().update(getDs().find(ContainsPic.class).filter("name", "first").filter("pic", picKey), getDs().createUpdateOperations(ContainsPic.class) .set("name", "A"), new UpdateOptions().upsert(true))); assertThat(getDs().find(ContainsPic.class).count(), is(1L)); getDs().delete(getDs().find(ContainsPic.class)); assertInserted(getDs().update(getDs().find(ContainsPic.class).filter("name", "first").filter("pic", pic), getDs().createUpdateOperations(ContainsPic.class).set("name", "second"), new UpdateOptions() .upsert(true))); assertThat(getDs().find(ContainsPic.class).count(), is(1L)); //test reading the object. final ContainsPic cp = getDs().find(ContainsPic.class) .find(new FindOptions().limit(1)) .next(); assertThat(cp, is(notNullValue())); assertThat(cp.getName(), is("second")); assertThat(cp.getPic(), is(notNullValue())); assertThat(cp.getPic().getName(), is(notNullValue())); assertThat(cp.getPic().getName(), is("fist")); }
@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)); }
@Test public void testBasic() throws Exception { getDs().save(new Normal("value")); Normal n = getDs().find(Normal.class) .find(new FindOptions().limit(1)) .next(); Assert.assertNotNull(n); Assert.assertNotNull(n.name); getDs().delete(n); getDs().save(new NormalWithNotSaved()); n = getDs().find(Normal.class) .find(new FindOptions().limit(1)) .next(); Assert.assertNotNull(n); Assert.assertNull(n.name); getDs().delete(n); getDs().save(new Normal("value21")); final NormalWithNotSaved notSaved = getDs().find(NormalWithNotSaved.class) .find(new FindOptions().limit(1)) .next(); Assert.assertNotNull(notSaved); Assert.assertNotNull(notSaved.name); Assert.assertEquals("never", notSaved.name); }
@Test public void testEmbedded() { getDs().delete(getDs().find(Hotel.class)); final Hotel borg = new Hotel(); borg.setName("Hotel Borg"); borg.setStars(4); borg.setTakesCreditCards(true); borg.setStartDate(new Date()); borg.setType(Hotel.Type.LEISURE); final Address address = new Address(); address.setStreet("Posthusstraeti 11"); address.setPostCode("101"); borg.setAddress(address); getDs().save(borg); assertEquals(1, getDs().getCount(Hotel.class)); assertNotNull(borg.getId()); final Hotel hotelLoaded = getDs().get(Hotel.class, borg.getId()); assertEquals(borg.getName(), hotelLoaded.getName()); assertEquals(borg.getAddress().getPostCode(), hotelLoaded.getAddress().getPostCode()); }
@Test public void testBasicType() { getDs().delete(getDs().find(HasSingleString.class)); getDs().save(new HasSingleString()); Assert.assertNotNull(getDs().find(HasSingleString.class) .find(new FindOptions().limit(1)) .next()); Assert.assertEquals(1, getDs().find(HasSingleString.class).count()); final HasManyStringsArray hms = getDs().find(HasManyStringsArray.class) .find(new FindOptions().limit(1)) .next(); Assert.assertNotNull(hms); Assert.assertNotNull(hms.strings); Assert.assertEquals(1, hms.strings.length); final HasManyStringsList hms2 = getDs().find(HasManyStringsList.class) .find(new FindOptions().limit(1)) .next(); Assert.assertNotNull(hms2); Assert.assertNotNull(hms2.strings); Assert.assertEquals(1, hms2.strings.size()); }
.find(new FindOptions().limit(1)) .next()); getDs().delete(getDs().find(User.class));