def test_repr_on_expands(self): t = Table([('A', int), ('B', int)]) t.append([1, 2]) e = t.expand('C', ['A', 'B'], lambda *args: sum(args), int) self.assertEqual(repr(e), "\n".join([ "| A (int) | B (int) | C (int) |", "| 1 | 2 | 3 |" ]) )
class TestNormalize(unittest.TestCase, TableTestMixin): def setUp(self): self.s = [ ('A', int), ('B', float), ('C', str), ] self.t = Table(self.s) self.t.extend([ [1, 0.0, 'x'], [2, 5.0, 'y'], [3, 10.0, 'z'], ]) def test_basic_normalize(self): t = self.t.normalize({"B":1.0}) self.assertEqual(list(t.B), [0, 0.5, 1]) def test_basic_standardization(self): t = self.t.standardize({"B":1.0}) result = list(t.B) self.assertEqual(result[1], 0) self.assertEqual(result[0], result[2]*-1) def test_whole_of_normalized_table(self): tn = self.t.normalize({"B":1.0}) expected = table_literal(""" | A (int) | B (float) | C (str) | | 1 | 0 | x | | 2 | 0.5 | y | | 3 | 1.0 | z | """) def test_whole_of_normalized_table_100(self): tn = self.t.normalize({"B":100}) expected = table_literal(""" | A (int) | B (float) | C (str) | | 1 | 0 | x | | 2 | 50 | y | | 3 | 100 | z | """) self.assertTablesEqual(tn, expected) def test_expand_of_normalized_table(self): tn = self.t.normalize({"B":1.0}).expand( name='D', col_type=float, input_columns=['A','B'], fn=lambda A,B: A * B ) expected = table_literal(""" | A (int) | B (float) | C (str) | D (float) | | 1 | 0 | x | 0.0 | | 2 | 0.5 | y | 1.0 | | 3 | 1.0 | z | 3.0 | """) self.assertTablesEqual(tn, expected) def test_simple_expand(self): t = self.t.expand( name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) ) self.assertEqual(list(t.D), [1,1,1]) def test_derived_columns_are_iterable(self): t = self.t.expand( name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) ) for _ in t.D: pass def test_derived_columns_can_be_printed(self): t = self.t.expand( name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) ) str(t.D) repr(t.D) def test_derived_columns_have_descriptions(self): t = self.t.expand( name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) ) self.assertEqual(t.D.description, "D (int)") def test_that_float_columns_have_descriptions(self): t = self.t.expand( name='D', input_columns=['A', 'B', 'C'], fn=lambda a,b,c: float(len(c) + a + b), col_type=float ) self.assertEqual(t.D.description, "D (float)") def test_simple_expand_and_materialize(self): t = self.t.expand( name='D', col_type=int, input_columns=['A', 'C'], fn=lambda A, C: len(C) + A ).copy() expected = [ (1, 0.0, 'x', 2), (2, 5.0, 'y', 3), (3, 10.0, 'z', 4), ] self.assertEqual(list(t), expected) self.assertIsInstance(t, Table) def test_simple_expand_and_slice(self): t = self.t.expand( name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) + 2 )[1:2] expected = [ (2, 5.0, 'y', 3), ] self.assertEqual(list(t), expected)
class TestExport(unittest.TestCase, TableTestMixin): def setUp(self): self.s = [ ('A', int), ('B', float), ('C', str), ] self.t = Table(self.s) self.t.extend([ [1, 0.0, 'x'], [2, 5.0, 'y'], [3, 10.0, 'z'], ]) def test_export_csv(self): output_file = StringIO() self.t.to_csv(output_file=output_file) output_file.seek(0) reader = csv.DictReader(output_file) row = next(reader) self.assertEqual(row, {"A":"1", "B":"0.0", "C":"x"}) def test_export_csv_with_dialect(self): output_file = StringIO() self.t.to_csv(output_file=output_file, dialect="excel") output_file.seek(0) reader = csv.DictReader(output_file, dialect="excel") row = next(reader) self.assertEqual(row, {"A":"1", "B":"0.0", "C":"x"}) def test_export_csv_with_dialect_types(self): output_file = StringIO() self.t.to_csv(output_file=output_file, dialect="excel", descriptions=True) output_file.seek(0) reader = csv.DictReader(output_file, dialect="excel") self.assertEqual(next(reader), {"A (int)":"1", "B (float)":"0.0", "C (str)":"x"}) def test_simple_expand(self): t = self.t.expand( name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) ) self.assertEqual(list(t.D), [1, 1, 1]) def test_derived_columns_are_iterable(self): t = self.t.expand( name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) ) for _ in t.D: pass def test_derived_columns_can_be_printed(self): t = self.t.expand( name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) ) str(t.D) repr(t.D) def test_derived_columns_have_descriptions(self): t = self.t.expand( name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) ) self.assertEqual(t.D.description, "D (int)") def test_that_float_columns_have_descriptions(self): t = self.t.expand( name='D', input_columns=['A', 'B', 'C'], fn=lambda a,b,c: float(len(c) + a + b), col_type=float ) self.assertEqual(t.D.description, "D (float)") def test_simple_expand_and_materialize(self): t = self.t.expand( name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) + 1 ).copy() def test_export_csv_with_dialect_tab(self): output_file = StringIO() self.t.to_csv(output_file=output_file, dialect="excel-tab") output_file.seek(0) reader = csv.DictReader(output_file, dialect="excel-tab") row = next(reader) self.assertEqual(row, {"A":"1", "B":"0.0", "C":"x"}) def test_simple_expand_and_slice(self): t = self.t.expand( name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) + 1 )[1:2] expected = [ (2, 5.0, 'y', 2), ] self.assertEqual(list(t), expected)
class TestExpandTable(unittest.TestCase): def setUp(self): self.s = [ ('A', int), ('B', float), ('C', str), ] self.t = Table(self.s) self.t.extend([ [1, 1.1, 'hello'], [2, 2.2, 'yello'], ]) def test_expand_const(self): t = self.t.expand_const(name='D', value='X', type=str) self.assertEqual(t.column_names, ['A', 'B', 'C', 'D']) self.assertEqual(t[0], (1, 1.1, 'hello', 'X')) def test_expand_const_does_not_affect_length(self): t = self.t.expand_const(name='D', type=str, value='X') self.assertEqual(len(t), len(self.t)) def test_simple_expand(self): t = self.t.expand(name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C)) self.assertEqual(list(t.D), [5, 5]) def test_derived_columns_are_iterable(self): t = self.t.expand(name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C)) for _ in t.D: pass def test_derived_columns_can_be_printed(self): t = self.t.expand(name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C)) str(t.D) repr(t.D) def test_derived_columns_have_descriptions(self): t = self.t.expand(name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C)) self.assertEqual(t.D.description, "D (int)") def test_that_float_columns_have_descriptions(self): t = self.t.expand(name='D', input_columns=['A', 'B', 'C'], fn=lambda a, b, c: float(len(c) + a + b), col_type=float) self.assertEqual(t.D.description, "D (float)") def test_simple_expand_and_materialize(self): t = self.t.expand(name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) + 1).copy() expected = [ (1, 1.1, 'hello', 6), (2, 2.2, 'yello', 6), ] self.assertEqual(list(t), expected) self.assertIsInstance(t, Table) def test_simple_expand_and_slice(self): t = self.t.expand(name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) + 1)[1:] expected = [ (2, 2.2, 'yello', 6), ] self.assertEqual(list(t), expected)
class TestExport(unittest.TestCase, TableTestMixin): def setUp(self): self.s = [ ('A', int), ('B', float), ('C', str), ] self.t = Table(self.s) self.t.extend([ [1, 0.0, 'x'], [2, 5.0, 'y'], [3, 10.0, 'z'], ]) def test_export_csv(self): output_file = StringIO() self.t.to_csv(output_file=output_file) output_file.seek(0) reader = csv.DictReader(output_file) row = next(reader) self.assertEqual(row, {"A": "1", "B": "0.0", "C": "x"}) def test_export_csv_with_dialect(self): output_file = StringIO() self.t.to_csv(output_file=output_file, dialect="excel") output_file.seek(0) reader = csv.DictReader(output_file, dialect="excel") row = next(reader) self.assertEqual(row, {"A": "1", "B": "0.0", "C": "x"}) def test_export_csv_with_dialect_types(self): output_file = StringIO() self.t.to_csv(output_file=output_file, dialect="excel", descriptions=True) output_file.seek(0) reader = csv.DictReader(output_file, dialect="excel") self.assertEqual(next(reader), { "A (int)": "1", "B (float)": "0.0", "C (str)": "x" }) def test_simple_expand(self): t = self.t.expand(name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C)) self.assertEqual(list(t.D), [1, 1, 1]) def test_derived_columns_are_iterable(self): t = self.t.expand(name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C)) for _ in t.D: pass def test_derived_columns_can_be_printed(self): t = self.t.expand(name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C)) str(t.D) repr(t.D) def test_derived_columns_have_descriptions(self): t = self.t.expand(name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C)) self.assertEqual(t.D.description, "D (int)") def test_that_float_columns_have_descriptions(self): t = self.t.expand(name='D', input_columns=['A', 'B', 'C'], fn=lambda a, b, c: float(len(c) + a + b), col_type=float) self.assertEqual(t.D.description, "D (float)") def test_simple_expand_and_materialize(self): t = self.t.expand(name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) + 1).copy() def test_export_csv_with_dialect_tab(self): output_file = StringIO() self.t.to_csv(output_file=output_file, dialect="excel-tab") output_file.seek(0) reader = csv.DictReader(output_file, dialect="excel-tab") row = next(reader) self.assertEqual(row, {"A": "1", "B": "0.0", "C": "x"}) def test_simple_expand_and_slice(self): t = self.t.expand(name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) + 1)[1:2] expected = [ (2, 5.0, 'y', 2), ] self.assertEqual(list(t), expected)
class TestExpandTable(unittest.TestCase): def setUp(self): self.s = [ ('A', int), ('B', float), ('C', str), ] self.t = Table(self.s) self.t.extend([ [1, 1.1, 'hello'], [2, 2.2, 'yello'], ]) def test_expand_const(self): t = self.t.expand_const( name='D', value='X', type=str ) self.assertEqual( t.column_names, ['A', 'B', 'C', 'D'] ) self.assertEqual( t[0], (1, 1.1, 'hello', 'X') ) def test_expand_const_does_not_affect_length(self): t = self.t.expand_const( name='D', type=str, value='X' ) self.assertEqual(len(t), len(self.t)) def test_simple_expand(self): t = self.t.expand( name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) ) self.assertEqual(list(t.D), [5, 5]) def test_derived_columns_are_iterable(self): t = self.t.expand( name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) ) for _ in t.D: pass def test_derived_columns_can_be_printed(self): t = self.t.expand( name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) ) str(t.D) repr(t.D) def test_derived_columns_have_descriptions(self): t = self.t.expand( name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) ) self.assertEqual(t.D.description, "D (int)") def test_that_float_columns_have_descriptions(self): t = self.t.expand( name='D', input_columns=['A', 'B', 'C'], fn=lambda a,b,c: float(len(c) + a + b), col_type=float ) self.assertEqual(t.D.description, "D (float)") def test_simple_expand_and_materialize(self): t = self.t.expand( name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) + 1 ).copy() expected = [ (1, 1.1, 'hello', 6), (2, 2.2, 'yello', 6), ] self.assertEqual(list(t), expected) self.assertIsInstance(t, Table) def test_simple_expand_and_slice(self): t = self.t.expand( name='D', col_type=int, input_columns=['C'], fn=lambda C: len(C) + 1 )[1:] expected = [ (2, 2.2, 'yello', 6), ] self.assertEqual(list(t), expected)