-
Notifications
You must be signed in to change notification settings - Fork 259
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
task(tests): introduce prebuilt mocks and mocking helpers #4197
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4197 +/- ##
=======================================
Coverage ? 84.90%
=======================================
Files ? 274
Lines ? 29677
Branches ? 0
=======================================
Hits ? 25196
Misses ? 4481
Partials ? 0 ☔ View full report in Codecov by Sentry. |
2c8e654
to
e9016b2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great. Passing on to @richvdh because a) I think he will have helpful feedback and b) I'm not available next week.
/// Overrides the sync/ endpoint with knowledge that the given | ||
/// invited/joined/knocked/left room exists, runs a sync and returns the | ||
/// given room. | ||
pub async fn sync_room(&self, room_id: &RoomId, room_data: impl Into<AnyRoomBuilder>) -> Room { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to think of a more informative name for this...
Maybe create_or_change_room
? yuck.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mock_and_run_sync_for_room
? 🥴
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some thoughts. Looks like a great improvement.
e9016b2
to
cfc43f4
Compare
Should be ready for another round of review! I'm planning to use this for writing more tests for media uploads. |
/// Mocks a sync endpoint. | ||
pub fn mock_sync(&self) -> MockSync<'_> { | ||
let mock = Mock::given(method("GET")) | ||
.and(path("/_matrix/client/r0/sync")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aside: shouldn't we be using /v3/
endpoints? /r0/
is supported by synapse for compatibility with old applications, but new codebases shouldn't be using it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah the default Client is configured with no information about matrix versions, so it supposes the first version by default. It usually doesn't matter, except in the few cases where it does, of course. I'll ask about this in the Rust room, but i'm tempted to keep it like this right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In reality we do use the /v3
endpoints, but in tests we don't bother filling out the supported version in the Client
and as @bnjbvr says, it falls back to a default version.
We could modify the constructor of the test Client
object to have the versions prefilled, but that will automatically mean that you need to update all tests that are using this test client.
I would say that we should do that, but not as part of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm 🎉
… instead of embedding one into the struct
a9f3928
to
8e73592
Compare
This introduces new test utilities, starting with
MatrixMockServer
. This is a pair of aMockServer
and aClient
(one can retrieve them respectively with.server()
and.client()
).It implements a few mock endpoints, expanding and limited the shared code as much as possible, so the mocks are still flexible to use as scoped/unscoped mounts, named, and so on.
It works like this:
mock_room_send()
. This returns a specializedMockSomething
data structure, with its own impl. For this example, it'sMockRoomSend
.MockRoomSend::error500()
; if you want it to succeed and return the event $42, callMockRoomSend::ok(event_id!("$42"))
. It's still possible to callrespond_with()
, as we do with wiremock MockBuilder, for maximum flexibility when the helpers aren't sufficient.MatrixMock
; this is a plainwiremock::Mock
with the server curried, so one doesn't have to pass it around when calling.mount()
or.mount_as_scoped()
. As such, it mostly defers its implementations towiremock::Mock
under the hood.Example
With this, we can go from that code:
To that code: