-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtest_gb2260.py
128 lines (102 loc) · 4.11 KB
/
test_gb2260.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# coding: utf-8
from __future__ import unicode_literals
import sys
from pytest import mark, raises
from gb2260 import Division, get
from gb2260.division import make_year_key
@mark.parametrize('code,stack_name,is_province,is_prefecture,is_county', [
('110101', u'北京市/市辖区/东城区', False, False, True),
('110100', u'北京市/市辖区', False, True, False),
('110000', u'北京市', True, False, False),
])
def test_division(code, stack_name, is_province, is_prefecture, is_county):
division = get(code)
assert division.code == code
assert division.is_province == is_province
assert division.is_prefecture == is_prefecture
assert division.is_county == is_county
assert '/'.join(x.name for x in division.stack()) == stack_name
@mark.skipif(sys.version_info[0] != 2, reason='requires python 2.x')
@mark.parametrize('code,year,repr_result,unicode_result', [
('110101', None,
"gb2260.get(u'110101')", u'<GB2260 110101 北京市/市辖区/东城区>'),
('110100', None,
"gb2260.get(u'110100')", u'<GB2260 110100 北京市/市辖区>'),
('110000', None,
"gb2260.get(u'110000')", u'<GB2260 110000 北京市>'),
('110000', 2006,
"gb2260.get(u'110000', 2006)", u'<GB2260-2006 110000 北京市>'),
])
def test_representation_python2(code, year, repr_result, unicode_result):
division = get(code, year)
assert repr(division) == repr_result
assert str(division) == unicode_result.encode('utf-8')
assert unicode(division) == unicode_result
assert isinstance(repr(division), str)
assert isinstance(str(division), str)
assert isinstance(unicode(division), unicode)
@mark.skipif(sys.version_info[0] != 3, reason='requires python 3.x')
@mark.parametrize('code,year,repr_result,unicode_result', [
('110101', None,
u"gb2260.get('110101')", u'<GB2260 110101 北京市/市辖区/东城区>'),
('110100', None,
u"gb2260.get('110100')", u'<GB2260 110100 北京市/市辖区>'),
('110000', None,
u"gb2260.get('110000')", u'<GB2260 110000 北京市>'),
('110000', 2006,
u"gb2260.get('110000', 2006)", u'<GB2260-2006 110000 北京市>'),
])
def test_representation_python3(code, year, repr_result, unicode_result):
division = get(code, year)
assert repr(division) == repr_result
assert str(division) == unicode_result
assert isinstance(repr(division), str)
assert isinstance(str(division), str)
def test_comparable():
assert get(110101) == Division(110101, u'东城区')
assert get(110101) != Division(110000, u'北京市')
assert get(110101, year=2006) != Division(110101, u'东城区')
def test_hashable():
division_set = set([
Division(110101, u'东城区'),
Division(110000, u'北京市'),
Division(110101, u'东城区'),
Division(110101, u'东城区', 2006),
])
assert division_set == set([
Division(110101, u'东城区'),
Division(110000, u'北京市'),
Division(110101, u'东城区', 2006),
])
def test_history_data():
get(522401, year=2010) == Division(522401, u'毕节市', 2010)
with raises(ValueError) as error:
get(522401)
assert error.value.args[0] == '522401 is not valid division code'
with raises(ValueError) as error:
get(110101, 2000)
assert error.value.args[0].startswith('year must be in')
def test_default_get():
assert get(522401, raise_on_error=False) is None
@mark.parametrize('code,name,year', [
(522401, u'毕节市', 2010),
(419000, u'省直辖县级行政区划', None),
])
def test_searching(code, name, year):
division = Division.search(code)
assert division.name == name
assert division.year == year
@mark.parametrize('year,result', [
(2013, (2013, 12)),
(201304, (2013, 4)),
('2013', (2013, 12)),
('201304', (2013, 4)),
(None, (2014, 12)),
])
def test_make_year_key(year, result):
assert make_year_key(year) == result
def test_make_invalid_year_key():
raises(ValueError, make_year_key, 20)
raises(ValueError, make_year_key, '20')
raises(ValueError, make_year_key, 20122222)
raises(ValueError, make_year_key, '20122222')