From 43a53f1c2b5a43d05d106ed58ac058357104c52c Mon Sep 17 00:00:00 2001 From: xrotwang Date: Fri, 22 Dec 2017 13:33:33 +0100 Subject: [PATCH] fixed tests to work on py3.5 --- tests/conftest.py | 14 ++++++ tests/test_apilib.py | 60 ++++++++++++------------- tests/test_declenum.py | 65 ++++++++++++++------------- tests/test_inifile.py | 16 +++---- tests/test_iso_639_3.py | 8 ++-- tests/test_jsonlib.py | 8 ++-- tests/test_lgr.py | 22 ++++------ tests/test_licenses.py | 12 ++--- tests/test_markup.py | 32 ++++++-------- tests/test_path.py | 94 ++++++++++++++++++++-------------------- tests/test_sfm.py | 4 +- tests/test_ziparchive.py | 8 ++-- 12 files changed, 169 insertions(+), 174 deletions(-) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..31aa671 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,14 @@ +# coding: utf8 +from __future__ import unicode_literals, print_function, division + +import pytest + +try: + import pathlib2 as pathlib +except ImportError: # pragma: no cover + import pathlib + + +@pytest.fixture +def tmppath(tmpdir): + return pathlib.Path(str(tmpdir)) diff --git a/tests/test_apilib.py b/tests/test_apilib.py index a657cea..96b41dc 100644 --- a/tests/test_apilib.py +++ b/tests/test_apilib.py @@ -1,47 +1,43 @@ # coding: utf8 from __future__ import unicode_literals, print_function, division -from unittest import TestCase import attr +import pytest +from clldutils.apilib import API, DataObject, latitude, longitude -class Tests(TestCase): - def test_API(self): - from clldutils.apilib import API - api = API('.') - assert api.repos.exists() - self.assertIn('repository', '%s' % api) +def test_API(): + api = API('.') + assert api.repos.exists() + assert 'repository' in '%s' % api + assert not api.path('unknown', 'path').exists() - assert not api.path('unknown', 'path').exists() - def test_DataObject(self): - from clldutils.apilib import DataObject +def test_DataObject(): + @attr.s + class C(DataObject): + x = attr.ib() + y = attr.ib(metadata=dict(ascsv=lambda v: 'xyz')) - @attr.s - class C(DataObject): - x = attr.ib() - y = attr.ib(metadata=dict(ascsv=lambda v: 'xyz')) + assert C.fieldnames() == ['x', 'y'] + assert C(None, 2).ascsv() == ['', 'xyz'] + assert C(['y', 'x'], 2).ascsv() == ['y;x', 'xyz'] + assert C({'y': 2}, 2).ascsv() == ['{"y": 2}', 'xyz'] + assert C(2.123456, 'x').ascsv() == ['2.12346', 'xyz'] + assert C(2, 'x').ascsv() == ['2', 'xyz'] - self.assertEqual(C.fieldnames(), ['x', 'y']) - self.assertEqual(C(None, 2).ascsv(), ['', 'xyz']) - self.assertEqual(C(['y', 'x'], 2).ascsv(), ['y;x', 'xyz']) - self.assertEqual(C({'y': 2}, 2).ascsv(), ['{"y": 2}', 'xyz']) - self.assertEqual(C(2.123456, 'x').ascsv(), ['2.12346', 'xyz']) - self.assertEqual(C(2, 'x').ascsv(), ['2', 'xyz']) - def test_latitude_longitude(self): - from clldutils.apilib import latitude, longitude +def test_latitude_longitude(): + @attr.s + class C(object): + lat = latitude() + lon = longitude() - @attr.s - class C(object): - lat = latitude() - lon = longitude() + assert C('', None).lat is None - self.assertIsNone(C('', None).lat) + with pytest.raises(ValueError): + C(lat=100, lon=50) - with self.assertRaises(ValueError): - C(lat=100, lon=50) - - with self.assertRaises(ValueError): - C(lat='10', lon='500') + with pytest.raises(ValueError): + C(lat='10', lon='500') diff --git a/tests/test_declenum.py b/tests/test_declenum.py index 8b43bb0..6921f63 100644 --- a/tests/test_declenum.py +++ b/tests/test_declenum.py @@ -1,45 +1,44 @@ # coding: utf8 from __future__ import unicode_literals, print_function, division -from unittest import TestCase +import pytest -class Tests(TestCase): - def test_DeclEnum_int(self): - from clldutils.declenum import DeclEnum +from clldutils.declenum import DeclEnum - class A(DeclEnum): - val1 = 2, 'x' - val2 = 3, 'y' - val3 = 1, 'z' - self.assertEqual(A.val1.name, 'val1') - self.assertEqual('{0}'.format(A.val1), '2') - self.assertGreater(A.val1, A.val3) - self.assertGreater(A.val2, A.val1) - self.assertEqual(A.val1, A.get(A.val1)) - self.assertEqual(A.val1, A.get(2)) - self.assertLess(A.get(1), A.get(3)) +def test_DeclEnum_int(): + class A(DeclEnum): + val1 = 2, 'x' + val2 = 3, 'y' + val3 = 1, 'z' - with self.assertRaises(ValueError): - A.get(5) + assert A.val1.name == 'val1' + assert '{0}'.format(A.val1) == '2' + assert A.val1 > A.val3 + assert A.val2 > A.val1 + assert A.val1 == A.get(A.val1) + assert A.val1 == A.get(2) + assert A.get(1) < A.get(3) - d = {v: v.description for v in A} - self.assertEqual(sorted(d)[0], A.val3) + with pytest.raises(ValueError): + A.get(5) - def test_DeclEnum(self): - from clldutils.declenum import DeclEnum + d = {v: v.description for v in A} + assert sorted(d)[0] == A.val3 - class A(DeclEnum): - val1 = '1', 'value 1' - val2 = '2', 'value 2' - for val, desc in A: - self.assertEqual(val, '1') - break +def test_DeclEnum(): + class A(DeclEnum): + val1 = '1', 'value 1' + val2 = '2', 'value 2' - self.assertEqual(len(list(A.values())), 2) - self.assertIn('1', repr(A.val1)) - self.assertEqual(A.from_string('1'), A.val1) - with self.assertRaises(ValueError): - A.from_string('x') - assert A.val1.__json__(None) == A.val1.__unicode__() + for val, desc in A: + assert val == '1' + break + + assert len(list(A.values())) == 2 + assert '1' in repr(A.val1) + assert A.from_string('1') == A.val1 + with pytest.raises(ValueError): + A.from_string('x') + assert A.val1.__json__(None) == A.val1.__unicode__() diff --git a/tests/test_inifile.py b/tests/test_inifile.py index 90b5eed..e4e323c 100644 --- a/tests/test_inifile.py +++ b/tests/test_inifile.py @@ -3,20 +3,20 @@ import pytest from clldutils.inifile import INI -from clldutils.path import Path +from clldutils.path import write_text -def test_encoding(tmpdir): - ini = tmpdir.join('test.ini') - ini.write_text('[äöü]\näöü = äöü', encoding='cp1252') +def test_encoding(tmppath): + ini = tmppath / 'test.ini' + write_text(ini, '[äöü]\näöü = äöü', encoding='cp1252') with pytest.raises(UnicodeDecodeError): - INI.from_file(str(ini)) + INI.from_file(ini) - assert INI.from_file(str(ini), encoding='cp1252')['äöü']['äöü'] == 'äöü' + assert INI.from_file(ini, encoding='cp1252')['äöü']['äöü'] == 'äöü' -def test_INI(tmpdir): +def test_INI(tmppath): ini = INI() ini.set('äüü', 'äöü', ('ä', 'ö', 'ü')) ini.set('a', 'b', 5) @@ -29,7 +29,7 @@ def test_INI(tmpdir): mt = '- a\n - aa\n - ab\n- b' ini.settext('text', 'multi', mt) - tmp = Path(tmpdir.join('test')) + tmp = tmppath / 'test' ini.write(tmp.as_posix()) with tmp.open(encoding='utf8') as fp: res = fp.read() diff --git a/tests/test_iso_639_3.py b/tests/test_iso_639_3.py index 0823d9d..3286488 100644 --- a/tests/test_iso_639_3.py +++ b/tests/test_iso_639_3.py @@ -21,12 +21,12 @@ def urlretrieve(url, dest): assert 'aab' in iso -def test_ISO(tmpdir): +def test_ISO(tmppath): from clldutils.iso_639_3 import ISO, Code - dated_zip = tmpdir.join('20121201.zip') - copy(FIXTURES.joinpath('iso.zip'), str(dated_zip)) - iso = ISO(Path(str(dated_zip))) + dated_zip = tmppath / '20121201.zip' + copy(FIXTURES.joinpath('iso.zip'), dated_zip) + iso = ISO(dated_zip) assert '{0}'.format(iso) == 'ISO 639-3 code tables from 2012-12-01' iso = ISO(FIXTURES.joinpath('iso.zip')) diff --git a/tests/test_jsonlib.py b/tests/test_jsonlib.py index 6a3d4a7..a670457 100644 --- a/tests/test_jsonlib.py +++ b/tests/test_jsonlib.py @@ -11,8 +11,8 @@ def test_parse_json_with_datetime(): assert parse(dict(d='2012-12-12T20:12:12.12'))['d'].year -def test_update(tmpdir): - p = str(tmpdir.join('test')) +def test_update(tmppath): + p = tmppath / 'test' with pytest.raises(ValueError): with update(p): pass # pragma: no cover @@ -28,9 +28,9 @@ def test_update(tmpdir): assert obj['a'] == 2 -def test_json(tmpdir): +def test_json(tmppath): d = {'a': 234, 'ä': 'öäüß'} - p = str(tmpdir.join('test')) + p = tmppath / 'test' dump(d, p, indent=4) for k, v in load(p).items(): assert d[k] == v diff --git a/tests/test_lgr.py b/tests/test_lgr.py index f03c75f..5fd744f 100644 --- a/tests/test_lgr.py +++ b/tests/test_lgr.py @@ -1,18 +1,14 @@ from __future__ import unicode_literals -from unittest import TestCase +def test_replace(): + from clldutils.lgr import replace -class Tests(TestCase): + for i, o in [ + ('1SG', ''), + ('DUR.DU-', '.-'), + ]: + assert replace(i) == o - def test_replace(self): - from clldutils.lgr import replace - - for i, o in [ - ('1SG', ''), - ('DUR.DU-', '.-'), - ]: - self.assertEqual(replace(i), o) - - self.assertEqual(replace('DUR.DU-', lambda m: m.group('pre') + '#'), '#.#-') - self.assertEqual(replace('.XX-', custom={'XX': 'x'}), '.-') + assert replace('DUR.DU-', lambda m: m.group('pre') + '#') == '#.#-' + assert replace('.XX-', custom={'XX': 'x'}) == '.-' diff --git a/tests/test_licenses.py b/tests/test_licenses.py index a668fd6..1289651 100644 --- a/tests/test_licenses.py +++ b/tests/test_licenses.py @@ -1,13 +1,9 @@ # coding: utf8 from __future__ import unicode_literals, print_function, division -from unittest import TestCase -class Tests(TestCase): - def test_find(self): - from clldutils.licenses import find +def test_find(): + from clldutils.licenses import find - self.assertEqual( - find('http://creativecommons.org/licenses/by/4.0').id, 'CC-BY-4.0') - self.assertEqual( - find('CC-BY-4.0').url, 'https://creativecommons.org/licenses/by/4.0/') + assert find('http://creativecommons.org/licenses/by/4.0').id == 'CC-BY-4.0' + assert find('CC-BY-4.0').url == 'https://creativecommons.org/licenses/by/4.0/' diff --git a/tests/test_markup.py b/tests/test_markup.py index d05923c..6a9bce6 100644 --- a/tests/test_markup.py +++ b/tests/test_markup.py @@ -1,26 +1,20 @@ # coding: utf8 from __future__ import unicode_literals, print_function, division -from unittest import TestCase from operator import itemgetter -class Tests(TestCase): - def test_Table(self): - from clldutils.markup import Table +def test_Table(): + from clldutils.markup import Table - t = Table() - self.assertEqual(t.render(), '') + t = Table() + assert t.render() == '' - t = Table('a', 'b', rows=[[1, 2], [3, 4]]) - self.assertEqual( - t.render(), - '| a | b |\n|----:|----:|\n| 1 | 2 |\n| 3 | 4 |') - self.assertEqual( - t.render(condensed=False), - '| a | b |\n|----:|----:|\n| 1 | 2 |\n| 3 | 4 |') - self.assertEqual( - t.render(verbose=True), - '| a | b |\n|----:|----:|\n| 1 | 2 |\n| 3 | 4 |\n\n(2 rows)\n\n') - self.assertEqual( - t.render(sortkey=itemgetter(1), reverse=True), - '| a | b |\n|----:|----:|\n| 3 | 4 |\n| 1 | 2 |') + t = Table('a', 'b', rows=[[1, 2], [3, 4]]) + assert t.render() == \ + '| a | b |\n|----:|----:|\n| 1 | 2 |\n| 3 | 4 |' + assert t.render(condensed=False) == \ + '| a | b |\n|----:|----:|\n| 1 | 2 |\n| 3 | 4 |' + assert t.render(verbose=True) == \ + '| a | b |\n|----:|----:|\n| 1 | 2 |\n| 3 | 4 |\n\n(2 rows)\n\n' + assert t.render(sortkey=itemgetter(1), reverse=True) == \ + '| a | b |\n|----:|----:|\n| 3 | 4 |\n| 1 | 2 |' diff --git a/tests/test_path.py b/tests/test_path.py index 9e1c73b..46133e1 100644 --- a/tests/test_path.py +++ b/tests/test_path.py @@ -6,54 +6,54 @@ from six import text_type import pytest -from clldutils.path import Path, Manifest, copytree, memorymapped +from clldutils.path import Path, Manifest, copytree, memorymapped, write_text def make_file(d, name='test.txt', text='test', encoding=None): - path = d.join(name) - path.write_text(text, encoding=encoding) - return Path(str(path)) + path = d / name + write_text(path, text, encoding=encoding) + return path -def test_Manifest(tmpdir): +def test_Manifest(tmppath): d = Path(__file__).parent m = {k: v for k, v in Manifest.from_dir(d).items()} - copytree(d, str(tmpdir.join('d'))) - assert m == Manifest.from_dir(Path(tmpdir.join('d'))) - copytree(d, Path(str(tmpdir.join('d', 'd')))) - assert m != Manifest.from_dir(Path(tmpdir.join('d'))) + copytree(d, tmppath / 'd') + assert m == Manifest.from_dir(tmppath / 'd') + copytree(d, tmppath / 'd' / 'd') + assert m != Manifest.from_dir(tmppath / 'd') -def test_Manifest2(tmpdir): - make_file(tmpdir, name='b.txt') - make_file(tmpdir, name='a.txt') - m = Manifest.from_dir(Path(tmpdir)) +def test_Manifest2(tmppath): + make_file(tmppath, name='b.txt') + make_file(tmppath, name='a.txt') + m = Manifest.from_dir(tmppath) assert '{0}'.format(m) == \ '098f6bcd4621d373cade4e832627b4f6 a.txt\n098f6bcd4621d373cade4e832627b4f6 b.txt' - m.write(Path(tmpdir)) - assert tmpdir.join('manifest-md5.txt').check() + m.write(Path(tmppath)) + assert tmppath.joinpath('manifest-md5.txt').exists() -def test_memorymapped(tmpdir): - p = make_file(tmpdir, text='äöü', encoding='utf-8') +def test_memorymapped(tmppath): + p = make_file(tmppath, text='äöü', encoding='utf-8') with memorymapped(p) as b: assert b.find('ö'.encode('utf-8')) == 2 -def test_read_write(tmpdir): +def test_read_write(tmppath): from clldutils.path import read_text, write_text text = 'äöüß' - p = Path(tmpdir.join('test')) + p = tmppath / 'test' assert write_text(p, text) == len(text) assert read_text(p) == text -def test_readlines(tmpdir): +def test_readlines(tmppath): from clldutils.path import readlines # Test files are read using universal newline mode: - fname = make_file(tmpdir, text='a\nb\r\nc\rd') + fname = make_file(tmppath, text='a\nb\r\nc\rd') assert len(readlines(fname)) == 4 lines = ['\t#ä '] @@ -68,17 +68,17 @@ def test_readlines(tmpdir): assert readlines(lines, strip=True, normalize='NFC') == [] -def test_import_module(tmpdir): +def test_import_module(tmppath): from clldutils.path import import_module - make_file(tmpdir, name='__init__.py', encoding='ascii', text='A = [1, 2, 3]') + make_file(tmppath, name='__init__.py', encoding='ascii', text='A = [1, 2, 3]') syspath = sys.path[:] - m = import_module(Path(tmpdir)) + m = import_module(tmppath) assert len(m.A) == 3 assert syspath == sys.path - make_file(tmpdir, name='abcd.py', encoding='ascii', text='A = [1, 2, 3]') - m = import_module(Path(tmpdir.join('abcd.py'))) + make_file(tmppath, name='abcd.py', encoding='ascii', text='A = [1, 2, 3]') + m = import_module(tmppath / 'abcd.py') assert len(m.A) == 3 @@ -104,66 +104,66 @@ def test_md5(): assert re.match('[a-f0-9]{32}$', md5(__file__)) -def test_copytree(tmpdir): +def test_copytree(tmppath): from clldutils.path import copytree - dst = Path(tmpdir.join('a', 'b')) - copytree(Path(tmpdir), dst) + dst = tmppath / 'a' / 'b' + copytree(tmppath, dst) assert dst.exists() with pytest.raises(OSError): copytree(dst, dst) -def test_copy(tmpdir): +def test_copy(tmppath): from clldutils.path import copy - src = make_file(tmpdir, name='test', text='abc') - dst = Path(tmpdir.join('other')) + src = make_file(tmppath, name='test', text='abc') + dst = tmppath / 'other' copy(src, dst) assert src.stat().st_size == dst.stat().st_size -def test_move(tmpdir): +def test_move(tmppath): from clldutils.path import move - dst = Path(tmpdir.join('a')) + dst = tmppath / 'a' dst.mkdir() - src = make_file(tmpdir, name='test') + src = make_file(tmppath, name='test') move(src, dst) assert not src.exists() assert dst.joinpath(src.name).exists() -def test_remove(tmpdir): +def test_remove(tmppath): from clldutils.path import remove with pytest.raises(OSError): - remove(Path(tmpdir.join('nonexistingpath'))) - tmp = make_file(tmpdir, name='test') + remove(tmppath / 'nonexistingpath') + tmp = make_file(tmppath, name='test') assert tmp.exists() remove(tmp) assert not tmp.exists() -def test_rmtree(tmpdir): +def test_rmtree(tmppath): from clldutils.path import rmtree with pytest.raises(OSError): - rmtree(str(tmpdir.join('nonexistingpath'))) - rmtree(str(tmpdir.join('nonexistingpath')), ignore_errors=True) - tmp = Path(tmpdir.join('test')) + rmtree(tmppath / 'nonexistingpath') + rmtree(tmppath / 'nonexistingpath', ignore_errors=True) + tmp = tmppath / 'test' tmp.mkdir() assert tmp.exists() rmtree(tmp) assert not tmp.exists() -def test_walk(tmpdir): +def test_walk(tmppath): from clldutils.path import walk - d = Path(tmpdir.join('testdir')) + d = tmppath / 'testdir' d.mkdir() - make_file(tmpdir, name='testfile') + make_file(tmppath, name='testfile') res = [p.name for p in walk(d.parent, mode='files')] assert 'testdir' not in res assert 'testfile' in res @@ -172,10 +172,10 @@ def test_walk(tmpdir): assert 'testfile' not in res -def test_git_describe(tmpdir, capsys): +def test_git_describe(tmppath, capsys): from clldutils.path import git_describe - d = Path(tmpdir.join('testdir')) + d = tmppath / 'testdir' with pytest.raises(ValueError): git_describe(d) d.mkdir() diff --git a/tests/test_sfm.py b/tests/test_sfm.py index 5744e5b..be493a6 100644 --- a/tests/test_sfm.py +++ b/tests/test_sfm.py @@ -5,7 +5,7 @@ from clldutils.sfm import SFM, Entry -def test_Dictionary(tmpdir): +def test_Dictionary(tmppath): p = Path(__file__).parent.joinpath('fixtures', 'test.sfm') d = SFM.from_file(p, keep_empty=True) assert d[1].get('empty') is not None @@ -13,7 +13,7 @@ def test_Dictionary(tmpdir): d = SFM.from_file(p) assert len(d) == 2 assert d[1].get('empty') is None - tmp = Path(tmpdir.join('test')) + tmp = tmppath / 'test' d.write(tmp) d2 = SFM() d2.read(tmp) diff --git a/tests/test_ziparchive.py b/tests/test_ziparchive.py index afa88a2..766e9da 100644 --- a/tests/test_ziparchive.py +++ b/tests/test_ziparchive.py @@ -2,13 +2,13 @@ from __future__ import unicode_literals, print_function, division -def test_ZipArchive(tmpdir): +def test_ZipArchive(tmppath): from clldutils.ziparchive import ZipArchive - fname, text, name = tmpdir.join('test.zip'), 'äöüß', 'test' + fname, text, name = tmppath / 'test.zip', 'äöüß', 'test' - with ZipArchive(str(fname), mode='w') as archive: + with ZipArchive(fname, mode='w') as archive: archive.write_text(text, name) - with ZipArchive(str(fname)) as archive: + with ZipArchive(fname) as archive: assert text == archive.read_text(name)