Skip to content

Commit

Permalink
tests: add testcases for openapi mods (#587)
Browse files Browse the repository at this point in the history
* tests: add testcases for openapi mods

* fmt: cargo fmt
  • Loading branch information
zhangkai803 authored Dec 30, 2023
1 parent 7d27c35 commit d38d287
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 4 deletions.
23 changes: 23 additions & 0 deletions crates/oapi/src/openapi/external_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,26 @@ impl ExternalDocs {
self
}
}

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

#[test]
fn test_default_external_docs() {
let external_docs = ExternalDocs::default();
assert_eq!(external_docs.url, "");
assert_eq!(external_docs.description, None);
}

#[test]
fn test_build_external_docs() {
let external_docs = ExternalDocs::default();
let external_docs_with_url = external_docs
.url("https://pet-api.external.docs")
.description("description");

assert_eq!(external_docs_with_url.url, "https://pet-api.external.docs");
assert_eq!(external_docs_with_url.description, Some("description".to_string()));
}
}
34 changes: 34 additions & 0 deletions crates/oapi/src/openapi/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,37 @@ impl Default for Header {
}
}
}

#[cfg(test)]
mod tests {
use assert_json_diff::assert_json_eq;
use serde_json::json;

use super::*;

#[test]
fn test_build_header() {
let header = Header::new(Object::with_type(SchemaType::String));
assert_json_eq!(
header,
json!({
"schema": {
"type": "string"
}
})
);

let header = header
.description("test description")
.schema(Object::with_type(SchemaType::Number));
assert_json_eq!(
header,
json!({
"description": "test description",
"schema": {
"type": "number"
}
})
);
}
}
29 changes: 28 additions & 1 deletion crates/oapi/src/openapi/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,41 @@ impl License {

#[cfg(test)]
mod tests {
use assert_json_diff::assert_json_eq;
use serde_json::json;

use crate::License;

use super::Contact;

#[test]
fn contact_new() {
fn build_contact() {
let contact = Contact::new();

assert!(contact.name.is_none());
assert!(contact.url.is_none());
assert!(contact.email.is_none());

let contact = contact
.name("salvo api")
.url("https://github.com/salvo-rs/salvo")
.email("[email protected]");
assert_json_eq!(
contact,
json!({
"name": "salvo api",
"url": "https://github.com/salvo-rs/salvo",
"email": "[email protected]"
})
);
}

#[test]
fn test_license_set_name() {
let license = License::default();
assert!(license.name.is_empty());

let license = license.name("MIT");
assert_json_eq!(license, json!({ "name": "MIT" }));
}
}
12 changes: 9 additions & 3 deletions crates/oapi/src/openapi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,14 +628,20 @@ mod tests {
"name": "MIT",
"url": "http://mit.licence"
},
"version": "1.0.0"
"version": "1.0.0",
"contact": {},
"termsOfService": "terms of service"
},
"paths": {}
}"#;
let doc: OpenApi = OpenApi::with_info(
Info::new("My api", "1.0.0")
Info::default()
.description("My api description")
.license(License::new("MIT").url("http://mit.licence")),
.license(License::new("MIT").url("http://mit.licence"))
.title("My api")
.version("1.0.0")
.terms_of_service("terms of service")
.contact(Contact::default()),
);
let serialized = doc.to_json()?;

Expand Down

0 comments on commit d38d287

Please sign in to comment.