Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
test(cargo-swagg): add snapshot tests for components
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysova committed Mar 22, 2020
1 parent c119a4a commit a96b24f
Show file tree
Hide file tree
Showing 6 changed files with 593 additions and 289 deletions.
289 changes: 0 additions & 289 deletions cargo-swagg/src/main.rs
Expand Up @@ -3,16 +3,6 @@ use std::fs;

mod printer;

use printer::{
api::{ApiModule, ApiStruct, BindApiMethod, HttpMethod, ImplApi},
components::{
parameters::ParametersModule, request_bodies::RequestBodiesModule, responses::ResponsesModule, Component,
ComponentsModule, EnumVariant, Field, FieldType, FormatFloat, FormatInteger, FormatString, NativeType,
},
paths::{ContentType, Path, PathsModule, QueryParam, ResponseEnum, ResponseStatus, StatusVariant},
GeneratedModule, Printable,
};

#[cfg(test)]
pub mod test;

Expand All @@ -23,284 +13,5 @@ fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {

// println!("{}", "OAuthAuthorizeRequest".to_snake_case());

let api: ApiStruct = api.info.into();

let m1 = BindApiMethod {
method: HttpMethod::Get,
name: "sessionGet".to_owned(),
path: "/session".to_owned(),
request_body: None,
};

let m2 = BindApiMethod {
method: HttpMethod::Post,
name: "sessionCreate".to_owned(),
path: "/session".to_owned(),
request_body: Some("SessionCreateBody".to_owned()),
};

let m3 = BindApiMethod {
method: HttpMethod::Post,
name: "registerConfirmation".to_owned(),
path: "/register/confirmation".to_owned(),
request_body: Some("RegisterConfirmation".to_owned()),
};

let methods = ImplApi {
api_name: api.api_name.clone(),
methods: vec![m1, m2, m3],
};

let api_module = ApiModule { api, methods };

let components_module =
ComponentsModule {
parameters: ParametersModule {
list: vec![
Component::Enum {
name: "OAuthResponseType".to_owned(),
description: Some(
"response_type is set to code indicating that you want an authorization code as the response."
.to_owned(),
),
variants: vec![EnumVariant {
name: "code".to_owned(),
description: None,
}],
},
Component::Type {
name: "OAuthClientId".to_owned(),
description: Some("The client_id is the identifier for your app".to_owned()),
type_value: FieldType::Internal("uuid::Uuid".to_owned()),
},
Component::Type {
name: "OAuthRedirectUri".to_owned(),
description: Some(
"redirect_uri may be optional depending on the API, but is highly recommended".to_owned(),
),
type_value: FieldType::Native(NativeType::String { format: Default::default() }),
},
],
},
responses: ResponsesModule {
list: vec![
Component::Object {
name: "RegisterConfirmationFailed".to_owned(),
fields: vec![Field {
name: "error".to_owned(),
required: true,
description: None,
field_type: FieldType::Custom("RegisterConfirmationFailedError".to_owned()),
}],
description: Some("Answer for registration confirmation".to_owned()),
},
Component::Enum {
name: "RegisterConfirmationFailedError".to_owned(),
variants: vec![
EnumVariant {
name: "code_invalid_or_expired".to_owned(),
description: None,
},
EnumVariant {
name: "email_already_activated".to_owned(),
description: None,
},
EnumVariant {
name: "invalid_form".to_owned(),
description: None,
},
],
description: None,
},
Component::Object {
name: "RegistrationRequestCreated".to_owned(),
description: Some(
"Registration link sent to email, now user can find out when the link expires".to_owned(),
),
fields: vec![Field {
name: "expiresAt".to_owned(),
required: true,
description: Some("UTC Unix TimeStamp when the link expires".to_owned()),
field_type: FieldType::Native(NativeType::Integer {
format: FormatInteger::Int64,
}),
}],
},
],
},
request_bodies: RequestBodiesModule {
list: vec![
Component::Object {
name: "Register".to_owned(),
description: None,
fields: vec![
Field {
name: "email".to_owned(),
required: true,
description: None,
field_type: FieldType::Native(NativeType::String {
format: FormatString::Email,
}),
},
Field {
name: "demo".to_owned(),
required: false,
description: None,
field_type: FieldType::Array(Box::new(FieldType::Array(Box::new(FieldType::Native(
NativeType::String {
format: FormatString::Email,
},
))))),
},
],
},
Component::Object {
name: "RegisterConfirmation".to_owned(),
description: None,
fields: vec![
Field {
name: "confirmationCode".to_owned(),
required: true,
description: None,
field_type: FieldType::Native(NativeType::String {
format: FormatString::default(),
}),
},
Field {
name: "firstName".to_owned(),
required: true,
description: None,
field_type: FieldType::Native(NativeType::String {
format: FormatString::default(),
}),
},
Field {
name: "lastName".to_owned(),
required: true,
description: None,
field_type: FieldType::Native(NativeType::String {
format: FormatString::default(),
}),
},
Field {
name: "password".to_owned(),
required: true,
description: None,
field_type: FieldType::Native(NativeType::String {
format: FormatString::default(),
}),
},
Field {
name: "demo".to_owned(),
required: false,
description: None,
field_type: FieldType::Native(NativeType::Float {
format: FormatFloat::default(),
}),
},
Field {
name: "customizer".to_owned(),
required: false,
description: None,
field_type: FieldType::Internal("crate::app::MySuperType".to_owned()),
},
],
},
],
},
};

let p1 = Path {
name: "registerConfirmation".to_owned(),
query_params: vec![],
response: ResponseEnum {
responses: vec![
StatusVariant {
status: ResponseStatus::Created,
response_type_name: None,
description: None,
content_type: None,
x_variant_name: None,
},
StatusVariant {
status: ResponseStatus::BadRequest,
response_type_name: Some("RegisterConfirmationFailed".to_owned()),
description: None,
content_type: Some(ContentType::Json),
x_variant_name: None,
},
StatusVariant {
status: ResponseStatus::InternalServerError,
response_type_name: None,
description: None,
content_type: Some(ContentType::Json),
x_variant_name: Some("Unexpected".to_owned()),
},
],
},
};

let p2 = Path {
name: "sessionCreate".to_owned(),
query_params: vec![
QueryParam {
name: "responseType".to_owned(),
type_ref: "OAuthResponseType".to_owned(),
description: Some(
"response_type is set to code indicating that you want an authorization code as the response."
.to_owned(),
),
required: true,
},
QueryParam {
name: "redirect_uri".to_owned(),
type_ref: "OAuthRedirectUri".to_owned(),
description: None,
required: false,
},
QueryParam {
name: "GlobalNameOfTheUniverse".to_owned(),
type_ref: "OAuthClientId".to_owned(),
description: None,
required: false,
},
],
response: ResponseEnum {
responses: vec![
StatusVariant {
status: ResponseStatus::Created,
response_type_name: None,
description: Some("User logined, cookies writed\nFoo".to_owned()),
content_type: None,
x_variant_name: None,
},
StatusVariant {
status: ResponseStatus::BadRequest,
response_type_name: Some("sessionCreateFailed".to_owned()),
description: None,
content_type: Some(ContentType::Json),
x_variant_name: None,
},
StatusVariant {
status: ResponseStatus::InternalServerError,
response_type_name: None,
description: None,
content_type: Some(ContentType::Json),
x_variant_name: Some("Unexpected".to_owned()),
},
],
},
};

let paths_module = PathsModule { paths: vec![p1, p2] };

let generated_module = GeneratedModule {
api_module,
components_module,
paths_module,
};

println!("{}", generated_module.print());

Ok(())
}
24 changes: 24 additions & 0 deletions cargo-swagg/src/printer/components/module.rs
Expand Up @@ -26,3 +26,27 @@ impl Printable for ComponentsModule {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::test::shot;
use insta::assert_snapshot;

#[test]
fn components_module_default() {
assert_snapshot!(shot(ComponentsModule::default()), @r###"
pub mod components {
pub mod parameters {
use serde::{Deserialize, Serialize};
}
pub mod request_bodies {
use serde::{Deserialize, Serialize};
}
pub mod responses {
use serde::{Deserialize, Serialize};
}
}
"###);
}
}
26 changes: 26 additions & 0 deletions cargo-swagg/src/printer/components/parameters.rs
Expand Up @@ -24,3 +24,29 @@ pub mod module {
}
}
}

