Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add testcases for openapi mods #587

Merged
merged 2 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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