def test_three_args_mappings(self): records = [ OrderedDict([('A', 'x'), ('B', 1)]), OrderedDict([('B', 2), ('A', 'y')]), # <- Different key order. ] load_data(self.cursor, 'testtable', records) # <- Three args. self.cursor.execute('SELECT A, B FROM testtable') self.assertEqual(self.cursor.fetchall(), [('x', 1), ('y', 2)])
def test_three_args_namedtuples(self): ntup = namedtuple('ntup', ['A', 'B']) records = [ ntup('x', 1), ntup('y', 2), ] load_data(self.cursor, 'testtable', records) # <- Three args. self.cursor.execute('SELECT A, B FROM testtable') self.assertEqual(self.cursor.fetchall(), [('x', 1), ('y', 2)])
def test_three_args(self): records = [ ['A', 'B'], # <- Used as header row. ('x', 1), ('y', 2), ] load_data(self.cursor, 'testtable', records) # <- Three args. self.cursor.execute('SELECT A, B FROM testtable') self.assertEqual(self.cursor.fetchall(), [('x', 1), ('y', 2)])
def test_four_args(self): columns = ['A', 'B'] records = [ ('x', 1), ('y', 2), ] load_data(self.cursor, 'testtable', columns, records) # <- Four args. self.cursor.execute('SELECT A, B FROM testtable') self.assertEqual(self.cursor.fetchall(), [('x', 1), ('y', 2)])
def test_empty_records(self): records = [] load_data(self.cursor, 'testtable1', ['A', 'B'], records) # <- Using four args. self.assertTrue(table_exists(self.cursor, 'testtable1'), 'should create table') self.cursor.execute('SELECT A, B FROM testtable1') self.assertEqual(self.cursor.fetchall(), [], 'should have zero records') load_data(self.cursor, 'testtable2', records) # <- Using three args. self.assertFalse(table_exists(self.cursor, 'testtable2'), 'should not create table')
def test_bad_columns_object(self): records = [('x', 1), ('y', 2)] columns = 'bad columns object' # <- Expects iterable of names, not this str. with self.assertRaises(TypeError): load_data(self.cursor, 'testtable', columns, records)
def test_column_default(self): load_data(self.cursor, 'testtable1', ['A', 'B'], [('x', 1)]) load_data(self.cursor, 'testtable1', ['A'], [('y',)]) load_data(self.cursor, 'testtable1', ['B'], [(3,)]) self.cursor.execute('SELECT A, B FROM testtable1') self.assertEqual(self.cursor.fetchall(), [('x', 1), ('y', ''), ('', 3)]) load_data(self.cursor, 'testtable2', ['A', 'B'], [('x', 1)], default=None) load_data(self.cursor, 'testtable2', ['A'], [('y',)]) load_data(self.cursor, 'testtable2', ['B'], [(3,)]) self.cursor.execute('SELECT A, B FROM testtable2') self.assertEqual(self.cursor.fetchall(), [('x', 1), ('y', None), (None, 3)])