lpil / pgo

🐘 Use PostgreSQL databases with PGO
https://hexdocs.pm/gleam_pgo/
Apache License 2.0
123 stars 12 forks source link

Mock Connection #30

Closed sporto closed 2 months ago

sporto commented 2 months ago

I would like to put the connection in my application context e.g.

pub type Context {
  Context(config: Config, static_directory: String, db_pool: pgo.Connection)
}

For unit testing, I want to create a fixture for the context. So I can test route handlers that expect a context. e.g.

pub fn fixture_context() -> Context {
  Context(static_directory: "", db_pool: pgo.mock_connection, config: config.fixture())
}

How could I do this? I wouldn't like to create a real DB connection for many unit tests. Just something that satisfies the Connection type.
Could this lib provide a mock connection?

lpil commented 2 months ago

This is a simple database connection library. If you wish for functionality unrelated to connecting to and querying a database you likely want to abstract over it in some fashion.

One thing you could do if have a zero arity function there that returns a connection, and have it panic if called in those tests that do not need a database.

sporto commented 2 months ago

Yes, that was my idea, something that just panics if actually called. Sorry, didn't mean a fully mocked connection, that will return things while called in test. That was bad naming.

Something like this I guess

@external(erlang, "rand", "uniform")
pub fn mock_connection() -> pgo.Connection

My question, was more if the library should provide a function like this. It wasn't obvious to me how to get a Connection, without actually calling connect.

lpil commented 2 months ago

It should not, no. This library is an API client and unrelated to mocking or testing.