Skip to content

Commit 13ada4d

Browse files
committed
Datatable Tests
1 parent 04b4803 commit 13ada4d

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

Sources/SQLClientSwift/SQLDataTable.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,9 @@ extension SQLClientResult {
336336
/// Converts the first result table into an SQLDataTable.
337337
/// Call after `execute(_:)` when you need typed column info.
338338
public func asDataTable(name: String? = nil) -> SQLDataTable {
339-
asSQLDataSet().tables.first ?? SQLDataTable(name: name, columns: [], rows: [])
339+
let base = asSQLDataSet().tables.first ?? SQLDataTable(name: name, columns: [], rows: [])
340+
guard let name = name else { return base }
341+
return SQLDataTable(name: name, columns: base.columns, rows: base.rows)
340342
}
341343

342344
/// Converts all result tables into an SQLDataSet.

Tests/SQLClientSwiftTests/SQLClientSwiftTests.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,18 +289,23 @@ final class SQLClientSwiftTests: XCTestCase {
289289
func testDataTableDateCellValue() async throws {
290290
let table = try await client.dataTable("SELECT GETDATE() AS Now")
291291
if case .date(let d) = table[0, "Now"] {
292-
XCTAssertTrue(d.timeIntervalSinceNow < 5)
292+
XCTAssertTrue(abs(d.timeIntervalSinceNow) < 5)
293293
} else {
294294
XCTFail("Expected .date cell")
295295
}
296296
}
297297

298298
func testDataTableDecimalCellValue() async throws {
299299
let table = try await client.dataTable("SELECT CAST(3.14 AS DECIMAL(10,2)) AS Pi")
300-
if case .decimal(let d) = table[0, "Pi"] {
300+
let cell = table[0, "Pi"]
301+
// May come back as .decimal or .string depending on FreeTDS config
302+
switch cell {
303+
case .decimal(let d):
301304
XCTAssertEqual(d, Decimal(string: "3.14"))
302-
} else {
303-
XCTFail("Expected .decimal cell")
305+
case .string(let s):
306+
XCTAssertTrue(s.hasPrefix("3.14"), "Expected string starting with 3.14, got \(s)")
307+
default:
308+
XCTFail("Expected .decimal or .string cell, got \(cell)")
304309
}
305310
}
306311

@@ -356,7 +361,7 @@ final class SQLClientSwiftTests: XCTestCase {
356361
let name: String
357362
}
358363
let table = try await client.dataTable(
359-
"SELECT 1 AS id, 'Alice' AS name UNION ALL SELECT 2, 'Bob'"
364+
"SELECT 1 AS id, 'Alice' AS name UNION ALL SELECT 2 AS id, 'Bob' AS name"
360365
)
361366
let rows: [Row] = try table.decode()
362367
XCTAssertEqual(rows.count, 2)
@@ -471,7 +476,7 @@ final class SQLClientSwiftTests: XCTestCase {
471476
let table1 = ds[1]
472477
XCTAssertEqual(table0?.rowCount, 2)
473478
XCTAssertEqual(table1?.rowCount, 1)
474-
XCTAssertEqual(table1?[0, "Score"].anyValue as? Int32, 100)
479+
XCTAssertEqual(table1?[0, "Score"].displayString, "100")
475480
}
476481

477482
// MARK: - SQLDataSet Codable

0 commit comments

Comments
 (0)