def getDefaultInstance(self, cls): """ Returns a new instance with the required values set. """ tool = avrotools.Creator(cls) instance = tool.getDefaultInstance() return instance
def getTypicalInstance(self, cls): """ Returns a typical instance of the specified protocol class. """ tool = avrotools.Creator(cls) instance = tool.getTypicalInstance() return instance
def _createInstance(self, requestClass): """ Returns a valid instance of the specified class. """ creator = avrotools.Creator(requestClass) instance = creator.getTypicalInstance() return instance
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
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, {})
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)
def getInvalidValue(self, cls, fieldName): """ Returns a value that should trigger a schema validation failure. """ value = avrotools.Creator(cls).getInvalidField(fieldName) return value