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
|
from agate import Table
from agate.data_types import Number, Text
from agate.testcase import AgateTestCase
from agate.type_tester import TypeTester
class TestNormalize(AgateTestCase):
def setUp(self):
self.rows = (
(1, 'c', 4, 'a'),
(2, 'e', 3, 'b'),
(None, 'g', 2, 'c')
)
self.number_type = Number()
self.text_type = Text()
self.column_names = ['one', 'two', 'three', 'four']
self.column_types = [self.number_type, self.text_type, self.number_type, self.text_type]
def test_normalize(self):
table = Table(self.rows, self.column_names, self.column_types)
normalized_table = table.normalize('one', 'three')
normal_rows = (
(1, 'three', 4),
(2, 'three', 3),
(None, 'three', 2)
)
self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['one', 'property', 'value'])
self.assertColumnTypes(normalized_table, [Number, Text, Number])
def test_normalize_column_types(self):
table = Table(self.rows, self.column_names, self.column_types)
normalized_table = table.normalize('one', 'three', column_types=[Text(), Text()])
normal_rows = (
(1, 'three', '4'),
(2, 'three', '3'),
(None, 'three', '2')
)
self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['one', 'property', 'value'])
self.assertColumnTypes(normalized_table, [Number, Text, Text])
def test_normalize_column_type_tester(self):
table = Table(self.rows, self.column_names, self.column_types)
normalized_table = table.normalize('one', 'three', column_types=TypeTester(force={'value': Text()}))
normal_rows = (
(1, 'three', '4'),
(2, 'three', '3'),
(None, 'three', '2')
)
self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['one', 'property', 'value'])
self.assertColumnTypes(normalized_table, [Number, Text, Text])
def test_normalize_multiple_fields(self):
table = Table(self.rows, self.column_names, self.column_types)
normalized_table = table.normalize('one', ['three', 'four'])
normal_rows = (
(1, 'three', '4'),
(1, 'four', 'a'),
(2, 'three', '3'),
(2, 'four', 'b'),
(None, 'three', '2'),
(None, 'four', 'c')
)
self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['one', 'property', 'value'])
self.assertColumnTypes(normalized_table, [Number, Text, Text])
def test_normalize_multiple_keys(self):
table = Table(self.rows, self.column_names, self.column_types)
normalized_table = table.normalize(['one', 'two'], ['three', 'four'])
normal_rows = (
(1, 'c', 'three', '4'),
(1, 'c', 'four', 'a'),
(2, 'e', 'three', '3'),
(2, 'e', 'four', 'b'),
(None, 'g', 'three', '2'),
(None, 'g', 'four', 'c')
)
self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['one', 'two', 'property', 'value'])
self.assertColumnTypes(normalized_table, [Number, Text, Text, Text])
def test_normalize_change_order(self):
table = Table(self.rows, self.column_names, self.column_types)
normalized_table = table.normalize('three', ['one', 'four'])
normal_rows = (
(4, 'one', '1'),
(4, 'four', 'a'),
(3, 'one', '2'),
(3, 'four', 'b'),
(2, 'one', None),
(2, 'four', 'c')
)
self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['three', 'property', 'value'])
self.assertColumnTypes(normalized_table, [Number, Text, Text])
|