Effect CommunityEC
Effect Community3y ago
7 replies
attila

Unit testing the SQL client

I have a function like this:
import * as Schema from "@effect/schema/Schema";
import * as Sql from "@sqlfx/mysql";
import { Effect, pipe } from "effect";
import * as event from "../event.js";
import * as reducer from "../reducer.js";

export type Args = Schema.To<typeof event.Event>;

export const make = (args: Args) =>
  pipe(
    Sql.tag,
    Effect.bindTo("sql"),
    Effect.bind("event", () => Schema.encode(event.Event)(args)),
    Effect.tap(
      ({ event, sql }) =>
        sql`INSERT INTO my_events ${sql({
          id: event.id,
          aggregate_id: event.aggregateId,
          account_id: event.accountId,
          user_id: event.userId,
          version: event.version,
          created_at: event.createdAt,
          data: event.data,
          schema_version: event.schemaVersion,
          type: event.type,
        })}`
    ),
    Effect.map(() => reducer.apply(null)(args))
  );

and I'm trying to unit test it. I don't want to spin up a test database, I'd just like to update the Sql service to return whatever I want to simulate in my test. The only thing I need to override is the return value of:
sql`INSERT INTO my_events ...`

I've got to the point where I can use Effect.updateService but I'm not sure how to override this one case because the client is an interface of many different function declarations. Does anyone here have experience with unit testing the sql client?

The tests in https://github.com/tim-smart/sqlfx create a compiler and only check if the generated query and params are correct.
Was this page helpful?