RPCErrorTests.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 testErrorCodeFromStatusCode() throws {
  59. XCTAssertNil(RPCError.Code(Status.Code.ok))
  60. XCTAssertEqual(RPCError.Code(Status.Code.cancelled), .cancelled)
  61. XCTAssertEqual(RPCError.Code(Status.Code.unknown), .unknown)
  62. XCTAssertEqual(RPCError.Code(Status.Code.invalidArgument), .invalidArgument)
  63. XCTAssertEqual(RPCError.Code(Status.Code.deadlineExceeded), .deadlineExceeded)
  64. XCTAssertEqual(RPCError.Code(Status.Code.notFound), .notFound)
  65. XCTAssertEqual(RPCError.Code(Status.Code.alreadyExists), .alreadyExists)
  66. XCTAssertEqual(RPCError.Code(Status.Code.permissionDenied), .permissionDenied)
  67. XCTAssertEqual(RPCError.Code(Status.Code.resourceExhausted), .resourceExhausted)
  68. XCTAssertEqual(RPCError.Code(Status.Code.failedPrecondition), .failedPrecondition)
  69. XCTAssertEqual(RPCError.Code(Status.Code.aborted), .aborted)
  70. XCTAssertEqual(RPCError.Code(Status.Code.outOfRange), .outOfRange)
  71. XCTAssertEqual(RPCError.Code(Status.Code.unimplemented), .unimplemented)
  72. XCTAssertEqual(RPCError.Code(Status.Code.internalError), .internalError)
  73. XCTAssertEqual(RPCError.Code(Status.Code.unavailable), .unavailable)
  74. XCTAssertEqual(RPCError.Code(Status.Code.dataLoss), .dataLoss)
  75. XCTAssertEqual(RPCError.Code(Status.Code.unauthenticated), .unauthenticated)
  76. }
  77. func testEquatableConformance() {
  78. XCTAssertEqual(
  79. RPCError(code: .cancelled, message: ""),
  80. RPCError(code: .cancelled, message: "")
  81. )
  82. XCTAssertEqual(
  83. RPCError(code: .cancelled, message: "message"),
  84. RPCError(code: .cancelled, message: "message")
  85. )
  86. XCTAssertEqual(
  87. RPCError(code: .cancelled, message: "message", metadata: ["foo": "bar"]),
  88. RPCError(code: .cancelled, message: "message", metadata: ["foo": "bar"])
  89. )
  90. XCTAssertNotEqual(
  91. RPCError(code: .cancelled, message: ""),
  92. RPCError(code: .cancelled, message: "message")
  93. )
  94. XCTAssertNotEqual(
  95. RPCError(code: .cancelled, message: "message"),
  96. RPCError(code: .unknown, message: "message")
  97. )
  98. XCTAssertNotEqual(
  99. RPCError(code: .cancelled, message: "message", metadata: ["foo": "bar"]),
  100. RPCError(code: .cancelled, message: "message", metadata: ["foo": "baz"])
  101. )
  102. }
  103. func testStatusCodeRawValues() {
  104. for (code, expected) in Self.statusCodeRawValue {
  105. XCTAssertEqual(code.rawValue, expected, "\(code) had unexpected raw value")
  106. }
  107. }
  108. }