feat: Updates to use swift-validations for database.
All checks were successful
CI / Linux Tests (push) Successful in 6m28s

This commit is contained in:
2026-02-01 00:55:44 -05:00
parent a3fb87f86e
commit 9276f88426
20 changed files with 667 additions and 361 deletions

View File

@@ -1,9 +1,10 @@
import DatabaseClient
import Dependencies
import Foundation
import ManualDCore
import Testing
@testable import DatabaseClient
@Suite
struct ComponentLossTests {
@@ -50,4 +51,18 @@ struct ComponentLossTests {
}
}
}
@Test(
arguments: [
ComponentLossModel(name: "", value: 0.2, projectID: UUID(0)),
ComponentLossModel(name: "Foo", value: -0.2, projectID: UUID(0)),
ComponentLossModel(name: "Foo", value: 1.2, projectID: UUID(0)),
ComponentLossModel(name: "", value: -0.2, projectID: UUID(0)),
]
)
func validations(model: ComponentLossModel) {
#expect(throws: (any Error).self) {
try model.validate()
}
}
}

View File

@@ -1,9 +1,10 @@
import DatabaseClient
import Dependencies
import Foundation
import ManualDCore
import Testing
@testable import DatabaseClient
@Suite
struct EquipmentTests {
@@ -51,4 +52,18 @@ struct EquipmentTests {
}
}
@Test(
arguments: [
EquipmentModel(staticPressure: -1, heatingCFM: 1000, coolingCFM: 1000, projectID: UUID(0)),
EquipmentModel(staticPressure: 0.5, heatingCFM: -1, coolingCFM: 1000, projectID: UUID(0)),
EquipmentModel(staticPressure: 0.5, heatingCFM: 1000, coolingCFM: -1000, projectID: UUID(0)),
EquipmentModel(staticPressure: 1.1, heatingCFM: 1000, coolingCFM: -1000, projectID: UUID(0)),
]
)
func validations(model: EquipmentModel) {
#expect(throws: (any Error).self) {
try model.validate()
}
}
}

View File

@@ -1,9 +1,10 @@
import DatabaseClient
import Dependencies
import Foundation
import ManualDCore
import Testing
@testable import DatabaseClient
@Suite
struct EquivalentLengthTests {
@@ -76,4 +77,47 @@ struct EquivalentLengthTests {
}
}
}
@Test(
arguments: [
EquivalentLength.Create(
projectID: UUID(0), name: "", type: .return, straightLengths: [], groups: []
),
EquivalentLength.Create(
projectID: UUID(0), name: "Testy", type: .return, straightLengths: [-1, 1], groups: []
),
EquivalentLength.Create(
projectID: UUID(0), name: "Testy", type: .return, straightLengths: [1, -1], groups: []
),
EquivalentLength.Create(
projectID: UUID(0), name: "Testy", type: .return, straightLengths: [1, 1],
groups: [
.init(group: -1, letter: "a", value: 1.0, quantity: 1)
]
),
EquivalentLength.Create(
projectID: UUID(0), name: "Testy", type: .return, straightLengths: [1, 1],
groups: [
.init(group: 1, letter: "1", value: 1.0, quantity: 1)
]
),
EquivalentLength.Create(
projectID: UUID(0), name: "Testy", type: .return, straightLengths: [1, 1],
groups: [
.init(group: 1, letter: "a", value: -1.0, quantity: 1)
]
),
EquivalentLength.Create(
projectID: UUID(0), name: "Testy", type: .return, straightLengths: [1, 1],
groups: [
.init(group: 1, letter: "a", value: 1.0, quantity: -1)
]
),
]
)
func validations(model: EquivalentLength.Create) {
#expect(throws: (any Error).self) {
try model.toModel().validate()
}
}
}

View File

