MCPcopy Create free account
hub / github.com/Effect-TS/effect / makeHarness

Function makeHarness

packages/effect/test/sql/SqlClient.test.ts:52–143  ·  view source on GitHub ↗
(options: {
  readonly spanAttributes?: ReadonlyArray<readonly [string, unknown]> | undefined
  readonly acquire?: Effect.Effect<void> | undefined
  readonly duringBegin?: Effect.Effect<void> | undefined
  readonly duringRollbackSavepoint?: Effect.Effect<void> | undefined
  readonly begin?: SqlError.SqlError | undefined
  readonly commit?: (() => Effect.Effect<void, SqlError.SqlError>) | undefined
  readonly onCommitFailure?: (() => Effect.Effect<void, SqlError.SqlError>) | undefined
  readonly savepoint?: SqlError.SqlError | undefined
  readonly rollbackSavepoint?: SqlError.SqlError | undefined
  readonly releaseSavepoint?: true | SqlError.SqlError | undefined
} = {})

Source from the content-addressed store, hash-verified

50 * `begin`, so a test can make either take time.
51 */
52const makeHarness = (options: {
53 readonly spanAttributes?: ReadonlyArray<readonly [string, unknown]> | undefined
54 readonly acquire?: Effect.Effect<void> | undefined
55 readonly duringBegin?: Effect.Effect<void> | undefined
56 readonly duringRollbackSavepoint?: Effect.Effect<void> | undefined
57 readonly begin?: SqlError.SqlError | undefined
58 readonly commit?: (() => Effect.Effect<void, SqlError.SqlError>) | undefined
59 readonly onCommitFailure?: (() => Effect.Effect<void, SqlError.SqlError>) | undefined
60 readonly savepoint?: SqlError.SqlError | undefined
61 readonly rollbackSavepoint?: SqlError.SqlError | undefined
62 readonly releaseSavepoint?: true | SqlError.SqlError | undefined
63} = {}) => {
64 const release = options.releaseSavepoint
65 const onCommitFailure = options.onCommitFailure
66 const calls: Array<string> = []
67 const conn: StubConnection = { id: "stub" }
68 const transactionService = Context.Service<
69 StubTransactionConnection,
70 readonly [conn: StubConnection, counter: number]
71 >(`test/SqlClient/TransactionConnection/${harnessIdCounter++}`)
72
73 let transactionActive = false
74 const savepoints: Array<number> = []
75
76 const record = (name: string) => Effect.sync(() => calls.push(name))
77
78 const withTransaction = SqlClient.makeWithTransaction({
79 transactionService,
80 spanAttributes: options.spanAttributes ?? [],
81 acquireConnection: () =>
82 Effect.as(
83 Effect.flatMap(
84 Effect.andThen(record("acquireConnection"), options.acquire ?? Effect.void),
85 () => Effect.addFinalizer(() => record("closeConnection"))
86 ),
87 conn
88 ),
89 begin: (_conn, statement) =>
90 Effect.flatMap(record(statement === undefined ? "begin" : `begin(${statement})`), () => {
91 if (options.begin !== undefined) {
92 return Effect.fail(options.begin)
93 }
94 transactionActive = true
95 return options.duringBegin ?? Effect.void
96 }),
97 savepoint: (_conn, id) =>
98 Effect.flatMap(record(`savepoint(${id})`), () => {
99 if (options.savepoint !== undefined) {
100 return Effect.fail(options.savepoint)
101 }
102 savepoints.push(id)
103 return Effect.void
104 }),
105 commit: () =>
106 Effect.flatMap(record("commit"), () =>
107 options.commit !== undefined
108 ? options.commit()
109 : transactionActive

Callers 3

SqlClient.test.tsFile · 0.85
Redis.test.tsFile · 0.85

Calls 8

sqlErrorFunction · 0.85
addFinalizerMethod · 0.80
recordFunction · 0.70
failMethod · 0.65
pushMethod · 0.65
commitMethod · 0.45
syncMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected