def test_update_item(self): table = Table("test", ['a']) table['a'] = dict(b='B') table['a'] = dict(b='C') self.assertTrue('a' in table) self.assertEqual(table['a'], {'a': 'a', 'b': 'C'}) self.assertEqual(1, len(table)) self.assertEqual([('a', 'C')], list(table.to_tuples()))
def test_insert_item(self): table = Table("test", ['a']) table['A'] = dict(b='B') table['a'] = dict(b='B') self.assertTrue('A' in table) self.assertTrue('a' in table) self.assertEqual(table['a'], {'a': 'a', 'b': 'B'}) self.assertEqual(table['A'], {'a': 'A', 'b': 'B'}) self.assertEqual(2, len(table)) self.assertEqual([('A', 'B'), ('a', 'B')], list(table.to_tuples()))
def test_tuples_with_array_column(self): table = Table("test", ['a']) table['A'] = dict(b='B', c=[1, 2], d='d') table['B'] = dict(e='E') expected_tuples = [('A', 'B', [1, 2], 'd', float('nan')), ('B', float('nan'), float('nan'), float('nan'), 'E') ] actual_tuples = table.to_tuples() for expected_tuple, actual_tuple in zip(expected_tuples, actual_tuples): self.assertTrue( all([ v1 == v2 or (math.isnan(v1) and math.isnan(v2)) for v1, v2 in zip(expected_tuple, actual_tuple) ]))
def test_insert_two_pack_item(self): table = Table("test", ['a']) table['A'] = dict(c=1, _packed=dict(b=['B', 'b'], d=['D', 'd'])) self.assertTrue('A' in table) self.assertEqual(table['A'], { 'a': 'A', 'index': [1, 2], 'b': ['B', 'b'], 'c': 1, 'd': ['D', 'd'] }) self.assertEqual(2, len(table)) self.assertEqual([('A', 1, 1, 'B', 'D'), ('A', 2, 1, 'b', 'd')], list(table.to_tuples()))
def test_missing_columns(self): table = Table("test", ['a']) table['A'] = dict(b='B') table['a'] = dict(c='C') self.assertTrue('A' in table) self.assertTrue('a' in table) self.assertEqual(table['A'], {'a': 'A', 'b': 'B'}) self.assertEqual(table['a'], {'a': 'a', 'c': 'C'}) self.assertEqual(2, len(table)) expected = [('A', 'B', float('nan')), ('a', float('nan'), 'C')] actual = table.to_tuples() for tuple1, tuple2 in zip(expected, actual): for val1, val2 in zip(tuple1, tuple2): if isinstance(val1, float) and math.isnan(val1): self.assertTrue(math.isnan(val2)) else: self.assertEqual(val1, val2)