typelevel / skunk

A data access library for Scala + Postgres.
https://typelevel.org/skunk/
MIT License
1.58k stars 160 forks source link

[Request] Add a `check` utility for test purposes #271

Open Daenyth opened 3 years ago

Daenyth commented 3 years ago

Basically similar to doobie's approach. Take a Query as an input, validate against a server connection that the query as written matches the types.

It shouldn't require inserting any rows, only that the tables involved exist.

Full-fledged integration tests for certain queries are often hard to write. They might involve many foreign key relations, and thus a lot of distracting and fiddly test setup, or they might be highly-parameterized queries (in terms of the sql structure - imagine a "list from table, maybe sort on a user-selected property, sometimes filter some other property by a user input, etc").

tpolecat commented 3 years ago

We could add something to Session like

def check(query: Query[_, _]): F[Unit] = prepare(query).use(_ => Applicative[F].unit)
// ditto for Command

and then provide doc on how to make a Session available in various unit testing frameworks. But I don't really want to add modules like we did with doobie because it becomes a maintenance issue.

Daenyth commented 3 years ago

So prepare does the type validation, even prior to executing the query? That's not bad at all!

tpolecat commented 3 years ago

Yes, it does Parse which will catch syntax errors, and then Describe which will catch any type mismatches. See https://github.com/tpolecat/skunk/blob/master/modules/core/src/main/scala/net/protocol/Prepare.scala#L43-L44