CodableTimestampTests.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright 2019 Google
  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 FirebaseCore
  17. import FirebaseFirestore
  18. import FirebaseFirestoreSwift
  19. import Foundation
  20. import XCTest
  21. class CodableTimestampTests: XCTestCase {
  22. func testTimestampEncodes() {
  23. let timestamp = Timestamp(seconds: 37, nanoseconds: 123)
  24. do {
  25. let jsonData = try JSONEncoder().encode(timestamp)
  26. let json = String(data: jsonData, encoding: .utf8)!
  27. // The ordering of attributes in the JSON output is not guaranteed, so just verify that
  28. // each required property is present.
  29. XCTAssert(json.contains("\"seconds\":37"))
  30. XCTAssert(json.contains("\"nanoseconds\":123"))
  31. } catch {
  32. XCTFail("Error: \(error)")
  33. }
  34. }
  35. func testTimestampDecodes() {
  36. let json = """
  37. {
  38. "seconds": 37,
  39. "nanoseconds": 122
  40. }
  41. """
  42. let jsonData: Data = json.data(using: .utf8)!
  43. let timestamp = try! JSONDecoder().decode(Timestamp.self, from: jsonData)
  44. XCTAssertEqual(37, timestamp.seconds)
  45. XCTAssertEqual(122, timestamp.nanoseconds)
  46. }
  47. }