def validate(fp, verbose=False, validator_name=None, allow_warnings=False): try: validator_class = Registry.find(validator_name) except KeyError: sys.stderr.write('Validator %s not found.' % validator_name) return 4 val = GeoFeedValidator(fp, validator=validator_class, store_raw_records=True) write_console('Validating feed: ') result = val.validate() write_console_line('DONE.') records_with_warnings = 0 records_with_errors = 0 for record in result.records: line_status = 'OK' if record.has_errors or record.has_warnings: line_status = '%s%s' % ('E' if record.has_errors else ' ', 'W' if record.has_warnings else ' ') if record.has_errors: records_with_errors += 1 if records_with_warnings: records_with_warnings += 1 if verbose or record.has_errors or record.has_warnings: write_console_line('[%s %d %s] %s', val.record_name, record.record_no, line_status, record.raw) for field_result in record.field_results: for err_string in field_result.errors: write_console_line(' E %s - %s', field_result.field.name, err_string) for warn_string in field_result.warnings: write_console_line(' W %s - %s', field_result.field.name, warn_string) write_console_line('%s%ss: %d TOTAL, %d VALID, %d ERROR, %d WARNING', val.record_name[0].upper(), val.record_name[1:], len(result.records), len(result.records) - records_with_errors, records_with_errors, records_with_warnings) write_console_line('Counts: %d ERROR%s, %d WARNING%s', result.error_count, 's' if result.error_count != 1 else '', result.warning_count, 's' if result.warning_count != 1 else '') if val.is_valid(allow_warnings=allow_warnings): write_console_line('*** Feed VALID ***') return 0 write_console_line('*** Feed INVALID ***') return 3
def test_0003_init_by_class(self): class Validator(BaseValidator): NAME = 'TestValidator' FIELDS = () validator = GeoFeedValidator('', validator=Validator) self.assertEqual(Validator.NAME, validator._validator_name) self.assertEqual(Validator, validator._validator)
def validate(fp, verbose=False, validator_name=None, allow_warnings=False): try: validator_class = Registry.find(validator_name) except KeyError: sys.stderr.write('Validator %s not found.' % validator_name) return 4 val = GeoFeedValidator(fp, validator=validator_class, store_raw_records=True) write_console('Validating feed: ') result = val.validate() write_console_line('DONE.') records_with_warnings = 0 records_with_errors = 0 for record in result.records: line_status = 'OK' if record.has_errors or record.has_warnings: line_status = '%s%s' % ('E' if record.has_errors else ' ', 'W' if record.has_warnings else ' ') if record.has_errors: records_with_errors += 1 if records_with_warnings: records_with_warnings += 1 if verbose or record.has_errors or record.has_warnings: write_console_line('[%s %d %s] %s', val.record_name, record.record_no, line_status, record.raw) for field_result in record.field_results: for err_string in field_result.errors: write_console_line(' E %s - %s', field_result.field.name, err_string) for warn_string in field_result.warnings: write_console_line(' W %s - %s', field_result.field.name, warn_string) write_console_line('%s%ss: %d TOTAL, %d VALID, %d ERROR, %d WARNING', val.record_name[0].upper(), val.record_name[1:], len(result.records), len(result.records)-records_with_errors, records_with_errors, records_with_warnings) write_console_line('Counts: %d ERROR%s, %d WARNING%s', result.error_count, 's' if result.error_count != 1 else '', result.warning_count, 's' if result.warning_count != 1 else '') if val.is_valid(allow_warnings=allow_warnings): write_console_line('*** Feed VALID ***') return 0 write_console_line('*** Feed INVALID ***') return 3
def test_0005_init_string(self): tv = GeoFeedValidator('')
def test_0004_init_filelike(self): tv = GeoFeedValidator(six.StringIO())
def test_0002_init_by_name(self): for name in Registry.names(): validator = GeoFeedValidator('', validator=name) self.assertEqual(name, validator._validator_name) self.assertEqual(Registry.find(name), validator._validator)
def test_0001_init_default_validator(self): validator = GeoFeedValidator('', validator=None) self.assertEqual(GeoFeedValidator.DEFAULT_VALIDATOR, validator._validator_name) self.assertEqual(Registry.find(GeoFeedValidator.DEFAULT_VALIDATOR), validator._validator)
def test_0010_fields(self): validator = GeoFeedValidator('') self.assertEqual(list(), validator.fields) validator.validate() self.assertEqual(validator._validator.FIELDS, validator.fields)
def test_0009_record_name(self): validator = GeoFeedValidator('') self.assertEqual('???', validator.record_name) validator.validate() self.assertEqual(validator._validator.RECORD_NAME, validator.record_name)
def test_0008_validate_by_is_valid(self): validator = GeoFeedValidator('') self.assertTrue(validator.is_valid(allow_warnings=False)) self.assertIsInstance(validator.validate(), ValidationResult)
def test_0007_validate(self): validator = GeoFeedValidator('') res = validator.validate() self.assertIsInstance(res, ValidationResult) self.assertTrue(res.is_valid(allow_warnings=False))