Skip to content

Commit be4a2cb

Browse files
authored
Add more integration tests (#36)
1 parent 9a20317 commit be4a2cb

7 files changed

+474
-38
lines changed

Tests/AsyncAwait/ChannelAsyncIntegrationTests.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import XCTest
1414

1515
@testable import PubNubSwiftChatSDK
1616

17-
class ChannelIntegrationTests: BaseAsyncIntegrationTestCase {
17+
class ChannelAsyncIntegrationTests: BaseAsyncIntegrationTestCase {
1818
var channel: ChannelImpl!
1919

2020
override func customSetup() async throws {
@@ -46,16 +46,29 @@ class ChannelIntegrationTests: BaseAsyncIntegrationTestCase {
4646
}
4747

4848
func testChannelAsync_Delete() async throws {
49-
_ = try await channel.delete(soft: false)
50-
let retrievedChannel = try await chat.getChannel(channelId: channel.id)
49+
let someChannel = try await chat.createChannel(id: randomString())
50+
let removalResult = try await someChannel.delete(soft: false)
51+
let retrievedChannel = try await chat.getChannel(channelId: someChannel.id)
52+
53+
XCTAssertNil(removalResult)
5154
XCTAssertNil(retrievedChannel)
55+
56+
addTeardownBlock { [unowned self] in
57+
_ = try? await chat.deleteChannel(id: someChannel.id)
58+
}
5259
}
5360

5461
func testChannelAsync_SoftDelete() async throws {
55-
_ = try await channel.delete(soft: true)
56-
let retrievedChannel = try await chat.getChannel(channelId: channel.id)
57-
XCTAssertNotNil(retrievedChannel)
58-
XCTAssertEqual(retrievedChannel?.id, channel.id)
62+
let someChannel = try await chat.createChannel(id: randomString())
63+
let removalResult = try await someChannel.delete(soft: true)
64+
let retrievedChannel = try await chat.getChannel(channelId: someChannel.id)
65+
66+
XCTAssertNotNil(removalResult)
67+
XCTAssertEqual(retrievedChannel?.id, someChannel.id)
68+
69+
addTeardownBlock { [unowned self] in
70+
_ = try? await chat.deleteChannel(id: someChannel.id)
71+
}
5972
}
6073

6174
func testChannelAsync_Forward() async throws {
@@ -410,8 +423,6 @@ class ChannelIntegrationTests: BaseAsyncIntegrationTestCase {
410423
expectation.assertForOverFulfill = true
411424
expectation.expectedFulfillmentCount = 1
412425

413-
_ = try await chat.createUser(user: UserImpl(chat: chat, id: randomString()))
414-
415426
let anotherUser = try await chat.createUser(user: UserImpl(chat: chat, id: randomString()))
416427
try await Task.sleep(nanoseconds: 3_000_000_000)
417428

@@ -435,6 +446,7 @@ class ChannelIntegrationTests: BaseAsyncIntegrationTestCase {
435446
}
436447

437448
await fulfillment(of: [expectation], timeout: 6)
449+
438450
addTeardownBlock { [unowned self] in
439451
task.cancel()
440452
_ = try? await chat.deleteUser(id: anotherUserId)

Tests/AsyncAwait/ChatAsyncIntegrationTests.swift

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,17 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase {
4141

4242
func testChatAsync_GetUsers() async throws {
4343
let user = try await chat.createUser(id: randomString())
44-
let users = try await chat.getUsers(limit: 10)
44+
let secondUser = try await chat.createUser(id: randomString())
45+
let getUsersResponse = try await chat.getUsers(filter: "id LIKE '\(Constants.prefix)*'")
4546

46-
XCTAssertFalse(
47-
users.users.isEmpty
47+
XCTAssertEqual(
48+
Set(getUsersResponse.users.map { $0.id }),
49+
Set([user.id, secondUser.id])
4850
)
51+
4952
addTeardownBlock { [unowned self] in
5053
_ = try? await chat.deleteUser(id: user.id)
54+
_ = try? await chat.deleteUser(id: secondUser.id)
5155
}
5256
}
5357

@@ -97,6 +101,30 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase {
97101
}
98102
}
99103

104+
func testChatAsync_GetUsersWithPagination() async throws {
105+
let user1 = try await chat.createUser(id: randomString(), name: "User1")
106+
let user2 = try await chat.createUser(id: randomString(), name: "User2")
107+
let user3 = try await chat.createUser(id: randomString(), name: "User3")
108+
109+
let firstPageResponse = try await chat.getUsers(filter: "id LIKE '\(Constants.prefix)*'", limit: 2)
110+
let secondPageResponse = try await chat.getUsers(filter: "id LIKE '\(Constants.prefix)*'", page: firstPageResponse.page)
111+
112+
XCTAssertEqual(firstPageResponse.users.count, 2)
113+
XCTAssertEqual(secondPageResponse.users.count, 1)
114+
115+
let idsFromFirstPage = Set(firstPageResponse.users.map { $0.id })
116+
let idsFromSecondPage = Set(secondPageResponse.users.map { $0.id })
117+
118+
XCTAssertTrue(idsFromFirstPage.isDisjoint(with: idsFromSecondPage))
119+
XCTAssertEqual(idsFromSecondPage.union(idsFromFirstPage), Set([user1.id, user2.id, user3.id]))
120+
121+
addTeardownBlock { [unowned self] in
122+
_ = try? await chat.deleteUser(id: user1.id)
123+
_ = try? await chat.deleteUser(id: user2.id)
124+
_ = try? await chat.deleteUser(id: user3.id)
125+
}
126+
}
127+
100128
func testChatAsync_WherePresent() async throws {
101129
let channelId = randomString()
102130
let channel = try await chat.createChannel(id: channelId, name: channelId)
@@ -168,18 +196,29 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase {
168196
let channel = try await chat.createChannel(id: randomString(), name: "ChannelName")
169197
let retrievedChannel = try await chat.getChannel(channelId: channel.id)
170198

171-
XCTAssertEqual(retrievedChannel?.id, channel.id)
199+
XCTAssertEqual(
200+
retrievedChannel?.id,
201+
channel.id
202+
)
203+
204+
addTeardownBlock { [unowned self] in
205+
_ = try? await chat.deleteChannel(id: channel.id)
206+
}
172207
}
173208

174209
func testChatAsync_GetChannels() async throws {
175210
let channel = try await chat.createChannel(id: randomString())
176-
let retrievedChannels = try await chat.getChannels()
211+
let secondChannel = try await chat.createChannel(id: randomString())
212+
let getChannelsResponse = try await chat.getChannels(filter: "id LIKE '\(Constants.prefix)*'")
177213

178-
XCTAssertFalse(
179-
retrievedChannels.channels.isEmpty
214+
XCTAssertEqual(
215+
Set(getChannelsResponse.channels.map { $0.id }),
216+
Set([channel.id, secondChannel.id])
180217
)
218+
181219
addTeardownBlock { [unowned self] in
182220
_ = try? await chat.deleteChannel(id: channel.id)
221+
_ = try? await chat.deleteChannel(id: secondChannel.id)
183222
}
184223
}
185224

@@ -221,6 +260,30 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase {
221260
}
222261
}
223262

263+
func testChatAsync_GetChannelsWithPagination() async throws {
264+
let channel1 = try await chat.createChannel(id: randomString(), name: "Channel1")
265+
let channel2 = try await chat.createChannel(id: randomString(), name: "Channel2")
266+
let channel3 = try await chat.createChannel(id: randomString(), name: "Channel3")
267+
268+
let firstPageResponse = try await chat.getChannels(filter: "id LIKE '\(Constants.prefix)*'", limit: 2)
269+
let secondPageResponse = try await chat.getChannels(filter: "id LIKE '\(Constants.prefix)*'", page: firstPageResponse.page)
270+
271+
XCTAssertEqual(firstPageResponse.channels.count, 2)
272+
XCTAssertEqual(secondPageResponse.channels.count, 1)
273+
274+
let idsFromFirstPage = Set(firstPageResponse.channels.map { $0.id })
275+
let idsFromSecondPage = Set(secondPageResponse.channels.map { $0.id })
276+
277+
XCTAssertTrue(idsFromFirstPage.isDisjoint(with: idsFromSecondPage))
278+
XCTAssertEqual(idsFromSecondPage.union(idsFromFirstPage), Set([channel1.id, channel2.id, channel3.id]))
279+
280+
addTeardownBlock { [unowned self] in
281+
_ = try? await chat.deleteChannel(id: channel1.id)
282+
_ = try? await chat.deleteChannel(id: channel2.id)
283+
_ = try? await chat.deleteChannel(id: channel3.id)
284+
}
285+
}
286+
224287
func testChatAsync_WhoIsPresent() async throws {
225288
let channel = try await chat.createChannel(
226289
id: randomString(),

Tests/AsyncAwait/UserAsyncIntegrationTests.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,22 @@ class UserAsyncIntegrationTests: BaseAsyncIntegrationTestCase {
134134
let deletedUser = try await createdUser.delete(soft: false)
135135

136136
XCTAssertNil(deletedUser)
137+
138+
addTeardownBlock { [unowned self] in
139+
_ = try? await chat.deleteUser(id: createdUser.id)
140+
}
137141
}
138142

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

143-
XCTAssertFalse(
144-
deletedUser?.active ?? true
145-
)
147+
XCTAssertFalse(deletedUser?.active ?? true)
148+
XCTAssertEqual(createdUser.id, deletedUser?.id)
146149

147-
XCTAssertEqual(
148-
createdUser.id,
149-
deletedUser?.id
150-
)
150+
addTeardownBlock { [unowned self] in
151+
_ = try? await chat.deleteUser(id: createdUser.id)
152+
}
151153
}
152154

153155
func testUserAsync_DeleteNotExistingUser() async throws {

Tests/BaseIntegrationTestCase.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ import PubNubSDK
1212
import PubNubSwiftChatSDK
1313
import XCTest
1414

15+
enum Constants {
16+
static let prefix = "swift-chat"
17+
}
18+
1519
class BaseIntegrationTestCase: XCTestCase {
1620
lazy var chat: ChatImpl! = IntegrationTestCaseConfiguration.createChatObject()
1721
}
1822

1923
extension BaseIntegrationTestCase {
20-
func randomString(length: Int = 6) -> String {
21-
RandomStringGenerator().randomString(length: length)
24+
func randomString(length: Int = 6, withPrefix: Bool = true) -> String {
25+
let randomStr = RandomStringGenerator().randomString(length: length)
26+
return withPrefix ? Constants.prefix + randomStr : randomStr
2227
}
2328
}

Tests/ChannelIntegrationTests.swift

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,38 +58,69 @@ class ChannelIntegrationTests: BaseClosureIntegrationTestCase {
5858
}
5959

6060
func testChannel_Delete() throws {
61+
let someChannel = try awaitResultValue {
62+
chat.createChannel(
63+
id: randomString(),
64+
completion: $0
65+
)
66+
}
6167
try awaitResult {
62-
channel.delete(
68+
someChannel.delete(
6369
soft: false,
6470
completion: $0
6571
)
6672
}
6773
XCTAssertNil(
6874
try awaitResultValue {
6975
chat.getChannel(
70-
channelId: channel.id,
76+
channelId: someChannel.id,
7177
completion: $0
7278
)
7379
}
7480
)
81+
82+
addTeardownBlock { [unowned self] in
83+
try awaitResult {
84+
chat.deleteChannel(
85+
id: someChannel.id,
86+
completion: $0
87+
)
88+
}
89+
}
7590
}
7691

7792
func testChannel_SoftDelete() throws {
93+
let someChannel = try awaitResultValue {
94+
chat.createChannel(
95+
id: randomString(),
96+
completion: $0
97+
)
98+
}
99+
78100
try awaitResult {
79-
channel.delete(
101+
someChannel.delete(
80102
soft: true,
81103
completion: $0
82104
)
83105
}
84106
let retrievedChannel = try awaitResultValue {
85107
chat.getChannel(
86-
channelId: channel.id,
108+
channelId: someChannel.id,
87109
completion: $0
88110
)
89111
}
90112

91-
XCTAssertNotNil(retrievedChannel)
92-
XCTAssertEqual(retrievedChannel?.id, channel.id)
113+
XCTAssertFalse(retrievedChannel?.active ?? true)
114+
XCTAssertEqual(retrievedChannel?.id, someChannel.id)
115+
116+
addTeardownBlock { [unowned self] in
117+
try awaitResult {
118+
chat.deleteChannel(
119+
id: someChannel.id,
120+
completion: $0
121+
)
122+
}
123+
}
93124
}
94125

95126
func testChannel_Forward() throws {
@@ -749,6 +780,7 @@ class ChannelIntegrationTests: BaseClosureIntegrationTestCase {
749780
for: [expectation],
750781
timeout: 6
751782
)
783+
752784
addTeardownBlock { [unowned self] in
753785
closeable.close()
754786
try awaitResult {

0 commit comments

Comments
 (0)