Files
effect/packages/sql/mysql2/test/Model.test.ts
T

149 lines
5.8 KiB
TypeScript

import { assert, describe, it } from "@effect/vitest"
import { Effect } from "effect"
import { Schema } from "effect/schema"
import { Model } from "effect/unstable/schema"
import { SqlClient, SqlModel } from "effect/unstable/sql"
import { MysqlContainer } from "./utils.ts"
class User extends Model.Class<User>("User")({
id: Model.Generated(Schema.Int),
name: Schema.String,
age: Schema.Int
}) {}
describe("SqlModel", () => {
it.effect("insert returns result", () =>
Effect.gen(function*() {
const repo = yield* SqlModel.makeRepository(User, {
tableName: "users",
idColumn: "id",
spanPrefix: "UserRepository"
})
const sql = yield* SqlClient.SqlClient
yield* sql`CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)`
const result = yield* repo.insert(User.insert.makeUnsafe({ name: "Alice", age: 30 }))
assert.deepStrictEqual(result, new User({ id: 1, name: "Alice", age: 30 }))
}).pipe(
Effect.provide(MysqlContainer.layerClient),
Effect.catchTag("ContainerError", () => Effect.void)
), { timeout: 60_000 })
it.effect("insert returns result with transforms", () =>
Effect.gen(function*() {
const repo = yield* SqlModel.makeRepository(User, {
tableName: "users",
idColumn: "id",
spanPrefix: "UserRepository"
})
const sql = yield* SqlClient.SqlClient
yield* sql`CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)`
const result = yield* repo.insert(User.insert.makeUnsafe({ name: "Alice", age: 30 }))
assert.deepStrictEqual(result, new User({ id: 1, name: "Alice", age: 30 }))
}).pipe(
Effect.provide(MysqlContainer.layerClientWithTransforms),
Effect.catchTag("ContainerError", () => Effect.void)
), { timeout: 60_000 })
it.effect("insertVoid", () =>
Effect.gen(function*() {
const repo = yield* SqlModel.makeRepository(User, {
tableName: "users",
idColumn: "id",
spanPrefix: "UserRepository"
})
const sql = yield* SqlClient.SqlClient
yield* sql`CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)`
const result = yield* repo.insertVoid(User.insert.makeUnsafe({ name: "Alice", age: 30 }))
assert.strictEqual(result, void 0)
}).pipe(
Effect.provide(MysqlContainer.layerClient),
Effect.catchTag("ContainerError", () => Effect.void)
), { timeout: 60_000 })
it.live("insert data loader returns result", () =>
Effect.gen(function*() {
const repo = yield* SqlModel.makeDataLoaders(User, {
tableName: "users",
idColumn: "id",
spanPrefix: "UserRepository",
window: 10
})
const sql = yield* SqlClient.SqlClient
yield* sql`CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)`
const [alice, john] = yield* Effect.all([
repo.insert(User.insert.makeUnsafe({ name: "Alice", age: 30 })),
repo.insert(User.insert.makeUnsafe({ name: "John", age: 30 }))
], { concurrency: "unbounded" })
assert.deepStrictEqual(alice.name, "Alice")
assert.deepStrictEqual(john.name, "John")
}).pipe(
Effect.provide(MysqlContainer.layerClient),
Effect.catchTag("ContainerError", () => Effect.void)
), { timeout: 60_000 })
it.live("findById data loader", () =>
Effect.gen(function*() {
const repo = yield* SqlModel.makeDataLoaders(User, {
tableName: "users",
idColumn: "id",
spanPrefix: "UserRepository",
window: 10
})
const sql = yield* SqlClient.SqlClient
yield* sql`CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)`
const alice = yield* repo.insert(User.insert.makeUnsafe({ name: "Alice", age: 30 }))
const john = yield* repo.insert(User.insert.makeUnsafe({ name: "John", age: 30 }))
const [alice2, john2] = yield* Effect.all([
repo.findById(alice.id),
repo.findById(john.id)
], { concurrency: "unbounded" })
assert.deepStrictEqual(alice2.name, "Alice")
assert.deepStrictEqual(john2.name, "John")
}).pipe(
Effect.provide(MysqlContainer.layerClient),
Effect.catchTag("ContainerError", () => Effect.void)
), { timeout: 60_000 })
it.effect("update returns result", () =>
Effect.gen(function*() {
const repo = yield* SqlModel.makeRepository(User, {
tableName: "users",
idColumn: "id",
spanPrefix: "UserRepository"
})
const sql = yield* SqlClient.SqlClient
yield* sql`CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)`
let result = yield* repo.insert(User.insert.makeUnsafe({ name: "Alice", age: 30 }))
result = yield* repo.update(User.update.makeUnsafe({ ...result, name: "Bob" }))
assert.deepStrictEqual(result, new User({ id: 1, name: "Bob", age: 30 }))
}).pipe(
Effect.provide(MysqlContainer.layerClient),
Effect.catchTag("ContainerError", () => Effect.void)
), { timeout: 60_000 })
it.effect("update returns result with transforms", () =>
Effect.gen(function*() {
const repo = yield* SqlModel.makeRepository(User, {
tableName: "users",
idColumn: "id",
spanPrefix: "UserRepository"
})
const sql = yield* SqlClient.SqlClient
yield* sql`CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)`
let result = yield* repo.insert(User.insert.makeUnsafe({ name: "Alice", age: 30 }))
result = yield* repo.update(User.update.makeUnsafe({ ...result, name: "Bob" }))
assert.deepStrictEqual(result, new User({ id: 1, name: "Bob", age: 30 }))
}).pipe(
Effect.provide(MysqlContainer.layerClientWithTransforms),
Effect.catchTag("ContainerError", () => Effect.void)
), { timeout: 60_000 })
})