Esempio n. 1
0
def _write_read_file_into_url(base_url, filename, compression=None, pkg=None):
    if not _is_installed(pkg, filename):
        return
    source_url = _build_source_url_from(base_url, filename, compression)
    if source_url is None:
        return
    actual = None
    if ".avro" in filename:
        toavro(_table, source_url)
        actual = fromavro(source_url)
    elif ".xlsx" in filename:
        toxlsx(_table, source_url, 'test1', mode='overwrite')
        toxlsx(_table2, source_url, 'test2', mode='add')
        actual = fromxlsx(source_url, 'test1')
    elif ".xls" in filename:
        toxls(_table, source_url, 'test')
        actual = fromxls(source_url, 'test')
    elif ".json" in filename:
        tojson(_table, source_url)
        actual = fromjson(source_url)
    elif ".csv" in filename:
        tocsv(_table, source_url, encoding="ascii", lineterminator="\n")
        actual = fromcsv(source_url, encoding="ascii")

    if actual is not None:
        _show__rows_from("Expected:", _table)
        _show__rows_from("Actual:", actual)
        ieq(_table, actual)
        ieq(_table, actual)  # verify can iterate twice
    else:
        print("\n    - %s SKIPPED " % filename, file=sys.stderr, end="")
Esempio n. 2
0
def test_tojson():

    # exercise function
    table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2))
    f = NamedTemporaryFile(delete=False)
    tojson(table, f.name)
    result = json.load(f)
    assert len(result) == 3
    assert result[0]['foo'] == 'a'
    assert result[0]['bar'] == 1
    assert result[1]['foo'] == 'b'
    assert result[1]['bar'] == 2
    assert result[2]['foo'] == 'c'
    assert result[2]['bar'] == 2
Esempio n. 3
0
def test_tojson():

    # exercise function
    table = (('foo', 'bar'),
             ('a', 1),
             ('b', 2),
             ('c', 2))
    f = NamedTemporaryFile(delete=False, mode='r')
    tojson(table, f.name)
    result = json.load(f)
    assert len(result) == 3
    assert result[0]['foo'] == 'a'
    assert result[0]['bar'] == 1
    assert result[1]['foo'] == 'b'
    assert result[1]['bar'] == 2
    assert result[2]['foo'] == 'c'
    assert result[2]['bar'] == 2
Esempio n. 4
0
def _write_read_file_into_url(base_url, filename, compression=None, pkg=None):
    if pkg is not None:
        if not _is_installed(pkg):
            print("\n    - %s SKIPPED " % filename, file=sys.stderr, end="")
            return
    is_local = base_url.startswith("./")
    if compression is not None:
        if is_local:
            return
        filename = filename + "." + compression
        codec = fsspec.utils.infer_compression(filename)
        if codec is None:
            print("\n    - %s SKIPPED " % filename, file=sys.stderr, end="")
            return
    print("\n    - %s " % filename, file=sys.stderr, end="")

    if is_local:
        source_url = base_url + filename
    else:
        source_url = os.path.join(base_url, filename)

    actual = None
    if ".avro" in filename:
        toavro(_table, source_url)
        actual = fromavro(source_url)
    elif ".xlsx" in filename:
        toxlsx(_table, source_url, 'test1', mode='overwrite')
        toxlsx(_table2, source_url, 'test2', mode='add')
        actual = fromxlsx(source_url, 'test1')
    elif ".xls" in filename:
        toxls(_table, source_url, 'test')
        actual = fromxls(source_url, 'test')
    elif ".json" in filename:
        tojson(_table, source_url)
        actual = fromjson(source_url)
    elif ".csv" in filename:
        tocsv(_table, source_url, encoding="ascii", lineterminator="\n")
        actual = fromcsv(source_url, encoding="ascii")

    if actual is not None:
        _show__rows_from("Expected:", _table)
        _show__rows_from("Actual:", actual)
        ieq(_table, actual)
        ieq(_table, actual)  # verify can iterate twice
    else:
        print("\n    - %s SKIPPED " % filename, file=sys.stderr, end="")
Esempio n. 5
0
def test_json_unicode():

    tbl = ((u'name', u'id'),
           (u'Արամ Խաչատրյան', 1),
           (u'Johann Strauß', 2),
           (u'Вагиф Сәмәдоғлу', 3),
           (u'章子怡', 4),
           )
    tojson(tbl, 'tmp/test_tojson_utf8.json')

    result = json.load(open('tmp/test_tojson_utf8.json'))
    assert len(result) == 4
    for a, b in zip(tbl[1:], result):
        assert a[0] == b['name']
        assert a[1] == b['id']

    actual = fromjson('tmp/test_tojson_utf8.json')
    ieq(tbl, actual)
Esempio n. 6
0
def test_json_unicode():

    tbl = ((u'id', u'name'),
           (1, u'Արամ Խաչատրյան'),
           (2, u'Johann Strauß'),
           (3, u'Вагиф Сәмәдоғлу'),
           (4, u'章子怡'),
           )
    fn = NamedTemporaryFile().name
    tojson(tbl, fn)

    result = json.load(open(fn))
    assert len(result) == 4
    for a, b in zip(tbl[1:], result):
        assert a[0] == b['id']
        assert a[1] == b['name']

    actual = fromjson(fn, header=['id', 'name'])
    ieq(tbl, actual)
Esempio n. 7
0
def test_json_unicode():

    tbl = (
        (u'name', u'id'),
        (u'Արամ Խաչատրյան', 1),
        (u'Johann Strauß', 2),
        (u'Вагиф Сәмәдоғлу', 3),
        (u'章子怡', 4),
    )
    tojson(tbl, 'tmp/test_tojson_utf8.json')

    result = json.load(open('tmp/test_tojson_utf8.json'))
    assert len(result) == 4
    for a, b in zip(tbl[1:], result):
        assert a[0] == b['name']
        assert a[1] == b['id']

    actual = fromjson('tmp/test_tojson_utf8.json')
    ieq(tbl, actual)
Esempio n. 8
0
def test_json_unicode():

    tbl = (
        (u'id', u'name'),
        (1, u'Արամ Խաչատրյան'),
        (2, u'Johann Strauß'),
        (3, u'Вагиф Сәмәдоғлу'),
        (4, u'章子怡'),
    )
    fn = NamedTemporaryFile().name
    tojson(tbl, fn)

    result = json.load(open(fn))
    assert len(result) == 4
    for a, b in zip(tbl[1:], result):
        assert a[0] == b['id']
        assert a[1] == b['name']

    actual = fromjson(fn)
    ieq(tbl, actual)