Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIS-259] Implement teams #905

Merged
merged 9 commits into from Mar 24, 2021
Merged

[CIS-259] Implement teams #905

merged 9 commits into from Mar 24, 2021

Conversation

DominikBucher12
Copy link
Contributor

@DominikBucher12 DominikBucher12 commented Mar 22, 2021

What this PR do:

  • Adds support for user teams and channel team inside those payloads

How to test it

  • Run tests if teams property is parsed from payload.

Where you can start

  • UserPayload and ChannelListPayload, check if there is correct relationship between objects.

@codecov
Copy link

codecov bot commented Mar 22, 2021

Codecov Report

Merging #905 (000e5bb) into main (1dec12d) will increase coverage by 1.90%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #905      +/-   ##
==========================================
+ Coverage   87.11%   89.01%   +1.90%     
==========================================
  Files         238      200      -38     
  Lines        9147     8130    -1017     
==========================================
- Hits         7968     7237     -731     
+ Misses       1179      893     -286     
Flag Coverage Δ
llc-tests 89.01% <100.00%> (+1.90%) ⬆️
llc-tests-ios12 89.01% <100.00%> (+1.90%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...lient/Endpoints/Payloads/CurrentUserPayloads.swift 92.30% <ø> (ø)
...at/APIClient/Endpoints/Payloads/UserPayloads.swift 100.00% <ø> (ø)
Sources/StreamChat/Models/User.swift 89.47% <ø> (ø)
...Client/Endpoints/Payloads/ChannelListPayload.swift 100.00% <100.00%> (ø)
Sources/StreamChat/Database/DTOs/ChannelDTO.swift 100.00% <100.00%> (ø)
Sources/StreamChat/Database/DTOs/TeamDTO.swift 100.00% <100.00%> (ø)
Sources/StreamChat/Database/DTOs/UserDTO.swift 96.00% <100.00%> (+0.08%) ⬆️
Sources/StreamChat/Models/Channel.swift 78.94% <100.00%> (ø)
...sts/StreamChatTests/DummyData/DevicePayloads.swift
...ests/StreamChatTests/DummyData/MemberPayload.swift
... and 35 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1dec12d...000e5bb. Read the comment docs.

Copy link
Contributor

@VojtaStavik VojtaStavik left a comment

Choose a reason for hiding this comment

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

Ideally, the tests should be in the same commit as the implementation, so it's easy to verify what's happening there.

It seems there are no tests for DTOs, correct? I see only tests for the payload serialization.

@DominikBucher12 DominikBucher12 added the 🌐 SDK: StreamChat (LLC) Tasks related to the StreamChat LLC SDK label Mar 23, 2021
Copy link
Member

@nuno-vieira nuno-vieira left a comment

Choose a reason for hiding this comment

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

Great Job 💪 Just added some questions 👍

Sources/StreamChat/Models/User.swift Outdated Show resolved Hide resolved
Copy link
Contributor

@VojtaStavik VojtaStavik left a comment

Choose a reason for hiding this comment

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

Almost there... just some nits

Sources/StreamChat/Database/DTOs/TeamDTO_Tests.swift Outdated Show resolved Hide resolved
Sources/StreamChat/Database/DTOs/TeamDTO_Tests.swift Outdated Show resolved Hide resolved
Sources/StreamChat/Database/DTOs/TeamDTO_Tests.swift Outdated Show resolved Hide resolved
Sources/StreamChat/Database/DTOs/TeamDTO_Tests.swift Outdated Show resolved Hide resolved
Sources/StreamChat/Database/DTOs/TeamDTO_Tests.swift Outdated Show resolved Hide resolved

let teamsArray: [TeamDTO] = try payload.teams.map {
let team = try saveTeam(teamId: $0)
team.users.insert(dto)
Copy link
Contributor

Choose a reason for hiding this comment

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

this is IMO not needed. The relationship is already set on L120

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay this is probably my gap in CoreData knowledge... But now I see you are right. It handles automatically... Thanks

Copy link
Contributor

Choose a reason for hiding this comment

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

So will you remove it then? :trollface:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed :trollface:

Sources/StreamChat/Database/DTOs/ChannelDTO.swift Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
Sources/StreamChat/Database/DTOs/ChannelDTO.swift Outdated Show resolved Hide resolved
Comment on lines 21 to 39
func test_teamsForUser_areStoredAndLoadedFromDB() {
let teamIds: [TeamId] = [.unique, .unique, .unique]
let userId: UserId = .unique

let payload: UserPayload<NoExtraData> = .dummy(userId: userId, teams: teamIds)

// Asynchronously save the user payload to the db, which should save teams for user as well.
try! database.writeSynchronously { session in
try! session.saveUser(payload: payload)
}

// Load the user from the db and check the fields are correct
var loadedUserDTO: UserDTO? {
database.viewContext.user(id: userId)
}

XCTAssertEqual(payload.teams.count, loadedUserDTO?.teams?.count)
XCTAssertEqual(loadedUserDTO?.teams?.first?.users.first, loadedUserDTO)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be among UserDTO tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved.

Comment on lines 41 to 59
func test_teamForChannel_isStoredAndLoadedFromDB() {
let teamId: TeamId = .unique
let channelId: ChannelId = .unique

let channelDetailPayload: ChannelDetailPayload<NoExtraData> = .dummy(cid: channelId, teamId: teamId)

// Asynchronously save the channel payload to the db, which should save the team for the channel as well.
try! database.writeSynchronously { session in
try! session.saveChannel(payload: channelDetailPayload, query: nil)
}

// Load the channel from the db and check the team is correct
var loadedChannelDTO: ChannelDTO? {
database.viewContext.channel(cid: channelId)
}

XCTAssertEqual(channelDetailPayload.team, loadedChannelDTO?.team?.id)
XCTAssertEqual(loadedChannelDTO?.team?.channels.first, loadedChannelDTO)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be among ChannelDTO tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved... and deleted file 🧙🏻‍♂️

Comment on lines 28 to 30
try! database.writeSynchronously { session in
try! session.saveUser(payload: payload)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If you mark the test throws you can change try! to try

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Comment on lines 33 to 35
var loadedUserDTO: UserDTO? {
database.viewContext.user(id: userId)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

let loadedUser = try XCTUnwrap(database.viewContext.user(id: userId))

would work better here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Damn that copypasting... Changed.


let teamsArray: [TeamDTO] = try payload.teams.map {
let team = try saveTeam(teamId: $0)
team.users.insert(dto)
Copy link
Contributor

Choose a reason for hiding this comment

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

So will you remove it then? :trollface:

static func dummy(cid: ChannelId) -> ChannelDetailPayload {
static func dummy(cid: ChannelId, teamId: TeamId? = nil) -> ChannelDetailPayload {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if this is needed tbh :) Why no just assign .unique to the team variable like we do it with other things?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I guess you are right, I wanted to have control over with which teams I creat and assign to dummy, but I guess it needs to be known only for cid. Thanks fixed.

@@ -43,6 +44,8 @@ class UserDTO_Tests: XCTestCase {
Assert.willBeEqual(payload.createdAt, loadedUserDTO?.userCreatedAt)
Assert.willBeEqual(payload.updatedAt, loadedUserDTO?.userUpdatedAt)
Assert.willBeEqual(payload.lastActiveAt, loadedUserDTO?.lastActivityAt)
Assert.willBeEqual(payload.teams.first, loadedUserDTO?.teams?.first?.id)
Assert.willBeEqual(loadedUserDTO?.teams?.first?.users.first, loadedUserDTO)
Copy link
Contributor

Choose a reason for hiding this comment

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

What exactly are you testing here? 🤯 This is already taken care of by CoreData, we don't need to test it explicitly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I started to panic when I figured out there are relationships to the TeamDTO, so I started to manually assign the relationships and test it... Only to figure out I don't need to :D

@@ -43,6 +44,8 @@ class UserDTO_Tests: XCTestCase {
Assert.willBeEqual(payload.createdAt, loadedUserDTO?.userCreatedAt)
Assert.willBeEqual(payload.updatedAt, loadedUserDTO?.userUpdatedAt)
Assert.willBeEqual(payload.lastActiveAt, loadedUserDTO?.lastActivityAt)
Assert.willBeEqual(payload.teams.first, loadedUserDTO?.teams?.first?.id)
Copy link
Contributor

Choose a reason for hiding this comment

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

We can be more specific here a do something like:

Assert.willBeEqual(payload.teams.sorted(), loadedUserDTO?.teams?.map { $0.id }.sorted())

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, much better..

Copy link
Contributor

@VojtaStavik VojtaStavik left a comment

Choose a reason for hiding this comment

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

Cool 🎉 There's are still some strange things happening in tests. Please fix and it's good to 🚢

CHANGELOG.md Outdated
@@ -20,6 +20,7 @@ _March 12, 2021_
- Expose `membership` value on `ChatChannel` which contains information about the current user membership [#885](https://github.com/GetStream/stream-chat-swift/issues/885)
- `ChatChannelMember` now contains channel-specific ban information: `isBannedFromChannel` and `banExpiresAt` [#885](https://github.com/GetStream/stream-chat-swift/issues/885)
- Channel-specific ban events are handled and the models are properly updated [#885](https://github.com/GetStream/stream-chat-swift/pull/885)
- Introduce support for [multitenancy](https://getstream.io/chat/docs/react/multi_tenant_chat/?language=swift) - `teams` for `User` and `team` for `Channel` are now exposed.
Copy link
Contributor

Choose a reason for hiding this comment

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

The link to the PR is missing (see other CHANGELOG items please)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, fixed❗️

VojtaStavik and others added 4 commits March 24, 2021 14:09
Co-authored-by: Dominik Bucher <d.bucher@centrum.cz>
Co-authored-by: Dominik Bucher <d.bucher@centrum.cz>
Co-authored-by: Dominik Bucher <d.bucher@centrum.cz>
Co-authored-by: Dominik Bucher <d.bucher@centrum.cz>
@VojtaStavik VojtaStavik merged commit 0aeb995 into main Mar 24, 2021
@VojtaStavik VojtaStavik deleted the CIS-259-Multi_tenant branch March 24, 2021 15:58
@VojtaStavik VojtaStavik mentioned this pull request Mar 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌐 SDK: StreamChat (LLC) Tasks related to the StreamChat LLC SDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants