Add support for FirTimestamp

This commit is contained in:
Oleksii Dykan
2018-05-19 19:50:58 +02:00
parent f1d465b936
commit c17c0603b2
3 changed files with 67 additions and 3 deletions

View File

@@ -95,6 +95,11 @@ class TestCodableFirebase: XCTestCase {
expectedValue: expected,
dateEncodingStrategy: .secondsSince1970,
dateDecodingStrategy: .secondsSince1970)
_testRoundTrip(of: TopLevelWrapper(FirTimestamp(date: Date(timeIntervalSince1970: seconds))),
expectedValue: expected,
dateEncodingStrategy: .secondsSince1970,
dateDecodingStrategy: .secondsSince1970)
_testRoundTrip(of: OptionalTopLevelWrapper(Date(timeIntervalSince1970: seconds)),
expectedValue: expected,
@@ -500,6 +505,22 @@ fileprivate struct Timestamp : Codable, Equatable {
}
}
fileprivate struct FirTimestamp : TimestampType, Equatable {
let date: Date
init(date: Date) {
self.date = date
}
func dateValue() -> Date {
return date
}
static func == (_ lhs: FirTimestamp, _ rhs: FirTimestamp) -> Bool {
return lhs.date == rhs.date
}
}
/// A simple referential counter type that encodes as a single Int value.
fileprivate final class Counter : Codable, Equatable {
var count: Int = 0

View File

@@ -128,7 +128,14 @@ class TestCodableFirestore: XCTestCase {
XCTAssertEqual((try? FirestoreEncoder().encode(val)) as NSDictionary?, ["value": val.value])
XCTAssertEqual(try? FirestoreDecoder().decode(TopLevelWrapper<DocumentReference>.self, from: ["value": val.value]), val)
}
func testEncodingTimestamp() {
let timestamp = Timestamp(date: Date())
let wrapper = TopLevelWrapper(timestamp)
XCTAssertEqual((try? FirestoreEncoder().encode(wrapper)) as NSDictionary?, ["value": timestamp])
XCTAssertEqual(try? FirestoreDecoder().decode(TopLevelWrapper<Timestamp>.self, from: ["value": timestamp]), wrapper)
}
private func _testEncodeFailure<T : Encodable>(of value: T) {
do {
let _ = try FirestoreEncoder().encode(value)
@@ -195,10 +202,29 @@ fileprivate class GeoPoint: NSObject, GeoPointType {
self.longitude = longitude
}
static func == (lhs: Point, rhs: Point) -> Bool {
return lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude
override func isEqual(_ object: Any?) -> Bool {
guard let other = object.flatMap({ $0 as? GeoPoint }) else { return false }
return latitude == other.latitude && longitude == other.longitude
}
}
// MARK: - ReferenceType
fileprivate class DocumentReference: NSObject, DocumentReferenceType {}
// MARK: - Timestamp
fileprivate class Timestamp: NSObject, TimestampType {
let date: Date
required init(date: Date) {
self.date = date
}
func dateValue() -> Date {
return date
}
override func isEqual(_ object: Any?) -> Bool {
guard let other = object.flatMap({ $0 as? Timestamp }) else { return false }
return date == other.date
}
}