def test_missing_header(log): errors = [] columns = [ {'number': 1, 'header': 'name1', 'field': Field({'name': 'name1'})}, {'number': 2, 'header': 'name2', 'field': Field({'name': 'name2'})}, ] checks.missing_header(errors, columns) assert log(errors) == [] assert len(columns) == 2
def test_non_matching_header_order_fields(log): errors = [] columns = [ {'number': 1, 'header': 'name1', 'field': Field({'name': 'name2'})}, {'number': 2, 'header': 'name2', 'field': Field({'name': 'name1'})}, {'number': 3, 'header': 'name3'}, ] checks.non_matching_header(errors, columns, order_fields=True) assert log(errors) == [] assert len(columns) == 3
def test_extra_header(log): errors = [] columns = [ { 'number': 1, 'header': 'name1', 'field': Field({'name': 'name1'}) }, { 'number': 2, 'header': 'name2', 'field': Field({'name': 'name2'}) }, ] sample = [] checks.extra_header(errors, columns, sample=sample) assert log(errors) == [] assert len(columns) == 2
def test_non_matching_header_problem(log): errors = [] columns = [ {'number': 1, 'header': 'name1', 'field': Field({'name': 'name2'})}, {'number': 2, 'header': 'name2', 'field': Field({'name': 'name1'})}, {'number': 3, 'header': 'name3'}, ] checks.non_matching_header(errors, columns) assert log(errors) == [ (None, 1, 'non-matching-header'), (None, 2, 'non-matching-header'), ] assert len(columns) == 1
def test_test_value_unique(): field = Field({ 'name': 'name', 'type': 'integer', 'constraints': {'unique': True} }) test = partial(field.test_value, constraint='unique') assert test(30000) == True assert test('bad') == False
def test_test_value_maxLength(): field = Field({ 'name': 'name', 'type': 'string', 'constraints': {'maxLength': 1} }) test = partial(field.test_value, constraint='maxLength') assert test('') == True assert test('a') == True assert test('ab') == False
def test_test_value_pattern(): field = Field({ 'name': 'name', 'type': 'integer', 'constraints': {'pattern': '3.*'} }) test = partial(field.test_value, constraint='pattern') assert test('3') == True assert test('321') == True assert test('123') == False
def test_string_missingValues(): field = Field({ 'name': 'name', 'type': 'string', 'missingValues': ['NA', 'N/A'] }) cast = field.cast_value assert cast('') == '' assert cast('NA') == None assert cast('N/A') == None
def test_number_missingValues(): field = Field({ 'name': 'name', 'type': 'number', 'missingValues': ['NA', 'N/A'] }) cast = field.cast_value assert cast('') == None assert cast('NA') == None assert cast('N/A') == None
def test_test_value_enum(): field = Field({ 'name': 'name', 'type': 'integer', 'constraints': {'enum': ['1', '2', '3']} }) test = partial(field.test_value, constraint='enum') assert test('1') == True assert test(1) == True assert test('4') == False assert test(4) == False
def test_test_value_maximum(): field = Field({ 'name': 'name', 'type': 'integer', 'constraints': {'maximum': 1} }) test = partial(field.test_value, constraint='maximum') assert test('0') == True assert test(0) == True assert test('1') == True assert test(1) == True assert test('2') == False assert test(2) == False
def test_test_value_required(): field = Field({ 'name': 'name', 'type': 'string', 'constraints': {'required': True} }) test = partial(field.test_value, constraint='required') assert test('test') == True assert test('null') == False assert test('none') == False assert test('nil') == False assert test('nan') == False assert test('-') == False assert test('') == False assert test(None) == False
def test_non_castable_value(log): errors = [] columns = [ { 'number': 1, 'header': 'name1', 'value': '1', 'field': Field({ 'name': 'name', 'type': 'integer' }) }, ] checks.non_castable_value(errors, columns, 1) assert log(errors) == [] assert len(columns) == 1 assert columns[0]['value'] == 1
def test_non_castable_value_problem(log): errors = [] columns = [ { 'number': 1, 'header': 'name1', 'value': 'value1', 'field': Field({ 'name': 'name', 'type': 'integer' }) }, ] checks.non_castable_value(errors, columns, 1) assert log(errors) == [ (1, 1, 'non-castable-value'), ] assert len(columns) == 0
def test_extra_header_infer(log): errors = [] columns = [ { 'number': 1, 'header': 'name1', 'field': Field({'name': 'name1'}) }, { 'number': 2, 'header': 'name2' }, ] sample = [] checks.extra_header(errors, columns, sample=sample, infer_fields=True) assert log(errors) == [] assert len(columns) == 2 assert columns[1]['field'].name == 'name2'
def test_required(): assert Field(DESCRIPTOR_MIN).required == False assert Field(DESCRIPTOR_MAX).required == True
def test_descriptor(): assert Field(DESCRIPTOR_MIN).descriptor == DESCRIPTOR_MIN
def test_name(): assert Field(DESCRIPTOR_MIN).name == 'id'
def test_test_value_not_supported_constraint(): assert Field(DESCRIPTOR_MIN).test_value('', constraint='bad') == True
def test_test_value_skip_constraints(): assert Field(DESCRIPTOR_MIN).test_value('', skip_constraints=True) == True
def test_test_value(): assert Field(DESCRIPTOR_MAX).test_value('1') == True assert Field(DESCRIPTOR_MAX).test_value('string') == False assert Field(DESCRIPTOR_MAX).test_value('') == False
def test_cast_value_null_case_insensitive(): assert Field({'name': 'name', 'type': 'number'}).cast_value('Null') == None
def test_cast_value(): # Success assert Field(DESCRIPTOR_MAX).cast_value('1') == 1 # Constraint error with pytest.raises(exceptions.ConstraintError): Field(DESCRIPTOR_MAX).cast_value('')
def test_type(): assert Field(DESCRIPTOR_MIN).type == 'string' assert Field(DESCRIPTOR_MAX).type == 'integer'
def test_format(): assert Field(DESCRIPTOR_MIN).format == 'default' assert Field(DESCRIPTOR_MAX).format == 'object'
def test_constraints(): assert Field(DESCRIPTOR_MIN).constraints == {} assert Field(DESCRIPTOR_MAX).constraints == {'required': True}