Skip to content

Commit

Permalink
fixed tests to work on py3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
xrotwang committed Dec 22, 2017
1 parent 6addd32 commit 43a53f1
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 174 deletions.
14 changes: 14 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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))
60 changes: 28 additions & 32 deletions tests/test_apilib.py
Original file line number Diff line number Diff line change
@@ -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')
65 changes: 32 additions & 33 deletions tests/test_declenum.py
Original file line number Diff line number Diff line change
@@ -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__()
16 changes: 8 additions & 8 deletions tests/test_inifile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions tests/test_iso_639_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
8 changes: 4 additions & 4 deletions tests/test_jsonlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
22 changes: 9 additions & 13 deletions tests/test_lgr.py
Original file line number Diff line number Diff line change
@@ -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', '<first person singular>'),
('DUR.DU-', '<durative>.<dual>-'),
]:
assert replace(i) == o

def test_replace(self):
from clldutils.lgr import replace

for i, o in [
('1SG', '<first person singular>'),
('DUR.DU-', '<durative>.<dual>-'),
]:
self.assertEqual(replace(i), o)

self.assertEqual(replace('DUR.DU-', lambda m: m.group('pre') + '#'), '#.#-')
self.assertEqual(replace('.XX-', custom={'XX': 'x'}), '.<x>-')
assert replace('DUR.DU-', lambda m: m.group('pre') + '#') == '#.#-'
assert replace('.XX-', custom={'XX': 'x'}) == '.<x>-'
12 changes: 4 additions & 8 deletions tests/test_licenses.py
Original file line number Diff line number Diff line change
@@ -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/'
32 changes: 13 additions & 19 deletions tests/test_markup.py
Original file line number Diff line number Diff line change
@@ -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 |'
Loading

0 comments on commit 43a53f1

Please sign in to comment.