test: snapshot the generated object rather than the JSON

We're going to be switching away from JSON-twiddling, so let's snapshot the
real object rather than the JSON.
This commit is contained in:
Richard van der Hoff
2025-01-14 12:40:38 +00:00
committed by Richard van der Hoff
parent fedf7d214f
commit c9bac4ff2b
5 changed files with 40 additions and 18 deletions

View File

@@ -22,8 +22,9 @@ insta = { workspace = true }
matrix-sdk-common = { path = "../../crates/matrix-sdk-common" }
matrix-sdk-test-macros = { version = "0.7.0", path = "../matrix-sdk-test-macros" }
once_cell = { workspace = true }
# Enabling the unstable feature for polls support.
ruma = { workspace = true, features = ["rand", "unstable-msc3381"] }
# Enable the unstable feature for polls support.
# "client-api-s" enables need the "server" feature of ruma-client-api, which is needed to serialize Response objects to JSON.
ruma = { workspace = true, features = ["client-api-s", "rand", "unstable-msc3381"] }
serde = { workspace = true }
serde_json = { workspace = true }

View File

@@ -2,7 +2,9 @@ use http::Response;
pub use matrix_sdk_test_macros::async_test;
use once_cell::sync::Lazy;
use ruma::{
api::{client::sync::sync_events::v3::Response as SyncResponse, IncomingResponse},
api::{
client::sync::sync_events::v3::Response as SyncResponse, IncomingResponse, OutgoingResponse,
},
room_id, user_id, RoomId, UserId,
};
use serde_json::Value as JsonValue;
@@ -169,3 +171,13 @@ pub fn ruma_response_from_json<ResponseType: IncomingResponse>(
Response::builder().status(200).body(json_bytes).expect("Failed to build HTTP response");
ResponseType::try_from_http_response(http_response).expect("Can't parse the response json")
}
/// Serialise a typed Ruma [`OutgoingResponse`] object to JSON.
pub fn ruma_response_to_json<ResponseType: OutgoingResponse>(
response: ResponseType,
) -> serde_json::Value {
let http_response: Response<Vec<u8>> =
response.try_into_http_response().expect("Failed to build HTTP response");
let json_bytes = http_response.into_body();
serde_json::from_slice(&json_bytes).expect("Can't parse the response JSON")
}

View File

@@ -7,7 +7,7 @@ use serde_json::{json, Value};
use super::keys_query::{keys_query, master_keys, KeysQueryUser};
use crate::{
ruma_response_from_json,
ruma_response_from_json, ruma_response_to_json,
test_json::keys_query::{device_keys_payload, self_signing_keys},
};
@@ -99,10 +99,14 @@ impl KeyDistributionTestData {
}
});
let response: KeyQueryResponse = ruma_response_from_json(&data);
with_settings!({sort_maps => true}, {
assert_json_snapshot!("KeyDistributionTestData::me_keys_query_response", data);
assert_json_snapshot!(
"KeyDistributionTestData::me_keys_query_response",
ruma_response_to_json(response.clone()),
);
});
ruma_response_from_json(&data)
response
}
/// Dan has cross-signing setup, one device is cross signed `JHPUERYQUW`,
@@ -204,11 +208,14 @@ impl KeyDistributionTestData {
}
});
let response: KeyQueryResponse = ruma_response_from_json(&data);
with_settings!({sort_maps => true}, {
assert_json_snapshot!("KeyDistributionTestData::dan_keys_query_response", data);
assert_json_snapshot!(
"KeyDistributionTestData::dan_keys_query_response",
ruma_response_to_json(response.clone()),
);
});
ruma_response_from_json(&data)
response
}
/// Same as `dan_keys_query_response` but `FRGNMZVOKA` was removed.
@@ -291,14 +298,14 @@ impl KeyDistributionTestData {
}
});
let response: KeyQueryResponse = ruma_response_from_json(&data);
with_settings!({sort_maps => true}, {
assert_json_snapshot!(
"KeyDistributionTestData::dan_keys_query_response_device_loggedout",
data
ruma_response_to_json(response.clone()),
);
});
ruma_response_from_json(&data)
response
}
/// Dave is a user that has not enabled cross-signing
@@ -646,10 +653,14 @@ impl VerificationViolationTestData {
}
});
let response: KeyQueryResponse = ruma_response_from_json(&data);
with_settings!({sort_maps => true}, {
assert_json_snapshot!("VerificationViolationTestData::own_keys_query_response_1", data);
assert_json_snapshot!(
"VerificationViolationTestData::own_keys_query_response_1",
ruma_response_to_json(response.clone()),
);
});
ruma_response_from_json(&data)
response
}
/// A second `/keys/query` response for Alice, containing a *different* set

View File

@@ -1,6 +1,6 @@
---
source: testing/matrix-sdk-test/src/test_json/keys_query_sets.rs
expression: data
expression: ruma_response_to_json(response.clone())
---
{
"device_keys": {
@@ -42,7 +42,6 @@ expression: data
}
}
},
"failures": {},
"master_keys": {
"@dan:localhost": {
"keys": {

View File

@@ -1,6 +1,6 @@
---
source: testing/matrix-sdk-test/src/test_json/keys_query_sets.rs
expression: data
expression: ruma_response_to_json(response.clone())
---
{
"device_keys": {
@@ -25,7 +25,6 @@ expression: data
}
}
},
"failures": {},
"master_keys": {
"@dan:localhost": {
"keys": {