def test_load_data(self): select = Selector() # <- Empty selector. self.assertEqual(select.fieldnames, []) readerlike1 = [['col1', 'col2'], ['a', 1], ['b', 2]] select.load_data(readerlike1) self.assertEqual(select.fieldnames, ['col1', 'col2']) readerlike2 = [['col1', 'col3'], ['c', 'x'], ['d', 'y']] select.load_data(readerlike2) self.assertEqual(select.fieldnames, ['col1', 'col2', 'col3'])
def test_repr(self): data = [['A', 'B'], ['x', 100], ['y', 200]] # Empty selector. select = Selector() regex = ('^<datatest.Selector object at [^\n>]+>\n' 'Empty - contains no data\.$') self.assertRegex(repr(select), regex) # Data-only (no args) source = Selector(data) regex = ("^<datatest.Selector object at [^\n>]+>\n" "Data from 1 source:\n" " {0}$".format(re.escape(repr(data)))) self.assertRegex(repr(source), regex) # Data with args and kwds. iterable = iter(data) source = Selector(iterable, 'foo', bar='baz') # Args don't change repr. regex = ('<datatest.Selector object at [^\n>]+>\n' 'Data from 1 source:\n' ' <[a-z_]+ object at [^\n>]+>') self.assertRegex(repr(source), regex) # Extended after instantiation. data1 = [['A', 'B'], ['x', 100]] data2 = [['A', 'B'], ['y', 200]] data3 = [['A', 'B'], ['z', 300]] source = Selector(data1) source.load_data(data2) source.load_data(data3) actual_repr = repr(source) self.assertTrue(actual_repr.startswith('<datatest.Selector object at')) self.assertTrue( actual_repr.endswith(">\n" "Data from 3 sources:\n" " [['A', 'B'], ['x', 100]]\n" " [['A', 'B'], ['y', 200]]\n" " [['A', 'B'], ['z', 300]]"))
def test_repr(self): data = [['A', 'B'], ['x', 100], ['y', 200]] # Empty selector. select = Selector() self.assertEqual(repr(select), '<Selector (no data loaded)>') # Data-only (no args) select = Selector(data) expected = "<Selector [['A', 'B'], ['x', 100], ['y', 200]]>" self.assertEqual(repr(select), expected) # Data with args (args don't affect repr) iterable = iter(data) select = Selector(iterable, 'foo', bar='baz') regex = '<Selector <[a-z_]+ object at [^\n>]+>>' self.assertRegex(repr(select), regex) # Extended after instantiation. select = Selector() select.load_data([['A', 'B'], ['z', 300]]) select.load_data([['A', 'B'], ['y', 200]]) select.load_data([['A', 'B'], ['x', 100]]) expected = ("<Selector (3 sources):\n" " [['A', 'B'], ['x', 100]]\n" " [['A', 'B'], ['y', 200]]\n" " [['A', 'B'], ['z', 300]]>") self.assertEqual(repr(select), expected) # Test long repr truncation. select = Selector([ ['xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'], ['yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'], ]) self.assertEqual(len(repr(select)), 72) expected = "<Selector [['xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'], ['yyyyyyyyyyy...yyyyy']]>" self.assertEqual(repr(select), expected)