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
|
from agate import Table
from agate.data_types import Number, Text
from agate.exceptions import DataTypeError
from agate.testcase import AgateTestCase
class TestMerge(AgateTestCase):
def setUp(self):
self.rows = (
(1, 4, 'a'),
(2, 3, 'b'),
(None, 2, 'c')
)
self.number_type = Number()
self.text_type = Text()
self.column_names = ['one', 'two', 'three']
self.column_types = [self.number_type, self.number_type, self.text_type]
def test_merge(self):
table_a = Table(self.rows, self.column_names, self.column_types)
table_b = Table(self.rows, self.column_names)
table_c = Table.merge([table_a, table_b])
self.assertIsNot(table_c, table_a)
self.assertIsNot(table_c, table_b)
self.assertColumnNames(table_c, self.column_names)
self.assertColumnTypes(table_c, [Number, Number, Text])
self.assertRows(table_c, self.rows + self.rows)
def test_merge_different_names(self):
table_a = Table(self.rows, self.column_names, self.column_types)
column_names = ['a', 'b', 'c']
table_b = Table(self.rows, column_names, self.column_types)
table_c = Table.merge([table_a, table_b])
self.assertIsNot(table_c, table_a)
self.assertIsNot(table_c, table_b)
self.assertColumnNames(table_c, self.column_names + column_names)
self.assertColumnTypes(table_c, [Number, Number, Text, Number, Number, Text])
self.assertSequenceEqual(table_c.rows[0], [1, 4, 'a', None, None, None])
self.assertSequenceEqual(table_c.rows[3], [None, None, None, 1, 4, 'a'])
for row in table_c.rows:
self.assertSequenceEqual(row.keys(), self.column_names + column_names)
def test_merge_mixed_names(self):
table_a = Table(self.rows, self.column_names, self.column_types)
column_names = ['two', 'one', 'four']
table_b = Table(self.rows, column_names, self.column_types)
table_c = Table.merge([table_a, table_b])
self.assertIsNot(table_c, table_a)
self.assertIsNot(table_c, table_b)
self.assertColumnNames(table_c, ['one', 'two', 'three', 'four'])
self.assertColumnTypes(table_c, [Number, Number, Text, Text])
self.assertSequenceEqual(table_c.rows[0], [1, 4, 'a', None])
self.assertSequenceEqual(table_c.rows[3], [4, 1, None, 'a'])
for row in table_c.rows:
self.assertSequenceEqual(row.keys(), ['one', 'two', 'three', 'four'])
def test_merge_different_types(self):
table_a = Table(self.rows, self.column_names, self.column_types)
column_types = [self.number_type, self.text_type, self.text_type]
table_b = Table(self.rows, self.column_names, column_types)
with self.assertRaises(DataTypeError):
Table.merge([table_a, table_b])
def test_merge_with_row_names(self):
table_a = Table(self.rows, self.column_names, self.column_types, row_names='three')
b_rows = (
(1, 4, 'd'),
(2, 3, 'e'),
(None, 2, 'f')
)
table_b = Table(b_rows, self.column_names, self.column_types, row_names='three')
table_c = Table.merge([table_a, table_b], row_names='three')
self.assertRowNames(table_c, ['a', 'b', 'c', 'd', 'e', 'f'])
def test_merge_with_column_names(self):
table_a = Table(self.rows, self.column_names, self.column_types, row_names='three')
b_rows = (
(1, 4, 'd'),
(2, 3, 'e'),
(None, 2, 'f')
)
c_rows = (
(1, 4, 'a'),
(2, 3, 'b'),
(None, 2, 'c'),
(None, 4, 'd'),
(None, 3, 'e'),
(None, 2, 'f')
)
table_b = Table(b_rows, ['a', 'two', 'three'], self.column_types, row_names='three')
table_c = Table.merge([table_a, table_b], column_names=table_a.column_names)
self.assertRows(table_c, c_rows)
|