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

Refactor the widget tests to use MockMatrixServer #4236

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

toger5
Copy link
Contributor

@toger5 toger5 commented Nov 8, 2024

This is a follow up PR on: #3987
And tries to use the MockMatrixServer wherever reasonable in the widget integration tests.

  • Public API changes documented in changelogs (optional)

Signed-off-by:

Base automatically changed from toger5/widget-driver-redactions to main November 8, 2024 13:21
@toger5 toger5 force-pushed the toger5/mock-matrix-server-widget-tests branch from b0790c0 to a628d83 Compare November 8, 2024 15:25
Copy link

codecov bot commented Nov 8, 2024

Codecov Report

Attention: Patch coverage is 76.31579% with 9 lines in your changes missing coverage. Please review.

Project coverage is 85.08%. Comparing base (079ec02) to head (62a1fcf).
Report is 26 commits behind head on main.

Files with missing lines Patch % Lines
crates/matrix-sdk/src/test_utils/mocks.rs 76.31% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4236      +/-   ##
==========================================
- Coverage   85.10%   85.08%   -0.02%     
==========================================
  Files         274      274              
  Lines       30203    30240      +37     
==========================================
+ Hits        25703    25729      +26     
- Misses       4500     4511      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@poljar poljar changed the title Toger5/mock-matrix-server-widget-tests Refactor the widget tests to use MockMatrixServer Nov 8, 2024
@toger5 toger5 force-pushed the toger5/mock-matrix-server-widget-tests branch from a628d83 to b345d64 Compare November 13, 2024 16:02
@toger5
Copy link
Contributor Author

toger5 commented Nov 13, 2024

How do i test /test_utils/mocks.rs?

@toger5 toger5 force-pushed the toger5/mock-matrix-server-widget-tests branch from b345d64 to c573c2d Compare November 13, 2024 16:11
@bnjbvr
Copy link
Member

bnjbvr commented Nov 13, 2024

How do i test /test_utils/mocks.rs?

We usually don't test the test utilities; they should be straightforward enough that using them in tests would reveal their issues.

@toger5 toger5 force-pushed the toger5/mock-matrix-server-widget-tests branch from c573c2d to e8d57ca Compare November 13, 2024 16:18
@poljar
Copy link
Contributor

poljar commented Nov 13, 2024

How do i test /test_utils/mocks.rs?

Examples in the docstrings will be run as the doc tests which can run and test the mocks. At least that's how I wrote all the examples for the MatrixMockServer.

@toger5 toger5 marked this pull request as ready for review November 13, 2024 19:15
@toger5 toger5 requested a review from a team as a code owner November 13, 2024 19:15
@toger5 toger5 requested review from Hywan, bnjbvr and poljar and removed request for a team and bnjbvr November 13, 2024 19:15
@poljar poljar removed the request for review from Hywan November 14, 2024 16:13
Copy link
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

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

Thanks for working on this. Left a couple of nits about linkifying doc references and a couple more examples would be neat.

