def test_object(self): l = csvw.NaturalLanguage( collections.OrderedDict([('en', ['abc', 'def']), ('de', '\u00e4\u00f6\u00fc')])) assert l.getfirst('de') == '\u00e4\u00f6\u00fc' assert l.get('en') == ['abc', 'def'] assert '{}'.format(l) == 'abc'
def test_serialize(self): l = csvw.NaturalLanguage('\u00e4') assert json.dumps(l.asdict()) == '"\\u00e4"' l.add('a') assert json.dumps(l.asdict()) == '["\\u00e4", "a"]' l.add('\u00f6', 'de') assert json.dumps(l.asdict()) == \ '{"und": ["\\u00e4", "a"], "de": "\\u00f6"}'
def test_all(self, tmpdir): with warnings.catch_warnings(): warnings.simplefilter("ignore") t = self._make_tablegroup(tmpdir) assert len(list(t.tables[0])) == 2 # Test appication of null property on columns: t = self._make_tablegroup(tmpdir) t.tables[0].tableSchema.columns[1].null = ['line'] assert list(t.tables[0])[0]['_col.2'] is None t = self._make_tablegroup(tmpdir) t.tables[0].tableSchema.columns[1].separator = 'n' assert list(t.tables[0])[0]['_col.2'] == ['li', 'e'] t = self._make_tablegroup(tmpdir) t.tables[0].tableSchema.columns[1].titles = csvw.NaturalLanguage( 'colname') assert 'colname' in list(t.tables[0])[0] t = self._make_tablegroup(tmpdir) t.dialect.header = True assert len(list(t.tables[0])) == 1 t = self._make_tablegroup(tmpdir, 'edferd,f\r\nabc,') t.tables[0].tableSchema.columns[0].required = True t.tables[0].tableSchema.columns[0].null = ['abc'] with pytest.raises( ValueError, match=r'csv\.txt:2:1 ID: required column value is missing' ): list(t.tables[0]) t = self._make_tablegroup(tmpdir, ',') t.tables[0].tableSchema.columns[0].required = True with pytest.raises(ValueError): list(t.tables[0]) t = self._make_tablegroup(tmpdir, 'abc,9\r\ndef,10') items = list(t.tables[0]) assert items[0]['_col.2'] > items[1]['_col.2'] t.tables[0].tableSchema.columns[1].datatype.base = 'integer' items = list(t.tables[0]) assert items[0]['_col.2'] < items[1]['_col.2']
def test_error(self): with pytest.raises(ValueError): csvw.NaturalLanguage(1)
def test_array(self): l = csvw.NaturalLanguage(['abc', 'def']) assert l.getfirst() == 'abc' assert l.get(None) == ['abc', 'def'] assert '{}'.format(l) == 'abc'
def test_string(self): l = csvw.NaturalLanguage('abc') assert l.getfirst() == 'abc' assert l.get(None) == ['abc'] assert '{}'.format(l) == 'abc'