ServerValueCodingTests.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright 2020 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import FirebaseDatabase
  17. import Foundation
  18. import XCTest
  19. class ServerTimestampTests: XCTestCase {
  20. func testDateRoundTrip() throws {
  21. struct Model: Codable, Equatable {
  22. @ServerTimestamp var timestamp: Date?
  23. }
  24. let model = Model(timestamp: Date(timeIntervalSince1970: 123_456_789.123))
  25. let dict = ["timestamp": 123_456_789_123]
  26. assertThat(model).roundTrips(to: dict)
  27. }
  28. func testTimestampEncoding() throws {
  29. struct Model: Codable, Equatable {
  30. @ServerTimestamp var timestamp: Date?
  31. }
  32. let model = Model()
  33. let dict = ["timestamp": [".sv": "timestamp"]]
  34. // We can't round trip since we only decode values as Ints
  35. // (for Date conversion) and never as the magic value.
  36. assertThat(model).encodes(to: dict)
  37. }
  38. }
  39. private struct CurrencyAmount: Codable, Equatable, Hashable, AdditiveArithmetic {
  40. static var zero: Self = CurrencyAmount(value: 0)
  41. static func + (lhs: Self, rhs: Self) -> Self {
  42. return Self(value: lhs.value + rhs.value)
  43. }
  44. static func += (lhs: inout Self, rhs: Self) {
  45. lhs.value += rhs.value
  46. }
  47. static func - (lhs: Self, rhs: Self) -> Self {
  48. return CurrencyAmount(value: lhs.value - rhs.value)
  49. }
  50. static func -= (lhs: inout Self, rhs: Self) {
  51. lhs.value -= rhs.value
  52. }
  53. var value: Decimal
  54. func encode(to encoder: Encoder) throws {
  55. var container = encoder.singleValueContainer()
  56. try container.encode(value)
  57. }
  58. init(value: Decimal) {
  59. self.value = value
  60. }
  61. init(from decoder: Decoder) throws {
  62. let container = try decoder.singleValueContainer()
  63. value = try container.decode(Decimal.self)
  64. }
  65. }
  66. extension CurrencyAmount: ExpressibleByIntegerLiteral {
  67. public init(integerLiteral value: Int) {
  68. self.value = Decimal(value)
  69. }
  70. }
  71. extension CurrencyAmount: ExpressibleByFloatLiteral {
  72. public init(floatLiteral value: Double) {
  73. self.value = Decimal(value)
  74. }
  75. }
  76. private func assertThat(_ dictionary: [String: Any],
  77. file: StaticString = #file,
  78. line: UInt = #line) -> DictionarySubject {
  79. return DictionarySubject(dictionary, file: file, line: line)
  80. }
  81. func assertThat<X: Equatable & Codable>(_ model: X, file: StaticString = #file,
  82. line: UInt = #line) -> CodableSubject<X> {
  83. return CodableSubject(model, file: file, line: line)
  84. }
  85. func assertThat<X: Equatable & Encodable>(_ model: X, file: StaticString = #file,
  86. line: UInt = #line) -> EncodableSubject<X> {
  87. return EncodableSubject(model, file: file, line: line)
  88. }
  89. class EncodableSubject<X: Equatable & Encodable> {
  90. var subject: X
  91. var file: StaticString
  92. var line: UInt
  93. init(_ subject: X, file: StaticString, line: UInt) {
  94. self.subject = subject
  95. self.file = file
  96. self.line = line
  97. }
  98. @discardableResult
  99. func encodes(to expected: [String: Any],
  100. using encoder: Database.Encoder = .init()) -> DictionarySubject {
  101. let encoded = assertEncodes(to: expected, using: encoder)
  102. return DictionarySubject(encoded, file: file, line: line)
  103. }
  104. func failsToEncode() {
  105. do {
  106. let encoder = Database.Encoder()
  107. encoder.keyEncodingStrategy = .convertToSnakeCase
  108. _ = try encoder.encode(subject)
  109. } catch {
  110. return
  111. }
  112. XCTFail("Failed to throw")
  113. }
  114. func failsEncodingAtTopLevel() {
  115. do {
  116. let encoder = Database.Encoder()
  117. encoder.keyEncodingStrategy = .convertToSnakeCase
  118. _ = try encoder.encode(subject)
  119. XCTFail("Failed to throw", file: file, line: line)
  120. } catch EncodingError.invalidValue(_, _) {
  121. return
  122. } catch {
  123. XCTFail("Unrecognized error: \(error)", file: file, line: line)
  124. }
  125. }
  126. private func assertEncodes(to expected: [String: Any],
  127. using encoder: Database.Encoder = .init()) -> [String: Any] {
  128. do {
  129. let enc = try encoder.encode(subject)
  130. XCTAssertEqual(enc as? NSDictionary, expected as NSDictionary, file: file, line: line)
  131. return (enc as! NSDictionary) as! [String: Any]
  132. } catch {
  133. XCTFail("Failed to encode \(X.self): error: \(error)")
  134. return ["": -1]
  135. }
  136. }
  137. }
  138. class CodableSubject<X: Equatable & Codable>: EncodableSubject<X> {
  139. func roundTrips(to expected: [String: Any],
  140. using encoder: Database.Encoder = .init(),
  141. decoder: Database.Decoder = .init()) {
  142. let reverseSubject = encodes(to: expected, using: encoder)
  143. reverseSubject.decodes(to: subject, using: decoder)
  144. }
  145. }
  146. class DictionarySubject {
  147. var subject: [String: Any]
  148. var file: StaticString
  149. var line: UInt
  150. init(_ subject: [String: Any], file: StaticString, line: UInt) {
  151. self.subject = subject
  152. self.file = file
  153. self.line = line
  154. }
  155. func decodes<X: Equatable & Codable>(to expected: X,
  156. using decoder: Database.Decoder = .init()) -> Void {
  157. do {
  158. let decoded = try decoder.decode(X.self, from: subject)
  159. XCTAssertEqual(decoded, expected)
  160. } catch {
  161. XCTFail("Failed to decode \(X.self): \(error)", file: file, line: line)
  162. }
  163. }
  164. }