def test_validation_errors(): assert json_schema.validation_errors({}) \ == [] assert json_schema.validation_errors({'type': 'null'}) \ == [] assert json_schema.validation_errors({'type': ['object'], 'properties': { 'a': {'type': 'string'}}}) \ == [] assert json_schema.validation_errors( { 'definitions': { 'address': { 'type': 'object', 'properties': { 'street_address': {'type': 'string'}, 'city': {'type': 'string'}, 'state': {'type': 'string'} }, 'required': ['street_address', 'city', 'state'] } }, 'type': 'object', 'properties': { 'billing_address': {'$ref': '#/definitions/address'}, 'shipping_address': {'$ref': '#/definitions/address'}}}) \ == []
def test_validation_errors__invalid_schemas(): invalid_type_test = json_schema.validation_errors({'type': 'well this should not work'}) assert invalid_type_test assert not _non_string_elements(invalid_type_test) recursive_schema_test = json_schema.validation_errors({ 'definitions': { 'person': { 'type': 'object', 'properties': { 'name': {'type': 'string'}, 'children': { 'type': 'array', 'items': {'$ref': '#/definitions/person'}, 'default': [] } } } }, 'type': 'object', 'properties': { 'person': {'$ref': '#/definitions/person'}}}) assert recursive_schema_test assert not _non_string_elements(recursive_schema_test) non_standard_schema_version = json_schema.validation_errors({'$schema': 'clearly not a valid schema version'}) assert non_standard_schema_version assert not _non_string_elements(non_standard_schema_version)
def test_validation_errors__invalid_draft_version(): draft_3 = json_schema.validation_errors({'$schema': 'http://json-schema.org/draft-03/schema#'}) assert draft_3 assert not _non_string_elements(draft_3) draft_6 = json_schema.validation_errors({'$schema': 'http://json-schema.org/draft-06/schema#'}) assert draft_6 assert not _non_string_elements(draft_6) draft_7 = json_schema.validation_errors({'$schema': 'http://json-schema.org/draft-07/schema#'}) assert draft_7 assert not _non_string_elements(draft_7)
def test_validation_errors__invalid_objects(): def _invalid_type_ex(ret): return re.match(r'.*not a dict.*', ret[0]) non_dict_object__int = json_schema.validation_errors(12345) assert _invalid_type_ex(non_dict_object__int) assert not _non_string_elements(non_dict_object__int) non_dict_object__string = json_schema.validation_errors('woah no') assert _invalid_type_ex(non_dict_object__string) assert not _non_string_elements(non_dict_object__string) non_dict_object__tuple = json_schema.validation_errors(('hello', 'world')) assert _invalid_type_ex(non_dict_object__tuple) assert not _non_string_elements(non_dict_object__tuple) non_dict_object__list = json_schema.validation_errors([1, 2, 3]) assert _invalid_type_ex(non_dict_object__list) assert not _non_string_elements(non_dict_object__list)
def _line_handler(state_tracker, target, invalid_records_detect, invalid_records_threshold, max_batch_rows, max_batch_size, line): try: line_data = json.loads(line) except json.decoder.JSONDecodeError: LOGGER.error("Unable to parse JSON: {}".format(line)) raise if 'type' not in line_data: raise TargetError('`type` is a required key: {}'.format(line)) if line_data['type'] == 'SCHEMA': if 'stream' not in line_data: raise TargetError('`stream` is a required key: {}'.format(line)) stream = line_data['stream'] if 'schema' not in line_data: raise TargetError('`schema` is a required key: {}'.format(line)) schema = line_data['schema'] schema_validation_errors = json_schema.validation_errors(schema) if schema_validation_errors: raise TargetError( '`schema` is an invalid JSON Schema instance: {}'.format(line), *schema_validation_errors) if 'key_properties' in line_data: key_properties = line_data['key_properties'] else: key_properties = None if stream not in state_tracker.streams: buffered_stream = BufferedSingerStream( stream, schema, key_properties, invalid_records_detect=invalid_records_detect, invalid_records_threshold=invalid_records_threshold) if max_batch_rows: buffered_stream.max_rows = max_batch_rows if max_batch_size: buffered_stream.max_buffer_size = max_batch_size state_tracker.register_stream(stream, buffered_stream) else: state_tracker.streams[stream].update_schema(schema, key_properties) elif line_data['type'] == 'RECORD': if 'stream' not in line_data: raise TargetError('`stream` is a required key: {}'.format(line)) state_tracker.handle_record_message(line_data['stream'], line_data) elif line_data['type'] == 'ACTIVATE_VERSION': if 'stream' not in line_data: raise TargetError('`stream` is a required key: {}'.format(line)) if 'version' not in line_data: raise TargetError('`version` is a required key: {}'.format(line)) if line_data['stream'] not in state_tracker.streams: raise TargetError( 'A ACTIVATE_VERSION for stream {} was encountered before a corresponding schema' .format(line_data['stream'])) stream_buffer = state_tracker.streams[line_data['stream']] state_tracker.flush_stream(line_data['stream']) target.activate_version(stream_buffer, line_data['version']) elif line_data['type'] == 'STATE': state_tracker.handle_state_message(line_data) else: raise TargetError('Unknown message type {} in message {}'.format( line_data['type'], line))