Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_from_csv(self):
tableset1 = TableSet(self.tables.values(), self.tables.keys())
tableset2 = TableSet.from_csv('examples/tableset', self.column_names)
self.assertSequenceEqual(tableset1.column_names, tableset2.column_names)
self.assertSequenceEqual([type(t) for t in tableset1.column_types], [type(t) for t in tableset2.column_types])
self.assertEqual(len(tableset1), len(tableset2))
for name in ['table1', 'table2', 'table3']:
self.assertEqual(len(tableset1[name].columns), len(tableset2[name].columns))
self.assertEqual(len(tableset1[name].rows), len(tableset2[name].rows))
self.assertSequenceEqual(tableset1[name].rows[0], tableset2[name].rows[0])
self.assertSequenceEqual(tableset1[name].rows[1], tableset2[name].rows[1])
self.assertSequenceEqual(tableset1[name].rows[2], tableset2[name].rows[2])
def test_aggregate_two_ops(self):
tableset = TableSet(self.tables.values(), self.tables.keys())
new_table = tableset.aggregate([
('count', Count()),
('number_sum', Sum('number')),
('number_mean', Mean('number'))
])
self.assertIsInstance(new_table, Table)
self.assertColumnNames(new_table, ('group', 'count', 'number_sum', 'number_mean'))
self.assertColumnTypes(new_table, [Text, Number, Number, Number])
self.assertRows(new_table, [
('table1', 3, 6, 2),
('table2', 3, 7, Decimal(7) / 3),
('table3', 3, 6, 2)
])
def test_get_column_names(self):
tableset = TableSet(self.tables.values(), self.tables.keys())
self.assertSequenceEqual(tableset.column_names, self.column_names)
def test_proxy_maintains_key(self):
number_type = Number()
tableset = TableSet(self.tables.values(), self.tables.keys(), key_name='foo', key_type=number_type)
self.assertEqual(tableset.key_name, 'foo')
self.assertEqual(tableset.key_type, number_type)
new_tableset = tableset.select(['number'])
self.assertEqual(new_tableset.key_name, 'foo')
self.assertEqual(new_tableset.key_type, number_type)
def test_aggregate_sum(self):
tableset = TableSet(self.tables.values(), self.tables.keys())
new_table = tableset.aggregate([
('count', Count()),
('number_sum', Sum('number'))
])
self.assertIsInstance(new_table, Table)
self.assertColumnNames(new_table, ('group', 'count', 'number_sum'))
self.assertColumnTypes(new_table, [Text, Number, Number])
self.assertRows(new_table, [
('table1', 3, 6),
('table2', 3, 7),
('table3', 3, 6)
])
def test_nested_aggregate_row_names(self):
tableset = TableSet(self.tables.values(), self.tables.keys(), key_name='test')
nested = tableset.group_by('letter')
results = nested.aggregate([
('count', Count()),
('number_sum', Sum('number'))
])
self.assertRowNames(results, [
('table1', 'a'),
('table1', 'b'),
('table2', 'b'),
('table2', 'a'),
('table2', 'c'),
('table3', 'a'),
('table3', 'c'),
def test_nested(self):
tableset = TableSet(self.tables.values(), self.tables.keys(), key_name='test')
nested = tableset.group_by('letter')
self.assertIsInstance(nested, TableSet)
self.assertEqual(len(nested), 3)
self.assertSequenceEqual(nested._column_names, ('letter', 'number'))
self.assertSequenceEqual(nested._column_types, (self.text_type, self.number_type))
self.assertIsInstance(nested['table1'], TableSet)
self.assertEqual(len(nested['table1']), 2)
self.assertSequenceEqual(nested['table1']._column_names, ('letter', 'number'))
self.assertSequenceEqual(nested['table1']._column_types, (self.text_type, self.number_type))
self.assertIsInstance(nested['table1']['a'], Table)
self.assertEqual(len(nested['table1']['a'].columns), 2)
self.assertEqual(len(nested['table1']['a'].rows), 2)
def test_having_complex(self):
tableset = TableSet(self.tables.values(), self.tables.keys(), key_name='test')
new_tableset = tableset.having([
('count', Count()),
('number_sum', Sum('number'))
], lambda t: t['count'] >= 3 and t['number_sum'] > 6)
self.assertIsInstance(new_tableset, TableSet)
self.assertSequenceEqual(new_tableset.keys(), ['table2'])
self.assertIs(new_tableset.values()[0], tableset['table2'])
self.assertEqual(new_tableset.key_name, 'test')
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']
def test_group_by(self):
table = Table(self.rows, self.column_names, self.column_types)
tableset = table.group_by('one')
self.assertIsInstance(tableset, TableSet)
self.assertEqual(len(tableset), 3)
self.assertEqual(tableset.key_name, 'one')
self.assertIsInstance(tableset.key_type, Text)
self.assertIn('a', tableset.keys())
self.assertIn('b', tableset.keys())
self.assertIn(None, tableset.keys())
self.assertSequenceEqual(tableset['a'].columns['one'], ('a', 'a'))
self.assertSequenceEqual(tableset['b'].columns['one'], ('b',))