def testRequestAndResponseClasses(self): requestClasses = protocol.getProtocolClasses(protocol.SearchRequest) responseClasses = protocol.getProtocolClasses(protocol.SearchResponse) self.assertEqual(len(requestClasses), len(responseClasses)) self.assertGreater(len(requestClasses), 0) for class_ in requestClasses: self.assertTrue(issubclass(class_, protocol.SearchRequest)) for class_ in responseClasses: self.assertTrue(issubclass(class_, protocol.SearchResponse)) valueListName = class_.getValueListName() self.assertGreater(len(valueListName), 0)
def testMoreFields(self): # Throws an exception when there are extra fields in the jsonDict for class_ in protocol.getProtocolClasses(): jsonDict = class_().toJsonDict() jsonDict['extra'] = 'extra' with self.assertRaises(avrotools.ValidationToolException): avrotools.ValidationTool.getInvalidFields(class_, jsonDict)
def testLessFields(self): # Returns a bogus field indicator # when there are fields missing from the jsonDict for class_ in protocol.getProtocolClasses(): validator = avrotools.Validator(class_) invalidFields = validator.getInvalidFields({}) for key, value in invalidFields.items(): self.assertEqual(value, avrotools.SchemaValidator.missingValue)
def testSameClasses(self): factories = [self.getDefaultInstance, self.getTypicalInstance, self.getRandomInstance] for cls in protocol.getProtocolClasses(): for factory in factories: i1 = factory(cls) i2 = cls.fromJsonDict(i1.toJsonDict()) self.verifyEqualityOperations(i1, i2)
def testMoreFields(self): # Throws an exception when there are extra fields in the jsonDict for class_ in protocol.getProtocolClasses(): jsonDict = class_().toJsonDict() jsonDict['extra'] = 'extra' validator = avrotools.Validator(class_) with self.assertRaises(avrotools.AvrotoolsException): validator.getInvalidFields(jsonDict)
def testEmbeddedValues(self): for cls in protocol.getProtocolClasses(): for member in cls.__slots__: if cls.isEmbeddedType(member): instance = cls.getEmbeddedType(member)() self.assertIsInstance(instance, protocol.ProtocolElement) else: self.assertRaises(KeyError, cls.getEmbeddedType, member)
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 testKeywordInstantiation(self): for cls in protocol.getProtocolClasses(): kwargs = {} instance = self.getDefaultInstance(cls) for key in instance.toJsonDict().keys(): val = getattr(instance, key) kwargs[key] = val secondInstance = cls(**kwargs) self.assertEqual(instance, secondInstance)
def testGeneratedObjects(self): # Test that generated objects pass validation instanceGenerator = utils.InstanceGenerator() for class_ in protocol.getProtocolClasses(): generatedInstance = instanceGenerator.generateInstance(class_) jsonDict = generatedInstance.toJsonDict() returnValue = avrotools.ValidationTool.getInvalidFields( class_, jsonDict) self.assertEqual(returnValue, {})
def testLessFields(self): # Returns a bogus field indicator # when there are fields missing from the jsonDict for class_ in protocol.getProtocolClasses(): validator = avrotools.Validator(class_) invalidFields = validator.getInvalidFields({}) for key, value in invalidFields.items(): self.assertEqual( value, avrotools.SchemaValidator.missingValue)
def validateClasses(self, factory): for cls in protocol.getProtocolClasses(): instance = factory(cls) jsonStr = instance.toJsonString() otherInstance = cls.fromJsonString(jsonStr) self.assertEqual(instance, otherInstance) jsonDict = instance.toJsonDict() otherInstance = cls.fromJsonDict(jsonDict) self.assertEqual(instance, otherInstance)
def testSameClasses(self): factories = [ self.getDefaultInstance, self.getTypicalInstance, self.getRandomInstance ] for cls in protocol.getProtocolClasses(): for factory in factories: i1 = factory(cls) i2 = cls.fromJsonDict(i1.toJsonDict()) self.verifyEqualityOperations(i1, i2)
def testMoreFields(self): # Returns a bogus field indicator # when there are extra fields in the jsonDict key = 'extra' for class_ in protocol.getProtocolClasses(): jsonDict = class_().toJsonDict() jsonDict[key] = None validator = avrotools.Validator(class_) invalidFields = validator.getInvalidFields(jsonDict) self.assertIn(key, invalidFields) self.assertEqual( invalidFields[key], avrotools.SchemaValidator.extraValue)
def testMoreFields(self): # Returns a bogus field indicator # when there are extra fields in the jsonDict key = 'extra' for class_ in protocol.getProtocolClasses(): jsonDict = class_().toJsonDict() jsonDict[key] = None validator = avrotools.Validator(class_) invalidFields = validator.getInvalidFields(jsonDict) self.assertIn(key, invalidFields) self.assertEqual(invalidFields[key], avrotools.SchemaValidator.extraValue)
def testDifferentValues(self): def factory(cls): return cls() factories = [factory, self.getTypicalInstance, self.getDefaultInstance, self.getRandomInstance] classes = list(protocol.getProtocolClasses()) c1 = classes[0] for c2 in classes[1:]: for factory in factories: i1 = factory(c1) i2 = factory(c2) self.assertFalse(i1 == i2) self.assertTrue(i1 != i2)
def testIntegrity(self): # Verifies that the values we put in are exactly what we get # back across all subclasses of SearchResponse for class_ in protocol.getProtocolClasses(protocol.SearchResponse): instances = [ self.getTypicalInstance(class_), self.getRandomInstance(class_)] for instance in instances: valueList = getattr(instance, class_.getValueListName()) builder = protocol.SearchResponseBuilder( class_, len(valueList), 2**32) for value in valueList: builder.addValue(value) builder.setNextPageToken(instance.nextPageToken) otherInstance = class_.fromJsonString(builder.getJsonString()) self.assertEqual(instance, otherInstance)
def testDifferentValues(self): def factory(cls): return cls() factories = [ factory, self.getTypicalInstance, self.getDefaultInstance, self.getRandomInstance ] classes = list(protocol.getProtocolClasses()) c1 = classes[0] for c2 in classes[1:]: for factory in factories: i1 = factory(c1) i2 = factory(c2) self.assertFalse(i1 == i2) self.assertTrue(i1 != i2)
def testIntegrity(self): # Verifies that the values we put in are exactly what we get # back across all subclasses of SearchResponse for class_ in protocol.getProtocolClasses(protocol.SearchResponse): instances = [ self.getTypicalInstance(class_), self.getRandomInstance(class_) ] for instance in instances: valueList = getattr(instance, class_.getValueListName()) builder = protocol.SearchResponseBuilder( class_, len(valueList), 2**32) for value in valueList: builder.addValue(value) builder.setNextPageToken(instance.nextPageToken) otherInstance = class_.fromJsonString(builder.getJsonString()) self.assertEqual(instance, otherInstance)
def testValidateBadValues(self): for cls in protocol.getProtocolClasses(): instance = self.getTypicalInstance(cls) jsonDict = instance.toJsonDict() self.assertFalse(cls.validate(None)) self.assertFalse(cls.validate([])) self.assertFalse(cls.validate(1)) # setting values to bad values should be invalid for key in jsonDict.keys(): dct = dict(jsonDict) dct[key] = self.getInvalidValue(cls, key) self.assertFalse(cls.validate(dct)) for c in utils.powerset(jsonDict.keys(), 10): if len(c) > 0: dct = dict(jsonDict) for f in c: dct[f] = self.getInvalidValue(cls, f) self.assertFalse(cls.validate(dct))
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 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 testLessFields(self): # Throws an exception when there are fields missing from the jsonDict for class_ in protocol.getProtocolClasses(): validator = avrotools.Validator(class_) with self.assertRaises(avrotools.AvrotoolsException): validator.getInvalidFields({})
def testAllClasses(self): classes = protocol.getProtocolClasses() self.assertGreater(len(classes), 0) for class_ in classes: self.assertTrue(issubclass(class_, protocol.ProtocolElement))
def validateClasses(self, factory): for cls in protocol.getProtocolClasses(): instance = factory(cls) jsonDict = instance.toJsonDict() self.assertTrue(cls.validate(jsonDict))
def testAllClasses(self): classes = protocol.getProtocolClasses() assert len(classes) > 0 for class_ in classes: self.assertTrue(issubclass(class_, protocol.ProtocolElement))