def test_wrong_number_of_values(self): self.cursor.execute('CREATE TEMPORARY TABLE test_table ("A", "B")') too_few = [('x',), ('y',)] with self.assertRaises(sqlite3.ProgrammingError): insert_records(self.cursor, 'test_table', ['A', 'B'], too_few) too_many = [('x', 1, 'foo'), ('y', 2, 'bar')] with self.assertRaises(sqlite3.ProgrammingError): insert_records(self.cursor, 'test_table', ['A', 'B'], too_many)
def test_no_records(self): cursor = self.cursor cursor.execute('CREATE TEMPORARY TABLE test_table ("A", "B")') records = iter([]) # <- Empty, no data. insert_records(cursor, 'test_table', ['A', 'B'], records) cursor.execute('SELECT * FROM test_table') results = cursor.fetchall() self.assertEqual(results, [])
def test_sqlite3_errors(self): """Sqlite errors should not be caught.""" # No such table. with self.assertRaises(sqlite3.OperationalError): records = [('x', 1), ('y', 2)] insert_records(self.cursor, 'missing_table', ['A', 'B'], records) # No column named X. with self.assertRaises(sqlite3.OperationalError): self.cursor.execute('CREATE TEMPORARY TABLE test_table ("A", "B")') records = [('a', 1), ('b', 2)] insert_records(self.cursor, 'test_table', ['X', 'B'], records)
def test_basic_insert(self): cursor = self.cursor cursor.execute('CREATE TEMPORARY TABLE test_table ("A", "B")') records = [ ('x', 1), ('y', 2), ] insert_records(cursor, 'test_table', ['A', 'B'], records) cursor.execute('SELECT * FROM test_table') results = cursor.fetchall() self.assertEqual(results, records)
def test_reordered_columns(self): cursor = self.cursor cursor.execute('CREATE TEMPORARY TABLE test_table ("A", "B")') records = [ (1, 'x'), (2, 'y'), ] columns = ['B', 'A'] # <- Column order doesn't match how table was created. insert_records(cursor, 'test_table', columns, records) cursor.execute('SELECT * FROM test_table') results = cursor.fetchall() expected = [ ('x', 1), ('y', 2), ] self.assertEqual(results, expected)