forked from dbt-labs/dbt-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_agate_helper.py
156 lines (130 loc) · 5.98 KB
/
test_agate_helper.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
import unittest
import agate
from datetime import datetime
from decimal import Decimal
from isodate import tzinfo
import os
from shutil import rmtree
from tempfile import mkdtemp
from dbt.clients import agate_helper
SAMPLE_CSV_DATA = """a,b,c,d,e,f,g
1,n,test,3.2,20180806T11:33:29.320Z,True,NULL
2,y,asdf,900,20180806T11:35:29.320Z,False,a string"""
SAMPLE_CSV_BOM_DATA = u'\ufeff' + SAMPLE_CSV_DATA
EXPECTED = [
[
1, 'n', 'test', Decimal('3.2'),
datetime(2018, 8, 6, 11, 33, 29, 320000, tzinfo=tzinfo.Utc()),
True, None,
],
[
2, 'y', 'asdf', 900,
datetime(2018, 8, 6, 11, 35, 29, 320000, tzinfo=tzinfo.Utc()),
False, 'a string',
],
]
EXPECTED_STRINGS = [
['1', 'n', 'test', '3.2', '20180806T11:33:29.320Z', 'True', None],
['2', 'y', 'asdf', '900', '20180806T11:35:29.320Z', 'False', 'a string'],
]
class TestAgateHelper(unittest.TestCase):
def setUp(self):
self.tempdir = mkdtemp()
def tearDown(self):
rmtree(self.tempdir)
def test_from_csv(self):
path = os.path.join(self.tempdir, 'input.csv')
with open(path, 'wb') as fp:
fp.write(SAMPLE_CSV_DATA.encode('utf-8'))
tbl = agate_helper.from_csv(path, ())
self.assertEqual(len(tbl), len(EXPECTED))
for idx, row in enumerate(tbl):
self.assertEqual(list(row), EXPECTED[idx])
def test_bom_from_csv(self):
path = os.path.join(self.tempdir, 'input.csv')
with open(path, 'wb') as fp:
fp.write(SAMPLE_CSV_BOM_DATA.encode('utf-8'))
tbl = agate_helper.from_csv(path, ())
self.assertEqual(len(tbl), len(EXPECTED))
for idx, row in enumerate(tbl):
self.assertEqual(list(row), EXPECTED[idx])
def test_from_csv_all_reserved(self):
path = os.path.join(self.tempdir, 'input.csv')
with open(path, 'wb') as fp:
fp.write(SAMPLE_CSV_DATA.encode('utf-8'))
tbl = agate_helper.from_csv(path, tuple('abcdefg'))
self.assertEqual(len(tbl), len(EXPECTED_STRINGS))
for expected, row in zip(EXPECTED_STRINGS, tbl):
self.assertEqual(list(row), expected)
def test_from_data(self):
column_names = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
data = [
{'a': '1', 'b': 'n', 'c': 'test', 'd': '3.2',
'e': '20180806T11:33:29.320Z', 'f': 'True', 'g': 'NULL'},
{'a': '2', 'b': 'y', 'c': 'asdf', 'd': '900',
'e': '20180806T11:35:29.320Z', 'f': 'False', 'g': 'a string'}
]
tbl = agate_helper.table_from_data(data, column_names)
self.assertEqual(len(tbl), len(EXPECTED))
for idx, row in enumerate(tbl):
self.assertEqual(list(row), EXPECTED[idx])
def test_datetime_formats(self):
path = os.path.join(self.tempdir, 'input.csv')
datetimes = [
'20180806T11:33:29.000Z',
'20180806T11:33:29Z',
'20180806T113329Z',
]
expected = datetime(2018, 8, 6, 11, 33, 29, 0, tzinfo=tzinfo.Utc())
for dt in datetimes:
with open(path, 'wb') as fp:
fp.write('a\n{}'.format(dt).encode('utf-8'))
tbl = agate_helper.from_csv(path, ())
self.assertEqual(tbl[0][0], expected)
def test_merge_allnull(self):
t1 = agate.Table([(1, 'a', None), (2, 'b', None)], ('a', 'b', 'c'))
t2 = agate.Table([(3, 'c', None), (4, 'd', None)], ('a', 'b', 'c'))
result = agate_helper.merge_tables([t1, t2])
self.assertEqual(result.column_names, ('a', 'b', 'c'))
assert isinstance(result.column_types[0], agate.data_types.Number)
assert isinstance(result.column_types[1], agate.data_types.Text)
assert isinstance(result.column_types[2], agate.data_types.Number)
self.assertEqual(len(result), 4)
def test_merge_mixed(self):
t1 = agate.Table([(1, 'a', None), (2, 'b', None)], ('a', 'b', 'c'))
t2 = agate.Table([(3, 'c', 'dog'), (4, 'd', 'cat')], ('a', 'b', 'c'))
t3 = agate.Table([(3, 'c', None), (4, 'd', None)], ('a', 'b', 'c'))
result = agate_helper.merge_tables([t1, t2])
self.assertEqual(result.column_names, ('a', 'b', 'c'))
assert isinstance(result.column_types[0], agate.data_types.Number)
assert isinstance(result.column_types[1], agate.data_types.Text)
assert isinstance(result.column_types[2], agate.data_types.Text)
self.assertEqual(len(result), 4)
result = agate_helper.merge_tables([t2, t3])
self.assertEqual(result.column_names, ('a', 'b', 'c'))
assert isinstance(result.column_types[0], agate.data_types.Number)
assert isinstance(result.column_types[1], agate.data_types.Text)
assert isinstance(result.column_types[2], agate.data_types.Text)
self.assertEqual(len(result), 4)
result = agate_helper.merge_tables([t1, t2, t3])
self.assertEqual(result.column_names, ('a', 'b', 'c'))
assert isinstance(result.column_types[0], agate.data_types.Number)
assert isinstance(result.column_types[1], agate.data_types.Text)
assert isinstance(result.column_types[2], agate.data_types.Text)
self.assertEqual(len(result), 6)
def test_nocast_string_types(self):
# String fields should not be coerced into a representative type
# See: https://github.com/dbt-labs/dbt-core/issues/2984
column_names = ['a', 'b', 'c', 'd', 'e']
result_set = [
{'a': '0005', 'b': '01T00000aabbccdd', 'c': 'true', 'd': 10, 'e': False},
{'a': '0006', 'b': '01T00000aabbccde', 'c': 'false', 'd': 11, 'e': True},
]
tbl = agate_helper.table_from_data_flat(data=result_set, column_names=column_names)
self.assertEqual(len(tbl), len(result_set))
expected = [
['0005', '01T00000aabbccdd', 'true', Decimal(10), False],
['0006', '01T00000aabbccde', 'false', Decimal(11), True],
]
for i, row in enumerate(tbl):
self.assertEqual(list(row), expected[i])