Reorganise tests
This commit is contained in:
parent
d14db450cf
commit
67d44533d6
10 changed files with 147 additions and 112 deletions
3
schemars/.gitignore
vendored
3
schemars/.gitignore
vendored
|
@ -1,4 +1,5 @@
|
|||
/target
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
||||
/tests/*.actual.*
|
||||
/tests/actual/*.json
|
||||
/tests/expected/README.md
|
3
schemars/tests/actual/README.md
Normal file
3
schemars/tests/actual/README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# Actual Generated Schemas
|
||||
|
||||
If a test fails because a generated schema did not match the expected JSON, then the actual schema will be written to a JSON file in this directory.
|
30
schemars/tests/expected/naming-custom.json
Normal file
30
schemars/tests/expected/naming-custom.json
Normal file
|
@ -0,0 +1,30 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "a-new-name-Array_Of_String-Integer-Integer",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"inner": {
|
||||
"$ref": "#/definitions/MySimpleStruct"
|
||||
},
|
||||
"t": {
|
||||
"type": "integer"
|
||||
},
|
||||
"u": {
|
||||
"type": "null"
|
||||
},
|
||||
"v": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"w": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
"another-new-name": {
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
}
|
30
schemars/tests/expected/naming-default.json
Normal file
30
schemars/tests/expected/naming-default.json
Normal file
|
@ -0,0 +1,30 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "MyStruct_For_Integer_And_Null_And_Boolean_And_Array_Of_String",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"inner": {
|
||||
"$ref": "#/definitions/MySimpleStruct"
|
||||
},
|
||||
"t": {
|
||||
"type": "integer"
|
||||
},
|
||||
"u": {
|
||||
"type": "null"
|
||||
},
|
||||
"v": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"w": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
"MySimpleStruct": {
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
use pretty_assertions::assert_eq;
|
||||
use schemars::schema::*;
|
||||
use schemars::{gen, schema_for};
|
||||
use serde_json::{from_str, to_string_pretty};
|
||||
use std::fs;
|
||||
use std::error::Error;
|
||||
|
||||
#[test]
|
||||
fn schema_matches_default_settings() -> Result<(), Box<dyn Error>> {
|
||||
let expected_json = fs::read_to_string("tests/schema.json")?;
|
||||
let expected: Schema = from_str(&expected_json)?;
|
||||
|
||||
let actual = schema_for!(Schema)?;
|
||||
fs::write("tests/schema.actual.json", to_string_pretty(&actual)?)?;
|
||||
|
||||
assert_eq!(actual, expected, "Generated schema did not match saved schema - generated schema has been written to \"tests/schema.actual.json\".");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn schema_matches_openapi3() -> Result<(), Box<dyn Error>> {
|
||||
let expected_json = fs::read_to_string("tests/schema-openapi3.json")?;
|
||||
let expected: Schema = from_str(&expected_json)?;
|
||||
|
||||
let actual = gen::SchemaSettings::openapi3()
|
||||
.into_generator()
|
||||
.into_root_schema_for::<Schema>()?;
|
||||
fs::write(
|
||||
"tests/schema-openapi3.actual.json",
|
||||
to_string_pretty(&actual)?,
|
||||
)?;
|
||||
|
||||
assert_eq!(actual, expected, "Generated schema did not match saved schema - generated schema has been written to \"tests/schema-openapi3.actual.json\".");
|
||||
Ok(())
|
||||
}
|
|
@ -1,87 +1,41 @@
|
|||
use pretty_assertions::assert_eq;
|
||||
use schemars::{schema_for, MakeSchema};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::error::Error;
|
||||
use schemars::MakeSchema;
|
||||
|
||||
const EXPECTED: &str = r#"
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "MyStruct_For_Integer_And_Null_And_Boolean_And_Array_Of_String",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"float": {
|
||||
"type": "number"
|
||||
},
|
||||
"t": {
|
||||
"type": "integer"
|
||||
},
|
||||
"u": {
|
||||
"type": "null"
|
||||
},
|
||||
"v": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"w": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"#;
|
||||
mod util;
|
||||
use util::*;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, MakeSchema)]
|
||||
struct MyStruct<T, U, V, W> {
|
||||
t: T,
|
||||
u: U,
|
||||
float: f32,
|
||||
v: V,
|
||||
w: W,
|
||||
#[derive(MakeSchema)]
|
||||
pub struct MyStruct<T, U, V, W> {
|
||||
pub t: T,
|
||||
pub u: U,
|
||||
pub v: V,
|
||||
pub w: W,
|
||||
pub inner: MySimpleStruct,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, MakeSchema)]
|
||||
#[serde(rename = "MyStruct")]
|
||||
struct MyRenamedStruct<T, U, V, W> {
|
||||
t: T,
|
||||
u: U,
|
||||
float: f32,
|
||||
v: V,
|
||||
w: W,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, MakeSchema)]
|
||||
#[serde(remote = "MyStruct")]
|
||||
struct MyRemoteStruct<T, U, V, W> {
|
||||
t: T,
|
||||
u: U,
|
||||
float: f32,
|
||||
v: V,
|
||||
w: W,
|
||||
}
|
||||
#[derive(MakeSchema)]
|
||||
pub struct MySimpleStruct {}
|
||||
|
||||
#[test]
|
||||
fn default_name_multiple_type_params() -> Result<(), Box<dyn Error>> {
|
||||
let actual = schema_for!(MyStruct<i32, (), bool, Vec<String>>)?;
|
||||
let expected = serde_json::from_str(EXPECTED)?;
|
||||
assert_eq!(actual, expected);
|
||||
Ok(())
|
||||
fn default_name_multiple_type_params() -> TestResult {
|
||||
test_default_generated_schema::<MyStruct<i32, (), bool, Vec<String>>>("naming-default")
|
||||
}
|
||||
|
||||
#[derive(MakeSchema)]
|
||||
#[serde(rename = "a-new-name-<W>-<T>-<T>")]
|
||||
pub struct MyRenamedStruct<T, U, V, W> {
|
||||
pub t: T,
|
||||
pub u: U,
|
||||
pub v: V,
|
||||
pub w: W,
|
||||
pub inner: MySimpleRenamedStruct,
|
||||
}
|
||||
|
||||
#[derive(MakeSchema)]
|
||||
#[serde(rename = "another-new-name")]
|
||||
pub struct MySimpleRenamedStruct {}
|
||||
|
||||
#[test]
|
||||
#[ignore] // not yet implemented
|
||||
fn overriden_with_rename_name_multiple_type_params() -> Result<(), Box<dyn Error>> {
|
||||
let actual = schema_for!(MyRenamedStruct<i32, (), bool, Vec<String>>)?;
|
||||
let expected = serde_json::from_str(EXPECTED)?;
|
||||
assert_eq!(actual, expected);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore] // not yet implemented
|
||||
fn overriden_with_remote_name_multiple_type_params() -> Result<(), Box<dyn Error>> {
|
||||
let actual = schema_for!(MyRemoteStruct<i32, (), bool, Vec<String>>)?;
|
||||
let expected = serde_json::from_str(EXPECTED)?;
|
||||
assert_eq!(actual, expected);
|
||||
Ok(())
|
||||
fn overriden_with_rename_name_multiple_type_params() -> TestResult {
|
||||
test_default_generated_schema::<MyRenamedStruct<i32, (), bool, Vec<String>>>("naming-custom")
|
||||
}
|
||||
|
|
15
schemars/tests/schema_for_schema.rs
Normal file
15
schemars/tests/schema_for_schema.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
use schemars::gen::SchemaSettings;
|
||||
use schemars::schema::Schema;
|
||||
|
||||
mod util;
|
||||
use util::*;
|
||||
|
||||
#[test]
|
||||
fn schema_matches_default_settings() -> TestResult {
|
||||
test_default_generated_schema::<Schema>("schema")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn schema_matches_openapi3() -> TestResult {
|
||||
test_generated_schema::<Schema>("schema-openapi3", SchemaSettings::openapi3())
|
||||
}
|
37
schemars/tests/util.rs
Normal file
37
schemars/tests/util.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
use pretty_assertions::assert_eq;
|
||||
use schemars::{gen::SchemaSettings, schema_for, MakeSchema};
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::panic;
|
||||
|
||||
pub type TestResult = Result<(), Box<dyn Error>>;
|
||||
|
||||
pub fn test_default_generated_schema<T: MakeSchema>(file: &str) -> TestResult {
|
||||
let expected_json = fs::read_to_string(format!("tests/expected/{}.json", file))?;
|
||||
let expected = serde_json::from_str(&expected_json)?;
|
||||
|
||||
let actual = schema_for!(T)?;
|
||||
|
||||
if actual != expected {
|
||||
let actual_json = serde_json::to_string_pretty(&actual)?;
|
||||
fs::write(format!("tests/actual/{}.json", file), actual_json)?;
|
||||
}
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn test_generated_schema<T: MakeSchema>(file: &str, settings: SchemaSettings) -> TestResult {
|
||||
let expected_json = fs::read_to_string(format!("tests/expected/{}.json", file))?;
|
||||
let expected = serde_json::from_str(&expected_json)?;
|
||||
|
||||
let actual = settings.into_generator().into_root_schema_for::<T>()?;
|
||||
|
||||
if actual != expected {
|
||||
let actual_json = serde_json::to_string_pretty(&actual)?;
|
||||
fs::write(format!("tests/actual/{}.json", file), actual_json)?;
|
||||
}
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
Ok(())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue