def test_compute_with_row_names(self): table = Table(self.rows, self.column_names, self.column_types, row_names='three') new_table = table.compute([ ('number', Formula(self.number_type, lambda r: r['two'] + r['three'])), ('text', Formula(self.text_type, lambda r: (r['one'] or '-') + six.text_type(r['three']))) ]) self.assertRowNames(new_table, [3, 5, 4, 6])
def test_compute_with_row_names(self): table = Table(self.rows, self.columns, row_names='three') new_table = table.compute([ (Formula(self.number_type, lambda r: r['two'] + r['three']), 'number'), (Formula(self.text_type, lambda r: (r['one'] or '-') + six.text_type(r['three'])), 'text') ]) self.assertSequenceEqual(new_table.rows[0], ('a', 2, 3, 4, 5, 'a3')) self.assertSequenceEqual(new_table.row_names, (3, 5, 4, 6))
def test_compute_replace_partial(self): new_table = self.table.compute([ ('two', Formula(self.number_type, lambda r: r['two'] + r['three'])), ('test', Formula(self.number_type, lambda r: 1)) ], replace=True) self.assertIsNot(new_table, self.table) self.assertColumnNames(new_table, ['one', 'two', 'three', 'four', 'test']) self.assertColumnTypes(new_table, [Text, Number, Number, Number, Number]) self.assertSequenceEqual(new_table.rows[0], ('a', 5, 3, 4, 1)) self.assertSequenceEqual(new_table.columns['two'], (5, 8, 6, 9))
def test_compute_creates_rows(self): table = Table(self.rows, self.columns) table2 = table.compute([(Formula(self.number_type, lambda r: r['one']), 'new2')]) table3 = table2.compute([(Formula(self.number_type, lambda r: r['one']), 'new3')]) self.assertIsNot(table.rows[0], table2.rows[0]) self.assertNotEqual(table.rows[0], table2.rows[0]) self.assertIsNot(table2.rows[0], table3.rows[0]) self.assertNotEqual(table2.rows[0], table3.rows[0]) self.assertSequenceEqual(table.rows[0], (1, 4, 'a'))
def test_compute(self): new_table = self.table.compute([ (Formula(self.number_type, lambda r: r['two'] + r['three']), 'number'), (Formula(self.text_type, lambda r: (r['one'] or '-') + six.text_type(r['three'])), 'text') ]) self.assertIsNot(new_table, self.table) self.assertEqual(len(new_table.rows), 4) self.assertEqual(len(new_table.columns), 6) self.assertSequenceEqual(new_table.rows[0], ('a', 2, 3, 4, 5, 'a3')) self.assertSequenceEqual(new_table.columns['number'], (5, 8, 6, 9)) self.assertSequenceEqual(new_table.columns['text'], ('a3', '-5', 'a4', 'b6'))
def test_compute_multiple(self): new_table = self.table.compute([ ('number', Formula(self.number_type, lambda r: r['two'] + r['three'])), ('text', Formula(self.text_type, lambda r: (r['one'] or '-') + six.text_type(r['three']))) ]) self.assertIsNot(new_table, self.table) self.assertColumnNames(new_table, ['one', 'two', 'three', 'four', 'number', 'text']) self.assertColumnTypes(new_table, [Text, Number, Number, Number, Number, Text]) self.assertSequenceEqual(new_table.rows[0], ('a', 2, 3, 4, 5, 'a3')) self.assertSequenceEqual(new_table.columns['number'], (5, 8, 6, 9)) self.assertSequenceEqual(new_table.columns['text'], ('a3', '-5', 'a4', 'b6'))
def test_compute(self): tableset = TableSet(self.tables.values(), self.tables.keys()) new_tableset = tableset.compute([ ('new_column', Formula(self.text_type, lambda r: '%(letter)s-%(number)i' % r)) ]) new_table = new_tableset['table1'] self.assertColumnNames(new_table, ('letter', 'number', 'new_column',)) self.assertColumnTypes(new_table, (Text, Number, Text)) self.assertRows(new_table, [ ('a', 1, 'a-1'), ('a', 3, 'a-3'), ('b', 2, 'b-2') ]) new_table = new_tableset['table2'] self.assertRows(new_table, [ ('b', 0, 'b-0'), ('a', 2, 'a-2'), ('c', 5, 'c-5') ]) new_table = new_tableset['table3'] self.assertSequenceEqual(new_table.rows[0], ('a', 1, 'a-1')) self.assertSequenceEqual(new_table.rows[1], ('a', 2, 'a-2')) self.assertSequenceEqual(new_table.rows[2], ('c', 3, 'c-3'))
def test_compute(self): tableset = TableSet(self.tables.values(), self.tables.keys()) new_tableset = tableset.compute([ (Formula(self.text_type, lambda r: '%(letter)s-%(number)i' % r), 'new_column') ]) new_table = new_tableset['table1'] self.assertEqual(len(new_table.rows), 3) self.assertEqual(len(new_table.columns), 3) self.assertSequenceEqual(new_table._column_types, (self.text_type, self.number_type, self.text_type,)) self.assertSequenceEqual(new_table._column_names, ('letter', 'number', 'new_column',)) self.assertSequenceEqual(new_table.rows[0], ('a', 1, 'a-1')) self.assertSequenceEqual(new_table.rows[1], ('a', 3, 'a-3')) self.assertSequenceEqual(new_table.rows[2], ('b', 2, 'b-2')) new_table = new_tableset['table2'] self.assertSequenceEqual(new_table.rows[0], ('b', 0, 'b-0')) self.assertSequenceEqual(new_table.rows[1], ('a', 2, 'a-2')) self.assertSequenceEqual(new_table.rows[2], ('c', 5, 'c-5')) new_table = new_tableset['table3'] self.assertSequenceEqual(new_table.rows[0], ('a', 1, 'a-1')) self.assertSequenceEqual(new_table.rows[1], ('a', 2, 'a-2')) self.assertSequenceEqual(new_table.rows[2], ('c', 3, 'c-3'))
def test_compute_multiple(self): new_table = self.table.compute([ (Formula(self.number_type, lambda r: r['two'] + r['three']), 'test') ]) self.assertIsNot(new_table, self.table) self.assertEqual(len(new_table.rows), 4) self.assertEqual(len(new_table.columns), 5) self.assertSequenceEqual(new_table.rows[0], ('a', 2, 3, 4, 5)) self.assertSequenceEqual(new_table.columns['test'], (5, 8, 6, 9))
def test_compute_replace_change_type(self): new_table = self.table.compute([ ('two', Formula(self.text_type, lambda r: 'a')) ], replace=True) self.assertIsNot(new_table, self.table) self.assertColumnNames(new_table, ['one', 'two', 'three', 'four']) self.assertColumnTypes(new_table, [Text, Text, Number, Number]) self.assertSequenceEqual(new_table.rows[0], ('a', 'a', 3, 4)) self.assertSequenceEqual(new_table.columns['two'], ('a', 'a', 'a', 'a'))
def test_formula_no_validate(self): new_table = self.table.compute([('test', Formula(self.number_type, lambda r: r['one'], cast=False))]) # Now everything is screwed up self.assertSequenceEqual( new_table.rows[0], ('a', Decimal('2'), Decimal('3'), Decimal('4'), 'a')) self.assertEqual(new_table.columns['test'][0], 'a')
def test_formula(self): new_table = self.table.compute([ ('test', Formula(self.number_type, lambda r: r['two'] + r['three'])) ]) self.assertIsNot(new_table, self.table) self.assertEqual(len(new_table.rows), 4) self.assertEqual(len(new_table.columns), 5) self.assertSequenceEqual( new_table.rows[0], ('a', Decimal('2'), Decimal('3'), Decimal('4'), Decimal('5'))) self.assertEqual(new_table.columns['test'][0], Decimal('5')) self.assertEqual(new_table.columns['test'][1], Decimal('8')) self.assertEqual(new_table.columns['test'][2], Decimal('6')) self.assertEqual(new_table.columns['test'][3], Decimal('7'))
def test_formula_invalid(self): with self.assertRaises(CastError): self.table.compute([('test', Formula(self.number_type, lambda r: r['one']))])