Skip to content

Commit f1c025d

Browse files
committed
add test for timestamp
1 parent b4d5e8b commit f1c025d

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

Firestore/Swift/Source/SwiftAPI/Pipeline/Expr/Constant.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ public struct Constant: Expr, BridgeWrapper, @unchecked Sendable {
3333
}
3434
}
3535

36-
// Initializer for numbers
36+
// Initializer for integer
37+
public init(_ value: Int) {
38+
self.init(value as Any)
39+
}
40+
41+
// Initializer for double
3742
public init(_ value: Double) {
3843
self.init(value as Any)
3944
}

Firestore/Swift/Tests/Integration/PipelineTests.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,4 +2550,49 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
25502550
XCTFail("No document retrieved for testSupportsMapMerge")
25512551
}
25522552
}
2553+
2554+
func testSupportsTimestampConversions() async throws {
2555+
let db = firestore()
2556+
let randomCol = collectionRef() // Unique collection for this test
2557+
2558+
// Add a dummy document to ensure the select stage has an input
2559+
try await randomCol.document("dummyTimeDoc").setData(["field": "value"])
2560+
2561+
let pipeline = db.pipeline()
2562+
.collection(randomCol.path)
2563+
.limit(1)
2564+
.select(
2565+
Constant(1_741_380_235).unixSecondsToTimestamp().as("unixSecondsToTimestamp"),
2566+
Constant(1_741_380_235_123).unixMillisToTimestamp().as("unixMillisToTimestamp"),
2567+
Constant(1_741_380_235_123_456).unixMicrosToTimestamp().as("unixMicrosToTimestamp"),
2568+
Constant(Timestamp(seconds: 1_741_380_235, nanoseconds: 123_456_789))
2569+
.timestampToUnixSeconds().as("timestampToUnixSeconds"),
2570+
Constant(Timestamp(seconds: 1_741_380_235, nanoseconds: 123_456_789))
2571+
.timestampToUnixMillis().as("timestampToUnixMillis"),
2572+
Constant(Timestamp(seconds: 1_741_380_235, nanoseconds: 123_456_789))
2573+
.timestampToUnixMicros().as("timestampToUnixMicros")
2574+
)
2575+
2576+
let snapshot = try await pipeline.execute()
2577+
XCTAssertEqual(
2578+
snapshot.results.count,
2579+
1,
2580+
"Should retrieve one document for timestamp conversions"
2581+
)
2582+
2583+
let expectedResults: [String: Sendable?] = [
2584+
"unixSecondsToTimestamp": Timestamp(seconds: 1_741_380_235, nanoseconds: 0),
2585+
"unixMillisToTimestamp": Timestamp(seconds: 1_741_380_235, nanoseconds: 123_000_000),
2586+
"unixMicrosToTimestamp": Timestamp(seconds: 1_741_380_235, nanoseconds: 123_456_000),
2587+
"timestampToUnixSeconds": 1_741_380_235,
2588+
"timestampToUnixMillis": 1_741_380_235_123,
2589+
"timestampToUnixMicros": 1_741_380_235_123_456,
2590+
]
2591+
2592+
if let resultDoc = snapshot.results.first {
2593+
TestHelper.compare(pipelineResult: resultDoc, expected: expectedResults)
2594+
} else {
2595+
XCTFail("No document retrieved for testSupportsTimestampConversions")
2596+
}
2597+
}
25532598
}

0 commit comments

Comments
 (0)