|
1
|
|
|
import unittest |
|
|
|
|
|
|
2
|
|
|
from datetime import date |
|
3
|
|
|
|
|
4
|
|
|
from foil.parsers import (make_converters, parse_bool, passthrough, |
|
5
|
|
|
parse_float, parse_int, parse_int_bool, |
|
6
|
|
|
parse_iso_date, parse_numeric, parse_quoted_bool, |
|
7
|
|
|
parse_quoted_float, parse_quoted_int, |
|
8
|
|
|
parse_quoted_string, parse_quoted_numeric, |
|
9
|
|
|
parse_broken_json) |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class TestTextParsers(unittest.TestCase): |
|
13
|
|
|
def test_bool(self): |
|
14
|
|
|
mock_data = [('true', True), ('false', False), |
|
15
|
|
|
('1', True), ('0', False), ('', None)] |
|
16
|
|
|
|
|
17
|
|
|
for input_expected in mock_data: |
|
18
|
|
|
with self.subTest(input_expect=input_expected): |
|
19
|
|
|
result = parse_bool(input_expected[0]) |
|
20
|
|
|
expected = input_expected[1] |
|
21
|
|
|
|
|
22
|
|
|
self.assertEqual(expected, result) |
|
23
|
|
|
|
|
24
|
|
|
def test_float(self): |
|
25
|
|
|
expected = 3.412 |
|
26
|
|
|
result = parse_float('3.412') |
|
27
|
|
|
|
|
28
|
|
|
self.assertEqual(result, expected) |
|
29
|
|
|
|
|
30
|
|
|
def test_int(self): |
|
31
|
|
|
expected = 3 |
|
32
|
|
|
result = parse_int('3') |
|
33
|
|
|
|
|
34
|
|
|
self.assertEqual(expected, result) |
|
35
|
|
|
|
|
36
|
|
|
def test_numeric_nan_none(self): |
|
37
|
|
|
nulls = [''] |
|
38
|
|
|
|
|
39
|
|
|
for val in nulls: |
|
40
|
|
|
with self.subTest(val=val): |
|
41
|
|
|
self.assertEqual(parse_numeric(float, val), None) |
|
42
|
|
|
|
|
43
|
|
|
def test_int_bool(self): |
|
44
|
|
|
mock_data = [(1, True), (0, False), (None, None)] |
|
45
|
|
|
|
|
46
|
|
|
for input_expected in mock_data: |
|
47
|
|
|
with self.subTest(input_expect=input_expected): |
|
48
|
|
|
result = parse_int_bool(input_expected[0]) |
|
49
|
|
|
expected = input_expected[1] |
|
50
|
|
|
|
|
51
|
|
|
self.assertEqual(expected, result) |
|
52
|
|
|
|
|
53
|
|
|
def test_parse_iso_date(self): |
|
54
|
|
|
mock_data = [('2014-04-04', date(2014, 4, 4)), ('', None), (None, None)] |
|
55
|
|
|
|
|
56
|
|
|
for input_expected in mock_data: |
|
57
|
|
|
with self.subTest(input_expect=input_expected): |
|
58
|
|
|
result = parse_iso_date(input_expected[0]) |
|
59
|
|
|
expected = input_expected[1] |
|
60
|
|
|
|
|
61
|
|
|
self.assertEqual(expected, result) |
|
62
|
|
|
|
|
63
|
|
|
def test_pass_through(self): |
|
64
|
|
|
expected = 123 |
|
65
|
|
|
result = passthrough(expected) |
|
66
|
|
|
|
|
67
|
|
|
self.assertEqual(expected, result) |
|
68
|
|
|
|
|
69
|
|
|
class TestQuotedTextParsers(unittest.TestCase): |
|
70
|
|
|
def test_bool(self): |
|
71
|
|
|
mock_data = [('"1"', True), ('"0"', False), ('""', None), ('', None)] |
|
72
|
|
|
|
|
73
|
|
|
for input_expected in mock_data: |
|
74
|
|
|
with self.subTest(input_expect=input_expected): |
|
75
|
|
|
result = parse_quoted_bool(input_expected[0]) |
|
76
|
|
|
expected = input_expected[1] |
|
77
|
|
|
|
|
78
|
|
|
self.assertEqual(expected, result) |
|
79
|
|
|
|
|
80
|
|
|
def test_float(self): |
|
81
|
|
|
expected = 3.412 |
|
82
|
|
|
result = parse_quoted_float('"3.412"') |
|
83
|
|
|
|
|
84
|
|
|
self.assertEqual(result, expected) |
|
85
|
|
|
|
|
86
|
|
|
def test_int(self): |
|
87
|
|
|
expected = 3 |
|
88
|
|
|
result = parse_quoted_int('"3"') |
|
89
|
|
|
|
|
90
|
|
|
self.assertEqual(expected, result) |
|
91
|
|
|
|
|
92
|
|
|
def test_numeric_nan(self): |
|
93
|
|
|
nulls = ['""', ''] |
|
94
|
|
|
|
|
95
|
|
|
for val in nulls: |
|
96
|
|
|
with self.subTest(val=val): |
|
97
|
|
|
self.assertEqual(parse_quoted_numeric(float, val), None) |
|
98
|
|
|
|
|
99
|
|
|
def test_parse_quoted_string(self): |
|
100
|
|
|
mock_data = [('""', None), ('"Hello"', 'Hello')] |
|
101
|
|
|
|
|
102
|
|
|
for input_expected in mock_data: |
|
103
|
|
|
with self.subTest(input_expect=input_expected): |
|
104
|
|
|
result = parse_quoted_string(input_expected[0]) |
|
105
|
|
|
expected = input_expected[1] |
|
106
|
|
|
|
|
107
|
|
|
self.assertEqual(expected, result) |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
class Klass: |
|
111
|
|
|
pass |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
class TestMakeConverters(unittest.TestCase): |
|
115
|
|
|
def test_casting_functions(self): |
|
116
|
|
|
datatype_names = ['ticker', 'shares', 'price', 'bought', 'custom'] |
|
117
|
|
|
inputted_types = [str, int, float, bool, Klass] |
|
118
|
|
|
expected_types = [passthrough, parse_int, parse_float, |
|
119
|
|
|
parse_bool, Klass] |
|
120
|
|
|
inputted = dict(zip(datatype_names, inputted_types)) |
|
121
|
|
|
|
|
122
|
|
|
expected = dict(zip(datatype_names, expected_types)) |
|
123
|
|
|
result = make_converters(inputted) |
|
124
|
|
|
|
|
125
|
|
|
self.assertEqual(expected, result) |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
class TestJSONParsers(unittest.TestCase): |
|
129
|
|
|
def test_parse_broken_json(self): |
|
130
|
|
|
broken_json = '{success:true}' |
|
131
|
|
|
|
|
132
|
|
|
expected = {'success': True} |
|
133
|
|
|
result = parse_broken_json(broken_json) |
|
134
|
|
|
|
|
135
|
|
|
self.assertEqual(expected, result) |
|
136
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.