def __makeDataObj(self, nCats, nAttribs, nRows, docId=1): rD = {} for cat in range(nCats): catName = "category_%d" % cat rD[catName] = [] for row in range(nRows): dD = {} for attrib in range(nAttribs): val = "val_%d_%d" % (row, attrib) attribName = "attribute_%d" % attrib dD[attribName] = val rD[catName].append(dD) dD = {} for attrib in range(nAttribs): val = "2018-01-30 12:01" attribName = "attribute_%d" % attrib dD[attribName] = dateutil.parser.parse(val) rD[catName].append(dD) # dD = {} for attrib in range(nAttribs): val = " " Φ Ψ α £ ℅ ☆ 𝕫 " attribName = "attribute_%d" % attrib dD[attribName] = unescapeXmlCharRef(val).encode( "utf-8").decode("utf-8") rD[catName].append(dD) rD["DOC_ID"] = "DOC_%d" % docId return rD
def translateXMLCharRefs(self, trfTup): """Convert XML Character references to unicode. Return: ReturnValue tuple """ if trfTup.isNull: return trfTup return TrfValue(unescapeXmlCharRef(trfTup.value), trfTup.atId, trfTup.origLength, False)
def testSchemaValidation1(self): """Test case - create collection and insert data with schema validation (ext. schema assignment) """ # Example of a Mongo flavor of JsonSchema vexpr = {"$jsonSchema": self.__mongoSchema} query = [("collMod", self.__collectionName), ("validator", vexpr), ("validationLevel", "moderate")] query = OrderedDict(query) try: with Connection(cfgOb=self.__cfgOb, resourceName=self.__resourceName) as client: mg = MongoDbUtil(client) if mg.databaseExists(self.__dbName): ok = mg.dropDatabase(self.__dbName) self.assertTrue(ok) # ok = mg.createDatabase(self.__dbName) self.assertTrue(ok) # 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) # mg.databaseCommand(self.__dbName, query) dObj = {"x": 1} rId = mg.insert(self.__dbName, self.__collectionName, dObj) logger.info("rId is %r", rId) self.assertEqual(rId, None) # s2 = unescapeXmlCharRef( " " Φ Ψ α £ ℅ ☆ 𝕫" ) dObj = { "strField1": "test value", "strField2": s2, "intField1": 50, "enumField1": "v3", "dblField1": 100.1 } rId = mg.insert(self.__dbName, self.__collectionName, dObj) logger.info("rId is %r", rId) 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) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def translateXMLCharRefsIt(self, trfTup): """Convert XML Character references to unicode. Return: ReturnValue tuple """ if trfTup.isNull: return trfTup # vL = [unescapeXmlCharRef(v) for v in trfTup.value] return TrfValue(vL, trfTup.atId, trfTup.origLength, False)
def testSchemaValidation3(self): """Test case - create collection and insert data with schema validation (warn mode) (integrated schema assignment) """ try: with Connection(cfgOb=self.__cfgOb, resourceName=self.__resourceName) as client: mg = MongoDbUtil(client) if mg.databaseExists(self.__dbName): ok = mg.dropDatabase(self.__dbName) self.assertTrue(ok) # ok = mg.createDatabase(self.__dbName) self.assertTrue(ok) # ok = mg.createCollection(self.__dbName, self.__collectionName, overWrite=True, bsonSchema=self.__mongoSchema, validationAction="warn") self.assertTrue(ok) ok = mg.databaseExists(self.__dbName) self.assertTrue(ok) ok = mg.collectionExists(self.__dbName, self.__collectionName) self.assertTrue(ok) # dObj = {"x": 1} rId = mg.insert(self.__dbName, self.__collectionName, dObj) logger.info("rId is %r", rId) self.assertNotEqual(rId, None) # s2 = unescapeXmlCharRef( " " Φ Ψ α £ ℅ ☆ 𝕫" ) dObj = { "strField1": "test value", "strField2": s2, "intField1": 50, "enumField1": "v3a", "dblField1": 100.1 } rId = mg.insert(self.__dbName, self.__collectionName, dObj) self.assertNotEqual(rId, None) logger.info("rId is %r", rId) 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) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def __unescape(self, iStr): """ Convert html character entities into unicode. """ return unescapeXmlCharRef(iStr)