Skip to content

Commit 96c69d9

Browse files
dejanbjcrossley3
authored andcommitted
Move test_data to main crate
Signed-off-by: Dejan Bosanac <[email protected]>
1 parent 211792f commit 96c69d9

20 files changed

+518
-501
lines changed

src/binding/actix/server_request.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,23 +156,18 @@ mod tests {
156156
use super::*;
157157
use actix_web::test;
158158

159+
use crate::test::fixtures;
159160
use crate::{EventBuilder, EventBuilderV10};
160161
use serde_json::json;
161-
162162
#[actix_rt::test]
163163
async fn test_request() {
164-
let expected = EventBuilderV10::new()
165-
.id("0001")
166-
.ty("example.test")
167-
.source("http://localhost/")
168-
.extension("someint", "10")
169-
.build()
170-
.unwrap();
164+
let mut expected = fixtures::v10::minimal();
165+
expected.set_extension("someint", "10");
171166

172167
let (req, payload) = test::TestRequest::post()
173168
.header("ce-specversion", "1.0")
174169
.header("ce-id", "0001")
175-
.header("ce-type", "example.test")
170+
.header("ce-type", "test_event.test_application")
176171
.header("ce-source", "http://localhost/")
177172
.header("ce-someint", "10")
178173
.to_http_parts();

src/event/builder.rs

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,223 @@ pub enum Error {
5858
))]
5959
InvalidUriRefError { attribute_name: &'static str },
6060
}
61+
62+
#[cfg(test)]
63+
mod tests {
64+
use crate::test::fixtures;
65+
use crate::Event;
66+
use crate::EventBuilder;
67+
use crate::EventBuilderV03;
68+
use crate::EventBuilderV10;
69+
use claim::*;
70+
use rstest::rstest;
71+
use serde_json::{json, Value};
72+
use serde_yaml;
73+
74+
/// Test conversions
75+
76+
#[test]
77+
fn v10_to_v03() {
78+
let in_event = fixtures::v10::full_json_data();
79+
let out_event = EventBuilderV03::from(in_event).build().unwrap();
80+
assert_eq!(fixtures::v03::full_json_data(), out_event)
81+
}
82+
83+
#[test]
84+
fn v03_to_v10() {
85+
let in_event = fixtures::v03::full_json_data();
86+
let out_event = EventBuilderV10::from(in_event).build().unwrap();
87+
assert_eq!(fixtures::v10::full_json_data(), out_event)
88+
}
89+
90+
/// Test YAML
91+
/// This test checks if the usage of serde_json::Value makes the Deserialize implementation incompatible with
92+
/// other Deserializers
93+
#[test]
94+
fn deserialize_yaml_should_succeed() {
95+
let input = r#"
96+
id: aaa
97+
type: bbb
98+
source: http://localhost
99+
datacontenttype: application/json
100+
data: true
101+
specversion: "1.0"
102+
"#;
103+
104+
let expected = EventBuilderV10::new()
105+
.id("aaa")
106+
.ty("bbb")
107+
.source("http://localhost")
108+
.data("application/json", serde_json::Value::Bool(true))
109+
.build()
110+
.unwrap();
111+
112+
let deserialize_result: Result<Event, serde_yaml::Error> = serde_yaml::from_str(input);
113+
assert_ok!(&deserialize_result);
114+
let deserialized = deserialize_result.unwrap();
115+
assert_eq!(deserialized, expected)
116+
}
117+
118+
/// Test Json
119+
/// This test is a parametrized test that uses data from tests/test_data
120+
#[rstest(
121+
in_event,
122+
out_json,
123+
case::minimal_v03(fixtures::v03::minimal(), fixtures::v03::minimal_json()),
124+
case::full_v03_no_data(fixtures::v03::full_no_data(), fixtures::v03::full_no_data_json()),
125+
case::full_v03_with_json_data(
126+
fixtures::v03::full_json_data(),
127+
fixtures::v03::full_json_data_json()
128+
),
129+
case::full_v03_with_xml_string_data(
130+
fixtures::v03::full_xml_string_data(),
131+
fixtures::v03::full_xml_string_data_json()
132+
),
133+
case::full_v03_with_xml_base64_data(
134+
fixtures::v03::full_xml_binary_data(),
135+
fixtures::v03::full_xml_base64_data_json()
136+
),
137+
case::minimal_v10(fixtures::v10::minimal(), fixtures::v10::minimal_json()),
138+
case::full_v10_no_data(fixtures::v10::full_no_data(), fixtures::v10::full_no_data_json()),
139+
case::full_v10_with_json_data(
140+
fixtures::v10::full_json_data(),
141+
fixtures::v10::full_json_data_json()
142+
),
143+
case::full_v10_with_xml_string_data(
144+
fixtures::v10::full_xml_string_data(),
145+
fixtures::v10::full_xml_string_data_json()
146+
),
147+
case::full_v10_with_xml_base64_data(
148+
fixtures::v10::full_xml_binary_data(),
149+
fixtures::v10::full_xml_base64_data_json()
150+
)
151+
)]
152+
fn serialize_should_succeed(in_event: Event, out_json: Value) {
153+
// Event -> serde_json::Value
154+
let serialize_result = serde_json::to_value(in_event.clone());
155+
assert_ok!(&serialize_result);
156+
let actual_json = serialize_result.unwrap();
157+
assert_eq!(&actual_json, &out_json);
158+
159+
// serde_json::Value -> String
160+
let actual_json_serialized = actual_json.to_string();
161+
assert_eq!(actual_json_serialized, out_json.to_string());
162+
163+
// String -> Event
164+
let deserialize_result: Result<Event, serde_json::Error> =
165+
serde_json::from_str(&actual_json_serialized);
166+
assert_ok!(&deserialize_result);
167+
let deserialize_json = deserialize_result.unwrap();
168+
assert_eq!(deserialize_json, in_event)
169+
}
170+
171+
/// This test is a parametrized test that uses data from tests/test_data
172+
#[rstest(
173+
in_json,
174+
out_event,
175+
case::minimal_v03(fixtures::v03::minimal_json(), fixtures::v03::minimal()),
176+
case::full_v03_no_data(fixtures::v03::full_no_data_json(), fixtures::v03::full_no_data()),
177+
case::full_v03_with_json_data(
178+
fixtures::v03::full_json_data_json(),
179+
fixtures::v03::full_json_data()
180+
),
181+
case::full_v03_with_json_base64_data(
182+
fixtures::v03::full_json_base64_data_json(),
183+
fixtures::v03::full_json_data()
184+
),
185+
case::full_v03_with_xml_string_data(
186+
fixtures::v03::full_xml_string_data_json(),
187+
fixtures::v03::full_xml_string_data()
188+
),
189+
case::full_v03_with_xml_base64_data(
190+
fixtures::v03::full_xml_base64_data_json(),
191+
fixtures::v03::full_xml_binary_data()
192+
),
193+
case::minimal_v10(fixtures::v10::minimal_json(), fixtures::v10::minimal()),
194+
case::full_v10_no_data(fixtures::v10::full_no_data_json(), fixtures::v10::full_no_data()),
195+
case::full_v10_with_json_data(
196+
fixtures::v10::full_json_data_json(),
197+
fixtures::v10::full_json_data()
198+
),
199+
case::full_v10_with_json_base64_data(
200+
fixtures::v10::full_json_base64_data_json(),
201+
fixtures::v10::full_json_data()
202+
),
203+
case::full_v10_with_xml_string_data(
204+
fixtures::v10::full_xml_string_data_json(),
205+
fixtures::v10::full_xml_string_data()
206+
),
207+
case::full_v10_with_xml_base64_data(
208+
fixtures::v10::full_xml_base64_data_json(),
209+
fixtures::v10::full_xml_binary_data()
210+
)
211+
)]
212+
fn deserialize_json_should_succeed(in_json: Value, out_event: Event) {
213+
let deserialize_result: Result<Event, serde_json::Error> = serde_json::from_value(in_json);
214+
assert_ok!(&deserialize_result);
215+
let deserialize_json = deserialize_result.unwrap();
216+
assert_eq!(deserialize_json, out_event)
217+
}
218+
219+
#[test]
220+
fn deserialize_with_null_attribute() {
221+
let in_json = json!({
222+
"specversion" : "1.0",
223+
"type" : "com.example.someevent",
224+
"source" : "/mycontext",
225+
"id" : "A234-1234-1234",
226+
"time" : null,
227+
"comexampleextension1" : "value",
228+
"comexampleothervalue" : 5,
229+
"datacontenttype" : "text/xml",
230+
"data" : "<much wow=\"xml\"/>"
231+
});
232+
233+
let out_event = EventBuilderV10::new()
234+
.ty("com.example.someevent")
235+
.source("/mycontext")
236+
.id("A234-1234-1234")
237+
.data("text/xml", "<much wow=\"xml\"/>")
238+
.extension("comexampleextension1", "value")
239+
.extension("comexampleothervalue", 5)
240+
.build()
241+
.unwrap();
242+
243+
let deserialize_result: Result<Event, serde_json::Error> = serde_json::from_value(in_json);
244+
assert_ok!(&deserialize_result);
245+
let deserialize_json = deserialize_result.unwrap();
246+
assert_eq!(deserialize_json, out_event)
247+
}
248+
249+
#[test]
250+
fn deserialize_with_null_ext() {
251+
let in_json = json!({
252+
"specversion" : "1.0",
253+
"type" : "com.example.someevent",
254+
"source" : "/mycontext",
255+
"id" : "A234-1234-1234",
256+
"time" : "2018-04-05T17:31:00Z",
257+
"comexampleextension1" : "value",
258+
"comexampleothervalue" : 5,
259+
"unsetextension": null,
260+
"datacontenttype" : "text/xml",
261+
"data" : "<much wow=\"xml\"/>"
262+
});
263+
264+
let out_event = EventBuilderV10::new()
265+
.ty("com.example.someevent")
266+
.source("/mycontext")
267+
.id("A234-1234-1234")
268+
.time("2018-04-05T17:31:00Z")
269+
.data("text/xml", "<much wow=\"xml\"/>")
270+
.extension("comexampleextension1", "value")
271+
.extension("comexampleothervalue", 5)
272+
.build()
273+
.unwrap();
274+
275+
let deserialize_result: Result<Event, serde_json::Error> = serde_json::from_value(in_json);
276+
assert_ok!(&deserialize_result);
277+
let deserialize_json = deserialize_result.unwrap();
278+
assert_eq!(deserialize_json, out_event)
279+
}
280+
}

