@Test public void testDisableValidation() { Child child1 = new Child("James", "Rigney"); validateClassName("children", getDs().createUpdateOperations(Parent.class) .removeAll("children", child1), false); validateClassName("children", getDs().createUpdateOperations(Parent.class) .disableValidation() .removeAll("children", child1), false); validateClassName("c", getDs().createUpdateOperations(Parent.class) .disableValidation() .removeAll("c", child1), true); }
@Test public void testUpdateFirstNoCreateWithWriteConcern() { List<EntityLogs> logs = new ArrayList<EntityLogs>(); for (int i = 0; i < 100; i++) { logs.add(createEntryLogs("name", "logs" + i)); } EntityLogs logs1 = logs.get(0); getDs().update(getDs().find(EntityLogs.class), getDs().createUpdateOperations(EntityLogs.class) .set("raw", new BasicDBObject("new", "value")), 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) ? new BasicDBObject("new", "value") : logs.get(i).raw, entityLogs.raw); } }
@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 testSetOnInsertWhenInserting() { checkMinServerVersion(2.4); ObjectId id = new ObjectId(); assertInserted(getDs().update(getDs().find(Circle.class).field("id").equal(id), getDs().createUpdateOperations(Circle.class).setOnInsert("radius", 2D), new UpdateOptions() .upsert(true))); final Circle updatedCircle = getDs().get(Circle.class, id); assertThat(updatedCircle, is(notNullValue())); assertThat(updatedCircle.getRadius(), is(2D)); }
@PrePersist void prePersist() { if (myLongId == null) { final String collName = ds.getCollection(getClass()).getName(); final Query<StoredId> q = ds.find(StoredId.class).filter("_id", collName); final UpdateOperations<StoredId> uOps = ds.createUpdateOperations(StoredId.class).inc("value"); StoredId newId = ds.findAndModify(q, uOps); if (newId == null) { newId = new StoredId(collName); ds.save(newId); } myLongId = newId.getValue(); } } }
@Test(expected = ValidationException.class) public void testValidationBadFieldName() { getDs().update(getDs().find(Circle.class).field("radius").equal(0), getDs().createUpdateOperations(Circle.class).inc("r", 1D)); }
@Test public void testMinKeepsCurrentDocumentValueWhenThisIsSmallerThanSuppliedValue() { checkMinServerVersion(2.6); final ObjectId id = new ObjectId(); final double originalValue = 3D; assertInserted(getDs().update(getDs().find(Circle.class).field("id").equal(id), getDs().createUpdateOperations(Circle.class).setOnInsert("radius", originalValue), new UpdateOptions().upsert(true))); assertUpdated(getDs().update(getDs().find(Circle.class).field("id").equal(id), getDs().createUpdateOperations(Circle.class).min("radius", 5D), new UpdateOptions().upsert(true)), 1); final Circle updatedCircle = getDs().get(Circle.class, id); assertThat(updatedCircle, is(notNullValue())); assertThat(updatedCircle.getRadius(), is(originalValue)); }
@Test public void testMinUsesSuppliedValueWhenThisIsSmallerThanCurrentDocumentValue() { checkMinServerVersion(2.6); final ObjectId id = new ObjectId(); final double newLowerValue = 2D; assertInserted(getDs().update(getDs().find(Circle.class).field("id").equal(id), getDs().createUpdateOperations(Circle.class).setOnInsert("radius", 3D), new UpdateOptions().upsert(true))); assertUpdated(getDs().update(getDs().find(Circle.class).field("id").equal(id), getDs().createUpdateOperations(Circle.class).min("radius", newLowerValue), new UpdateOptions().upsert(true)), 1); final Circle updatedCircle = getDs().get(Circle.class, id); assertThat(updatedCircle, is(notNullValue())); assertThat(updatedCircle.getRadius(), is(newLowerValue)); }
@Test public void testSetOnInsertWhenUpdating() { checkMinServerVersion(2.4); ObjectId id = new ObjectId(); assertInserted(getDs().update(getDs().find(Circle.class).field("id").equal(id), getDs().createUpdateOperations(Circle.class).setOnInsert("radius", 1D), new UpdateOptions() .upsert(true))); assertUpdated(getDs().update(getDs().find(Circle.class).field("id").equal(id), getDs().createUpdateOperations(Circle.class).setOnInsert("radius", 2D), new UpdateOptions() .upsert(true)), 1); final Circle updatedCircle = getDs().get(Circle.class, id); assertThat(updatedCircle, is(notNullValue())); assertThat(updatedCircle.getRadius(), is(1D)); }
@Test @SuppressWarnings("deprecation") public void testInsertUpdate() { assertInserted(getDs().update(getDs().find(Circle.class).field("radius").equal(0), getDs().createUpdateOperations(Circle.class).inc("radius", 1D), true)); assertInserted(getDs().update(getDs().find(Circle.class).field("radius").equal(0), getDs().createUpdateOperations(Circle.class).inc("radius", 1D), new UpdateOptions() .upsert(true))); }
@Test public void testVersionsWithFindAndModify() { final ALongPrimitive initial = new ALongPrimitive(); Datastore ds = getDs(); ds.save(initial); Query<ALongPrimitive> query = ds.find(ALongPrimitive.class) .field("id").equal(initial.getId()); UpdateOperations<ALongPrimitive> update = ds.createUpdateOperations(ALongPrimitive.class) .set("text", "some new value"); ALongPrimitive postUpdate = ds.findAndModify(query, update); Assert.assertEquals(initial.version + 1, postUpdate.version); }
@Test public void testVersionedUpsert() { final Datastore datastore = getDs(); Versioned entity = new Versioned(); entity.setName("Value 1"); Query<Versioned> query = datastore.find(Versioned.class); query.filter("name", "Value 1"); UpdateOperations<Versioned> ops = datastore.createUpdateOperations(Versioned.class); ops.set("name", "Value 3"); datastore.update(query, ops, new UpdateOptions().upsert(true)); entity = datastore.find(Versioned.class).find(new FindOptions().limit(1)).tryNext(); Assert.assertEquals("Value 3", entity.getName()); Assert.assertEquals(1, entity.getVersion().longValue()); }
@Test public void testUpdateWithDifferentType() { final ContainsInt cInt = new ContainsInt(); cInt.val = 21; getDs().save(cInt); final UpdateResults res = getDs().update(getDs().find(ContainsInt.class), getDs().createUpdateOperations(ContainsInt.class).inc("val", 1.1D), new UpdateOptions()); assertUpdated(res, 1); assertThat(getDs().find(ContainsInt.class) .find(new FindOptions().limit(1)) .next() .val, is(22)); }
@Test public void testIncVersionNotOverridingOtherInc() { final Versioned version1 = new Versioned(); version1.setCount(0); getDs().save(version1); assertEquals(new Long(1), version1.getVersion()); assertEquals(0, version1.getCount()); Query<Versioned> query = getDs().find(Versioned.class); query.field("_id").equal(version1.getId()); UpdateOperations<Versioned> up = getDs().createUpdateOperations(Versioned.class).inc("count"); getDs().update(query, up, new UpdateOptions().upsert(true)); final Versioned version2 = getDs().get(Versioned.class, version1.getId()); assertEquals(new Long(2), version2.getVersion()); assertEquals(1, version2.getCount()); }
@Test public void testVersionsWithUpdate() { final ALongPrimitive initial = new ALongPrimitive(); Datastore ds = getDs(); ds.save(initial); Query<ALongPrimitive> query = ds.find(ALongPrimitive.class) .field("id").equal(initial.getId()); UpdateOperations<ALongPrimitive> update = ds.createUpdateOperations(ALongPrimitive.class) .set("text", "some new value"); UpdateResults results = ds.update(query, update); assertEquals(1, results.getUpdatedCount()); ALongPrimitive postUpdate = ds.get(ALongPrimitive.class, initial.getId()); Assert.assertEquals(initial.version + 1, postUpdate.version); }
@Test public void testEmbeddedMapUpdateOperationsOnInterfaceValue() throws Exception { getMorphia().map(ContainsMapOfEmbeddedGoos.class).map(ContainsMapOfEmbeddedInterfaces.class); final Goo g1 = new Goo("Scott"); final Goo g2 = new Goo("Ralph"); final ContainsMapOfEmbeddedInterfaces cmoei = new ContainsMapOfEmbeddedInterfaces(); cmoei.values.put("first", g1); getDs().save(cmoei); getDs().update(cmoei, getDs().createUpdateOperations(ContainsMapOfEmbeddedInterfaces.class).set("values.second", g2)); //check className in the map values. final BasicDBObject goo = (BasicDBObject) ((BasicDBObject) getDs().getCollection(ContainsMapOfEmbeddedInterfaces.class) .findOne() .get("values")) .get("second"); assertTrue("className should be here.", goo.containsField(getMorphia().getMapper().getOptions().getDiscriminatorField())); }
@Test //@Ignore("waiting on issue 184") public void testEmbeddedMapUpdateOperations() throws Exception { getMorphia().map(ContainsMapOfEmbeddedGoos.class).map(ContainsMapOfEmbeddedInterfaces.class); final Goo g1 = new Goo("Scott"); final Goo g2 = new Goo("Ralph"); final ContainsMapOfEmbeddedGoos cmoeg = new ContainsMapOfEmbeddedGoos(); cmoeg.values.put("first", g1); getDs().save(cmoeg); getDs().update(cmoeg, getDs().createUpdateOperations(ContainsMapOfEmbeddedGoos.class).set("values.second", g2)); //check className in the map values. final BasicDBObject goo = (BasicDBObject) ((BasicDBObject) getDs().getCollection(ContainsMapOfEmbeddedGoos.class) .findOne() .get("values")).get( "second"); assertFalse("className should not be here.", goo.containsField( getMorphia().getMapper().getOptions().getDiscriminatorField())); }
@Test public void isolated() { Assume.assumeTrue(serverIsAtMostVersion(3.6)); UpdateOperations<Circle> updates = getDs().createUpdateOperations(Circle.class) .inc("radius", 1D); assertFalse(updates.isIsolated()); updates.isolated(); assertTrue(updates.isIsolated()); getDs().update(getDs().find(Circle.class) .field("radius").equal(0), updates, new UpdateOptions() .upsert(true) .writeConcern(WriteConcern.ACKNOWLEDGED)); }
@Test public void testElemMatchUpdate() { // setUp Object id = getDs().save(new ContainsIntArray()).getId(); assertThat(getDs().get(ContainsIntArray.class, id).values, arrayContaining(1, 2, 3)); // do patch Query<ContainsIntArray> q = getDs().createQuery(ContainsIntArray.class) .filter("id", id) .filter("values", 2); UpdateOperations<ContainsIntArray> ops = getDs().createUpdateOperations(ContainsIntArray.class) .set("values.$", 5); getDs().update(q, ops); // expected assertThat(getDs().get(ContainsIntArray.class, id).values, arrayContaining(1, 5, 3)); }
@Test public void testIt() throws Exception { getMorphia().map(Employee.class); getDs().save(new Employee("Mister", "GOD", null, 0)); final Employee boss = getDs().find(Employee.class).field("manager").equal(null) .find(new FindOptions().limit(1)) .next(); // get an employee without a manager Assert.assertNotNull(boss); final Key<Employee> key = getDs().save(new Employee("Scott", "Hernandez", getDs().getKey(boss), 150 * 1000)); Assert.assertNotNull(key); final UpdateResults res = getDs().update(boss, getDs().createUpdateOperations(Employee.class) .addToSet("underlings", key)); //add Scott as an employee of his manager Assert.assertNotNull(res); Assert.assertTrue("Should update existing document", res.getUpdatedExisting()); Assert.assertEquals("Should update one document", 1, res.getUpdatedCount()); final Employee scottsBoss = getDs().find(Employee.class).filter("underlings", key) .find(new FindOptions().limit(1)) .next(); // get Scott's boss Assert.assertNotNull(scottsBoss); Assert.assertEquals(boss.id, scottsBoss.id); }