feat: Working on mocks and mock storage.

This commit is contained in:
2025-01-16 16:29:46 -05:00
parent 94b2b1e50c
commit b6e7fe915f
7 changed files with 397 additions and 17 deletions

View File

@@ -1,6 +1,7 @@
@testable import DatabaseClientLive
import Dependencies
import FluentSQLiteDriver
import Foundation
import Logging
import NIO
import SharedModels
@@ -48,10 +49,12 @@ struct DatabaseClientTests {
}
}
@Test
func employees() async throws {
@Test(arguments: EmployeeTestFactory.testCases)
func employees(factory: EmployeeTestFactory) async throws {
try await withDatabase(migrations: Employee.Migrate()) {
$0.database.employees = .live(database: $1)
$0.uuid = .incrementing
$0.date = .init { Date() }
$0.database.employees = factory.handler($1)
} operation: {
@Dependency(\.database.employees) var employees
@@ -90,11 +93,11 @@ struct DatabaseClientTests {
}
}
@Test
func vendors() async throws {
@Test(arguments: VendorTestFactory.testCases)
func vendors(factory: VendorTestFactory) async throws {
try await withDatabase(migrations: Vendor.Migrate(), VendorBranch.Migrate()) {
$0.database.vendors = .live(database: $1)
$0.database.vendorBranches = .live(database: $1)
$0.database.vendorBranches = factory.handler($1).0
$0.database.vendors = factory.handler($1).1
} operation: {
@Dependency(\.database.vendorBranches) var branches
@Dependency(\.database.vendors) var vendors
@@ -119,7 +122,7 @@ struct DatabaseClientTests {
let fetchedWithBranches = try await vendors.get(vendor.id, .withBranches)
#expect(fetchedWithBranches!.branches!.first == branch)
let updated = try await vendors.update(vendor.id, .init(name: "Johnstone"))
let updated = try await vendors.update(vendor.id, with: .init(name: "Johnstone"))
#expect(updated.name == "Johnstone")
try await vendors.delete(vendor.id)
@@ -129,11 +132,11 @@ struct DatabaseClientTests {
}
}
@Test
func vendorBranches() async throws {
@Test(arguments: VendorTestFactory.testCases)
func vendorBranches(factory: VendorTestFactory) async throws {
try await withDatabase(migrations: Vendor.Migrate(), VendorBranch.Migrate()) {
$0.database.vendors = .live(database: $1)
$0.database.vendorBranches = .live(database: $1)
$0.database.vendorBranches = factory.handler($1).0
$0.database.vendors = factory.handler($1).1
} operation: {
@Dependency(\.database.vendorBranches) var branches
@Dependency(\.database.vendors) var vendors
@@ -180,6 +183,8 @@ struct DatabaseClientTests {
}
try await withDependencies {
$0.uuid = .incrementing
$0.date = .init { Date() }
setupDependencies(&$0, database)
} operation: {
try await operation()
@@ -192,3 +197,22 @@ struct DatabaseClientTests {
await dbs.shutdownAsync()
}
}
struct EmployeeTestFactory {
let handler: (any Database) -> DatabaseClient.Employees
static var testCases: [Self] { [
.init(handler: { .live(database: $0) }),
.init(handler: { _ in .mock([]) })
] }
}
struct VendorTestFactory {
let handler: (any Database) -> (DatabaseClient.VendorBranches, DatabaseClient.Vendors)
static var testCases: [Self] { [
.init(handler: { (.live(database: $0), .live(database: $0)) }),
.init(handler: { _ in (.mock([]), .mock([])) })
] }
}