crates/matrix-sdk/src/test_utils/mocks.rs Outdated Show resolved Hide resolved
crates/matrix-sdk/src/test_utils/mocks.rs Outdated Show resolved Hide resolved
crates/matrix-sdk/src/test_utils/mocks.rs Outdated Show resolved Hide resolved
crates/matrix-sdk/src/test_utils/mocks.rs Outdated Show resolved Hide resolved
crates/matrix-sdk/src/test_utils/mocks.rs Outdated Show resolved Hide resolved
crates/matrix-sdk/src/test_utils/mocks.rs Outdated Show resolved Hide resolved
crates/matrix-sdk/src/test_utils/mocks.rs Outdated Show resolved Hide resolved
crates/matrix-sdk/src/test_utils/mocks.rs Outdated Show resolved Hide resolved
crates/matrix-sdk/src/test_utils/mocks.rs Outdated Show resolved Hide resolved
crates/matrix-sdk/src/test_utils/mocks.rs Outdated Show resolved Hide resolved
crates/matrix-sdk/src/test_utils/mocks.rs Outdated Show resolved Hide resolved
impl<'a> MockEndpoint<'a, RoomMessagesEndpoint> {
/// Returns a messages endpoint that emulates success, i.e. the messages
/// provided as `response` could be retrieved.
pub fn ok(self, response: impl Into<Value>) -> MatrixMock<'a> {
Copy link
Member

Choose a reason for hiding this comment

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

No, we should not use impl Into<Value>, otherwise we lose all the usefulness of these mocking APIs by reverting to "we don't know what the return value looks like, it can be an arbitrary JSON". Instead we should use high-level types. We have another place where we do mock messages, grep for fn mock_messages (…it's even better, we do have two implementations :D).

@toger5 toger5 force-pushed the toger5/mock-matrix-server-widget-tests branch from 2df7183 to 0af992b Compare November 18, 2024 19:45
@toger5 toger5 force-pushed the toger5/mock-matrix-server-widget-tests branch 4 times, most recently from 40276b7 to a7d0264 Compare November 25, 2024 11:40
@toger5 toger5 force-pushed the toger5/mock-matrix-server-widget-tests branch from a7d0264 to 5d5bec4 Compare November 25, 2024 14:47
@toger5 toger5 requested review from bnjbvr and poljar November 25, 2024 15:47
Copy link
Member

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

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

Super nice, thanks. Feel free to address the last comments, or let me know if you're bored with this PR, and then I can do it myself as a followup :)

crates/matrix-sdk/src/test_utils/mocks.rs Show resolved Hide resolved
crates/matrix-sdk/src/test_utils/mocks.rs Show resolved Hide resolved
crates/matrix-sdk/src/test_utils/mocks.rs Outdated Show resolved Hide resolved
crates/matrix-sdk/src/test_utils/mocks.rs Show resolved Hide resolved
crates/matrix-sdk/src/test_utils/mocks.rs Show resolved Hide resolved
crates/matrix-sdk/src/test_utils/mocks.rs Outdated Show resolved Hide resolved
Comment on lines +1087 to +1089
let endpoint = RoomSendStateEndpoint { event_type: Some(event_type), ..self.endpoint };
let matcher = path_regex(Self::generate_path_regex(&endpoint));
Self { endpoint, mock: self.mock.and(matcher), ..self }
Copy link
Member

Choose a reason for hiding this comment

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

I think here it would be better to generate the path regex at the end, just before building the final MatrixMock.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Where would that be?
There are numerous places we build the final MatrixMock. Do you want to implement all the methods from MockEndpoint<'a, T> in
MockEndpoint<'a, RoomSendStateEndpoint>. What if someone adds a new method to the generic version that can build a matrix mock but forgets to update this version to build the regex?

Comment on lines +1146 to +1148
let endpoint = RoomSendStateEndpoint { state_key: Some(state_key), ..self.endpoint };
let matcher = path_regex(Self::generate_path_regex(&endpoint));
Self { endpoint, mock: self.mock.and(matcher), ..self }
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, same here: we should fill the state_key field in RoomSendStateEndpoint, but not make the matcher more precise there.

let matcher = path_regex(Self::generate_path_regex(&endpoint));
Self { endpoint, mock: self.mock.and(matcher), ..self }
}

Copy link
Member

Choose a reason for hiding this comment

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

note: doc comment for the ok_with_event_id is incorrect, as it duplicates the doc comment for mock_room_send without any change in the example code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean the doc comment for ok?
I suppose this is still helpful to have for ok. What other test du you have in mind?

crates/matrix-sdk/src/test_utils/mocks.rs Show resolved Hide resolved
@toger5 toger5 force-pushed the toger5/mock-matrix-server-widget-tests branch from 966bf1c to 62a1fcf Compare November 26, 2024 17:35
@bnjbvr bnjbvr self-requested a review November 28, 2024 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants