# Unittest for postgresql database

**URL:** <https://discourse.julialang.org/t/unittest-for-postgresql-database/102720>\
**Category:** General Usage\
**Created:** [August 11, 2023, 3:48pm UTC](https://discourse.julialang.org/t/unittest-for-postgresql-database/102720 "2023-08-11T15:48:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cbe](https://avatars.discourse-cdn.com/v4/letter/c/8e8cbc/32.png) [@cbe](https://discourse.julialang.org/u/cbe)\
**Post date:** [August 11, 2023, 3:48pm UTC](https://discourse.julialang.org/t/unittest-for-postgresql-database/102720/1 "2023-08-11T15:48:17Z")

</div>

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:

```julia
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?
