示例#1
0
    def test_remote_schema(self, mocker, tmpdir):
        schema = """
        {
            "columns": [
                {"name": "countryCode", "datatype": "string"},
                {"name": "name", "datatype": "string"}
            ]
        }
        """
        mocker.patch(
            'csvw.metadata.urlopen',
            mocker.Mock(return_value=io.BytesIO(schema.encode('utf8'))))
        tg = self._make_tablegroup(tmpdir,
                                   metadata="""{
  "@context": "http://www.w3.org/ns/csvw",
  "tables": [{
    "url": "countries.csv",
    "tableSchema": "url"
  }]
}""")
        assert len(tg.tables[0].tableSchema.columns) == 2

        # The remote content has been inlined:
        out = pathlib.Path(str(tmpdir)) / 'md.json'
        tg.to_file(out)
        assert 'countryCode' in out.read_text(encoding='utf8')
示例#2
0
def test_write_file_exists(tmpdir):
    target = pathlib.Path(str(tmpdir / 'db.sqlite3'))
    target.touch(exist_ok=False)
    mtime = target.stat().st_mtime
    tg = TableGroup.from_file(FIXTURES / 'csv.txt-metadata.json')
    db = Database(tg, fname=target)
    with pytest.raises(ValueError, match=r'already exists'):
        db.write()
    time.sleep(0.1)
    db.write(force=True)
    assert target.stat().st_mtime > mtime
示例#3
0
def _make_table_like(cls, tmpdir, data=None, metadata=None, mdname=None):
    md = tmpdir / 'md'
    if metadata is None:
        shutil.copy(str(FIXTURES / mdname), str(md))
    else:
        md.write_text(str(metadata), encoding='utf-8')
    if isinstance(data, dict):
        for fname, content in data.items():
            (tmpdir / fname).write_text(content, encoding='utf-8')
    else:
        data = data or (FIXTURES / 'csv.txt').read_text(encoding='utf-8')
        with pathlib.Path(str(tmpdir / 'csv.txt')).open('w',
                                                        encoding='utf-8',
                                                        newline='') as f:
            f.write(data)
    return cls.from_file(str(md))
示例#4
0
 def test_commentPrefix(self, tmpdir):
     fpath = tmpdir / 'test'
     t = csvw.TableGroup.fromvalue({
         "dialect": {
             "commentPrefix": "$"
         },
         "tables": [{
             "url": str(fpath),
             "tableSchema": {
                 "columns": [{
                     "name": "col1",
                     "datatype": "string"
                 }]
             }
         }]
     })
     t._fname = pathlib.Path(str(fpath))
     fpath.write_text("""col1\n"$val"\n""", encoding='utf8')
     res = list(t.tables[0])
     assert res[0]['col1'] == '$val'
示例#5
0
文件: test_dsv.py 项目: Anaphory/csvw
import io
import csv
import shutil
from collections import OrderedDict

from csvw._compat import pathlib

import pytest

from csvw.dsv import (iterrows, UnicodeReader, UnicodeWriter, rewrite, Dialect,
                      add_rows, filter_rows_as_dict)

TESTDIR = pathlib.Path(__file__).parent / 'fixtures'

QUOTING = ['QUOTE_ALL', 'QUOTE_MINIMAL', 'QUOTE_NONNUMERIC', 'QUOTE_NONE']


def test_iterrows_invalid():
    with pytest.raises(ValueError, match=r'either namedtuples or dicts'):
        next(iterrows([], namedtuples=True, dicts=True))


def test_iterrows(rows=[['first', 'line'], ['s\u00fccond',
                                            'l\u00e4ne\u00df']]):
    assert list(iterrows(TESTDIR / 'csv.txt')) == rows

    lines = ['\t'.join(r) for r in rows]
    assert list(iterrows(lines, delimiter='\t')) == rows

    for lt in ['\n', '\r\n', '\r']:
        # Simulate file opened in text mode:
示例#6
0
def test_Link():
    li = csvw.Link('abc.csv')
    assert li.resolve(None) == 'abc.csv'
    assert li.resolve('http://example.com') == 'http://example.com/abc.csv'
    assert li.resolve(pathlib.Path('.')) == pathlib.Path('abc.csv')
示例#7
0
 def test_link(self):
     l = csvw.Link('a.csv')
     assert '{}'.format(l) == l.resolve(None)
     assert 'http://example.org/a.csv' == l.resolve('http://example.org')
     base = pathlib.Path('.')
     assert base == l.resolve(base).parent
示例#8
0
import io
import json
import shutil
import collections
import warnings

from csvw._compat import pathlib, json_open

import pytest

import csvw
from csvw.dsv import Dialect

FIXTURES = pathlib.Path(__file__).parent / 'fixtures'


def test_URITemplate():
    ut = csvw.URITemplate('http://example.org')
    assert ut == csvw.URITemplate('http://example.org')
    assert ut != csvw.URITemplate('https://example.org')
    assert ut != 1


def test_Link():
    li = csvw.Link('abc.csv')
    assert li.resolve(None) == 'abc.csv'
    assert li.resolve('http://example.com') == 'http://example.com/abc.csv'
    assert li.resolve(pathlib.Path('.')) == pathlib.Path('abc.csv')


def test_column_init():
示例#9
0
文件: db.py 项目: Anaphory/csvw
 def __init__(self, tg, fname=None, translate=None):
     self.translate = translate or Database.name_translator
     self.fname = pathlib.Path(fname) if fname else None
     self.init_schema(tg)
     self._connection = None  # For in-memory dbs we need to keep the connection!