@@ -1,5 +1,4 @@
import Dependencies
import DependenciesTestSupport
import Fluent
import FluentSQLiteDriver
import ManualDCore
@@ -151,4 +150,55 @@ struct ProjectTests {
}
}
@Test(
arguments: [
ProjectModel(
name: "", streetAddress: "1234 Sesame St", city: "Nowhere", state: "OH", zipCode: "55555",
sensibleHeatRatio: nil, userID: UUID(0)
),
ProjectModel(
name: "Testy", streetAddress: "", city: "Nowhere", state: "OH", zipCode: "55555",
sensibleHeatRatio: nil, userID: UUID(0)
),
ProjectModel(
name: "Testy", streetAddress: "1234 Sesame St", city: "", state: "OH", zipCode: "55555",
sensibleHeatRatio: nil, userID: UUID(0)
),
ProjectModel(
name: "Testy", streetAddress: "1234 Sesame St", city: "Nowhere", state: "",
zipCode: "55555",
sensibleHeatRatio: nil, userID: UUID(0)
),
ProjectModel(
name: "Testy", streetAddress: "1234 Sesame St", city: "Nowhere", state: "OH",
zipCode: "",
sensibleHeatRatio: nil, userID: UUID(0)
),
ProjectModel(
name: "Testy", streetAddress: "1234 Sesame St", city: "Nowhere", state: "OH",
zipCode: "55555",
sensibleHeatRatio: -1, userID: UUID(0)
),
ProjectModel(
name: "Testy", streetAddress: "1234 Sesame St", city: "Nowhere", state: "OH",
zipCode: "55555",
sensibleHeatRatio: 1.1, userID: UUID(0)
),
]
)
func validations(model: ProjectModel) {
var errors = [String]()
#expect(throws: (any Error).self) {
do {
try model.validate()
} catch {
// Just checking to make sure I'm not testing the same error over and over /
// making sure I've reset to good values / only testing one property at a time.
#expect(!errors.contains("\(error)"))
errors.append("\(error)")
throw error
}
}
}
}

View File

@@ -1,8 +1,10 @@
import DatabaseClient
import Dependencies
import Foundation
import ManualDCore
import Testing
import Validations
@testable import DatabaseClient
@Suite
struct RoomTests {
@@ -63,4 +65,124 @@ struct RoomTests {
}
}
}
@Test(
arguments: [
Room.Create(
projectID: UUID(0),
name: "",
heatingLoad: 12345,
coolingTotal: 12344,
coolingSensible: nil,
registerCount: 1
),
Room.Create(
projectID: UUID(0),
name: "Test",
heatingLoad: -12345,
coolingTotal: 12344,
coolingSensible: nil,
registerCount: 1
),
Room.Create(
projectID: UUID(0),
name: "Test",
heatingLoad: 12345,
coolingTotal: -12344,
coolingSensible: nil,
registerCount: 1
),
Room.Create(
projectID: UUID(0),
name: "Test",
heatingLoad: 12345,
coolingTotal: 12344,
coolingSensible: -123,
registerCount: 1
),
Room.Create(
projectID: UUID(0),
name: "Test",
heatingLoad: 12345,
coolingTotal: 12344,
coolingSensible: nil,
registerCount: -1
),
Room.Create(
projectID: UUID(0),
name: "",
heatingLoad: -12345,
coolingTotal: -12344,
coolingSensible: -1,
registerCount: -1
),
]
)
func validations(room: Room.Create) throws {
#expect(throws: (any Error).self) {
// do {
try room.toModel().validate()
// } catch {
// print("\(error)")
// throw error
// }
}
}
// @Test(
// arguments: [
// Room.Update(
// name: "",
// heatingLoad: 12345,
// coolingTotal: 12344,
// coolingSensible: nil,
// registerCount: 1
// ),
// Room.Update(
// name: "Test",
// heatingLoad: -12345,
// coolingTotal: 12344,
// coolingSensible: nil,
// registerCount: 1
// ),
// Room.Update(
// name: "Test",
// heatingLoad: 12345,
// coolingTotal: -12344,
// coolingSensible: nil,
// registerCount: 1
// ),
// Room.Update(
// name: "Test",
// heatingLoad: 12345,
// coolingTotal: 12344,
// coolingSensible: -123,
// registerCount: 1
// ),
// Room.Update(
// name: "Test",
// heatingLoad: 12345,
// coolingTotal: 12344,
// coolingSensible: nil,
// registerCount: -1
// ),
// Room.Update(
// name: "",
// heatingLoad: -12345,
// coolingTotal: -12344,
// coolingSensible: -1,
// registerCount: -1
// ),
// ]
// )
// func updateValidations(room: Room.Update) throws {
// #expect(throws: (any Error).self) {
// // do {
// try room.validate()
// // } catch {
// // print("\(error)")
// // throw error
// // }
// }
// }
}

