-
Notifications
You must be signed in to change notification settings - Fork 155
/
test_denormalize.py
110 lines (81 loc) · 4.01 KB
/
test_denormalize.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
#!/usr/bin/env python
# -*- coding: utf8 -*-
from agate import Table
from agate.data_types import *
from agate.type_tester import TypeTester
from agate.testcase import AgateTestCase
class TestDenormalize(AgateTestCase):
def setUp(self):
self.rows = (
('Jane', 'Code', 'gender', 'female'),
('Jane', 'Code', 'age', '27'),
('Jim', 'Program', 'gender', 'male'),
('Jim', 'Bytes', 'age', '24')
)
self.text_type = Text()
self.column_names = ['first_name', 'last_name', 'property', 'value']
self.column_types = [self.text_type, self.text_type, self.text_type, self.text_type]
def test_denormalize(self):
table = Table(self.rows, self.column_names, self.column_types)
normalized_table = table.denormalize('first_name', 'property', 'value')
normal_rows = (
('Jane', 'female', 27),
('Jim', 'male', 24),
)
self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['first_name', 'gender', 'age'])
self.assertColumnTypes(normalized_table, [Text, Text, Number])
self.assertRowNames(normalized_table, ['Jane', 'Jim'])
def test_denormalize_no_key(self):
table = Table(self.rows, self.column_names, self.column_types)
normalized_table = table.denormalize(None, 'property', 'value')
# NB: value has been overwritten
normal_rows = (
('male', 24),
)
self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['gender', 'age'])
self.assertColumnTypes(normalized_table, [Text, Number])
def test_denormalize_multiple_keys(self):
table = Table(self.rows, self.column_names, self.column_types)
normalized_table = table.denormalize(['first_name', 'last_name'], 'property', 'value')
normal_rows = (
('Jane', 'Code', 'female', 27),
('Jim', 'Program', 'male', None),
('Jim', 'Bytes', None, 24),
)
self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['first_name', 'last_name', 'gender', 'age'])
self.assertColumnTypes(normalized_table, [Text, Text, Text, Number])
self.assertRowNames(normalized_table, [('Jane', 'Code'), ('Jim', 'Program'), ('Jim', 'Bytes')])
def test_denormalize_default_value(self):
table = Table(self.rows, self.column_names, self.column_types)
normalized_table = table.denormalize(['first_name', 'last_name'], 'property', 'value', default_value='hello')
normal_rows = (
('Jane', 'Code', 'female', '27'),
('Jim', 'Program', 'male', 'hello'),
('Jim', 'Bytes', 'hello', '24'),
)
self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['first_name', 'last_name', 'gender', 'age'])
self.assertColumnTypes(normalized_table, [Text, Text, Text, Text])
def test_denormalize_column_types(self):
table = Table(self.rows, self.column_names, self.column_types)
normalized_table = table.denormalize(None, 'property', 'value', column_types=[Text(), Number()])
# NB: value has been overwritten
normal_rows = (
('male', 24),
)
self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['gender', 'age'])
self.assertColumnTypes(normalized_table, [Text, Number])
def test_denormalize_column_type_tester(self):
table = Table(self.rows, self.column_names, self.column_types)
normalized_table = table.denormalize(None, 'property', 'value', column_types=TypeTester(force={'gender': Text()}))
# NB: value has been overwritten
normal_rows = (
('male', 24),
)
self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['gender', 'age'])
self.assertColumnTypes(normalized_table, [Text, Number])