Skip to content

Commit

Permalink
Set includeCustomMessageType false by default, fixes for contract tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jguz-pubnub committed Nov 18, 2024
1 parent bb2a65d commit 458e1a6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Sources/PubNub/PubNub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ public extension PubNub {
includeMeta: Bool = false,
includeUUID: Bool = true,
includeMessageType: Bool = true,
includeCustomMessageType: Bool = true,
includeCustomMessageType: Bool = false,
page: PubNubBoundedPage? = PubNubBoundedPageBase(),
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<(messagesByChannel: [String: [PubNubMessage]], next: PubNubBoundedPage?), Error>) -> Void)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,62 +36,42 @@ public class PubNubHistoryContractTestSteps: PubNubContractTestCase {

self.wait(for: [historyExpect], timeout: 60.0)
}

Match(["And"], "^history response contains messages (with|without) ('(.*)' and '(.*)') message types$") { args, _ in
Match(["And"], "^history response contains messages (with|without) (?:customMessageType|'([^']*)' and '([^']*)' (?:message )?types?)$") { args, _ in
guard let matches = args, let inclusionFlag = matches.first else {
XCTAssertNotNil(args?.first, "Step match failed")
return
}

guard let lastResult = self.lastResult() else {
XCTAssert(false, "Fetch history didn't returned response")
return
XCTFail("Fetch history didn't returned response"); return
}

guard let result = lastResult as? (messagesByChannel: [String: [PubNubMessageBase]], next: PubNubBoundedPage?),
let channel = result.messagesByChannel.first?.key, let messages = result.messagesByChannel[channel] else {
XCTAssert(false, "Fetch history returned unexpected response")
return
guard
let result = lastResult as? (messagesByChannel: [String: [PubNubMessageBase]], next: PubNubBoundedPage?),
let channel = result.messagesByChannel.first?.key, let messages = result.messagesByChannel[channel]
else {
XCTFail("Fetch history returned unexpected response"); return
}

XCTAssertGreaterThan(messages.count, 0)

let messagesWithTypes = messages.compactMap { $0.messageType }
XCTAssertFalse(inclusionFlag == "with" && messagesWithTypes.count == 0)
XCTAssertFalse(inclusionFlag == "without" && messagesWithTypes.count > 0)
var messagesWithTypes: [String] = []

if matches.count > 1 {
XCTAssertTrue(messagesWithTypes.map { String(describing: $0.rawValue) }.allSatisfy { Array(matches[1...]).contains($0) })
} else {
XCTAssertEqual(inclusionFlag == "with" ? messages.count : 0, messagesWithTypes.count)
}
}

Match(["And"], "^history response contains messages (with|without) ('(.*)' and '(.*)' )?types$") { args, _ in
guard let matches = args, let inclusionFlag = matches.first else {
XCTAssertNotNil(args?.first, "Step match failed")
return
switch self.currentScenario?.name {
case "Client can fetch history without customMessageType enabled by default":
messagesWithTypes = messages.compactMap { $0.customMessageType }
case "Client can fetch history with customMessageType":
messagesWithTypes = messages.compactMap { $0.customMessageType }
case "Client can fetch history with message types":
messagesWithTypes = messages.compactMap { String(describing: $0.messageType.rawValue) }
default:
XCTFail("Unexpected condition")
}

guard let lastResult = self.lastResult() else {
XCTAssert(false, "Fetch history didn't returned response")
return
}

guard let result = lastResult as? (messagesByChannel: [String: [PubNubMessageBase]], next: PubNubBoundedPage?),
let channel = result.messagesByChannel.first?.key, let messages = result.messagesByChannel[channel] else {
XCTAssert(false, "Fetch history returned unexpected response")
return
}

XCTAssertGreaterThan(messages.count, 0)

let messagesWithTypes = messages.compactMap { $0.customMessageType }
XCTAssertFalse(inclusionFlag == "with" && messagesWithTypes.count == 0)
XCTAssertFalse(inclusionFlag == "without" && messagesWithTypes.count > 0)

if matches.count > 1 {
XCTAssertTrue(messagesWithTypes.map { $0 }.allSatisfy { Array(matches[1...]).contains($0) })
XCTAssertTrue(messagesWithTypes.map { String(describing: $0.rawValue) }.allSatisfy { Array(matches[1...]).contains($0) })
} else {
XCTAssertEqual(inclusionFlag == "with" ? messages.count : 0, messagesWithTypes.count)
}
Expand All @@ -118,6 +98,33 @@ public class PubNubHistoryContractTestSteps: PubNubContractTestCase {

self.wait(for: [historyExpect], timeout: 60.0)
}

When("^I fetch message history with (messageType|customMessageType) for '(.*)' channel$") { args, _ in
guard let type = args?.first, let channel = args?.last else {
XCTAssertNotNil(args?.first, "Step match failed")
return
}

let historyExpect = self.expectation(description: "Fetch history Response")
let includeMessageType = true
let includeCustomMessageType = type == "customMessageType"

self.client.fetchMessageHistory(
for: [channel],
includeMessageType: includeMessageType,
includeCustomMessageType: includeCustomMessageType
) { result in
switch result {
case let .success((messagesByChannel, next)):
self.handleResult(result: (messagesByChannel, next))
case let .failure(error):
self.handleResult(result: error)
}
historyExpect.fulfill()
}

self.wait(for: [historyExpect], timeout: 60.0)
}

Then("the response contains pagination info") { _, _ in
guard let lastResult = self.lastResult() else {
Expand Down

0 comments on commit 458e1a6

Please sign in to comment.