Files
helder/Tests/HelderCoreTests/Schema/SchemaModelTests.swift
T

97 lines
4.8 KiB
Swift

import Foundation
import HelderCore
import Testing
@Suite struct SchemaModelTests {
@Test func tablesAreSortedAndCounted() {
let model = SchemaFixtures.contractModel
#expect(model.tables.map(\.name) == ["conversations", "messages", "users"])
#expect(model.connectionLabel == "mysql · odynt")
#expect(model.tableCount == 3)
#expect(model.columnCount == 10)
#expect(model.relationCount == 2)
#expect(model.generatedDate == ISO8601DateFormatter().date(from: "2026-11-02T13:32:05Z"))
}
@Test func columnsCarryTheirKeys() throws {
let model = SchemaFixtures.contractModel
let users = try #require(model.table(named: "users"))
#expect(users.columns[0] == SchemaColumn(name: "id", type: "bigint unsigned", isPrimary: true))
#expect(users.columns[1].isUnique)
#expect(users.columns[1].keyLabel == "UQ")
#expect(users.columns[2].displayType == "timestamp?")
#expect(users.rowsLabel == "1,284")
let conversations = try #require(model.table(named: "conversations"))
let userID = conversations.columns[1]
#expect(userID.isForeign)
#expect(userID.foreignTarget == SchemaColumnRef(table: "users", column: "id"))
#expect(userID.keyLabel == "FK")
#expect(userID.flags == "FK → users.id · NULL")
#expect(conversations.columns[0].flags == "PRIMARY · NOT NULL")
#expect(users.columns[1].flags == "UNIQUE · NOT NULL")
#expect(conversations.rowsLabel == "")
#expect(model.table(named: "messages")?.rowsLabel == "214,330")
}
@Test func edgesPointAtTheReferencedRowAndSkipMissingTables() {
let model = SchemaFixtures.contractModel
#expect(model.edges == [
SchemaEdge(from: "conversations", column: "user_id", columnIndex: 1, to: "users", targetColumnIndex: 0),
SchemaEdge(from: "messages", column: "conversation_id", columnIndex: 1, to: "conversations", targetColumnIndex: 0),
])
#expect(model.outgoing(from: "conversations").map(\.to) == ["users"])
#expect(model.incoming(to: "conversations").map(\.from) == ["messages"])
// The ghost_id key points at a table that is not in the dump: still a FK column, but no line.
#expect(model.table(named: "conversations")?.columns[3].isForeign == true)
}
@Test func theTargetRowFallsBackToThePrimaryKeyThenTheFirstRow() {
let dump = SchemaDump(tables: [
.init(name: "a", columns: [.init(name: "code"), .init(name: "uuid")],
indexes: [.init(columns: ["uuid"], primary: true)]),
.init(name: "b", columns: [.init(name: "x")]),
.init(name: "c", columns: [.init(name: "id"), .init(name: "a_ref"), .init(name: "b_ref")],
foreignKeys: [.init(columns: ["a_ref"], foreignTable: "a", foreignColumns: ["missing"]),
.init(columns: ["b_ref"], foreignTable: "b")]),
])
let edges = SchemaModel(dump: dump).edges
#expect(edges.map(\.targetColumnIndex) == [1, 0])
}
@Test func aKeyOnAColumnThatIsNotListedDrawsNoLine() {
let dump = SchemaDump(tables: [
.init(name: "a", columns: [.init(name: "id")]),
.init(name: "b", columns: [.init(name: "id")], foreignKeys: [.init(columns: ["a_id"], foreignTable: "a")]),
])
#expect(SchemaModel(dump: dump).edges.isEmpty)
}
@Test func theTypeFallsBackToTheTypeName() {
let dump = SchemaDump(tables: [.init(name: "t", columns: [.init(name: "a", typeName: "int"), .init(name: "b")])])
#expect(SchemaModel(dump: dump).tables[0].columns.map(\.type) == ["int", ""])
}
@Test func anAutoIncrementColumnIsPrimaryWhenNoIndexSaysSo() {
let dump = SchemaDump(tables: [.init(name: "t", columns: [.init(name: "id", autoIncrement: true), .init(name: "a")])])
#expect(SchemaModel(dump: dump).tables[0].columns.map(\.isPrimary) == [true, false])
}
@Test func aCompositeUniqueIndexDoesNotMarkItsColumns() {
let dump = SchemaDump(tables: [.init(name: "t", columns: [.init(name: "a"), .init(name: "b")],
indexes: [.init(columns: ["a", "b"], unique: true)])])
#expect(SchemaModel(dump: dump).tables[0].columns.map(\.isUnique) == [false, false])
}
@Test("the connection label falls back", arguments: [
(SchemaDump(connection: "main", driver: "pgsql", database: "shop"), "pgsql · shop"),
(SchemaDump(connection: "main", database: "shop"), "main · shop"),
(SchemaDump(connection: "main", driver: ""), "main"),
(SchemaDump(database: "shop"), "shop"),
(SchemaDump(), "database"),
])
func connectionLabel(dump: SchemaDump, expected: String) {
#expect(SchemaModel(dump: dump).connectionLabel == expected)
}
}