feat: Begins live database client tests.
All checks were successful
CI / Linux Tests (push) Successful in 5m35s

This commit is contained in:
2026-01-30 12:02:11 -05:00
parent 4f3cc2c7ea
commit c32ffcff8c
17 changed files with 332 additions and 178 deletions

View File

@@ -0,0 +1,39 @@
import App
import DatabaseClient
import Dependencies
import Fluent
import FluentSQLiteDriver
import Foundation
import NIO
import Vapor
// Helper to create an in-memory database for testing.
func withDatabase(
setupDependencies: (inout DependencyValues) -> Void = { _ in },
operation: () async throws -> Void
) async throws {
let app = try await Application.make(.testing)
do {
try await configure(app)
let database = app.db
try await app.autoMigrate()
try await withDependencies {
$0.uuid = .incrementing
$0.date = .init { Date() }
$0.database = .live(database: database)
setupDependencies(&$0)
} operation: {
try await operation()
}
try await app.autoRevert()
try await app.asyncShutdown()
} catch {
try? await app.autoRevert()
try await app.asyncShutdown()
throw error
}
}

View File

@@ -0,0 +1,29 @@
import Dependencies
import DependenciesTestSupport
import Fluent
import FluentSQLiteDriver
import ManualDCore
import Testing
import Vapor
@testable import DatabaseClient
@Suite
struct ProjectTests {
@Test
func sanity() {
#expect(Bool(true))
}
// @Test
// func createProject() {
// try await withDatabase(migrations: Project.Migrate()) {
// $0.database.projects = .live(database: $1)
// } operation: {
// @Dependency(\.database.projects) var projects
//
// let project = try await projects.c
// }
// }
}

View File

@@ -0,0 +1,76 @@
import DatabaseClient
import Dependencies
import Foundation
import ManualDCore
import Testing
@testable import DatabaseClient
@Suite
struct UserDatabaseTests {
@Test
func createUser() async throws {
try await withDatabase {
@Dependency(\.database.users) var users
let user = try await users.create(
.init(email: "testy@example.com", password: "super-secret", confirmPassword: "super-secret")
)
#expect(user.email == "testy@example.com")
// Test login the user in
let token = try await users.login(
.init(email: "testy@example.com", password: "super-secret")
)
// Test logging out
try await users.logout(token.id)
try await users.delete(user.id)
let shouldBeNilUser = try await users.get(user.id)
#expect(shouldBeNilUser == nil)
}
}
@Test
func createUserFails() async throws {
try await withDatabase {
@Dependency(\.database.users) var users
await #expect(throws: ValidationError.self) {
try await users.create(.init(email: "", password: "", confirmPassword: ""))
}
await #expect(throws: ValidationError.self) {
try await users.create(.init(email: "testy@example.com", password: "", confirmPassword: ""))
}
await #expect(throws: ValidationError.self) {
try await users.create(
.init(email: "testy@example.com", password: "super-secret", confirmPassword: ""))
}
}
}
@Test
func deleteFailsWithInvalidUserID() async throws {
try await withDatabase {
@Dependency(\.database.users) var users
await #expect(throws: NotFoundError.self) {
try await users.delete(UUID(0))
}
}
}
@Test
func logoutIgnoresUnfoundTokenID() async throws {
try await withDatabase {
@Dependency(\.database.users) var users
try await users.logout(UUID(0))
}
}
}