Beispiel #1
0
def test_json_parser_keyed_with_keys_provided():
    dialect = JsonDialect(keys=["name", "id"])
    with Resource(path="data/table.keyed.json", dialect=dialect) as resource:
        assert resource.dialect.keyed is True
        assert resource.header == ["name", "id"]
        assert resource.read_rows() == [
            {"id": 1, "name": "english"},
            {"id": 2, "name": "中国人"},
        ]
Beispiel #2
0
def test_table_jsonl_write_keyed(tmpdir):
    source = "data/table.csv"
    target = str(tmpdir.join("table.jsonl"))
    dialect = JsonDialect(keyed=True)
    with Table(source) as table:
        table.write(target, dialect=dialect)
    with Table(target, dialect=dialect) as table:
        assert table.header == ["id", "name"]
        assert table.read_data() == [[1, "english"], [2, "中国人"]]
Beispiel #3
0
def test_json_parser_write_keyed(tmpdir):
    dialect = JsonDialect(keyed=True)
    source = Resource("data/table.csv")
    target = Resource(path=str(tmpdir.join("table.json")), dialect=dialect)
    source.write(target)
    with open(target.fullpath) as file:
        assert json.load(file) == [
            {"id": 1, "name": "english"},
            {"id": 2, "name": "中国人"},
        ]
Beispiel #4
0
def test_json_parser_write_decimal(tmpdir):
    dialect = JsonDialect(keyed=True)
    source = Resource([["id", "name"], [1.5, "english"], [2.5, "german"]])
    target = Resource(path=str(tmpdir.join("table.json")), dialect=dialect)
    source.write(target)
    with open(target.fullpath) as file:
        assert json.load(file) == [
            {"id": "1.5", "name": "english"},
            {"id": "2.5", "name": "german"},
        ]
Beispiel #5
0
def test_jsonl_parser_write_keyed(tmpdir):
    dialect = JsonDialect(keyed=True)
    source = Resource("data/table.csv")
    target = Resource(str(tmpdir.join("table.jsonl")), dialect=dialect)
    source.write(target)
    with target:
        assert target.header == ["id", "name"]
        assert target.read_rows() == [
            {"id": 1, "name": "english"},
            {"id": 2, "name": "中国人"},
        ]
Beispiel #6
0
def test_table_json_write_decimal(tmpdir):
    source = [["id", "name"], [1.5, "english"], [2.5, "german"]]
    target = str(tmpdir.join("table.json"))
    dialect = JsonDialect(keyed=True)
    with Table(source) as table:
        table.write(target, dialect=dialect)
    with open(target) as file:
        assert json.load(file) == [
            {
                "id": "1.5",
                "name": "english"
            },
            {
                "id": "2.5",
                "name": "german"
            },
        ]
Beispiel #7
0
def test_table_json_write_keyed(tmpdir):
    source = "data/table.csv"
    target = str(tmpdir.join("table.json"))
    dialect = JsonDialect(keyed=True)
    with Table(source) as table:
        table.write(target, dialect=dialect)
    with open(target) as file:
        assert json.load(file) == [
            {
                "id": 1,
                "name": "english"
            },
            {
                "id": 2,
                "name": "中国人"
            },
        ]
Beispiel #8
0
def test_table_json_keyed_with_keys_provided():
    dialect = JsonDialect(keys=["name", "id"])
    with Table("data/table.keyed.json", dialect=dialect) as table:
        assert table.dialect.keyed is True
        assert table.header == ["name", "id"]
        assert table.read_data() == [["english", 1], ["中国人", 2]]
Beispiel #9
0
def test_table_dialect_json_property():
    source = '{"root": [["header1", "header2"], ["value1", "value2"]]}'
    dialect = JsonDialect(property="root")
    with Table(source, scheme="text", format="json", dialect=dialect) as table:
        assert table.header == ["header1", "header2"]
        assert table.read_data() == [["value1", "value2"]]