-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_utils.py
214 lines (167 loc) · 6.3 KB
/
test_utils.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# -*- coding: utf-8 -*-
#
# This file is part of CERN Document Server.
# Copyright (C) 2015, 2017 CERN.
#
# Invenio is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02D111-1307, USA.
from __future__ import absolute_import
import json
import os
import pytest
from dojson.utils import filter_values
from cds_dojson.marc21.fields.books.errors import MissingRequiredField
from cds_dojson.marc21.fields.books.utils import (
extract_volume_info,
extract_volume_number,
)
from cds_dojson.utils import (
MementoDict,
convert_date_to_iso_8601,
for_each_squash,
not_accessed_keys,
yaml2json,
)
def test_for_each_squash():
"""Check if for_each_squash works correctly."""
@for_each_squash
@filter_values
def field(self, key, value):
return {'a': value.get('1'), 'b': value.get('2')}
squashed = field(None, None, {'1': 'foo', '2': 'bar'})
assert squashed == {'a': 'foo', 'b': 'bar'}
squashed = field(None, None, [{'1': 'foo'}, {'2': 'bar'}])
assert squashed == {'a': 'foo', 'b': 'bar'}
squashed = field(None, None, [{'1': 'foo', '2': 'bar2'}, {'2': 'bar'}])
assert squashed == {'a': 'foo', 'b': ['bar2', 'bar']}
def test_convert_date_to_iso_8601():
"""Check if convert_date_to_iso_8601 works correctly"""
string_dates = ('14/12/1989', '2013-11-22', '1999', 'Sep 1970',
'08 May 2002', '25 Feb 2009', '13 Dec 2002',
'23 August 2005', '1989/12/14', '10/09/13', '', None, )
iso_dates = ('1989-12-14', '2013-11-22', '1999-01-01', '1970-09-01',
'2002-05-08', '2009-02-25', '2002-12-13', '2005-08-23',
'1989-12-14', '2013-09-10', '', None, )
for string_date, iso_date in zip(string_dates, iso_dates):
assert convert_date_to_iso_8601(string_date) == iso_date
def test_not_accessed_keys():
"""Check not_accessed_keys function."""
d1 = MementoDict([])
assert not not_accessed_keys(d1)
d1 = MementoDict([('a', [1, 2, 3])])
assert not_accessed_keys(d1) == {'a'}
d1 = MementoDict([
('a', [1, 2, 3]),
('b', MementoDict({'1': 1}))
])
assert not_accessed_keys(d1) == {'a', 'b1'}
d1 = MementoDict([
('a', [1, 2, 3]),
('b', MementoDict({'1': 1})),
('c', 1)
])
assert not_accessed_keys(d1) == {'a', 'b1', 'c'}
d1 = MementoDict([
('a', [1, 2, 3]),
('b', MementoDict({'1': 1})),
('c', 1),
('d', [MementoDict({'1': 1, '2': 2}), MementoDict({'1': 2, '2': 2})])
])
assert not_accessed_keys(d1) == {'a', 'b1', 'c', 'd1', 'd2'}
assert not d1.accessed_keys
d1['d'][0]['1']
assert not_accessed_keys(d1) == {'a', 'b1', 'c', 'd1', 'd2'}
d2 = d1.get('b')
assert not_accessed_keys(d1) == {'a', 'b1', 'c', 'd1', 'd2'}
for k, v in d2.iteritems():
pass
assert not_accessed_keys(d1) == {'a', 'c', 'd1', 'd2'}
d1.get('c')
assert not_accessed_keys(d1) == {'a', 'd1', 'd2'}
d1 = MementoDict([
('a', [1, 2, 3]),
('b', MementoDict({'1': 1})),
('c', 1),
('d', [MementoDict({'1': 1, '2': 2}), MementoDict({'1': 2, '2': 2})]),
('e', MementoDict([('1', 1), ('1', 2), ('1', 3)]))
])
assert not_accessed_keys(d1) == {'a', 'b1', 'c', 'd1', 'd2', 'e1'}
def test_memento_dict():
"""Check MementoDict class."""
d = MementoDict({'a': 1, 'b': 2})
assert d == {'a': 1, 'b': 2}
d = MementoDict([('a', 1), ('b', 2)])
assert d == {'a': 1, 'b': 2}
d = MementoDict([('a', 1), ('a', 2)])
assert d == {'a': [1, 2]}
# NOTE: is this the expected behavior?
d = MementoDict([('a', 1), ('a', [2, 3])])
assert d == {'a': [1, 2, 3]}
def test_yaml2json():
"""Test yaml to json file conversion"""
root_dir = './tests/fixtures/books/'
source = os.path.join(root_dir, 'ymls/')
destination = os.path.join(root_dir, 'results')
yaml2json(source, destination)
with open(os.path.join(root_dir, 'books_title_mock.json')) as s:
# read the mock JSON file
json_mock = json.load(s)
assert os.path.exists(destination)
with open(os.path.join(destination, 'books_title.json')) as s:
# read the newly created JSON file
json_converted = json.load(s)
assert json_mock == json_converted
volume_params = [
('v1', '1', False),
('v 1', '1', False),
('v.1', '1', False),
('v. 1', '1', False),
('v . 1', '1', False),
('vol.1', '1', False),
('Vol. 2', '2', False),
('voL . 1', None, False),
('volume.1', '1', False),
('Volume. 1', '1', False),
('volume . 3', '3', False),
('pt 5', '5', False),
('part 5', '5', False),
('par 5', None, False),
('v. A', 'A', False),
('val. 5', None, False),
('part III', 'III', False),
('March 1996', None, False),
]
@pytest.mark.parametrize('value, expected, raise_exception', volume_params)
def test_extract_volume_number(value, expected, raise_exception):
"""Test extracting volume number."""
if raise_exception:
with pytest.raises(MissingRequiredField):
assert extract_volume_number(
value,
raise_exception=raise_exception
) == expected
else:
assert extract_volume_number(value) == expected
volume_info_params = [
(
'print version, paperback ({})'.format(vol_str),
dict(volume=expected, description='print version, paperback') if expected else None
)
for vol_str, expected, raise_exception in volume_params
]
volume_info_params.append(('print version, paperback v.5', None))
@pytest.mark.parametrize('value, expected', volume_info_params)
def test_extract_volume_info(value, expected):
"""Test extracting volume info."""
assert extract_volume_info(value) == expected