Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions Tests/AsyncAwait/ChannelAsyncIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import XCTest

@testable import PubNubSwiftChatSDK

class ChannelIntegrationTests: BaseAsyncIntegrationTestCase {
class ChannelAsyncIntegrationTests: BaseAsyncIntegrationTestCase {
var channel: ChannelImpl!

override func customSetup() async throws {
Expand Down Expand Up @@ -46,16 +46,29 @@ class ChannelIntegrationTests: BaseAsyncIntegrationTestCase {
}

func testChannelAsync_Delete() async throws {
_ = try await channel.delete(soft: false)
let retrievedChannel = try await chat.getChannel(channelId: channel.id)
let someChannel = try await chat.createChannel(id: randomString())
let removalResult = try await someChannel.delete(soft: false)
let retrievedChannel = try await chat.getChannel(channelId: someChannel.id)

XCTAssertNil(removalResult)
XCTAssertNil(retrievedChannel)

addTeardownBlock { [unowned self] in
_ = try? await chat.deleteChannel(id: someChannel.id)
}
}

func testChannelAsync_SoftDelete() async throws {
_ = try await channel.delete(soft: true)
let retrievedChannel = try await chat.getChannel(channelId: channel.id)
XCTAssertNotNil(retrievedChannel)
XCTAssertEqual(retrievedChannel?.id, channel.id)
let someChannel = try await chat.createChannel(id: randomString())
let removalResult = try await someChannel.delete(soft: true)
let retrievedChannel = try await chat.getChannel(channelId: someChannel.id)

XCTAssertNotNil(removalResult)
XCTAssertEqual(retrievedChannel?.id, someChannel.id)

addTeardownBlock { [unowned self] in
_ = try? await chat.deleteChannel(id: someChannel.id)
}
}

func testChannelAsync_Forward() async throws {
Expand Down Expand Up @@ -410,8 +423,6 @@ class ChannelIntegrationTests: BaseAsyncIntegrationTestCase {
expectation.assertForOverFulfill = true
expectation.expectedFulfillmentCount = 1

_ = try await chat.createUser(user: UserImpl(chat: chat, id: randomString()))

let anotherUser = try await chat.createUser(user: UserImpl(chat: chat, id: randomString()))
try await Task.sleep(nanoseconds: 3_000_000_000)

Expand All @@ -435,6 +446,7 @@ class ChannelIntegrationTests: BaseAsyncIntegrationTestCase {
}

await fulfillment(of: [expectation], timeout: 6)

addTeardownBlock { [unowned self] in
task.cancel()
_ = try? await chat.deleteUser(id: anotherUserId)
Expand Down
77 changes: 70 additions & 7 deletions Tests/AsyncAwait/ChatAsyncIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase {

func testChatAsync_GetUsers() async throws {
let user = try await chat.createUser(id: randomString())
let users = try await chat.getUsers(limit: 10)
let secondUser = try await chat.createUser(id: randomString())
let getUsersResponse = try await chat.getUsers(filter: "id LIKE 'swift-chat*'")

XCTAssertFalse(
users.users.isEmpty
XCTAssertEqual(
Set(getUsersResponse.users.map { $0.id }),
Set([user.id, secondUser.id])
)

addTeardownBlock { [unowned self] in
_ = try? await chat.deleteUser(id: user.id)
_ = try? await chat.deleteUser(id: secondUser.id)
}
}

Expand Down Expand Up @@ -97,6 +101,30 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase {
}
}

func testChatAsync_GetUsersWithPagination() async throws {
let user1 = try await chat.createUser(id: randomString(), name: "User1")
let user2 = try await chat.createUser(id: randomString(), name: "User2")
let user3 = try await chat.createUser(id: randomString(), name: "User3")

let firstPageResponse = try await chat.getUsers(filter: "id LIKE 'swift-chat*'", limit: 2)
let secondPageResponse = try await chat.getUsers(filter: "id LIKE 'swift-chat*'", page: firstPageResponse.page)

XCTAssertTrue(firstPageResponse.users.count == 2)
XCTAssertTrue(secondPageResponse.users.count == 1)

let idsFromFirstPage = Set(firstPageResponse.users.map { $0.id })
let idsFromSecondPage = Set(secondPageResponse.users.map { $0.id })

XCTAssertTrue(idsFromFirstPage.isDisjoint(with: idsFromSecondPage))
XCTAssertEqual(idsFromSecondPage.union(idsFromFirstPage), Set([user1.id, user2.id, user3.id]))

addTeardownBlock { [unowned self] in
_ = try? await chat.deleteUser(id: user1.id)
_ = try? await chat.deleteUser(id: user2.id)
_ = try? await chat.deleteUser(id: user3.id)
}
}

func testChatAsync_WherePresent() async throws {
let channelId = randomString()
let channel = try await chat.createChannel(id: channelId, name: channelId)
Expand Down Expand Up @@ -168,18 +196,29 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase {
let channel = try await chat.createChannel(id: randomString(), name: "ChannelName")
let retrievedChannel = try await chat.getChannel(channelId: channel.id)

XCTAssertEqual(retrievedChannel?.id, channel.id)
XCTAssertEqual(
retrievedChannel?.id,
channel.id
)

addTeardownBlock { [unowned self] in
_ = try? await chat.deleteChannel(id: channel.id)
}
}

func testChatAsync_GetChannels() async throws {
let channel = try await chat.createChannel(id: randomString())
let retrievedChannels = try await chat.getChannels()
let secondChannel = try await chat.createChannel(id: randomString())
let getChannelsResponse = try await chat.getChannels(filter: "id LIKE 'swift-chat*'")

XCTAssertFalse(
retrievedChannels.channels.isEmpty
XCTAssertEqual(
Set(getChannelsResponse.channels.map { $0.id }),
Set([channel.id, secondChannel.id])
)

addTeardownBlock { [unowned self] in
_ = try? await chat.deleteChannel(id: channel.id)
_ = try? await chat.deleteChannel(id: secondChannel.id)
}
}

Expand Down Expand Up @@ -221,6 +260,30 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase {
}
}

func testChatAsync_GetChannelsWithPagination() async throws {
let channel1 = try await chat.createChannel(id: randomString(), name: "Channel1")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't channel ids have prefix "swift-chat"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The randomString() has an optional withPrefix: argument (default: true) that prepends swift-chat. There's no need to pass it explicitly unless overriding the default. I will extract swift-chat as a constant to not to repeat it in many places

let channel2 = try await chat.createChannel(id: randomString(), name: "Channel2")
let channel3 = try await chat.createChannel(id: randomString(), name: "Channel3")

let firstPageResponse = try await chat.getChannels(filter: "id LIKE 'swift-chat*'", limit: 2)
let secondPageResponse = try await chat.getChannels(filter: "id LIKE 'swift-chat*'", page: firstPageResponse.page)

XCTAssertTrue(firstPageResponse.channels.count == 2)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clearer failures wouldn't
XCTAssertEqual(count, 2)
be better choice ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will change it

XCTAssertTrue(secondPageResponse.channels.count == 1)

let idsFromFirstPage = Set(firstPageResponse.channels.map { $0.id })
let idsFromSecondPage = Set(secondPageResponse.channels.map { $0.id })

XCTAssertTrue(idsFromFirstPage.isDisjoint(with: idsFromSecondPage))
XCTAssertEqual(idsFromSecondPage.union(idsFromFirstPage), Set([channel1.id, channel2.id, channel3.id]))

addTeardownBlock { [unowned self] in
_ = try? await chat.deleteChannel(id: channel1.id)
_ = try? await chat.deleteChannel(id: channel2.id)
_ = try? await chat.deleteChannel(id: channel3.id)
}
}

func testChatAsync_WhoIsPresent() async throws {
let channel = try await chat.createChannel(
id: randomString(),
Expand Down
16 changes: 9 additions & 7 deletions Tests/AsyncAwait/UserAsyncIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,22 @@ class UserAsyncIntegrationTests: BaseAsyncIntegrationTestCase {
let deletedUser = try await createdUser.delete(soft: false)

XCTAssertNil(deletedUser)

addTeardownBlock { [unowned self] in
_ = try? await chat.deleteUser(id: createdUser.id)
}
}

func testUserAsync_SoftDelete() async throws {
let createdUser = try await chat.createUser(user: testableUser())
let deletedUser = try await createdUser.delete(soft: true)

XCTAssertFalse(
deletedUser?.active ?? true
)
XCTAssertFalse(deletedUser?.active ?? true)
XCTAssertEqual(createdUser.id, deletedUser?.id)

XCTAssertEqual(
createdUser.id,
deletedUser?.id
)
addTeardownBlock { [unowned self] in
_ = try? await chat.deleteUser(id: createdUser.id)
}
}

func testUserAsync_DeleteNotExistingUser() async throws {
Expand Down
5 changes: 3 additions & 2 deletions Tests/BaseIntegrationTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class BaseIntegrationTestCase: XCTestCase {
}

extension BaseIntegrationTestCase {
func randomString(length: Int = 6) -> String {
RandomStringGenerator().randomString(length: length)
func randomString(length: Int = 6, withPrefix: Bool = true) -> String {
let randomStr = RandomStringGenerator().randomString(length: length)
return withPrefix ? "swift-chat" + randomStr : randomStr
}
}
44 changes: 38 additions & 6 deletions Tests/ChannelIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,69 @@ class ChannelIntegrationTests: BaseClosureIntegrationTestCase {
}

func testChannel_Delete() throws {
let someChannel = try awaitResultValue {
chat.createChannel(
id: randomString(),
completion: $0
)
}
try awaitResult {
channel.delete(
someChannel.delete(
soft: false,
completion: $0
)
}
XCTAssertNil(
try awaitResultValue {
chat.getChannel(
channelId: channel.id,
channelId: someChannel.id,
completion: $0
)
}
)

addTeardownBlock { [unowned self] in
try awaitResult {
chat.deleteChannel(
id: someChannel.id,
completion: $0
)
}
}
}

func testChannel_SoftDelete() throws {
let someChannel = try awaitResultValue {
chat.createChannel(
id: randomString(),
completion: $0
)
}

try awaitResult {
channel.delete(
someChannel.delete(
soft: true,
completion: $0
)
}
let retrievedChannel = try awaitResultValue {
chat.getChannel(
channelId: channel.id,
channelId: someChannel.id,
completion: $0
)
}

XCTAssertNotNil(retrievedChannel)
XCTAssertEqual(retrievedChannel?.id, channel.id)
XCTAssertFalse(retrievedChannel?.active ?? true)
XCTAssertEqual(retrievedChannel?.id, someChannel.id)

addTeardownBlock { [unowned self] in
try awaitResult {
chat.deleteChannel(
id: someChannel.id,
completion: $0
)
}
}
}

func testChannel_Forward() throws {
Expand Down Expand Up @@ -749,6 +780,7 @@ class ChannelIntegrationTests: BaseClosureIntegrationTestCase {
for: [expectation],
timeout: 6
)

addTeardownBlock { [unowned self] in
closeable.close()
try awaitResult {
Expand Down
Loading