示例#1
0
 def getDefaultInstance(self, cls):
     """
     Returns a new instance with the required values set.
     """
     tool = avrotools.Creator(cls)
     instance = tool.getDefaultInstance()
     return instance
示例#2
0
 def getTypicalInstance(self, cls):
     """
     Returns a typical instance of the specified protocol class.
     """
     tool = avrotools.Creator(cls)
     instance = tool.getTypicalInstance()
     return instance
示例#3
0
 def _createInstance(self, requestClass):
     """
     Returns a valid instance of the specified class.
     """
     creator = avrotools.Creator(requestClass)
     instance = creator.getTypicalInstance()
     return instance
示例#4
0
 def getRandomInstance(self, cls):
     """
     Returns an instance of the specified class with randomly generated
     values conforming to the schema.
     """
     tool = avrotools.Creator(cls)
     instance = tool.getRandomInstance()
     return instance
示例#5
0
 def testGeneratedObjects(self):
     # Test that generated objects pass validation
     for class_ in protocol.getProtocolClasses():
         creator = avrotools.Creator(class_)
         validator = avrotools.Validator(class_)
         generatedInstance = creator.getTypicalInstance()
         jsonDict = generatedInstance.toJsonDict()
         returnValue = validator.getInvalidFields(jsonDict)
         self.assertEqual(returnValue, {})
示例#6
0
 def testOptionalFields(self):
     # test that omission of optional fields does not throw
     # an exception, but omission of a required field returns
     # a dict only mentioning that field
     for class_ in protocol.getProtocolClasses():
         validator = avrotools.Validator(class_)
         creator = avrotools.Creator(class_)
         generatedInstance = creator.getTypicalInstance()
         jsonDict = generatedInstance.toJsonDict()
         requiredFields = class_.requiredFields
         for key in jsonDict.keys():
             if key not in requiredFields:
                 del jsonDict[key]
         returnValue = validator.getInvalidFields(jsonDict)
         self.assertEqual(returnValue, {})
         if len(requiredFields) != 0:
             requiredKey = list(requiredFields)[0]
             del jsonDict[requiredKey]
             returnValue = validator.getInvalidFields(jsonDict)
             self.assertEqual(returnValue[requiredKey],
                              avrotools.SchemaValidator.missingValue)
             self.assertEqual(len(returnValue), 1)
示例#7
0
 def getInvalidValue(self, cls, fieldName):
     """
     Returns a value that should trigger a schema validation failure.
     """
     value = avrotools.Creator(cls).getInvalidField(fieldName)
     return value