def test_0002_missing_field(self): nw_field = NetworkField() city_field = CityField() res = RecordValidationResult(456, (nw_field, city_field), {nw_field: '8.8.8.0/24'}, '8.8.8.0/24') res.validate() nw_field_result = res.get_field_result('network') city_field_result = res.get_field_result(city_field) self.assertEqual(0, len(nw_field_result.errors)) self.assertEqual(netaddr.IPNetwork('8.8.8.0/24'), nw_field_result.value) self.assertEqual(netaddr.IPNetwork('8.8.8.0/24'), res.get_field_value('network')) self.assertEqual(netaddr.IPNetwork('8.8.8.0/24'), res.get_field_value(nw_field)) self.assertEqual(1, len(city_field_result.errors)) self.assertEqual(None, city_field_result.value) self.assertEqual(['Field is missing.'], city_field_result.errors) self.assertEqual(None, res.get_field_value('city')) self.assertEqual(None, res.get_field_value(city_field)) self.assertTrue(res.has_errors) self.assertFalse(res.has_warnings) self.assertEqual(1, res.error_count) self.assertEqual(0, res.warning_count)
def test_0003_get_field_result_invalid(self): nw_field = NetworkField() city_field = CityField() res = RecordValidationResult(456, (nw_field, city_field), {nw_field: '8.8.8.0/24'}, '8.8.8.0/24') res.validate() self.assertEqual(None, res.get_field_result('subdivision')) self.assertEqual(None, res.get_field_value('subdivision'))
def test_0000_no_store_records(self): nw_field = NetworkField() city_field = CityField() vr = ValidationResult((nw_field, city_field), store_raw_records=False) vr.add_record({nw_field: '8.8.8.0/24'}, '8.8.8.0/24') self.assertFalse(vr.is_valid()) self.assertEqual(1, vr.error_count) self.assertEqual(0, vr.warning_count) self.assertEqual(list(), vr.records_raw) self.assertEqual(1, len(vr.records)) self.assertEqual((nw_field, city_field), vr.fields)
def test_0004_missing_geo_info(self): fields = (CountryField(), SubdivisionField(), CityField(), ZipCodeField()) c_field, sd_field, city_field, zc_field = fields class TestValidator(BaseValidator): NAME = 'test' FIELDS = fields def get_records(self): return [ ( # First case: subdivision w/out country { c_field: '', sd_field: 'AT-1', city_field: '', zc_field: '' }, ',AT-1,,'), ( # Second case: city w/out country { c_field: '', sd_field: '', city_field: 'Test', zc_field: '' }, ',,Test,'), ( # Third case: zipcode w/out country { c_field: '', sd_field: '', city_field: '', zc_field: 'Test' }, ',,,Test'), ( # Fourth case: country/subdivision mismatch { c_field: 'DE', sd_field: 'AT-1', city_field: '', zc_field: '' }, 'DE,AT-1,,') ] tv = TestValidator('') res = tv.validate() self.assertFalse(res.is_valid(allow_warnings=True)) case1_record, case2_record, case3_record, case4_record = res.records self.assertEqual(4, res.error_count) self.assertEqual(0, res.warning_count) for r in res.records: self.assertEqual(1, r.error_count) self.assertEqual(0, r.warning_count) self.assertEqual( ['Subdivision specified, but country missing/invalid.'], case1_record.get_field_result(SubdivisionField).errors) self.assertEqual(['City specified, but country missing/invalid.'], case2_record.get_field_result(CityField).errors) self.assertEqual(['Zipcode specified, but country missing/invalid.'], case3_record.get_field_result(ZipCodeField).errors) self.assertEqual( ['Subdivision not a subdivison of given country.'], case4_record.get_field_result(SubdivisionField).errors)
def test_0003_add_field_warnings_add_field_errors(self): nw_field = NetworkField() city_field = CityField() zipcode_field = ZipCodeField() subdivision_field = SubdivisionField() vr = ValidationResult((nw_field, city_field), store_raw_records=True) vr.add_record({nw_field: '8.8.8.0/24'}, '8.8.8.0/24') vr.add_record({nw_field: '8.8.8.1/32'}, '8.8.8.1/32') self.assertFalse(vr.is_valid()) self.assertEqual(2, vr.error_count) self.assertEqual(0, vr.warning_count) self.assertEqual(2, len(vr.records)) self.assertEqual(['8.8.8.0/24', '8.8.8.1/32'], vr.records_raw) self.assertEqual((nw_field, city_field), vr.fields) records = vr.records # 2 errors: base # 0 warnings: base for record in records: record.add_field_errors(nw_field, 'test_error') # 2 more errors record.add_field_errors( city_field, ['test_error1', 'test_error2']) # 4 more errors record.add_field_warnings(nw_field, 'test_warning') # 2 more warnings record.add_field_warnings( city_field, ['test_warning1', 'test_warning2']) # 4 more warnings record.add_field_errors(zipcode_field, ('test_error3', )) # 2 more errors record.add_field_warnings(subdivision_field, ('test_warning3', )) # 2 more warnings self.assertFalse(vr.is_valid()) self.assertEqual(10, vr.error_count) self.assertEqual(8, vr.warning_count) for record in records: self.assertTrue(record.has_errors) self.assertEqual(5, record.error_count) self.assertEqual(4, record.warning_count) self.assertFalse(record.was_ignored) nw_field_result = record.get_field_result('network') self.assertEqual(1, len(nw_field_result.errors)) self.assertEqual(1, len(nw_field_result.warnings)) self.assertEqual(['test_error'], nw_field_result.errors) self.assertEqual(['test_warning'], nw_field_result.warnings) city_field_result = record.get_field_result(city_field) self.assertEqual(3, len(city_field_result.errors)) self.assertEqual(2, len(city_field_result.warnings)) self.assertEqual( ['Field is missing.', 'test_error1', 'test_error2'], city_field_result.errors) self.assertEqual(['test_warning1', 'test_warning2'], city_field_result.warnings) zipcode_field_result = record.get_field_result(zipcode_field) self.assertEqual(1, len(zipcode_field_result.errors)) self.assertEqual(0, len(zipcode_field_result.warnings)) self.assertEqual(['test_error3'], zipcode_field_result.errors) subdivision_field_result = record.get_field_result( subdivision_field) self.assertEqual(0, len(subdivision_field_result.errors)) self.assertEqual(1, len(subdivision_field_result.warnings)) self.assertEqual(['test_warning3'], subdivision_field_result.warnings)