def testReplaceSingle(self): """Test case - create collection and insert document and then replace document -""" try: with Connection(cfgOb=self.__cfgOb, resourceName=self.__resourceName) as client: mg = MongoDbUtil(client) ok = mg.createCollection(self.__dbName, self.__collectionName) self.assertTrue(ok) ok = mg.databaseExists(self.__dbName) self.assertTrue(ok) ok = mg.collectionExists(self.__dbName, self.__collectionName) self.assertTrue(ok) # dObj = self.__makeDataObj(2, 5, 5, 1) rId = mg.insert(self.__dbName, self.__collectionName, dObj) self.assertTrue(rId is not None) # Note that dObj is mutated by additional key '_id' that is added on insert - # rObj = mg.fetchOne(self.__dbName, self.__collectionName, "_id", rId) logger.debug("Return Object %s", pprint.pformat(rObj)) self.assertEqual(len(dObj), len(rObj)) self.assertEqual(dObj, rObj) # # Now replace with a new document with the same document id dObj = self.__makeDataObj(3, 2, 2, 1) logger.debug("Replace Object %s", pprint.pformat(dObj)) rId = mg.replace(self.__dbName, self.__collectionName, dObj, {"DOC_ID": "DOC_1"}, upsertFlag=True) # self.assertTrue(rId is not None) rObj = mg.fetchOne(self.__dbName, self.__collectionName, "DOC_ID", "DOC_1") rObj.pop("_id", None) dObj.pop("_id", None) logger.debug("Return Object %s", pprint.pformat(rObj)) self.assertEqual(len(dObj), len(rObj)) self.assertEqual(dObj, rObj) # # Now replace with a new document with a different key dObj2 = self.__makeDataObj(5, 5, 5, 2) logger.debug("Replace Object %s", pprint.pformat(dObj)) # rId = mg.replace(self.__dbName, self.__collectionName, dObj2, {"DOC_ID": "DOC_2"}, upsertFlag=True) rObj = mg.fetchOne(self.__dbName, self.__collectionName, "DOC_ID", "DOC_2") rObj.pop("_id", None) dObj2.pop("_id", None) logger.debug("Return Object %s", pprint.pformat(rObj)) self.assertEqual(len(dObj2), len(rObj)) self.assertEqual(dObj2, rObj) # except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def __transform(self, databaseName, collectionName, docSelectList, logIncrement=100): """Return a list of object identifiers for the input selection query.""" # ok = True try: self.__valInst = self.__getValidator(databaseName, collectionName, schemaLevel="full") with Connection(cfgOb=self.__cfgOb, resourceName=self.__resourceName) as client: mg = MongoDbUtil(client) if mg.collectionExists(databaseName, collectionName): numDoc = len(docSelectList) for ii, dD in enumerate(docSelectList, 1): if "_id" not in dD: continue rObj = mg.fetchOne(databaseName, collectionName, "_id", dD["_id"]) del rObj["_id"] # fOk = True if self.__oAdapt: self.__validateObj(databaseName, collectionName, rObj, label="Original") fOk, rObj = self.__oAdapt.filter(rObj) self.__validateObj(databaseName, collectionName, rObj, label="Updated") if fOk: rOk = mg.replace(databaseName, collectionName, rObj, dD) if rOk is None: tId = rObj[ "rcsb_id"] if rObj and "rcsb_id" in rObj else "anonymous" logger.error("%r %r (%r) failing", databaseName, collectionName, tId) # logger.info("rObj.keys() %r", list(rObj.keys())) # logger.info("rObj.items() %s", rObj.items()) rOk = False ok = ok and rOk # if ii % logIncrement == 0 or ii == numDoc: logger.info("Replace status %r object (%d of %d)", ok, ii, numDoc) # except Exception as e: logger.exception("Failing with %s", str(e)) return ok