Exemple #1
0
    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()
Exemple #2
0
    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()
Exemple #3
0
 def testCreateDropDatabase(self):
     """Test case -  create/drop database -"""
     try:
         with Connection(cfgOb=self.__cfgOb, resourceName=self.__resourceName) as client:
             mg = MongoDbUtil(client)
             ok = mg.createDatabase(self.__dbName)
             self.assertTrue(ok)
             ok = mg.dropDatabase(self.__dbName)
             self.assertTrue(ok)
             ok = mg.databaseExists(self.__dbName)
             self.assertFalse(ok)
             #
             ok = mg.createDatabase(self.__dbName)
             self.assertTrue(ok)
             ok = mg.dropDatabase(self.__dbName)
             self.assertTrue(ok)
             ok = mg.databaseExists(self.__dbName)
             self.assertFalse(ok)
     except Exception as e:
         logger.exception("Failing with %s", str(e))
         self.fail()