RPCErrorTests.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2023, gRPC Authors All rights reserved.
  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 GRPCCore
  17. import XCTest
  18. final class RPCErrorTests: XCTestCase {
  19. private static let statusCodeRawValue: [(RPCError.Code, Int)] = [
  20. (.cancelled, 1),
  21. (.unknown, 2),
  22. (.invalidArgument, 3),
  23. (.deadlineExceeded, 4),
  24. (.notFound, 5),
  25. (.alreadyExists, 6),
  26. (.permissionDenied, 7),
  27. (.resourceExhausted, 8),
  28. (.failedPrecondition, 9),
  29. (.aborted, 10),
  30. (.outOfRange, 11),
  31. (.unimplemented, 12),
  32. (.internalError, 13),
  33. (.unavailable, 14),
  34. (.dataLoss, 15),
  35. (.unauthenticated, 16),
  36. ]
  37. func testCustomStringConvertible() {
  38. XCTAssertDescription(RPCError(code: .dataLoss, message: ""), #"dataLoss: """#)
  39. XCTAssertDescription(RPCError(code: .unknown, message: "message"), #"unknown: "message""#)
  40. XCTAssertDescription(RPCError(code: .aborted, message: "message"), #"aborted: "message""#)
  41. }
  42. func testErrorFromStatus() throws {
  43. var status = Status(code: .ok, message: "")
  44. // ok isn't an error
  45. XCTAssertNil(RPCError(status: status))
  46. status.code = .invalidArgument
  47. var error = try XCTUnwrap(RPCError(status: status))
  48. XCTAssertEqual(error.code, .invalidArgument)
  49. XCTAssertEqual(error.message, "")
  50. XCTAssertEqual(error.metadata, [:])
  51. status.code = .cancelled
  52. status.message = "an error message"
  53. error = try XCTUnwrap(RPCError(status: status))
  54. XCTAssertEqual(error.code, .cancelled)
  55. XCTAssertEqual(error.message, "an error message")
  56. XCTAssertEqual(error.metadata, [:])
  57. }
  58. func testEquatableConformance() {
  59. XCTAssertEqual(
  60. RPCError(code: .cancelled, message: ""),
  61. RPCError(code: .cancelled, message: "")
  62. )
  63. XCTAssertEqual(
  64. RPCError(code: .cancelled, message: "message"),
  65. RPCError(code: .cancelled, message: "message")
  66. )
  67. XCTAssertEqual(
  68. RPCError(code: .cancelled, message: "message", metadata: ["foo": "bar"]),
  69. RPCError(code: .cancelled, message: "message", metadata: ["foo": "bar"])
  70. )
  71. XCTAssertNotEqual(
  72. RPCError(code: .cancelled, message: ""),
  73. RPCError(code: .cancelled, message: "message")
  74. )
  75. XCTAssertNotEqual(
  76. RPCError(code: .cancelled, message: "message"),
  77. RPCError(code: .unknown, message: "message")
  78. )
  79. XCTAssertNotEqual(
  80. RPCError(code: .cancelled, message: "message", metadata: ["foo": "bar"]),
  81. RPCError(code: .cancelled, message: "message", metadata: ["foo": "baz"])
  82. )
  83. }
  84. func testStatusCodeRawValues() {
  85. for (code, expected) in Self.statusCodeRawValue {
  86. XCTAssertEqual(code.rawValue, expected, "\(code) had unexpected raw value")
  87. }
  88. }
  89. }