HeartbeatTests.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import XCTest
  15. @testable import FirebaseCoreInternal
  16. class TimePeriodTests: XCTestCase {
  17. override func setUpWithError() throws {
  18. XCTAssertEqual(TimePeriod.allCases, [.daily])
  19. }
  20. func testTimeIntervals() throws {
  21. TimePeriod.allCases.forEach { period in
  22. XCTAssertEqual(period.timeInterval, Double(period.rawValue) * 86400)
  23. }
  24. }
  25. func testTimePeriodRawValues() throws {
  26. let dailyTimePeriod = TimePeriod.daily
  27. XCTAssertEqual(dailyTimePeriod.rawValue, 1)
  28. }
  29. }
  30. class HeartbeatTests: XCTestCase {
  31. var heartbeat: Heartbeat!
  32. var heartbeatData: Data!
  33. override func setUpWithError() throws {
  34. heartbeat = Heartbeat(
  35. agent: "dummy_agent",
  36. date: Date(timeIntervalSince1970: 1_635_739_200), // 2021-11-01
  37. timePeriods: [.daily],
  38. version: 100
  39. )
  40. heartbeatData = try JSONEncoder().encode(heartbeat)
  41. }
  42. func testHeartbeatCurrentVersion() throws {
  43. XCTAssertEqual(Heartbeat(agent: #function, date: Date()).version, 0)
  44. }
  45. func testDecodeAndEncode() throws {
  46. // Given
  47. let json = """
  48. {
  49. "agent": "dummy_agent",
  50. "date": 657432000,
  51. "timePeriods": [1],
  52. "version": 100
  53. }
  54. """
  55. let data = try XCTUnwrap(json.data(using: .utf8))
  56. // When
  57. let decodedHeartbeat = try JSONDecoder()
  58. .decode(Heartbeat.self, from: data)
  59. let encodedHeartbeat = try JSONEncoder()
  60. .encode(decodedHeartbeat)
  61. // Then
  62. XCTAssertEqual(decodedHeartbeat.agent, heartbeat.agent)
  63. XCTAssertEqual(decodedHeartbeat.date, heartbeat.date)
  64. XCTAssertEqual(decodedHeartbeat.timePeriods, heartbeat.timePeriods)
  65. XCTAssertEqual(decodedHeartbeat.version, heartbeat.version)
  66. XCTAssertEqual(encodedHeartbeat, heartbeatData)
  67. }
  68. }