Files
2026-10-19 12:47:16 +01:00

99 lines
4.3 KiB
Swift

import XCTest
@testable import DBSyn
final class DBSynTests: XCTestCase {
func testSQLSplitsMultipleStatements() {
XCTAssertEqual(SQLStatementSplitter.split("USE AAMEDIUK; SELECT * FROM ADCUSERS;"),
["USE AAMEDIUK", "SELECT * FROM ADCUSERS"])
}
func testSQLSplitterPreservesQuotedSemicolons() {
XCTAssertEqual(SQLStatementSplitter.split("SELECT 'a;b'; SELECT `a;b` FROM t;"),
["SELECT 'a;b'", "SELECT `a;b` FROM t"])
}
func testSQLSplitterIgnoresComments() {
XCTAssertEqual(SQLStatementSplitter.split("# comment;\nUSE db; /* comment; */ SELECT 1;"),
["USE db", "SELECT 1"])
}
func testSQLSplitterIgnoresEmptyStatements() {
XCTAssertEqual(SQLStatementSplitter.split(";; SELECT 1;;;"), ["SELECT 1"])
}
func testUSEDatabaseUnquoted() {
XCTAssertEqual(SQLStatementSplitter.usedDatabase("USE AAMEDIUK"), "AAMEDIUK")
}
func testUSEDatabaseQuoted() {
XCTAssertEqual(SQLStatementSplitter.usedDatabase("USE `my``db`"), "my`db")
}
func testUSEDatabaseRejectsOtherStatements() {
XCTAssertNil(SQLStatementSplitter.usedDatabase("SELECT * FROM ADCUSERS"))
}
func testSQLFormatterUppercasesKeywords() {
XCTAssertEqual(SQLFormatter.uppercaseKeywords("select * from ADCUSERS where id = 1"),
"SELECT * FROM ADCUSERS WHERE id = 1")
}
func testSQLFormatterPreservesStringLiteralsAndComments() {
let input = "select 'select from' # select stays lower\nfrom users"
XCTAssertEqual(SQLFormatter.uppercaseKeywords(input),
"SELECT 'select from' # select stays lower\nFROM users")
}
func testConnectionProfileCodableRoundTrip() throws {
var profile = ConnectionProfile()
profile.name = "Development"
profile.database = "AAMEDIUK"
let decoded = try JSONDecoder().decode(ConnectionProfile.self, from: JSONEncoder().encode(profile))
XCTAssertEqual(decoded, profile)
}
func testIntrospectionCacheCodableRoundTrip() throws {
let connectionID = UUID()
let column = DatabaseModel.IntrospectedColumn(table: "ADCUSERS", name: "id", dataType: "bigint", nullable: false, key: "PRI", defaultValue: nil, extra: "auto_increment")
let snapshot = DatabaseModel.SchemaIntrospection(connectionID: connectionID, database: "AAMEDIUK", loadedAt: Date(timeIntervalSince1970: 1000), tables: ["ADCUSERS"], views: ["ACTIVE_USERS"], columnsByTable: ["ADCUSERS": [column]])
let decoded = try JSONDecoder().decode(DatabaseModel.SchemaIntrospection.self, from: JSONEncoder().encode(snapshot))
XCTAssertEqual(decoded.connectionID, connectionID)
XCTAssertEqual(decoded.database, "AAMEDIUK")
XCTAssertEqual(decoded.tables, ["ADCUSERS"])
XCTAssertEqual(decoded.views, ["ACTIVE_USERS"])
XCTAssertEqual(decoded.columnsByTable["ADCUSERS"]?.first?.name, "id")
}
func testDiagramSelectNone() {
XCTAssertEqual(DiagramLogic.selectedObjects([], available: ["A", "B"]), [])
}
func testDiagramSelectSubsetAndIgnoreRemovedObjects() {
XCTAssertEqual(DiagramLogic.selectedObjects(["A", "MISSING"], available: ["A", "B"]), ["A"])
}
func testDiagramSelectAll() {
let available = ["A", "B", "VIEW_C"]
XCTAssertEqual(DiagramLogic.selectedObjects(Set(available), available: available), Set(available))
}
func testDiagramLayoutContainsEverySelectedTable() {
let names = (0..<25).map { "table_\($0)" }
let positions = DiagramLogic.arrangedPositions(names)
XCTAssertEqual(positions.count, names.count)
XCTAssertEqual(positions["table_0"], CGPoint(x: 30, y: 30))
XCTAssertEqual(Set(positions.values.map { "\($0.x),\($0.y)" }).count, names.count)
}
func testDiagramDragRespectsZoom() {
let origin = CGPoint(x: 100, y: 200)
XCTAssertEqual(DiagramLogic.translatedPosition(origin, translation: CGSize(width: 60, height: -40), zoom: 2),
CGPoint(x: 130, y: 180))
}
func testDiagramDragHandlesZeroZoom() {
let point = DiagramLogic.translatedPosition(.zero, translation: CGSize(width: 10, height: 5), zoom: 0)
XCTAssertTrue(point.x.isFinite && point.y.isFinite)
}
}