From e8003e1e70ea2a0e388d13e736e3e64c7dfdd1e9 Mon Sep 17 00:00:00 2001 From: Octogonapus Date: Wed, 7 Feb 2024 17:45:56 -0500 Subject: [PATCH] add test --- test/runtests.jl | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 583be13..9339aa6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -350,3 +350,39 @@ ret = columntable(res) # load on closed connection should throw @test_throws ArgumentError MySQL.load(ct, conn, "test194") + +@testset "transactions" begin + DBInterface.execute(conn, "DROP TABLE IF EXISTS TransactionTest") + DBInterface.execute(conn, "CREATE TABLE TransactionTest (a int)") + + conn2 = DBInterface.connect(MySQL.Connection, "", ""; option_file=joinpath(dirname(pathof(MySQL)), "../test/", "my.ini")) + + try + # happy path + DBInterface.transaction(conn) do + DBInterface.execute(conn, "INSERT INTO TransactionTest (a) VALUES (1)") + + # we can see the result inside our transaction + result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable + @test result.a == [1] + + # and can't see it outside our transaction + result = DBInterface.execute(conn2, "SELECT * FROM TransactionTest") |> Tables.columntable + @test isempty(result.a) + end + result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable + @test result.a == [1] + result = DBInterface.execute(conn2, "SELECT * FROM TransactionTest") |> Tables.columntable + @test result.a == [1] + + # roll back due to exception + @test_throws ErrorException DBInterface.transaction(conn) do + DBInterface.execute(conn, "INSERT INTO TransactionTest (a) VALUES (2)") + error("force rollback") + end + result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable + @test result.a == [1] # the table did not change + finally + close(conn2) + end +end