Пример #1
0
    def test_basic_creation(self):
        self.assertEqual(self.count_tables(), 0, msg='starting with zero tables')

        create_table(self.cursor, 'test_table1', ['A', 'B'])  # <- Create table!
        self.assertEqual(self.count_tables(), 1, msg='one table')

        create_table(self.cursor, 'test_table2', ['A', 'B'])  # <- Create table!
        self.assertEqual(self.count_tables(), 2, msg='two tables')
Пример #2
0
    def test_sqlite3_errors(self):
        """Sqlite errors should not be caught."""
        # Table already exists.
        create_table(self.cursor, 'test_table1', ['A', 'B'])
        with self.assertRaises(sqlite3.OperationalError):
            create_table(self.cursor, 'test_table1', ['A', 'B'])

        # Duplicate column name.
        with self.assertRaises(sqlite3.OperationalError):
            create_table(self.cursor, 'test_table2', ['A', 'B', 'A'])

        # Duplicate column name (after normalization).
        with self.assertRaises(sqlite3.OperationalError):
            create_table(self.cursor, 'test_table3', ['A', 'B', '  A  '])

        # Duplicate empty/all-whitespace string columns (uses modified message).
        with self.assertRaises(sqlite3.OperationalError) as cm:
            create_table(self.cursor, 'test_table4', ['', 'B', '    '])
Пример #3
0
    def test_default_value(self):
        # When unspecified, default is empty string.
        create_table(self.cursor, 'test_table1', ['A', 'B'])
        self.cursor.execute("INSERT INTO test_table1 (A) VALUES ('foo')")
        self.cursor.execute("INSERT INTO test_table1 (B) VALUES ('bar')")

        self.cursor.execute('SELECT * FROM test_table1')
        expected = [
            ('foo', ''),  # <- Default in column B
            ('', 'bar'),  # <- Default in column A
        ]
        self.assertEqual(self.cursor.fetchall(), expected)

        # Setting default to None.
        create_table(self.cursor, 'test_table2', ['A', 'B'], default=None)
        self.cursor.execute("INSERT INTO test_table2 (A) VALUES ('foo')")
        self.cursor.execute("INSERT INTO test_table2 (B) VALUES ('bar')")

        self.cursor.execute('SELECT * FROM test_table2')
        expected = [
            ('foo', None),  # <- Default in column B
            (None, 'bar'),  # <- Default in column A
        ]
        self.assertEqual(self.cursor.fetchall(), expected)