#[cfg(test)]
mod tests {
use super::super::Component;
use super::*;
use crate::test::shot;
use insta::assert_snapshot;

#[test]
fn parameters_with_some_components() {
assert_snapshot!(shot(ParametersModule {
list: vec![
Component::Enum { name: "Example".to_owned(), description: None, variants: vec![] },
Component::Object { name: "Test".to_owned(), description: None, fields: vec![] },
]
}), @r###"
pub mod parameters {
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub enum Example {}
#[derive(Debug, Serialize, Deserialize)]
pub struct Test {}
}
"###);
}
}
26 changes: 26 additions & 0 deletions cargo-swagg/src/printer/components/request_bodies.rs
Expand Up @@ -24,3 +24,29 @@ pub mod module {
}
}
}

#[cfg(test)]
mod tests {
use super::super::Component;
use super::*;
use crate::test::shot;
use insta::assert_snapshot;

#[test]
fn request_bodies_with_some_components() {
assert_snapshot!(shot(RequestBodiesModule {
list: vec![
Component::Enum { name: "Example".to_owned(), description: None, variants: vec![] },
Component::Object { name: "Test".to_owned(), description: None, fields: vec![] },
]
}), @r###"
pub mod request_bodies {
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub enum Example {}
#[derive(Debug, Serialize, Deserialize)]
pub struct Test {}
}
"###);
}
}

0 comments on commit a96b24f

Please sign in to comment.