matrix_sdk_test: Use static JSON values instead of reading files

master
Devin R 2020-06-21 14:01:04 -04:00
parent fc077bcd6b
commit 4a7b3a103c
9 changed files with 1485 additions and 55 deletions

View File

@ -1404,7 +1404,7 @@ mod test {
use crate::RegistrationBuilder; use crate::RegistrationBuilder;
use matrix_sdk_base::JsonStore; use matrix_sdk_base::JsonStore;
use matrix_sdk_test::{EventBuilder, EventsFile}; use matrix_sdk_test::{EventBuilder, EventsJson};
use mockito::{mock, Matcher}; use mockito::{mock, Matcher};
use tempfile::tempdir; use tempfile::tempdir;
@ -1522,8 +1522,8 @@ mod test {
client.restore_login(session).await.unwrap(); client.restore_login(session).await.unwrap();
let mut response = EventBuilder::default() let mut response = EventBuilder::default()
.add_room_event(EventsFile::Member, RoomEvent::RoomMember) .add_room_event(EventsJson::Member, RoomEvent::RoomMember)
.add_room_event(EventsFile::PowerLevels, RoomEvent::RoomPowerLevels) .add_room_event(EventsJson::PowerLevels, RoomEvent::RoomPowerLevels)
.build_sync_response(); .build_sync_response();
client client

View File

@ -1808,7 +1808,7 @@ mod test {
#[async_test] #[async_test]
async fn test_joined_room_creation() { async fn test_joined_room_creation() {
let mut sync_response = EventBuilder::default() let mut sync_response = EventBuilder::default()
.add_room_event(EventsFile::Member, RoomEvent::RoomMember) .add_room_event(EventsJson::Member, RoomEvent::RoomMember)
.build_sync_response(); .build_sync_response();
let client = get_client().await; let client = get_client().await;
let room_id = get_room_id(); let room_id = get_room_id();
@ -2216,7 +2216,7 @@ mod test {
let room_id = get_room_id(); let room_id = get_room_id();
let mut sync_response = EventBuilder::default() let mut sync_response = EventBuilder::default()
.add_room_event(EventsFile::Member, RoomEvent::RoomMember) .add_room_event(EventsJson::Member, RoomEvent::RoomMember)
.build_sync_response(); .build_sync_response();
client client

View File

@ -577,7 +577,7 @@ mod test {
}; };
use crate::identifiers::{EventId, UserId}; use crate::identifiers::{EventId, UserId};
use crate::{BaseClient, Session}; use crate::{BaseClient, Session};
use matrix_sdk_test::{async_test, sync_response, EventBuilder, EventsFile, SyncResponseFile}; use matrix_sdk_test::{async_test, sync_response, EventBuilder, EventsJson, SyncResponseFile};
use std::time::SystemTime; use std::time::SystemTime;
@ -633,8 +633,8 @@ mod test {
let user_id = UserId::try_from("@example:localhost").unwrap(); let user_id = UserId::try_from("@example:localhost").unwrap();
let mut response = EventBuilder::default() let mut response = EventBuilder::default()
.add_room_event(EventsFile::Member, RoomEvent::RoomMember) .add_room_event(EventsJson::Member, RoomEvent::RoomMember)
.add_room_event(EventsFile::PowerLevels, RoomEvent::RoomPowerLevels) .add_room_event(EventsJson::PowerLevels, RoomEvent::RoomPowerLevels)
.build_sync_response(); .build_sync_response();
client.receive_sync_response(&mut response).await.unwrap(); client.receive_sync_response(&mut response).await.unwrap();
@ -662,7 +662,7 @@ mod test {
let room_id = get_room_id(); let room_id = get_room_id();
let mut response = EventBuilder::default() let mut response = EventBuilder::default()
.add_state_event(EventsFile::Aliases, StateEvent::RoomAliases) .add_state_event(EventsJson::Aliases, StateEvent::RoomAliases)
.build_sync_response(); .build_sync_response();
client.receive_sync_response(&mut response).await.unwrap(); client.receive_sync_response(&mut response).await.unwrap();
@ -680,7 +680,7 @@ mod test {
let room_id = get_room_id(); let room_id = get_room_id();
let mut response = EventBuilder::default() let mut response = EventBuilder::default()
.add_state_event(EventsFile::Alias, StateEvent::RoomCanonicalAlias) .add_state_event(EventsJson::Alias, StateEvent::RoomCanonicalAlias)
.build_sync_response(); .build_sync_response();
client.receive_sync_response(&mut response).await.unwrap(); client.receive_sync_response(&mut response).await.unwrap();
@ -698,7 +698,7 @@ mod test {
let room_id = get_room_id(); let room_id = get_room_id();
let mut response = EventBuilder::default() let mut response = EventBuilder::default()
.add_state_event(EventsFile::Name, StateEvent::RoomName) .add_state_event(EventsJson::Name, StateEvent::RoomName)
.build_sync_response(); .build_sync_response();
client.receive_sync_response(&mut response).await.unwrap(); client.receive_sync_response(&mut response).await.unwrap();

View File

@ -198,7 +198,7 @@ impl RoomMember {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use matrix_sdk_test::{async_test, EventBuilder, EventsFile}; use matrix_sdk_test::{async_test, EventBuilder, EventsJson};
use crate::events::collections::all::RoomEvent; use crate::events::collections::all::RoomEvent;
use crate::events::room::member::MembershipState; use crate::events::room::member::MembershipState;
@ -234,8 +234,8 @@ mod test {
let room_id = get_room_id(); let room_id = get_room_id();
let mut response = EventBuilder::default() let mut response = EventBuilder::default()
.add_room_event(EventsFile::Member, RoomEvent::RoomMember) .add_room_event(EventsJson::Member, RoomEvent::RoomMember)
.add_room_event(EventsFile::PowerLevels, RoomEvent::RoomPowerLevels) .add_room_event(EventsJson::PowerLevels, RoomEvent::RoomPowerLevels)
.build_sync_response(); .build_sync_response();
client.receive_sync_response(&mut response).await.unwrap(); client.receive_sync_response(&mut response).await.unwrap();
@ -258,9 +258,9 @@ mod test {
let room_id = get_room_id(); let room_id = get_room_id();
let mut response = EventBuilder::default() let mut response = EventBuilder::default()
.add_room_event(EventsFile::Member, RoomEvent::RoomMember) .add_room_event(EventsJson::Member, RoomEvent::RoomMember)
.add_room_event(EventsFile::PowerLevels, RoomEvent::RoomPowerLevels) .add_room_event(EventsJson::PowerLevels, RoomEvent::RoomPowerLevels)
.add_presence_event(EventsFile::Presence) .add_presence_event(EventsJson::Presence)
.build_sync_response(); .build_sync_response();
client.receive_sync_response(&mut response).await.unwrap(); client.receive_sync_response(&mut response).await.unwrap();

View File

@ -15,3 +15,4 @@ serde_json = "1.0.53"
http = "0.2.1" http = "0.2.1"
matrix-sdk-common = { version = "0.1.0", path = "../matrix_sdk_common" } matrix-sdk-common = { version = "0.1.0", path = "../matrix_sdk_common" }
matrix-sdk-test-macros = { version = "0.1.0", path = "../matrix_sdk_test_macros" } matrix-sdk-test-macros = { version = "0.1.0", path = "../matrix_sdk_test_macros" }
lazy_static = "1.4.0"

View File

@ -15,12 +15,15 @@ use matrix_sdk_common::events::{
EventJson, TryFromRaw, EventJson, TryFromRaw,
}; };
use matrix_sdk_common::identifiers::RoomId; use matrix_sdk_common::identifiers::RoomId;
use serde_json::Value as JsonValue;
pub use matrix_sdk_test_macros::async_test; pub use matrix_sdk_test_macros::async_test;
/// Embedded event files pub mod test_json;
/// Static `serde_json::Value`s
#[derive(Debug)] #[derive(Debug)]
pub enum EventsFile { pub enum EventsJson {
Alias, Alias,
Aliases, Aliases,
Create, Create,
@ -67,15 +70,15 @@ impl EventBuilder {
/// Add an event to the room events `Vec`. /// Add an event to the room events `Vec`.
pub fn add_ephemeral<Ev: TryFromRaw>( pub fn add_ephemeral<Ev: TryFromRaw>(
mut self, mut self,
file: EventsFile, json: EventsJson,
variant: fn(Ev) -> Event, variant: fn(Ev) -> Event,
) -> Self { ) -> Self {
let val: &str = match file { let val: &JsonValue = match json {
EventsFile::Typing => include_str!("../../test_data/events/typing.json"), EventsJson::Typing => &test_json::TYPING,
_ => panic!("unknown ephemeral event file {:?}", file), _ => panic!("unknown ephemeral event {:?}", json),
}; };
let event = serde_json::from_str::<EventJson<Ev>>(&val) let event = serde_json::from_value::<EventJson<Ev>>(val.clone())
.unwrap() .unwrap()
.deserialize() .deserialize()
.unwrap(); .unwrap();
@ -87,14 +90,14 @@ impl EventBuilder {
#[allow(clippy::match_single_binding, unused)] #[allow(clippy::match_single_binding, unused)]
pub fn add_account<Ev: TryFromRaw>( pub fn add_account<Ev: TryFromRaw>(
mut self, mut self,
file: EventsFile, json: EventsJson,
variant: fn(Ev) -> Event, variant: fn(Ev) -> Event,
) -> Self { ) -> Self {
let val: &str = match file { let val: &JsonValue = match json {
_ => panic!("unknown account event file {:?}", file), _ => panic!("unknown account event {:?}", json),
}; };
let event = serde_json::from_str::<EventJson<Ev>>(&val) let event = serde_json::from_value::<EventJson<Ev>>(val.clone())
.unwrap() .unwrap()
.deserialize() .deserialize()
.unwrap(); .unwrap();
@ -105,16 +108,16 @@ impl EventBuilder {
/// Add an event to the room events `Vec`. /// Add an event to the room events `Vec`.
pub fn add_room_event<Ev: TryFromRaw>( pub fn add_room_event<Ev: TryFromRaw>(
mut self, mut self,
file: EventsFile, json: EventsJson,
variant: fn(Ev) -> RoomEvent, variant: fn(Ev) -> RoomEvent,
) -> Self { ) -> Self {
let val = match file { let val: &JsonValue = match json {
EventsFile::Member => include_str!("../../test_data/events/member.json"), EventsJson::Member => &test_json::MEMBER,
EventsFile::PowerLevels => include_str!("../../test_data/events/power_levels.json"), EventsJson::PowerLevels => &test_json::POWER_LEVELS,
_ => panic!("unknown room event file {:?}", file), _ => panic!("unknown room event json {:?}", json),
}; };
let event = serde_json::from_str::<EventJson<Ev>>(&val) let event = serde_json::from_value::<EventJson<Ev>>(val.clone())
.unwrap() .unwrap()
.deserialize() .deserialize()
.unwrap(); .unwrap();
@ -183,17 +186,17 @@ impl EventBuilder {
/// Add a state event to the state events `Vec`. /// Add a state event to the state events `Vec`.
pub fn add_state_event<Ev: TryFromRaw>( pub fn add_state_event<Ev: TryFromRaw>(
mut self, mut self,
file: EventsFile, json: EventsJson,
variant: fn(Ev) -> StateEvent, variant: fn(Ev) -> StateEvent,
) -> Self { ) -> Self {
let val = match file { let val: &JsonValue = match json {
EventsFile::Alias => include_str!("../../test_data/events/alias.json"), EventsJson::Alias => &test_json::ALIAS,
EventsFile::Aliases => include_str!("../../test_data/events/aliases.json"), EventsJson::Aliases => &test_json::ALIASES,
EventsFile::Name => include_str!("../../test_data/events/name.json"), EventsJson::Name => &test_json::NAME,
_ => panic!("unknown state event file {:?}", file), _ => panic!("unknown state event {:?}", json),
}; };
let event = serde_json::from_str::<EventJson<Ev>>(&val) let event = serde_json::from_value::<EventJson<Ev>>(val.clone())
.unwrap() .unwrap()
.deserialize() .deserialize()
.unwrap(); .unwrap();
@ -202,13 +205,13 @@ impl EventBuilder {
} }
/// Add an presence event to the presence events `Vec`. /// Add an presence event to the presence events `Vec`.
pub fn add_presence_event(mut self, file: EventsFile) -> Self { pub fn add_presence_event(mut self, json: EventsJson) -> Self {
let val = match file { let val: &JsonValue = match json {
EventsFile::Presence => include_str!("../../test_data/events/presence.json"), EventsJson::Presence => &test_json::PRESENCE,
_ => panic!("unknown presence event file {:?}", file), _ => panic!("unknown presence event {:?}", json),
}; };
let event = serde_json::from_str::<EventJson<PresenceEvent>>(&val) let event = serde_json::from_value::<EventJson<PresenceEvent>>(val.clone())
.unwrap() .unwrap()
.deserialize() .deserialize()
.unwrap(); .unwrap();
@ -338,16 +341,16 @@ pub enum SyncResponseFile {
/// Get specific API responses for testing /// Get specific API responses for testing
pub fn sync_response(kind: SyncResponseFile) -> SyncResponse { pub fn sync_response(kind: SyncResponseFile) -> SyncResponse {
let data = match kind { let data: &JsonValue = match kind {
SyncResponseFile::All => include_bytes!("../../test_data/more_sync.json").to_vec(), SyncResponseFile::All => &test_json::MORE_SYNC,
SyncResponseFile::Default => include_bytes!("../../test_data/sync.json").to_vec(), SyncResponseFile::Default => &test_json::SYNC,
SyncResponseFile::DefaultWithSummary => { SyncResponseFile::DefaultWithSummary => &test_json::DEFAULT_SYNC,
include_bytes!("../../test_data/sync_with_summary.json").to_vec() SyncResponseFile::Invite => &test_json::INVITE_SYNC,
} SyncResponseFile::Leave => &test_json::LEAVE_SYNC,
SyncResponseFile::Invite => include_bytes!("../../test_data/invite_sync.json").to_vec(),
SyncResponseFile::Leave => include_bytes!("../../test_data/leave_sync.json").to_vec(),
}; };
let response = Response::builder().body(data.to_vec()).unwrap(); let response = Response::builder()
.body(data.to_string().as_bytes().to_vec())
.unwrap();
SyncResponse::try_from(response).unwrap() SyncResponse::try_from(response).unwrap()
} }

View File

@ -0,0 +1,381 @@
use lazy_static::lazy_static;
use serde_json::{json, Value as JsonValue};
lazy_static! {
pub static ref ALIAS: JsonValue = json!({
"content": {
"alias": "#tutorial:localhost"
},
"event_id": "$15139375513VdeRF:localhost",
"origin_server_ts": 151393755,
"sender": "@example:localhost",
"state_key": "",
"type": "m.room.canonical_alias",
"unsigned": {
"age": 703422
}
});
}
lazy_static! {
pub static ref ALIASES: JsonValue = json!({
"content": {
"aliases": [
"#tutorial:localhost"
]
},
"event_id": "$15139375516NUgtD:localhost",
"origin_server_ts": 151393755,
"sender": "@example:localhost",
"state_key": "localhost",
"type": "m.room.aliases",
"unsigned": {
"age": 703422
}
});
}
lazy_static! {
pub static ref CREATE: JsonValue = json!({
"content": {
"creator": "@example:localhost",
"m.federate": true,
"room_version": "1"
},
"event_id": "$151957878228ekrDs:localhost",
"origin_server_ts": 15195787,
"sender": "@example:localhost",
"state_key": "",
"type": "m.room.create",
"unsigned": {
"age": 139298
}
});
}
lazy_static! {
pub static ref FULLY_READ: JsonValue = json!({
"content": {
"event_id": "$someplace:example.org"
},
"room_id": "!somewhere:example.org",
"type": "m.fully_read"
});
}
lazy_static! {
pub static ref HISTORY_VISIBILITY: JsonValue = json!({
"content": {
"history_visibility": "world_readable"
},
"event_id": "$151957878235ricnD:localhost",
"origin_server_ts": 151957878,
"sender": "@example:localhost",
"state_key": "",
"type": "m.room.history_visibility",
"unsigned": {
"age": 1392989
}
});
}
lazy_static! {
pub static ref JOIN_RULES: JsonValue = json!({
"content": {
"join_rule": "public"
},
"event_id": "$151957878231iejdB:localhost",
"origin_server_ts": 151957878,
"sender": "@example:localhost",
"state_key": "",
"type": "m.room.join_rules",
"unsigned": {
"age": 1392989
}
});
}
lazy_static! {
pub static ref MEMBER: JsonValue = json!({
"content": {
"avatar_url": null,
"displayname": "example",
"membership": "join"
},
"event_id": "$151800140517rfvjc:localhost",
"membership": "join",
"origin_server_ts": 151800140,
"sender": "@example:localhost",
"state_key": "@example:localhost",
"type": "m.room.member",
"unsigned": {
"age": 297036,
"replaces_state": "$151800111315tsynI:localhost",
"prev_content": {
"avatar_url": null,
"displayname": "example",
"membership": "invite"
}
}
});
}
lazy_static! {
pub static ref MESSAGE_EMOTE: JsonValue = json!({
"content": {
"body": "is dancing", "format": "org.matrix.custom.html",
"formatted_body": "<strong>is dancing</strong>",
"msgtype": "m.emote"
},
"event_id": "$152037280074GZeOm:localhost",
"origin_server_ts": 152037280,
"sender": "@example:localhost",
"type": "m.room.message",
"unsigned": {
"age": 598971
}
});
}
lazy_static! {
pub static ref MESSAGE_NOTICE: JsonValue = json!({
"origin_server_ts": 153356516,
"sender": "@_neb_github:matrix.org",
"event_id": "$153356516319138IHRIC:matrix.org",
"unsigned": {
"age": 743
},
"content": {
"body": "https://github.com/matrix-org/matrix-python-sdk/issues/266 : Consider allowing MatrixClient.__init__ to take sync_token kwarg",
"format": "org.matrix.custom.html",
"formatted_body": "<a href='https://github.com/matrix-org/matrix-python-sdk/pull/313'>313: nio wins!</a>",
"msgtype": "m.notice"
},
"type": "m.room.message",
"room_id": "!YHhmBTmGBHGQOlGpaZ:matrix.org"
});
}
lazy_static! {
pub static ref MESSAGE_TEXT: JsonValue = json!({
"content": {
"body": "is dancing", "format": "org.matrix.custom.html",
"formatted_body": "<strong>is dancing</strong>",
"msgtype": "m.text"
},
"event_id": "$152037280074GZeOm:localhost",
"origin_server_ts": 152037280,
"sender": "@example:localhost",
"type": "m.room.message",
"unsigned": {
"age": 598971
}
});
}
lazy_static! {
pub static ref NAME: JsonValue = json!({
"content": {
"name": "room name"
},
"event_id": "$15139375513VdeRF:localhost",
"origin_server_ts": 151393755,
"sender": "@example:localhost",
"state_key": "",
"type": "m.room.name",
"unsigned": {
"age": 703422
}
});
}
lazy_static! {
pub static ref POWER_LEVELS: JsonValue = json!({
"content": {
"ban": 50,
"events": {
"m.room.avatar": 50,
"m.room.canonical_alias": 50,
"m.room.history_visibility": 100,
"m.room.name": 50,
"m.room.power_levels": 100,
"m.room.message": 25
},
"events_default": 0,
"invite": 0,
"kick": 50,
"redact": 50,
"state_default": 50,
"users": {
"@example:localhost": 100,
"@bob:localhost": 0
},
"users_default": 0
},
"event_id": "$15139375512JaHAW:localhost",
"origin_server_ts": 151393755,
"sender": "@example:localhost",
"state_key": "",
"type": "m.room.power_levels",
"unsigned": {
"age": 703422
}
}
);
}
lazy_static! {
pub static ref PRESENCE: JsonValue = json!({
"content": {
"avatar_url": "mxc://localhost:wefuiwegh8742w",
"currently_active": false,
"last_active_ago": 1,
"presence": "online",
"status_msg": "Making cupcakes"
},
"sender": "@example:localhost",
"type": "m.presence"
});
}
lazy_static! {
pub static ref REDACTED_INVALID: JsonValue = json!({
"content": {},
"event_id": "$15275046980maRLj:localhost",
"origin_server_ts": 1527504698,
"sender": "@example:localhost",
"type": "m.room.message"
});
}
lazy_static! {
pub static ref REDACTED_STATE: JsonValue = json!({
"content": {},
"event_id": "$example_id:example.org",
"origin_server_ts": 153232493,
"sender": "@example:example.org",
"state_key": "test_state_key",
"type": "m.some.state",
"unsigned": {
"age": 3069315,
"redacted_because": {
"content": {},
"event_id": "$redaction_example_id:example.org",
"origin_server_ts": 153232494,
"redacts": "$example_id:example.org",
"sender": "@example:example:org",
"type": "m.room.redaction",
"unsigned": {"age": 30693147}
},
"redacted_by": "$redaction_example_id:example.org"
}
});
}
lazy_static! {
pub static ref REDACTED: JsonValue = json!({
"content": {},
"event_id": "$15275046980maRLj:localhost",
"origin_server_ts": 1527504698,
"sender": "@example:localhost",
"type": "m.room.message",
"unsigned": {
"age": 19334,
"redacted_because": {
"content": {},
"event_id": "$15275047031IXQRi:localhost",
"origin_server_ts": 1527504703,
"redacts": "$15275046980maRLj:localhost",
"sender": "@example:localhost",
"type": "m.room.redaction",
"unsigned": {
"age": 14523
}
},
"redacted_by": "$15275047031IXQRi:localhost"
}
});
}
lazy_static! {
pub static ref REDACTION: JsonValue = json!({
"content": {
"reason": "😀"
},
"event_id": "$151957878228ssqrJ:localhost",
"origin_server_ts": 151957878,
"sender": "@example:localhost",
"type": "m.room.redaction",
"redacts": "$151957878228ssqrj:localhost"
});
}
lazy_static! {
pub static ref ROOM_AVATAR: JsonValue = json!({
"content": {
"info": {
"h": 398,
"mimetype": "image/jpeg",
"size": 31037,
"w": 394
},
"url": "mxc://domain.com/JWEIFJgwEIhweiWJE"
},
"event_id": "$143273582443PhrSn:domain.com",
"origin_server_ts": 143273582,
"room_id": "!jEsUZKDJdhlrceRyVU:domain.com",
"sender": "@example:domain.com",
"state_key": "",
"type": "m.room.avatar",
"unsigned": {
"age": 1234
}
});
}
lazy_static! {
pub static ref TAG: JsonValue = json!({
"content": {
"tags": {
"u.work": {
"order": 0.9
}
}
},
"type": "m.tag"
});
}
lazy_static! {
pub static ref TOPIC: JsonValue = json!({
"content": {
"topic": "😀"
},
"event_id": "$151957878228ssqrJ:localhost",
"origin_server_ts": 151957878,
"sender": "@example:localhost",
"state_key": "",
"type": "m.room.topic",
"unsigned": {
"age": 1392989,
"prev_content": {
"topic": "test"
},
"prev_sender": "@example:localhost",
"replaces_state": "$151957069225EVYKm:localhost"
}
});
}
lazy_static! {
pub static ref TYPING: JsonValue = json!({
"content": {
"user_ids": [
"@alice:matrix.org",
"@bob:example.com"
]
},
"room_id": "!jEsUZKDJdhlrceRyVU:example.org",
"type": "m.typing"
});
}

View File

@ -0,0 +1,11 @@
//! Test data for the matrix-sdk crates.
//!
//! Exporting each const allows all the test data to have a single source of truth.
//! When running `cargo publish` no external folders are allowed so all the
//! test data needs to be contained within this crate.
pub mod events;
pub mod sync;
pub use events::{ALIAS, ALIASES, MEMBER, NAME, POWER_LEVELS, PRESENCE, TYPING};
pub use sync::{DEFAULT_SYNC, INVITE_SYNC, LEAVE_SYNC, MORE_SYNC, SYNC};

File diff suppressed because it is too large Load Diff