src/event/message.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ impl BinarySerializer<Event> for EventBinarySerializer {
118118
mod tests {
119119
use super::*;
120120
use crate::message::Error;
121+
use crate::test::fixtures;
122+
use std::convert::TryInto;
121123

122124
#[test]
123125
fn binary_deserializer_unrecognized_attribute_v03() {
@@ -168,4 +170,65 @@ mod tests {
168170
.to_string()
169171
)
170172
}
173+
174+
#[test]
175+
fn message_v03_roundtrip_structured() -> Result<()> {
176+
assert_eq!(
177+
fixtures::v03::full_json_data(),
178+
StructuredDeserializer::into_event(fixtures::v03::full_json_data())?
179+
);
180+
Ok(())
181+
}
182+
183+
#[test]
184+
fn message_v03_roundtrip_binary() -> Result<()> {
185+
//TODO this code smells because we're missing a proper way in the public APIs
186+
// to destructure an event and rebuild it
187+
let wanna_be_expected = fixtures::v03::full_json_data();
188+
let data: serde_json::Value = wanna_be_expected.data().unwrap().clone().try_into()?;
189+
let bytes = serde_json::to_vec(&data)?;
190+
let expected = EventBuilderV03::from(wanna_be_expected.clone())
191+
.data(wanna_be_expected.datacontenttype().unwrap(), bytes)
192+
.build()
193+
.unwrap();
194+
195+
assert_eq!(
196+
expected,
197+
BinaryDeserializer::into_event(fixtures::v03::full_json_data())?
198+
);
199+
Ok(())
200+
}
201+
202+
#[test]
203+
fn message_v10_roundtrip_structured() -> Result<()> {
204+
assert_eq!(
205+
fixtures::v10::full_json_data(),
206+
StructuredDeserializer::into_event(fixtures::v10::full_json_data())?
207+
);
208+
Ok(())
209+
}
210+
211+
#[test]
212+
fn message_v10_roundtrip_binary() -> Result<()> {
213+
//TODO this code smells because we're missing a proper way in the public APIs
214+
// to destructure an event and rebuild it
215+
let wanna_be_expected = fixtures::v10::full_json_data();
216+
let data: serde_json::Value = wanna_be_expected
217+
.data()
218+
.cloned()
219+
.unwrap()
220+
.try_into()
221+
.unwrap();
222+
let bytes = serde_json::to_vec(&data)?;
223+
let expected = EventBuilderV10::from(wanna_be_expected.clone())
224+
.data(wanna_be_expected.datacontenttype().unwrap(), bytes)
225+
.build()
226+
.unwrap();
227+
228+
assert_eq!(
229+
expected,
230+
BinaryDeserializer::into_event(fixtures::v10::full_json_data())?
231+
);
232+
Ok(())
233+
}
171234
}

src/event/v03/attributes.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,20 @@ impl crate::event::message::AttributesDeserializer for super::Attributes {
220220
#[cfg(test)]
221221
mod tests {
222222
use super::*;
223+
use crate::test::fixtures;
223224
use chrono::NaiveDateTime;
224225

226+
#[test]
227+
fn iter_v03_test() {
228+
let in_event = fixtures::v03::full_json_data();
229+
let mut iter_v03 = in_event.iter_attributes();
230+
231+
assert_eq!(
232+
("specversion", AttributeValue::SpecVersion(SpecVersion::V03)),
233+
iter_v03.next().unwrap()
234+
);
235+
}
236+
225237
#[test]
226238
fn iterator_test_v03() {
227239
let a = Attributes {

0 commit comments

Comments
 (0)