Skip to content

Commit

Permalink
Change RelayResponseDecodingTest to use XCTUnwrap instead of if let t…
Browse files Browse the repository at this point in the history
…o make the test code cleaner
  • Loading branch information
tyiu committed May 19, 2024
1 parent a44290d commit 21eae6d
Showing 1 changed file with 76 additions and 109 deletions.
185 changes: 76 additions & 109 deletions Tests/NostrSDKTests/RelayResponseDecodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,175 +13,142 @@ final class RelayResponseDecodingTests: XCTestCase, FixtureLoading {
func testDecodeEventMessage() throws {
let data = try loadFixtureData("event")

if let relayResponse = RelayResponse.decode(data: data) {
guard case .event(let subscriptionId, let event) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(subscriptionId, "some-subscription-id")
XCTAssertNotNil(event)
XCTAssertTrue(event is TextNoteEvent)
XCTAssertEqual(event.id, "fa5ed84fc8eeb959fd39ad8e48388cfc33075991ef8e50064cfcecfd918bb91b")
} else {
XCTFail("failed to decode")
let relayResponse = try XCTUnwrap(RelayResponse.decode(data: data))
guard case .event(let subscriptionId, let event) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(subscriptionId, "some-subscription-id")
XCTAssertNotNil(event)
XCTAssertTrue(event is TextNoteEvent)
XCTAssertEqual(event.id, "fa5ed84fc8eeb959fd39ad8e48388cfc33075991ef8e50064cfcecfd918bb91b")
}

func testDecodeOkMessage() throws {
let data = try loadFixtureData("ok_success")

if let relayResponse = RelayResponse.decode(data: data) {
guard case .ok(let eventId, let success, let message) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(eventId, "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30")
XCTAssertEqual(success, true)
XCTAssertEqual(message.prefix, .unknown)
XCTAssertEqual(message.message, "")
} else {
XCTFail("failed to decode")
let relayResponse = try XCTUnwrap(RelayResponse.decode(data: data))
guard case .ok(let eventId, let success, let message) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(eventId, "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30")
XCTAssertEqual(success, true)
XCTAssertEqual(message.prefix, .unknown)
XCTAssertEqual(message.message, "")
}

func testDecodeOkMessageWithReason() throws {
let data = try loadFixtureData("ok_success_reason")

if let relayResponse = RelayResponse.decode(data: data) {
guard case .ok(let eventId, let success, let message) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(eventId, "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30")
XCTAssertEqual(success, true)
XCTAssertEqual(message.prefix, .pow)
XCTAssertEqual(message.message, "difficulty: 25>=24")
} else {
XCTFail("failed to decode")
let relayResponse = try XCTUnwrap(RelayResponse.decode(data: data))
guard case .ok(let eventId, let success, let message) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(eventId, "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30")
XCTAssertEqual(success, true)
XCTAssertEqual(message.prefix, .pow)
XCTAssertEqual(message.message, "difficulty: 25>=24")
}

func testDecodeOkMessageWithReasonPrefixNoMessage() throws {
let data = try loadFixtureData("ok_success_reason_prefix_no_message")

if let relayResponse = RelayResponse.decode(data: data) {
guard case .ok(let eventId, let success, let message) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(eventId, "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30")
XCTAssertEqual(success, true)
XCTAssertEqual(message.prefix, .pow)
XCTAssertEqual(message.message, "")
} else {
XCTFail("failed to decode")
let relayResponse = try XCTUnwrap(RelayResponse.decode(data: data))
guard case .ok(let eventId, let success, let message) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(eventId, "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30")
XCTAssertEqual(success, true)
XCTAssertEqual(message.prefix, .pow)
XCTAssertEqual(message.message, "")
}

func testDecodeOkMessageWithUnknownReason() throws {
let data = try loadFixtureData("ok_unknown_reason")

if let relayResponse = RelayResponse.decode(data: data) {
guard case .ok(let eventId, let success, let message) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(eventId, "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30")
XCTAssertEqual(success, true)
XCTAssertEqual(message.prefix, .unknown)
XCTAssertEqual(message.message, "unknown: reason: unknown")
} else {
XCTFail("failed to decode")
let relayResponse = try XCTUnwrap(RelayResponse.decode(data: data))
guard case .ok(let eventId, let success, let message) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(eventId, "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30")
XCTAssertEqual(success, true)
XCTAssertEqual(message.prefix, .unknown)
XCTAssertEqual(message.message, "unknown: reason: unknown")
}

func testDecodeOkFailWithReason() throws {
let data = try loadFixtureData("ok_fail_reason")

if let relayResponse = RelayResponse.decode(data: data) {
guard case .ok(let eventId, let success, let message) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(eventId, "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30")
XCTAssertEqual(success, false)
XCTAssertEqual(message.prefix, .blocked)
XCTAssertEqual(message.message, "tor exit nodes not allowed")
} else {
XCTFail("failed to decode")
let relayResponse = try XCTUnwrap(RelayResponse.decode(data: data))
guard case .ok(let eventId, let success, let message) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(eventId, "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30")
XCTAssertEqual(success, false)
XCTAssertEqual(message.prefix, .blocked)
XCTAssertEqual(message.message, "tor exit nodes not allowed")
}

func testDecodeEOSEMessage() throws {
let data = try loadFixtureData("eose")

if let relayResponse = RelayResponse.decode(data: data) {
guard case .eose(let subscriptionId) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(subscriptionId, "some-subscription-id")
} else {
XCTFail("failed to decode")
let relayResponse = try XCTUnwrap(RelayResponse.decode(data: data))
guard case .eose(let subscriptionId) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(subscriptionId, "some-subscription-id")
}

func testDecodeClosedMessage() throws {
let data = try loadFixtureData("closed")

if let relayResponse = RelayResponse.decode(data: data) {
guard case .closed(let subscriptionId, let message) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(subscriptionId, "some-subscription-id")
XCTAssertEqual(message.prefix, .error)
XCTAssertEqual(message.message, "shutting down idle subscription")
} else {
XCTFail("failed to decode")
let relayResponse = try XCTUnwrap(RelayResponse.decode(data: data))
guard case .closed(let subscriptionId, let message) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(subscriptionId, "some-subscription-id")
XCTAssertEqual(message.prefix, .error)
XCTAssertEqual(message.message, "shutting down idle subscription")
}

func testDecodeNoticeMessage() throws {
let data = try loadFixtureData("notice")

if let relayResponse = RelayResponse.decode(data: data) {
guard case .notice(let message) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(message, "there was an error")
} else {
XCTFail("failed to decode")
let relayResponse = try XCTUnwrap(RelayResponse.decode(data: data))
guard case .notice(let message) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(message, "there was an error")
}

func testDecodeAuthChallenge() throws {
let data = try loadFixtureData("auth_challenge")

if let relayResponse = RelayResponse.decode(data: data) {
guard case .auth(let challenge) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(challenge, "some-challenge-string")
} else {
XCTFail("failed to decode")
let relayResponse = try XCTUnwrap(RelayResponse.decode(data: data))
guard case .auth(let challenge) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(challenge, "some-challenge-string")
}

func testDecodeCount() throws {
let data = try loadFixtureData("count_response")

if let relayResponse = RelayResponse.decode(data: data) {
guard case .count(let subscriptionId, let count) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(subscriptionId, "subscription-id")
XCTAssertEqual(count, 238)
} else {
XCTFail("failed to decode")
let relayResponse = try XCTUnwrap(RelayResponse.decode(data: data))
guard case .count(let subscriptionId, let count) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(subscriptionId, "subscription-id")
XCTAssertEqual(count, 238)
}
}

0 comments on commit 21eae6d

Please sign in to comment.