def test_mysql_connector_connect_args(mock_connect): dbse = db.DataBase(db_name="test_db", cnf_dir=CNF_PATH) dbse.connect() mock_connect.assert_called_with( user="******", password="******", database="my-database", host="0.0.0.0", charset="utf8", ssl_ca="/dir/to/ca.pem", ssl_cert="/dir/to/client-cert.pem", ssl_key="/dir/to/client-key.pem" )
def test_ctx_mgr_close_called(mock_connect): conn_mock = MagicMock() conn_mock.close = MagicMock() conn_mock.cursor = MagicMock() conn_mock.commit = MagicMock() mock_connect.return_value = conn_mock with db.DataBase(db_name="test_db", cnf_dir=CNF_PATH) as dbse: assert dbse.CONN is None dbse.execute("an sql string") assert dbse.CONN is conn_mock assert conn_mock.close.called
def test_ctx_mgr_commit_not_called_on_exception(mock_connect): conn_mock = MagicMock() conn_mock.close = MagicMock() cursor_mock = MagicMock() cursor_mock.execute = MagicMock() cursor_mock.side_effect = Exception conn_mock.cursor = cursor_mock conn_mock.commit = MagicMock() mock_connect.return_value = conn_mock try: with db.DataBase(db_name="test_db", cnf_dir=CNF_PATH) as dbse: assert dbse.CONN is None dbse.execute("an sql string") assert dbse.CONN is conn_mock except Exception as e: pass assert not conn_mock.commit.called
def test_close_called(mock_connect): dbse = db.DataBase(db_name="test_db", cnf_dir=CNF_PATH) dbse.connect() dbse.CONN.close = MagicMock() dbse.close() assert dbse.CONN.close.called
def test_mysql_connector_connect_called(mock_connect): dbse = db.DataBase(db_name="test_db", cnf_dir=CNF_PATH) dbse.connect() assert mock_connect.called
def test_CONN_is_None_before_query_in_context_manager(): with db.DataBase(db_name="test_db", cnf_dir=CNF_PATH) as dbse: assert dbse.CONN is None
def test_CONN_is_None_on_instantiation(): dbse = db.DataBase(db_name="test_db", cnf_dir=CNF_PATH) assert dbse.CONN is None
def test_instantiation(): dbse = db.DataBase(db_name="test_db", cnf_dir=CNF_PATH) assert type(dbse) == db.DataBase