Skip to content

How to use this library in a testable way? #19

@dedale

Description

@dedale

Hello,

I want to extract the connection part from the code to make it testable. I have introduced a class like the following:

module Queries =
    type Schema (connectionF: unit -> Connection) =

        member __.CreateTables () = querySingleAsync<int> connectionF {
            script """
                DROP TABLE IF EXISTS Bird;
                CREATE TABLE Bird (
                    Id INTEGER NOT NULL PRIMARY KEY,
                    Name VARCHAR(255) NOT NULL,
                    Alias VARCHAR(255) NULL
                );
            """
    }

Unfortunately, when I try to create the schema, I get the following exception:

System.InvalidOperationException: Sequence contains no elements
at System.Linq.ThrowHelper.ThrowNoElementsException()
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at Dapper.SqlMapper.ThrowZeroRows(Row row) in C:\projects\dapper\Dapper\SqlMapper.cs:line 1160
at Dapper.SqlMapper.QueryRowAsync[T](IDbConnection cnn, Row row, Type effectiveType, CommandDefinition command)
in C:\projects\dapper\Dapper\SqlMapper.Async.cs:line 485

If I add the following to Queries module, it works as expected:

let private mkOnDiskConnectionString (dataSource: string) = sprintf "Data Source = %s;" dataSource
let mkOnDisk () = new SqliteConnection (mkOnDiskConnectionString "./example.db")
let private connectionF2 () = Connection.SqliteConnection (mkOnDisk())
let querySeqAsync2<'R> = querySeqAsync<'R> (connectionF2)
let querySingleAsync2<'R> = querySingleOptionAsync<'R> (connectionF2)
member __.CreateTables () = querySingleAsync2<int> {
    script """ ... """
}

What should I do to extract the connection part ? I cannot move QueryBuilders to Schema type because of generic parameters...

Thanks for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions