CodecHelperTests.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2024 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 Foundation
  15. import XCTest
  16. @testable import FirebaseDataConnect
  17. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  18. final class CodecHelperTests: XCTestCase {
  19. override func setUpWithError() throws {}
  20. override func tearDownWithError() throws {}
  21. func testNoDashUUIDString() throws {
  22. let uuidStr = "B7ACD615-1140-48F6-A522-26260D5C9367"
  23. let uuid = UUID(uuidString: uuidStr)
  24. // lowercase this for test since the UUID class converts uuid to lowercase
  25. let uuidNoDashStr = "B7ACD615114048F6A52226260D5C9367".lowercased()
  26. let uuidConverter = UUIDCodableConverter()
  27. let convertedUuid = try uuidConverter.encode(input: uuid)
  28. XCTAssertEqual(convertedUuid, uuidNoDashStr)
  29. }
  30. func testConvertToSystemUUIDType() throws {
  31. let uuidStr = "B7ACD615-1140-48F6-A522-26260D5C9367"
  32. let uuid = UUID(uuidString: uuidStr)
  33. let uuidNoDashStr = "B7ACD615114048F6A52226260D5C9367"
  34. let uuidConverter = UUIDCodableConverter()
  35. let uuidConverted = try uuidConverter.decode(input: uuidNoDashStr)
  36. XCTAssertEqual(uuid, uuidConverted)
  37. }
  38. func testConvertUUIDToNil() throws {
  39. let uuidConverter = UUIDCodableConverter()
  40. let uuid: UUID? = nil
  41. let uuidString = try uuidConverter.encode(input: uuid)
  42. XCTAssertNil(uuidString)
  43. }
  44. func testConvertUUIDFromNil() throws {
  45. let uuidConverter = UUIDCodableConverter()
  46. let uuidString: String? = nil
  47. let uuid: UUID? = try uuidConverter.decode(input: uuidString)
  48. XCTAssertNil(uuid)
  49. }
  50. func testInt64ToString() throws {
  51. let int64Converter = Int64CodableConverter()
  52. let int64Val: Int64 = 9_223_372_036_854_775_807
  53. let expectedVal = "9223372036854775807"
  54. let convertedVal = try int64Converter.encode(input: int64Val)
  55. XCTAssertEqual(convertedVal, expectedVal)
  56. }
  57. func testStringToInt64() throws {
  58. let int64Converter = Int64CodableConverter()
  59. let expectedVal: Int64 = 9_223_372_036_854_775_807
  60. let stringVal = "9223372036854775807"
  61. let convertedVal = try int64Converter.decode(input: stringVal)
  62. XCTAssertEqual(convertedVal, expectedVal)
  63. }
  64. func testCodecHelper() throws {
  65. let codecVals = TestCodecValues()
  66. let jsonEncoder = JSONEncoder()
  67. let jsonData = try jsonEncoder.encode(codecVals)
  68. let jsonDecoder = JSONDecoder()
  69. let codecValsDecoded = try jsonDecoder.decode(TestCodecValues.self, from: jsonData)
  70. XCTAssertEqual(codecVals, codecValsDecoded)
  71. }
  72. struct TestCodecValues: Codable, Equatable {
  73. enum CodingKeys: String, CodingKey {
  74. case largeVal
  75. case uuidVal
  76. }
  77. var largeVal: Int64 = 9_223_372_036_854_775_807
  78. var uuidVal: UUID = .init()
  79. init() {}
  80. init(from decoder: any Decoder) throws {
  81. var container = try decoder.container(keyedBy: CodingKeys.self)
  82. let codecHelper = CodecHelper<CodingKeys>()
  83. largeVal = try codecHelper.decode(Int64.self, forKey: .largeVal, container: &container)
  84. uuidVal = try codecHelper.decode(UUID.self, forKey: .uuidVal, container: &container)
  85. }
  86. func encode(to encoder: any Encoder) throws {
  87. var container = encoder.container(keyedBy: CodingKeys.self)
  88. let codecHelper = CodecHelper<CodingKeys>()
  89. try codecHelper.encode(largeVal, forKey: .largeVal, container: &container)
  90. try codecHelper.encode(uuidVal, forKey: .uuidVal, container: &container)
  91. }
  92. }
  93. }