def test_initialization(self): conn = sqlite3.connect(':memory:') adbi_conn = ADBI(conn) self.assertIsInstance(adbi_conn, ADBI, "Got correct object returned") self.assertEqual(adbi_conn.connection, conn, "Connection has been saved") self.assertEqual(adbi_conn.wrapped_db_param_style, sqlite3.paramstyle, "Got expected param style") # Try again, forcing the param style. adbi_conn = ADBI(conn, 'my_style') self.assertIsInstance(adbi_conn, ADBI, "Got correct object returned") self.assertEqual(adbi_conn.connection, conn, "Connection has been saved") self.assertEqual(adbi_conn.wrapped_db_param_style, 'my_style', "Got provided param style") # Once more for a connection that is a submodule. mock_conn = Mock() mock_conn.__class__.__module__ = 'sqlite3.testing.thing' adbi_conn = ADBI(conn) self.assertEqual(adbi_conn.connection, conn, "Connection has been saved") self.assertEqual(adbi_conn.wrapped_db_param_style, sqlite3.paramstyle, "Got expected param style")
def test_cursor(self): mock_db = Mock() adbi_conn = ADBI(mock_db, 'qmark') curs = adbi_conn.cursor() self.assertIsInstance(curs, ADBICursor, "Got expected ADBICursor object") self.assertEqual(curs._cursor, mock_db.cursor.return_value, "Cursor from original DB connection was used") mock_db.cursor.assert_called_with()
def test_schema_dir_property(self): mock_db = Mock() adbi_conn = ADBI(mock_db, 'qmark') self.assertIsNone(adbi_conn.schema_dir, "No schema directory given to start with") # Assign a value. adbi_conn.schema_dir = 'tests/sql' self.assertEqual(adbi_conn.schema_dir, Path('tests/sql'), "Got expected schema_dir set") # What about a path that doesn't exist. with self.assertRaises(ValueError): adbi_conn.schema_dir = '/foo/bar/' # Or we give it a file instead of a dir. with self.assertRaises(ValueError): adbi_conn.schema_dir = 'tests/test_ADBI.py'
def test_schema_file_format_propety(self): mock_db = Mock() adbi_conn = ADBI(mock_db, 'qmark') self.assertEqual(adbi_conn.schema_file_format, 'schema-{version}.sql', "Got default schema file format") # Adjust the file format. adbi_conn.schema_file_format = 'foo_{version}_sql' self.assertEqual(adbi_conn.schema_file_format, 'foo_{version}_sql', "Got updated file format") # Provide an invalid format. with self.assertRaises(ValueError): adbi_conn.schema_file_format = 'foo_{0}_sql' # Provide a static string with self.assertRaises(ValueError): adbi_conn.schema_file_format = 'foo_sql'
def test_rollback(self): mock_db = Mock(spec=[]) adbi_conn = ADBI(mock_db, 'qmark') # No rollback method exists. self.assertTrue(adbi_conn.rollback(), "rollback succeeds") # Rollback method exists. mock_db = Mock(spec=['rollback']) mock_db.rollback = Mock() adbi_conn = ADBI(mock_db, 'qmark') self.assertTrue(adbi_conn.rollback(), "rollback succeeds") mock_db.rollback.assert_called_with()
def test_commit(self): mock_db = Mock() adbi_conn = ADBI(mock_db, 'qmark') adbi_conn.commit() mock_db.commit.assert_called_with()
def test_close(self): mock_db = Mock() adbi_conn = ADBI(mock_db, 'qmark') adbi_conn.close() mock_db.close.assert_called_with()