Hello,
I would like to test a function doing a call to a database, but I want to mock my connection. I want to test that the returned type of my function is correct without actually calling the database.
I’m thinking about something like this:
using Test
using Pretend
using LibPQ
using DataFrames
function function_to_test(conn::LibPQ.Connection)
sql = "select * from my_table;"
result = execute(conn, sql)
result_df = DataFrame(result)
return result_df
end
Pretend.activate()
@testset verbose = true "Test my function" begin
@testset "Mock LibPQ.execute" begin
conn = nothing
@mockable LibPQ.execute(conn::LibPQ.Connection, query::String)
fake_execute(::Nothing, ::String) = [Dict("some_string" => "string", "some_int" => 234)]
apply(execute => fake_execute) do
@test typeof(function_to_test(conn)) == DataFrame
end
end
end
My problem his that LibPQ.execute is expecting as first parameter a LipBQ.Connection. How can I mock this connection? Or pass nothing?