def test_execute_single_operations_no_params(mocker: MockerFixture, monkeypatch): conn = mocker.patch("pymssqlutils.methods._get_connection", autospec=True) cursor = ( conn.return_value.__enter__.return_value.cursor.return_value.__enter__.return_value ) result = sql.execute("test query") assert cursor.execute.call_args_list == [(("test query",),)] assert isinstance(result, DatabaseResult) with pytest.raises(ValueError): result.data assert result.ok assert result.commit
def test_execute_single_operations_single_params(mocker: MockerFixture, monkeypatch): monkeypatch.setenv("MSSQL_SERVER", "server") conn = mocker.patch("pymssqlutils.methods._get_connection", autospec=True) cursor = ( conn.return_value.__enter__.return_value.cursor.return_value.__enter__.return_value ) result = sql.execute("select %s val", 2) assert cursor.execute.call_args_list == [ (("select 2 val",),), ] assert isinstance(result, DatabaseResult) with pytest.raises(ValueError): result.data assert result.ok assert result.commit
def test_execute_multiple_operations_no_params_batched( mocker: MockerFixture, monkeypatch ): monkeypatch.setenv("MSSQL_SERVER", "server") conn = mocker.patch("pymssqlutils.methods._get_connection", autospec=True) cursor = ( conn.return_value.__enter__.return_value.cursor.return_value.__enter__.return_value ) result = sql.execute(["test query", "second query"] * 2, batch_size=2) assert cursor.execute.call_args_list == [ (("test query\n;second query",),), (("test query\n;second query",),), ] assert isinstance(result, DatabaseResult) with pytest.raises(ValueError): result.data assert result.ok assert result.commit
def test_datetime_offset_handling(): model = { "col1": "hello", "col2": 1.23, "col3": datetime(2020, 6, 1, 12, 30, tzinfo=timezone(timedelta(hours=-1))), "col4": True, } result = sql.execute( [ "CREATE TABLE #temp (col1 VARCHAR(100), col2 DECIMAL(6,2), col3 DATETIMEOFFSET, col4 TINYINT)", f"INSERT INTO #temp {model_to_values(model)}", "SELECT * FROM #temp", ], fetch=True, ) assert result.data[0]["col1"] == "hello" assert result.data[0]["col2"] == 1.23 assert result.data[0]["col3"] == datetime( 2020, 6, 1, 12, 30, tzinfo=timezone(timedelta(hours=-1)) ) assert result.data[0]["col4"] == 1
def test_execute_many_operations(): result = sql.execute([f"SELECT {val} val" for val in range(100)], fetch=True) assert result.data[0]["val"] == 99
def test_execute_batched(): sql.execute( "SELECT %s a, %s b, %s c", [(val, val + 1, val + 2) for val in range(1000)], batch_size=500, )
def test_execute_many(): sql.execute("SELECT %s val", [(1,), (2,), (3,)])
def test_execute_with_fetch(): result = sql.execute("SELECT 'hello' col", fetch=True) assert result.data[0]["col"] == "hello"
def test_execute(): sql.execute("CREATE TABLE #temp (val int)")