Skip to content

Commit db4427c

Browse files
committed
Correct tests for unicode and CSV #280
* Ensure CSV tests use sorted checks #280 * use different expectations for Python 2 and 3 Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent 5d05d27 commit db4427c

File tree

2 files changed

+23
-41
lines changed

2 files changed

+23
-41
lines changed

tests/test_cmd.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
from __future__ import print_function
1919
from __future__ import unicode_literals
2020

21-
import unittest
22-
2321
from attributecode import CRITICAL
2422
from attributecode import DEBUG
2523
from attributecode import ERROR
@@ -28,23 +26,9 @@
2826
from attributecode import WARNING
2927
from attributecode import cmd
3028
from attributecode import Error
31-
from attributecode import util
32-
33-
34-
class CmdTest(unittest.TestCase):
3529

36-
def check_csv(self, expected, result):
37-
"""
38-
Compare two CSV files at locations as lists of ordered items.
39-
"""
40-
def as_items(csvfile):
41-
return sorted([i.items() for i in util.load_csv(csvfile)])
4230

43-
expected = as_items(expected)
44-
result = as_items(result)
45-
assert expected == result
46-
47-
# NB: this test depends on py.test stdout/err capture capabilities
31+
# NB: these tests depends on py.test stdout/err capture capabilities
4832
def test_log_errors(capsys):
4933
quiet = False
5034
errors = [Error(CRITICAL, 'msg1'),
@@ -66,6 +50,7 @@ def test_log_errors(capsys):
6650
assert '' == err
6751
assert expected_out == out
6852

53+
6954
def test_log_errors_with_quiet(capsys):
7055
quiet = True
7156
errors = [Error(CRITICAL, 'msg1'),

tests/test_model.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from collections import OrderedDict
2222
import json
2323
import posixpath
24+
import sys
2425
import unittest
2526
from unittest.case import expectedFailure
2627

@@ -42,12 +43,13 @@
4243
from attributecode.util import load_csv
4344

4445

45-
def check_csvs(expected, result):
46+
def check_csv(expected, result):
4647
"""
47-
Assert that the contents of two CSV files are equal.
48+
Assert that the contents of two CSV files locations `expected` and
49+
`result` are equal.
4850
"""
49-
expected = sorted([d.items() for d in load_csv(expected)])
50-
result = sorted([d.items() for d in load_csv(result)])
51+
expected = sorted([sorted(d.items()) for d in load_csv(expected)])
52+
result = sorted([sorted(d.items()) for d in load_csv(result)])
5153
assert expected == result
5254

5355

@@ -312,8 +314,12 @@ def test_parse_rejects_non_ascii_names_and_accepts_unicode_values(self):
312314
(u'owner', 'Matías Aguirre')]
313315
assert expected == result
314316

317+
expected_msg = "Invalid line: 3: 'Matías: unicode field name\\n'"
318+
if sys.version_info[0] < 3: # Python 2
319+
expected_msg = "Invalid line: 3: 'Mat\\xedas: unicode field name\\n'"
320+
315321
expected_errors = [
316-
Error(CRITICAL, "Invalid line: 3: 'Matías: unicode field name\\n'")]
322+
Error(CRITICAL, expected_msg)]
317323
assert expected_errors == errors
318324

319325
def test_parse_handles_blank_lines_and_spaces_in_field_names(self):
@@ -1046,7 +1052,7 @@ def test_write_output_csv(self):
10461052
model.write_output([a], tmp_file, format='csv')
10471053

10481054
expected = get_test_loc('load/expected.csv')
1049-
check_csvs(expected, tmp_file)
1055+
check_csv(expected, tmp_file)
10501056

10511057
def test_write_output_json(self):
10521058
path = 'load/this.ABOUT'
@@ -1183,17 +1189,6 @@ def test_collect_inventory_works_with_relative_paths(self):
11831189
assert expected == result1
11841190
assert expected == result2
11851191

1186-
def check_csv(self, expected, result):
1187-
"""
1188-
Compare two CSV files at locations as lists of ordered items.
1189-
"""
1190-
def as_items(csvfile):
1191-
return sorted([i.items() for i in util.load_csv(csvfile)])
1192-
1193-
expected = as_items(expected)
1194-
result = as_items(result)
1195-
assert expected == result
1196-
11971192
def test_collect_inventory_basic_from_directory(self):
11981193
location = get_test_loc('inventory/basic')
11991194
result = get_temp_file()
@@ -1205,7 +1200,7 @@ def test_collect_inventory_basic_from_directory(self):
12051200
assert expected_errors == errors
12061201

12071202
expected = get_test_loc('inventory/basic/expected.csv')
1208-
self.check_csv(expected, result)
1203+
check_csv(expected, result)
12091204

12101205
def test_collect_inventory_with_about_resource_path_from_directory(self):
12111206
location = get_test_loc('inventory/basic_with_about_resource_path')
@@ -1218,7 +1213,7 @@ def test_collect_inventory_with_about_resource_path_from_directory(self):
12181213
assert expected_errors == errors
12191214

12201215
expected = get_test_loc('inventory/basic_with_about_resource_path/expected.csv')
1221-
self.check_csv(expected, result)
1216+
check_csv(expected, result)
12221217

12231218
def test_collect_inventory_with_no_about_resource_from_directory(self):
12241219
location = get_test_loc('inventory/no_about_resource_key')
@@ -1231,13 +1226,15 @@ def test_collect_inventory_with_no_about_resource_from_directory(self):
12311226
assert expected_errors == errors
12321227

12331228
expected = get_test_loc('inventory/no_about_resource_key/expected.csv')
1234-
self.check_csv(expected, result)
1229+
check_csv(expected, result)
12351230

1236-
# FIXME: The self.check_csv is failing because there are many keys in the ABOUT files that are
1237-
# not supported. Instead of removing all the non-supported keys in the output
1238-
# and do the comparison, it may be best to apply the mapping to include theses keys
12391231
@expectedFailure
12401232
def test_collect_inventory_complex_from_directory(self):
1233+
# FIXME: check_csv is failing because there are many keys in
1234+
# the ABOUT files that are not supported. Instead of removing
1235+
# all the non-supported keys in the output and do the
1236+
# comparison, it may be best to apply the mapping to include
1237+
# theses keys
12411238
location = get_test_loc('inventory/complex')
12421239
result = get_temp_file()
12431240
errors, abouts = model.collect_inventory(location)
@@ -1247,7 +1244,7 @@ def test_collect_inventory_complex_from_directory(self):
12471244
assert all(e.severity == INFO for e in errors)
12481245

12491246
expected = get_test_loc('inventory/complex/expected.csv')
1250-
self.check_csv(expected, result)
1247+
check_csv(expected, result)
12511248

12521249

12531250
class GroupingsTest(unittest.TestCase):

0 commit comments

Comments
 (0)