Skip to content

Commit 4619682

Browse files
committed
test(whirlybird): adding tests for body
1 parent 93bd85c commit 4619682

File tree

1 file changed

+107
-0
lines changed
  • crates/whirlybird/src/journey/entity

1 file changed

+107
-0
lines changed

crates/whirlybird/src/journey/entity/body.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,110 @@ impl From<&RedMaple<EventWrapper>> for Body {
6363
})
6464
}
6565
}
66+
67+
#[cfg(test)]
68+
mod tests {
69+
use time::OffsetDateTime;
70+
71+
use crate::journey::{Maple, ValidMapleID};
72+
73+
use super::*;
74+
75+
const VALID_BODY_TEXT: &str = "Some Valid text";
76+
const VALID_BODY_TEXT_TWO: &str = "Some Other Valid text";
77+
const EMPTY_BODY_TEXT: &str = "";
78+
79+
#[test]
80+
fn test_empty_body_from_string() -> Result<(), String> {
81+
let empty_string = Body::try_from(EMPTY_BODY_TEXT.to_string());
82+
match empty_string {
83+
Ok(_) => Err("body should never be empty".to_owned()),
84+
Err(err) => match err {
85+
BuildingError::TextCannotBeEmpty => Ok(()),
86+
},
87+
}
88+
}
89+
90+
#[test]
91+
fn test_valid_body_from_string() -> Result<(), String> {
92+
let valid_string = Body::try_from(VALID_BODY_TEXT.to_string());
93+
match valid_string {
94+
Ok(_) => Ok(()),
95+
Err(err) => Err(format!(
96+
"a valid body should not get error. but instead it got one: {err}"
97+
)),
98+
}
99+
}
100+
101+
#[test]
102+
fn test_body_from_redmaple() -> Result<(), String> {
103+
let first_event = {
104+
let this_event_time = OffsetDateTime::now_utc();
105+
let new_maple = Maple::new(
106+
this_event_time.into(),
107+
Body::try_from(VALID_BODY_TEXT.to_string()).map_err(|e| {
108+
format!("a valid body should not get error. but instead it got one: {e}")
109+
})?,
110+
);
111+
112+
EventWrapper::new(
113+
this_event_time.into(),
114+
this_event_time,
115+
Event::MapleCreated(new_maple),
116+
)
117+
};
118+
119+
let the_redmaple = RedMaple::new(vec![first_event]);
120+
let _ = match Body::try_from(&the_redmaple) {
121+
Ok(b) => match b {
122+
Body::OneLineText(text) => {
123+
if text == VALID_BODY_TEXT {
124+
Ok(())
125+
} else {
126+
Err(format!("wanted '{VALID_BODY_TEXT}', instead go '{text}'"))
127+
}
128+
}
129+
},
130+
Err(e) => Err(format!(
131+
"Should be able to get a body. but instead it got one: {e}"
132+
)),
133+
}?;
134+
135+
let second_event = {
136+
let this_event_time = OffsetDateTime::now_utc();
137+
let valid_maple_id = ValidMapleID::try_from(&the_redmaple).map_err(|err| format!("I redmaple with events should be able to give out valid maple ids but it could not. instead got: {err}"))?;
138+
139+
EventWrapper::new(
140+
this_event_time.into(),
141+
this_event_time,
142+
Event::MapleBodyUpdated(
143+
valid_maple_id,
144+
Body::try_from(VALID_BODY_TEXT_TWO.to_owned()).map_err(|err| {
145+
format!("Should be able to get a body. but instead it got one: {err}")
146+
.to_owned()
147+
})?,
148+
),
149+
)
150+
};
151+
152+
let redmaple_v2 = the_redmaple.into_appended(second_event);
153+
_ = match Body::try_from(&redmaple_v2) {
154+
Ok(b) => match b {
155+
Body::OneLineText(text) => {
156+
if text == VALID_BODY_TEXT_TWO.to_string() {
157+
Ok(())
158+
} else {
159+
Err(format!(
160+
"wanted '{VALID_BODY_TEXT_TWO}', instead got '{text}'"
161+
))
162+
}
163+
}
164+
},
165+
Err(e) => Err(format!(
166+
"Should be able to get a body. but instead it got: {e}"
167+
)),
168+
}?;
169+
170+
Ok(())
171+
}
172+
}

0 commit comments

Comments
 (0)