def test_repr_joined_table(self): p = Table([('Owner Id', int), 'Pokemon', ('Level', int)]) p.extend([ [1, 'Pikachu', 18], [99, 'Mew', 43], # Cannot be joined ]) o = Table([('Owner Id', int), ('Name', str)]) o.append([1, 'Ash Ketchum']) j = p.left_join( keys=('Owner Id',), other=o ) expected = [ "| Owner Id (int) | Pokemon | Level (int) | Name (str) |", "| 1 | Pikachu | 18 | Ash Ketchum |", "| 99 | Mew | 43 | None |" ] for resultrow, expectedrow in zip( repr(j).split('\n'), expected): self.assertEqual( resultrow, expectedrow )
def test_joined_table_repr(self): p = Table([('Owner Id', int), 'Pokemon', ('Level', int)]) o = Table([('Owner Id', int), ('Owner Name', str)]) j = p.left_join(keys=('Owner Id',), other=o) self.assertEquals( repr(j), "| Owner Id (int) | Pokemon | Level (int) | Owner Name (str) |" )
def test_column_descriptions_on_join(self): p = Table([('Owner Id', int), 'Pokemon', ('Level', int)]) p.extend([ [1, 'Pikachu', 18], ]) o = Table([('Owner Id', int), ('Name', str)]) o.append([1, 'Ash Ketchum']) j = p.left_join( keys=('Owner Id',), other=o ) self.assertEqual( ['Owner Id (int)', 'Pokemon', 'Level (int)', 'Name (str)'], j._column_descriptions )
class TestJoinWithNonMatchingKeys(unittest.TestCase): def setUp(self): self.pokedex = Table(['pokemon', 'owner', 'level']) self.pokedex.extend([ ['Charmander', 'Ash', 12], ['Pikachu', 'Ash', 15], ['Squirtle', 'Ash', 19], ['Starmie', 'Misty', 19], ['Togepi', 'Misty', 5], ['Onyx', 'Brock', 22], ['Meowth', 'Team Rocket', 22], ]) self.types = Table(['pkmn', 'type']) self.types.extend([ ['Togepi', 'Fairy'], ['Onyx', 'Rock'], ['Meowth', 'Normal'], ['Pikachu', 'Electric'], ['Squirtle', 'Water'], ['Starmie', 'Water'], ['Charmander', 'Fire'], ]) def test_basic_join(self): t = self.pokedex.left_join( keys=('pokemon',), other=self.types, other_keys=('pkmn',), ) self.assertIsInstance(t, JoinTable) self.assertEquals( t._key_columns, [t.pokemon, ] ) self.assertEquals( t.column_names, ['pokemon', 'owner', 'level', 'type'] )
def test_repr_bigger_broken_join_with_project(self): p = Table([('Owner Id', int), 'Pokemon', ('Level', int)]) p.extend([ [1, 'Pikachu', 18], [1, 'Bulbasaur', 22], [1, 'Charmander', 12], [3, 'Togepi', 5], [1, 'Starmie', 44], [9, 'Mew', 99], ]) o = Table([('Owner Id', int), ('Name', str)]) o.append([1, 'Ash Ketchum']) o.append([2, 'Brock']) o.append([3, 'Misty']) j = p.left_join( keys=('Owner Id',), other=o ) j2 = j.project('Pokemon', 'Level', 'Name') print(repr(j2))
def test_joined_table_repr_one_row(self): p = Table([('Owner Id', int), 'Pokemon', ('Level', int)]) p.extend([ [1, 'Pikachu', 18], ]) o = Table([('Owner Id', int), ('Name', str)]) o.append([1, 'Ash Ketchum']) j = p.left_join( keys=('Owner Id',), other=o ) self.assertEquals( j[0], (1, 'Pikachu', 18, 'Ash Ketchum') ) self.assertEquals( list(j)[0], (1, 'Pikachu', 18, 'Ash Ketchum'), )
def test_incomplete_join(self): p = Table([('Owner Id', int), 'Pokemon', ('Level', int)]) p.extend([ [1, 'Pikachu', 18], [2, 'Blastoise', 22], [3, 'Weedle', 4], ]) o = Table([('Owner Id', int), ('Owner Name', str)]) o.append([1, 'Ash Ketchum']) o.append([2, 'Brock']) o.append([2, 'Misty']) j = p.left_join( keys=('Owner Id',), other=o ) self.assertEquals( j.column_names, ['Owner Id', 'Pokemon', 'Level', 'Owner Name'] )
def test_get_maximum_column_widths_for_join(self): p = Table([('Owner Id', int), 'Pokemon', ('Level', int)]) p.extend([ [1, 'Pikachu', 18], ]) o = Table([('Owner Id', int), ('Name', str)]) o.append([1, 'Ash Ketchum']) j = p.left_join( keys=('Owner Id',), other=o ) expected = [ len(j._get_column('Owner Id').description), len(j._get_column('Pokemon').description), len(j._get_column('Level').description), len('Ash Ketchum'), ] widths = j._get_column_widths() self.assertEqual( expected, widths )
class TestJoin(unittest.TestCase): def setUp(self): self.pokedex = Table(['pokemon', 'owner', ('level', 'i')]) self.pokedex.extend([ ['Charmander', 'Ash', 12], ['Pikachu', 'Ash', 15], ['Squirtle', 'Ash', 19], ['Starmie', 'Misty', 19], ['Togepi', 'Misty', 5], ['Onyx', 'Brock', 22], ['Meowth', 'Team Rocket', 22], ['Mew', None, 99], ]) self.types = Table(['pokemon', 'type']) self.types.extend([ ['Togepi', 'Fairy'], ['Onyx', 'Rock'], ['Meowth', 'Normal'], ['Pikachu', 'Electric'], ['Squirtle', 'Water'], ['Starmie', 'Water'], ['Charmander', 'Fire'], ]) def test_join_interface(self): t = self.pokedex.left_join( keys=('pokemon',), other=self.types ) self.assertEquals( t._key_columns, [t.pokemon, ] ) self.assertEquals( t.column_names, ['pokemon', 'owner', 'level', 'type'] ) def test_join_schema(self): t = self.pokedex.left_join( keys=('pokemon',), other=self.types ) expected = [ ('pokemon', object), ('owner', object), ('level', 'i'), ('type', object), ] self.assertEquals( t.schema, expected ) def test_join_indeces_function(self): t = self.pokedex.left_join( keys=('pokemon',), other=self.types ) self.assertEquals( list(t._join_indices_func()), [6, 3, 4, 5, 0, 1, 2, None] ) def test_join_row(self): t = self.pokedex.left_join( keys=('pokemon',), other=self.types ) self.assertEquals( t[0], ('Charmander', 'Ash', 12, 'Fire') ) def test_join_iter(self): t = self.pokedex.left_join( keys=('pokemon',), other=self.types ) tl = list(t) self.assertEquals( tl[1], ('Pikachu', 'Ash', 15, 'Electric') ) def test_impossible_join(self): t = self.pokedex.left_join( keys=('pokemon',), other=self.types ) self.assertEquals( t[7], ('Mew', None, 99, None) ) def test_over_read(self): t = self.pokedex.left_join( keys=('pokemon',), other=self.types ) with self.assertRaises(IndexError): t[99]
from eztable import Table p = Table([('Owner Id', int), 'Pokemon', ('Level', int)]) p.extend([ [1, 'Pikachu', 18], [1, 'Bulbasaur', 22], [1, 'Charmander', 12], [3, 'Togepi', 5], [1, 'Starmie', 44], [9, 'Mew', 99], ]) print(p) o = Table([('Owner Id', int), ('Name', str)]) o.append([1, 'Ash Ketchum']) o.append([2, 'Brock']) o.append([3, 'Misty']) print(o) j = p.left_join(keys=('Owner Id', ), other=o) print(j) j2 = j.project('Pokemon', 'Level', 'Name') print(j2) restricted = j2.restrict(['Name'], lambda n: n == 'Ash Ketchum') print(restricted) sliced = j2[1:4] print(sliced) j3 = j2.copy() i = j3.add_index(('Pokemon', )).reindex() print(i[('Pikachu', )])
p = Table([('Owner Id', int), 'Pokemon', ('Level', int)]) p.extend([ [1, 'Pikachu', 18], [1, 'Bulbasaur', 22], [1, 'Charmander', 12], [3, 'Togepi', 5], [1, 'Starmie', 44], [9, 'Mew', 99], ]) print(p) o = Table([('Owner Id', int), ('Name', str)]) o.append([1, 'Ash Ketchum']) o.append([2, 'Brock']) o.append([3, 'Misty']) print(o) j = p.left_join( keys=('Owner Id',), other=o ) print(j) j2 = j.project('Pokemon', 'Level', 'Name') print(j2) restricted = j2.restrict(['Name'], lambda n: n == 'Ash Ketchum') print(restricted) sliced = j2[1:4] print(sliced) j3 = j2.copy() i = j3.add_index(('Pokemon',)).reindex() print(i[('Pikachu',)])