Skip to content

Commit

Permalink
tests: add test case for openapi/response && openapi/request_body && …
Browse files Browse the repository at this point in the history
…openapi/example
  • Loading branch information
zhangkai803 committed Dec 28, 2023
1 parent 9c2a227 commit 711466b
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 10 deletions.
27 changes: 27 additions & 0 deletions crates/oapi/src/openapi/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,30 @@ impl Example {
self
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_example() {
let example = Example::new();
assert!(example.summary.is_empty());
assert!(example.description.is_empty());
assert!(example.value.is_none());
assert!(example.external_value.is_empty());

let example = example.summary("summary");
assert!(example.summary == "summary");

let example = example.description("description");
assert!(example.description == "description");

let example = example.external_value("external_value");
assert!(example.external_value == "external_value");

let example = example.value(serde_json::Value::String("value".to_string()));
assert!(example.value.is_some());
assert!(example.value.unwrap() == serde_json::Value::String("value".to_string()));
}
}
31 changes: 29 additions & 2 deletions crates/oapi/src/openapi/request_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ mod tests {
"application/json",
Content::new(crate::Ref::from_schema_name("EmailPayload")),
);
let serialized = serde_json::to_string_pretty(&request_body)?;
println!("serialized json:\n {serialized}");

assert_json_eq!(
request_body,
json!({
Expand All @@ -111,4 +110,32 @@ mod tests {
);
Ok(())
}

#[test]
fn request_body_merge() {
let mut request_body = RequestBody::new();
let other_request_body = RequestBody::new()
.description("Merged requestBody")
.required(Required::True)
.add_content(
"application/json",
Content::new(crate::Ref::from_schema_name("EmailPayload")),
);

request_body.merge(other_request_body);
assert_json_eq!(
request_body,
json!({
"description": "Merged requestBody",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/EmailPayload"
}
}
},
"required": true
})
);
}
}
105 changes: 97 additions & 8 deletions crates/oapi/src/openapi/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl From<Ref> for RefOr<Response> {

#[cfg(test)]
mod tests {
use super::{Content, Response, Responses};
use super::{BTreeMap, Content, Header, Ref, RefOr, Response, Responses};
use assert_json_diff::assert_json_eq;
use serde_json::json;

Expand All @@ -199,25 +199,114 @@ mod tests {

#[test]
fn response_builder() -> Result<(), serde_json::Error> {
let request_body = Response::new("A sample response").add_content(
"application/json",
Content::new(crate::Ref::from_schema_name("MySchemaPayload")),
);
let serialized = serde_json::to_string_pretty(&request_body)?;
println!("serialized json:\n {serialized}");
let request_body = Response::new("A sample response")
.description("A sample response description")
.add_content(
"application/json",
Content::new(crate::Ref::from_schema_name("MySchemaPayload")),
)
.add_header("content-type", Header::default().description("application/json"));

assert_json_eq!(
request_body,
json!({
"description": "A sample response",
"description": "A sample response description",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/MySchemaPayload"
}
}
},
"headers": {
"content-type": {
"description": "application/json",
"schema": {
"type": "string"
}
}
}
})
);
Ok(())
}

#[test]
fn test_responses_from_btree_map() {
let input = BTreeMap::from([
("response1".to_string(), Response::new("response1")),
("response2".to_string(), Response::new("response2")),
]);

let expected = Responses {
0: BTreeMap::from([
("response1".to_string(), RefOr::T(Response::new("response1"))),
("response2".to_string(), RefOr::T(Response::new("response2"))),
]),
};

let actual = Responses::from(input);

assert_eq!(expected, actual);
}

#[test]
fn test_responses_from_kv_sequence() {
let input = [
("response1".to_string(), Response::new("response1")),
("response2".to_string(), Response::new("response2")),
];

let expected = Responses {
0: BTreeMap::from([
("response1".to_string(), RefOr::T(Response::new("response1"))),
("response2".to_string(), RefOr::T(Response::new("response2"))),
]),
};

let actual = Responses::from(input);

assert_eq!(expected, actual);
}

#[test]
fn test_responses_from_iter() {
let input = [
("response1".to_string(), Response::new("response1")),
("response2".to_string(), Response::new("response2")),
];

let expected = Responses {
0: BTreeMap::from([
("response1".to_string(), RefOr::T(Response::new("response1"))),
("response2".to_string(), RefOr::T(Response::new("response2"))),
]),
};

let actual = Responses::from_iter(input);

assert_eq!(expected, actual);
}

#[test]
fn test_responses_into_iter() {
let responses = Responses::new();
let responses = responses.response("response1", Response::new("response1"));
assert_eq!(1, responses.into_iter().collect::<Vec<_>>().len());
}

#[test]
fn test_btree_map_from_responses() {
let expected = BTreeMap::from([
("response1".to_string(), RefOr::T(Response::new("response1"))),
("response2".to_string(), RefOr::T(Response::new("response2"))),
]);

let actual = BTreeMap::from(
Responses::new()
.response("response1", Response::new("response1"))
.response("response2", Response::new("response2")),
);
assert_eq!(expected, actual);
}
}

0 comments on commit 711466b

Please sign in to comment.