def testInsertList(self): """Test case - create collection and insert data -""" 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) # dList = [] for ii in range(100): dList.append(self.__makeDataObj(2, 5, 5, ii)) # keyName = "DOC_ID" rIdL = mg.insertList(self.__dbName, self.__collectionName, dList, keyNames=[keyName], salvage=True) self.assertEqual(len(rIdL), len(dList)) # # Note that dObj is mutated by additional key '_id' that is added on insert - # for ii, rId in enumerate(rIdL): rObj = mg.fetchOne(self.__dbName, self.__collectionName, "_id", rId) logger.debug("Return Object %s", pprint.pformat(rObj)) jj = int(rObj["DOC_ID"][4:]) self.assertEqual(len(dList[jj]), len(rObj)) self.assertEqual(dList[jj], rObj) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def testReplaceList(self): """Test case - create collection and insert document list - replace and upsert document list""" try: with Connection(cfgOb=self.__cfgOb, resourceName=self.__resourceName) as client: nDocs = 10 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) # dList = [] for ii in range(nDocs): dObj = self.__makeDataObj(2, 5, 5, ii) dList.append(dObj) # keyName = "DOC_ID" rIdL = mg.insertList(self.__dbName, self.__collectionName, dList, keyNames=[keyName], salvage=True) self.assertEqual(len(rIdL), len(dList)) # for ii, rId in enumerate(rIdL): rObj = mg.fetchOne(self.__dbName, self.__collectionName, "_id", rId) # logger.debug("Return Object %s", pprint.pformat(rObj)) self.assertEqual(len(dList[ii]), len(rObj)) self.assertEqual(dList[ii], rObj) # # Replace with 2x the list length - half are duplicates id's dList = [] for ii in range(nDocs + nDocs): dObj = self.__makeDataObj(4, 10, 10, ii) dList.append(dObj) # updL = mg.replaceList(self.__dbName, self.__collectionName, dList, ["DOC_ID"], upsertFlag=True) # logger.info("Upserted id list length %d", len(updL)) for ii in range(nDocs + nDocs): kVal = "DOC_%d" % ii rObj = mg.fetchOne(self.__dbName, self.__collectionName, "DOC_ID", kVal) if not rObj: logger.info("Failing to recover doc %s", kVal) # logger.debug("Return Object %s", pprint.pformat(rObj)) rObj.pop("_id", None) dList[ii].pop("_id", None) self.assertEqual(len(dList[ii]), len(rObj)) self.assertEqual(dList[ii], rObj) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def testSingleIndex(self): """Test case - create collection, create simple single index, insert document list, read check documents""" try: with Connection(cfgOb=self.__cfgOb, resourceName=self.__resourceName) as client: nDocs = 100 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) # # Create before insert ok = mg.createIndex(self.__dbName, self.__collectionName, keyList=["DOC_ID"], indexName="primary", indexType="DESCENDING", uniqueFlag=True) self.assertTrue(ok) dList = [] for ii in range(nDocs): dObj = self.__makeDataObj(2, 5, 5, ii) dList.append(dObj) # keyName = "DOC_ID" rIdL = mg.insertList(self.__dbName, self.__collectionName, dList, keyNames=[keyName], salvage=True) self.assertEqual(len(dList), len(rIdL)) # for ii in range(nDocs): kVal = "DOC_%d" % ii rObj = mg.fetchOne(self.__dbName, self.__collectionName, "DOC_ID", kVal) # logger.debug("Return Object %s" % pprint.pformat(rObj)) rObj.pop("_id", None) dList[ii].pop("_id", None) self.assertEqual(len(dList[ii]), len(rObj)) self.assertEqual(dList[ii], rObj) # ok = mg.dropIndex(self.__dbName, self.__collectionName, indexName="primary") self.assertTrue(ok) ok = mg.createIndex(self.__dbName, self.__collectionName, keyList=["DOC_ID"], indexName="primary", indexType="DESCENDING", uniqueFlag=True) self.assertTrue(ok) ok = mg.reIndex(self.__dbName, self.__collectionName) self.assertTrue(ok) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def testSingleIndexSelect(self): """Test case - create collection, create simple single index, insert document list, read check documents.""" try: logger.debug("Starting testSingleIndexSelect") with Connection(cfgOb=self.__cfgOb, resourceName=self.__resourceName) as client: nDocs = 100 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) # # Create before insert ok = mg.createIndex(self.__dbName, self.__collectionName, keyList=["DOC_ID"], indexName="primary", indexType="DESCENDING", uniqueFlag=True) self.assertTrue(ok) dList = [] nRows = 5 for ii in range(nDocs): dObj = self.__makeDataObj(2, 5, nRows, ii) dList.append(dObj) # keyName = "DOC_ID" rIdL = mg.insertList(self.__dbName, self.__collectionName, dList, keyNames=[keyName], salvage=True) self.assertEqual(len(dList), len(rIdL)) # for ii in range(nDocs): kVal = "DOC_%d" % ii rObj = mg.fetchOne(self.__dbName, self.__collectionName, "DOC_ID", kVal) # logger.debug("Return Object %s" % pprint.pformat(rObj)) rObj.pop("_id", None) dList[ii].pop("_id", None) self.assertEqual(len(dList[ii]), len(rObj)) self.assertEqual(dList[ii], rObj) # ok = mg.dropIndex(self.__dbName, self.__collectionName, indexName="primary") self.assertTrue(ok) ok = mg.createIndex(self.__dbName, self.__collectionName, keyList=["DOC_ID"], indexName="primary", indexType="DESCENDING", uniqueFlag=True) self.assertTrue(ok) ok = mg.reIndex(self.__dbName, self.__collectionName) self.assertTrue(ok) # with Connection(cfgOb=self.__cfgOb, resourceName=self.__resourceName) as client: mg = MongoDbUtil(client) ii = mg.count(self.__dbName, self.__collectionName) logger.debug("collection length %d", ii) # dList = mg.fetch(self.__dbName, self.__collectionName, ["DOC_ID"]) self.assertEqual(len(dList), nDocs) logger.debug("Fetch length %d", len(dList)) for ii, dD in enumerate(dList): logger.debug("Fetch num %d: %r", ii, dD) # dList = mg.fetch(self.__dbName, self.__collectionName, ["category_0.attribute_0"], queryD={"category_0.attribute_0": "val_0_0"}) self.assertEqual(len(dList), nDocs) logger.debug("Fetch length %d", len(dList)) for ii, dD in enumerate(dList): logger.debug("Fetch num %d: %r", ii, dD) atName = "category_0.attribute_0" vL0 = mg.distinct(self.__dbName, self.__collectionName, atName) self.assertEqual(len(vL0), nRows + 2) logger.debug("vL0 %r", vL0) vL1 = mg.distinct(self.__dbName, self.__collectionName, "category_1.attribute_0") self.assertEqual(len(vL1), nRows + 2) for v in vL0: num = mg.count(self.__dbName, self.__collectionName, countFilter={atName: v}) logger.debug("%s value %s (%d)", atName, v, num) self.assertGreaterEqual(num, 100) # except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def __loadDocuments(self, dbName, collectionName, docList, loadType="full", readBackCheck=False, keyNames=None): # # Load database/collection with input document list - # failList = [] rIdL = [] successList = [] logger.debug( "Loading dbName %s collectionName %s with document count %d keynames %r", dbName, collectionName, len(docList), keyNames) if keyNames: # map the document list to some document key if this is provided indD = {} indL = [] try: for ii, doc in enumerate(docList): dIdTup = self.__getKeyValues(doc, keyNames) indD[dIdTup] = ii indL = list(range(len(docList))) except Exception as e: logger.exception("Failing ii %d d %r with %s", ii, doc, str(e)) try: with Connection(cfgOb=self.__cfgOb, resourceName=self.__resourceName) as client: mg = MongoDbUtil(client) # if loadType == "replace" and keyNames: dTupL = mg.deleteList(dbName, collectionName, docList, keyNames) logger.debug("Deleted document status %r", (dTupL, )) # rIdL = mg.insertList(dbName, collectionName, docList, keyNames=keyNames) logger.debug("Insert returns rIdL length %r", len(rIdL)) # --- # If there is a failure then determine the specific successes and failures - # successList = docList failList = [] if len(rIdL) != len(docList): if keyNames: successIndList = [] for rId in rIdL: rObj = mg.fetchOne(dbName, collectionName, "_id", rId) dIdTup = self.__getKeyValues(rObj, keyNames) successIndList.append(indD[dIdTup]) failIndList = list(set(indL) - set(successIndList)) failList = [docList[ii] for ii in failIndList] successList = [docList[ii] for ii in successIndList] else: # fail the whole batch if we don't have visibility into each document failList = docList successList = [] # rbStatus = True if readBackCheck and keyNames: # # Note that objects in docList are mutated by the insert operation with the additional key '_id', # hence, it is possible to compare the fetched object with the input object. # for ii, rId in enumerate(rIdL): rObj = mg.fetchOne(dbName, collectionName, "_id", rId) dIdTup = self.__getKeyValues(rObj, keyNames) jj = indD[dIdTup] if rObj != docList[jj]: rbStatus = False break # if readBackCheck and not rbStatus: return False, successList, failList # return len(rIdL) == len(docList), successList, failList except Exception as e: logger.exception("Failing %r %r (len=%d) %s with %s", dbName, collectionName, len(docList), keyNames, str(e)) return False, [], docList