View File

@@ -1,9 +1,10 @@
import DatabaseClient
import Dependencies
import Foundation
import ManualDCore
import Testing
@testable import DatabaseClient
@Suite
struct TrunkSizeTests {
@@ -64,4 +65,29 @@ struct TrunkSizeTests {
}
}
}
@Test(
arguments: [
TrunkModel(projectID: UUID(0), type: .return, height: 8, name: ""),
TrunkModel(projectID: UUID(0), type: .return, height: -8, name: "Test"),
]
)
func validations(model: TrunkModel) {
#expect(throws: (any Error).self) {
try model.validate()
}
}
@Test(
arguments: [
TrunkRoomModel(trunkID: UUID(0), roomID: UUID(0), registers: [-1, 1], type: .return),
TrunkRoomModel(trunkID: UUID(0), roomID: UUID(0), registers: [1, -1], type: .return),
TrunkRoomModel(trunkID: UUID(0), roomID: UUID(0), registers: [], type: .return),
]
)
func trunkRoomModelValidations(model: TrunkRoomModel) {
#expect(throws: (any Error).self) {
try model.validate()
}
}
}

View File

@@ -1,4 +1,3 @@
import DatabaseClient
import Dependencies
import Foundation
import ManualDCore
@@ -41,26 +40,6 @@ struct UserDatabaseTests {
}
}
@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 {
@@ -148,4 +127,64 @@ struct UserDatabaseTests {
}
}
@Test(
arguments: [
UserProfileModel(
userID: UUID(0), firstName: "", lastName: "McTestface", companyName: "Acme Co.",
streetAddress: "1234 Sesame St", city: "Nowhere", state: "CA", zipCode: "55555"
),
UserProfileModel(
userID: UUID(0), firstName: "Testy", lastName: "", companyName: "Acme Co.",
streetAddress: "1234 Sesame St", city: "Nowhere", state: "CA", zipCode: "55555"
),
UserProfileModel(
userID: UUID(0), firstName: "Testy", lastName: "McTestface", companyName: "",
streetAddress: "1234 Sesame St", city: "Nowhere", state: "CA", zipCode: "55555"
),
UserProfileModel(
userID: UUID(0), firstName: "Testy", lastName: "McTestface", companyName: "Acme Co.",
streetAddress: "", city: "Nowhere", state: "CA", zipCode: "55555"
),
UserProfileModel(
userID: UUID(0), firstName: "Testy", lastName: "McTestface", companyName: "Acme Co.",
streetAddress: "1234 Sesame St", city: "", state: "CA", zipCode: "55555"
),
UserProfileModel(
userID: UUID(0), firstName: "Testy", lastName: "McTestface", companyName: "Acme Co.",
streetAddress: "1234 Sesame St", city: "Nowhere", state: "", zipCode: "55555"
),
UserProfileModel(
userID: UUID(0), firstName: "Testy", lastName: "McTestface", companyName: "Acme Co.",
streetAddress: "1234 Sesame St", city: "Nowhere", state: "CA", zipCode: ""
),
]
)
func profileValidations(model: UserProfileModel) {
var errors = [String]()
#expect(throws: (any Error).self) {
do {
try model.validate()
} catch {
// Just checking to make sure I'm not testing the same error over and over /
// making sure I've reset to good values / only testing one property at a time.
#expect(!errors.contains("\(error)"))
errors.append("\(error)")
throw error
}
}
}
@Test(
arguments: [
User.Create(email: "", password: "super-secret", confirmPassword: "super-secret"),
User.Create(email: "testy@example.com", password: "", confirmPassword: "super-secret"),
User.Create(email: "testy@example.com", password: "super-secret", confirmPassword: ""),
User.Create(email: "testy@example.com", password: "super", confirmPassword: "super"),
]
)
func userValidations(model: User.Create) {
#expect(throws: (any Error).self) {
try model.validate()
}
}
}