def test_schema_to_markdown_file_837(tmpdir): descriptor = { "fields": [ { "name": "id", "description": "Any positive integer", "type": "integer", "constraints": { "minimum": 1 }, }, { "name": "age", "title": "Age", "description": "Any number >= 1", "type": "number", "constraints": { "minimum": 1 }, }, ] } md_file_path = "data/fixtures/output-markdown/schema.md" with open(md_file_path, encoding="utf-8") as file: expected = file.read() target = str(tmpdir.join("schema.md")) schema = Schema(descriptor) schema.to_markdown(path=target).strip() with open(target, encoding="utf-8") as file: output = file.read() assert expected == output
def test_schema_remove_field_error_not_found(): schema = Schema(DESCRIPTOR_MIN) with pytest.raises(FrictionlessException) as excinfo: schema.remove_field("bad") error = excinfo.value.error assert error.code == "schema-error" assert error.note == 'field "bad" does not exist'
def test_schema_to_markdown_table_837(): descriptor = { "fields": [ { "name": "id", "description": "Any positive integer", "type": "integer", "constraints": { "minimum": 1 }, }, { "name": "age", "title": "Age", "description": "Any number >= 1", "type": "number", "constraints": { "minimum": 1 }, }, ] } schema = Schema(descriptor) md_file_path = "data/fixtures/output-markdown/schema-table.md" with open(md_file_path, encoding="utf-8") as file: expected = file.read() assert schema.to_markdown(table=True).strip() == expected
def test_schema_infer_no_names(): sample = [[1], [2], [3]] schema = Schema() schema.infer(sample) assert schema == { "fields": [{"name": "field1", "type": "integer"}], }
def test_schema_summary_without_required(): descriptor = { "fields": [ { "name": "test_1", "type": "string", "format": "default" }, { "name": "test_2", "type": "string", "format": "default" }, { "name": "test_3", "type": "string", "format": "default" }, ] } schema = Schema(descriptor) output = schema.to_summary() assert (output.count("| name | type | required |") and output.count("| test_1 | string | |") and output.count("| test_2 | string | |") and output.count("| test_3 | string | |"))
def test_schema_infer_confidence_full(): sample = [ ["1", "39", "Paul"], ["2", "23", "Jimmy"], ["3", "36", "Jane"], ["4", "N/A", "Judy"], ] names = ["id", "age", "name"] schema = Schema() schema.infer(sample, names=names, confidence=1) assert schema == { "fields": [ { "name": "id", "type": "integer" }, { "name": "age", "type": "string" }, { "name": "name", "type": "string" }, ], }
def test_schema_summary(): schema = Schema(DESCRIPTOR_MAX) output = schema.to_summary() assert (output.count("| name | type | required |") and output.count("| id | string | True |") and output.count("| height | number | |") and output.count("| age | integer | |") and output.count("| name | string | |"))
def test_schema_read_cells_wrong_type(): schema = Schema(DESCRIPTOR_MAX) source = ["string", "notdecimal", "10.6", "string", "string"] target = ["string", None, None, "string", "string"] cells, notes = schema.read_cells(source) assert cells == target assert notes[1] == {"type": 'type is "number/default"'} assert notes[2] == {"type": 'type is "integer/default"'}
def test_validate_invalid(): schema = Schema({"fields": {}}) report = schema.validate() assert report.flatten(["code", "note"]) == [ [ "schema-error", '"{} is not of type \'array\'" at "fields" in metadata and at "properties/fields/type" in profile', ], ]
def test_schema_to_json(tmpdir): # Write target = str(tmpdir.join("schema.json")) schema = Schema(DESCRIPTOR_MIN) schema.to_json(target) # Read with open(target, encoding="utf-8") as file: assert schema == json.load(file)
def test_schema_to_yaml(tmpdir): # Write target = str(tmpdir.join("schema.yaml")) schema = Schema(DESCRIPTOR_MIN) schema.to_yaml(target) # Read with open(target, encoding="utf-8") as file: assert schema == yaml.safe_load(file)
def test_schema_descriptor_expand(): schema = Schema(DESCRIPTOR_MIN) schema.expand() schema == { "fields": [ {"name": "id", "type": "string", "format": "default"}, {"name": "height", "type": "integer", "format": "default"}, ], "missingValues": [""], }
def test_schema_standard_specs_properties(create_descriptor): options = dict( fields=[], missing_values=[], primary_key=[], foreign_keys=[], ) schema = (Schema(**options) if not create_descriptor else Schema( helpers.create_descriptor(**options))) assert schema.fields == [] assert schema.missing_values == [] assert schema.primary_key == [] assert schema.foreign_keys == []
def test_schema_metadata_error_message(): schema = Schema({"fields": [{"name": "name", "type": "other"}]}) note = schema.metadata_errors[0]["note"] assert len(schema.metadata_errors) == 1 assert "is not valid" in note assert "{'name': 'name', 'type': 'other'}" in note assert "is not valid under any of the given schema" in note
def test_convert_meta_type_to_goodtable_type(): with open("tests/data/inputs/basic_meta_data.json") as f: meta = json.load(f) gt_schema = convert_meta_to_goodtables_schema(meta) assert Schema(gt_schema).metadata_valid is True
def test_type_custom(): # Type class CustomType(Type): def read_cell(self, cell): return [cell] # Plugin class CustomPlugin(Plugin): def create_type(self, field): if field.type == "custom": return CustomType(field) # Testing system.register("custom", CustomPlugin()) schema = Schema(fields=[Field(type="integer"), Field(type="custom")]) resource = Resource(path="data/table.csv", schema=schema) assert resource.read_rows() == [ { "integer": 1, "custom": ["english"] }, { "integer": 2, "custom": ["中国人"] }, ]
def test_missing_label(): schema = Schema(fields=[Field(name="id"), Field(name="name"), Field(name="extra")]) with Resource(path="data/table.csv", schema=schema) as resource: header = resource.header assert header == ["id", "name", "extra"] assert header.labels == ["id", "name"] assert header.valid is False
def test_table_dialect_header_case_is_false(): dialect = Dialect(header_case=False) schema = Schema(fields=[Field(name="ID"), Field(name="NAME")]) with Table("data/table.csv", dialect=dialect, schema=schema) as table: assert table.schema.field_names == ["ID", "NAME"] assert table.header == ["id", "name"] assert table.header.valid is True
def test_schema_pprint_1029(): descriptor = { "fields": [ { "name": "test_1", "type": "string", "format": "default" }, { "name": "test_2", "type": "string", "format": "default" }, { "name": "test_3", "type": "string", "format": "default" }, ] } schema = Schema(descriptor) expected = """{'fields': [{'format': 'default', 'name': 'test_1', 'type': 'string'}, {'format': 'default', 'name': 'test_2', 'type': 'string'}, {'format': 'default', 'name': 'test_3', 'type': 'string'}]}""" assert repr(schema) == expected
def test_schema_from_sample_confidence_less(): sample = [ ["1", "39", "Paul"], ["2", "23", "Jimmy"], ["3", "36", "Jane"], ["4", "N/A", "Judy"], ] names = ["id", "age", "name"] schema = Schema.from_sample(sample, names=names, confidence=0.75) assert schema == { "fields": [ { "name": "id", "type": "integer" }, { "name": "age", "type": "integer" }, { "name": "name", "type": "string" }, ], }
def test_schema_metadata_not_valid(): assert not Schema("data/schema-invalid-empty.json").metadata_valid assert not Schema("data/schema-invalid-pk-string.json").metadata_valid assert not Schema("data/schema-invalid-pk-array.json").metadata_valid assert not Schema("data/schema-invalid-fk-string.json").metadata_valid assert not Schema("data/schema-invalid-fk-no-reference.json").metadata_valid assert not Schema("data/schema-invalid-fk-array.json").metadata_valid assert not Schema("data/schema-invalid-fk-string-array-ref.json").metadata_valid assert not Schema("data/schema-invalid-fk-array-string-ref.json").metadata_valid
def test_table_dialect_header_case_default(): schema = Schema(fields=[Field(name="ID"), Field(name="NAME")]) with Table("data/table.csv", schema=schema) as table: assert table.schema.field_names == ["ID", "NAME"] assert table.header == ["id", "name"] assert table.header.valid is False assert table.header.errors[0].code == "non-matching-header" assert table.header.errors[1].code == "non-matching-header"
def test_resource_layout_header_case_is_false(): layout = Layout(header_case=False) schema = Schema(fields=[Field(name="ID"), Field(name="NAME")]) with Resource("data/table.csv", layout=layout, schema=schema) as resource: assert resource.schema.field_names == ["ID", "NAME"] assert resource.labels == ["id", "name"] assert resource.header == ["ID", "NAME"] assert resource.header.valid is True
def test_schema_valid_examples(): schema = Schema({ "fields": [ { "name": "name", "type": "string", "example": "John" }, { "name": "age", "type": "integer", "example": 42 }, ] }) assert schema.get_field("name").example == "John" assert len(schema.metadata_errors) == 0
def test_resource_layout_header_case_default(): schema = Schema(fields=[Field(name="ID"), Field(name="NAME")]) with Resource("data/table.csv", schema=schema) as resource: assert resource.schema.field_names == ["ID", "NAME"] assert resource.labels == ["id", "name"] assert resource.header == ["ID", "NAME"] assert resource.header.valid is False assert resource.header.errors[0].code == "incorrect-label" assert resource.header.errors[1].code == "incorrect-label"
def test_missing_header(): schema = Schema( fields=[Field(name="id"), Field(name="name"), Field(name="extra")]) resource = Resource(path="data/table.csv", schema=schema) header = resource.read_header() assert header == ["id", "name"] assert header.valid is False
def test_schema_not_supported_type_issue_goodatbles_304(): schema = Schema( {"fields": [{ "name": "name" }, { "name": "age", "type": "bad" }]}) assert schema.metadata_valid is False assert schema.fields[1] == {"name": "age", "type": "bad"}
def test_schema_field_time_format_issue_177(): descriptor = { "fields": [{ "name": "myfield", "type": "time", "format": "%H:%M:%S" }] } schema = Schema(descriptor) assert schema
def test_schema_field_date_format_issue_177(): descriptor = { "fields": [{ "name": "myfield", "type": "date", "format": "%d/%m/%y" }] } schema = Schema(descriptor) assert schema
def test_schema_validation(): schema = Schema("erp/export/static/schema.json") resource = Resource( "erp/export/static/exemple-valide.csv", schema=schema, ) result_schema = validate_schema(schema) result_resource = validate_resource(resource) assert result_schema.get("valid") is True assert result_resource.get("valid") is True