This repository has been archived on 2026-02-12. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
swift-duct-calc/Tests/DatabaseClientTests/RoomTests.swift

234 lines
5.3 KiB
Swift

import Dependencies
import Foundation
import ManualDCore
import Parsing
import Testing
@testable import DatabaseClient
@Suite
struct RoomTests {
@Test
func happyPath() async throws {
try await withTestUserAndProject { _, project in
@Dependency(\.database.rooms) var rooms
let room = try await rooms.create(
.init(projectID: project.id, name: "Test", heatingLoad: 1234, coolingTotal: 1234)
)
let fetched = try await rooms.fetch(project.id)
#expect(fetched == [room])
let got = try await rooms.get(room.id)
#expect(got == room)
let updated = try await rooms.update(
room.id,
.init(rectangularSizes: [.init(id: UUID(0), register: 1, height: 8)])
)
#expect(updated.id == room.id)
let updatedSize = try await rooms.updateRectangularSize(
room.id, .init(id: UUID(0), register: 1, height: 10)
)
#expect(updatedSize.id == room.id)
let deletedSize = try await rooms.deleteRectangularSize(room.id, UUID(0))
#expect(deletedSize.rectangularSizes == nil)
try await rooms.delete(room.id)
}
}
@Test
func createMany() async throws {
try await withTestUserAndProject { _, project in
@Dependency(\.database.rooms) var rooms
let created = try await rooms.createMany([
.init(projectID: project.id, name: "Test 1", heatingLoad: 1234, coolingTotal: 1234),
.init(projectID: project.id, name: "Test 2", heatingLoad: 1234, coolingTotal: 1234),
])
#expect(created.count == 2)
#expect(created[0].name == "Test 1")
#expect(created[1].name == "Test 2")
}
}
@Test
func notFound() async throws {
try await withDatabase {
@Dependency(\.database.rooms) var rooms
await #expect(throws: NotFoundError.self) {
try await rooms.delete(UUID(0))
}
await #expect(throws: NotFoundError.self) {
try await rooms.deleteRectangularSize(UUID(0), UUID(1))
}
await #expect(throws: NotFoundError.self) {
try await rooms.update(UUID(0), .init())
}
await #expect(throws: NotFoundError.self) {
try await rooms.updateRectangularSize(UUID(0), .init(height: 8))
}
}
}
@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
),
Room.Create(
projectID: UUID(0),
name: "Test",
heatingLoad: 12345,
coolingTotal: nil,
coolingSensible: nil,
registerCount: 1
),
]
)
func validations(room: Room.Create) throws {
#expect(throws: (any Error).self) {
// do {
try room.toModel().validate()
// } catch {
// print("\(error)")
// throw error
// }
}
}
@Test
func csvParsing() throws {
let input = """
Name,Heating Load,Cooling Total,Cooling Sensible,Register Count
Bed-1,12345,12345,,2
Bed-2,1223,,1123,1
"""[...].utf8
let commaSeparator = ParsePrint {
OneOf {
",".utf8
", ".utf8
}
}
let rowParser = ParsePrint {
Prefix { $0 != UInt8(ascii: ",") }.map(.string)
",".utf8
Double.parser()
Skip { commaSeparator }
// ",".utf8
Optionally {
Double.parser()
}
Skip { commaSeparator }
// ",".utf8
Optionally {
Double.parser()
}
Skip { commaSeparator }
// ",".utf8
Int.parser()
}
.map(.memberwise(Row.init))
let csvRowParser = OneOf {
rowParser.map { CSVRowType.row($0) }
Prefix { $0 != UInt8(ascii: "\n") }.map(.string).map { CSVRowType.header($0) }
}
let rowsParser = Many {
csvRowParser
} separator: {
"\n".utf8
}
let rows = try rowsParser.parse(input)
print("rows: \(rows)")
#expect(rows.count == 3)
#expect(rows.rows.count == 2)
print(String(try rowParser.print(rows.rows.first!))!)
}
}
enum CSVRowType {
case header(String)
case row(Row)
}
struct Row {
let name: String
let heatingLoad: Double
let coolingTotal: Double?
let coolingSensible: Double?
let registerCount: Int
}
extension Array where Element == CSVRowType {
var rows: [Row] {
reduce(into: [Row]()) {
if case .row(let row) = $1 {
$0.append(row)
}
}
}
}