Skip to content

Commit

Permalink
fix with regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtbuilds committed Dec 14, 2023
1 parent e3ca1f5 commit c737728
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 7 deletions.
6 changes: 3 additions & 3 deletions libninja/src/rust/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,11 @@ pub fn to_rust_example_value(ty: &Ty, name: &str, spec: &HirSpec, use_ref_value:
}
Ty::Model(model) => {
let record = spec.get_record(model)?;
let force_not_ref = model.ends_with("Required");
let force_ref = model.ends_with("Required");
match record {
Record::Struct(Struct { name: _name, fields, nullable }) => {
let fields = fields.iter().map(|(name, field)| {
let not_ref = force_not_ref || field.optional;
let not_ref = !force_ref || field.optional;
let mut value = to_rust_example_value(&field.ty, name, spec, !not_ref)?;
let name = name.to_rust_ident();
if field.optional {
Expand All @@ -339,7 +339,7 @@ pub fn to_rust_example_value(ty: &Ty, name: &str, spec: &HirSpec, use_ref_value:
quote!(#model::#variant)
}
Record::TypeAlias(name, HirField { ty, optional, .. }) => {
let not_ref = force_not_ref || !optional;
let not_ref = !force_ref || !optional;
let ty = to_rust_example_value(ty, name, spec, not_ref)?;
if *optional {
quote!(Some(#ty))
Expand Down
File renamed without changes.
File renamed without changes.
96 changes: 96 additions & 0 deletions libninja/tests/files/plaid_watchlist.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
openapi: 3.0.0
info:
title: The Plaid API
description: The Plaid REST API. Please see https://plaid.com/docs/api for more details.
termsOfService: https://plaid.com/legal/
contact:
name: Plaid Developer Team
url: https://plaid.com
version: 2020-09-14_1.474.3
servers:
- url: https://production.plaid.com
description: Production
- url: https://development.plaid.com
description: Development
- url: https://sandbox.plaid.com
description: Sandbox
paths:
/watchlist_screening/individual/create:
post:
tags:
- plaid
summary: Create a watchlist screening for a person
description: Create a new Watchlist Screening to check your customer against watchlists defined in the associated Watchlist Program. If your associated program has ongoing screening enabled, this is the profile information that will be used to monitor your customer over time.
externalDocs:
url: https://plaid.com/docs/api/products/monitor/#watchlist_screeningindividualcreate
operationId: watchlistScreeningIndividualCreate
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/WatchlistScreeningIndividualCreateRequest'
required: true
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
examples:
example-1:
value:
assignee: 54350110fedcbaf01234ffee
audit_trail:
dashboard_user_id: 54350110fedcbaf01234ffee
source: dashboard
timestamp: 2020-07-24T03:26:02Z
client_user_id: your-db-id-3b24110
id: scr_52xR9LKo77r1Np
request_id: saKrIBuEB9qJZng
search_terms:
country: US
date_of_birth: 1990-05-29
document_number: C31195855
legal_name: Aleksey Potemkin
version: 1
watchlist_program_id: prg_2eRPsDnL66rZ7H
status: cleared
x-plaid-business-unit-context: BUSINESS_UNIT_PLAID

components:
schemas:
WatchlistScreeningIndividualCreateRequest:
description: Request input for creating an individual watchlist screening
type: object
properties:
search_terms:
$ref: '#/components/schemas/WatchlistScreeningRequestSearchTerms'
client_user_id:
$ref: '#/components/schemas/ClientUserID'
required:
- search_terms
ClientUserID:
example: your-db-id-3b24110
title: ClientUserID
description: A unique ID that identifies the end user in your system. This ID can also be used to associate user-specific data from other Plaid products. Financial Account Matching requires this field and the `/link/token/create` `client_user_id` to be consistent. Personally identifiable information, such as an email address or phone number, should not be used in the `client_user_id`.
type: string
minLength: 1
WatchlistScreeningRequestSearchTerms:
description: Search inputs for creating a watchlist screening
type: object
properties:
watchlist_program_id:
type: string
legal_name:
type: string
date_of_birth:
type: string
format: date
document_number:
type: string
country:
type: string
required:
- watchlist_program_id
- legal_name
19 changes: 19 additions & 0 deletions libninja/tests/files/plaid_watchlist_expected.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use plaid::PlaidClient;
use plaid::model::*;
#[tokio::main]
async fn main() {
let client = PlaidClient::from_env();
let search_terms = WatchlistScreeningRequestSearchTerms {
country: Some("your country".to_owned()),
date_of_birth: Some(chrono::Utc::now().date_naive()),
document_number: Some("your document number".to_owned()),
legal_name: "your legal name".to_owned(),
watchlist_program_id: "your watchlist program id".to_owned(),
};
let response = client
.watchlist_screening_individual_create(search_terms)
.client_user_id("your client user id")
.await
.unwrap();
println!("{:#?}", response);
}
27 changes: 23 additions & 4 deletions libninja/tests/test_example_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,30 @@ use ln_core::{extract_spec, PackageConfig};
use ln_core::extractor::add_operation_models;
use pretty_assertions::assert_eq;

const PLAID_PROCESSOR: &str = include_str!("plaid_processor/plaid.processor.yaml");

#[test]
fn test_example_generation_with_refs() {
let spec: OpenAPI = serde_yaml::from_str(PLAID_PROCESSOR).unwrap();
let s = include_str!("files/plaid_processor.yaml");
let spec: OpenAPI = serde_yaml::from_str(s).unwrap();
let spec = extract_spec(&spec).unwrap();
let spec = add_operation_models(Language::Rust, spec).unwrap();

let op = spec.operations.iter().next().unwrap();
let opt = PackageConfig {
package_name: "plaid".to_string(),
service_name: "Plaid".to_string(),
language: Language::Rust,
package_version: "1.0".to_string(),
config: Default::default(),
dest: Default::default(),
};
let example = rust::generate_example(op, &opt, &spec).unwrap();
assert_eq!(example, include_str!("files/plaid_processor_expected.rs"));
}

#[test]
fn test_example_generation_with_refs2() {
let s = include_str!("files/plaid_watchlist.yaml");
let spec: OpenAPI = serde_yaml::from_str(s).unwrap();
let spec = extract_spec(&spec).unwrap();
let spec = add_operation_models(Language::Rust, spec).unwrap();

Expand All @@ -23,5 +42,5 @@ fn test_example_generation_with_refs() {
dest: Default::default(),
};
let example = rust::generate_example(op, &opt, &spec).unwrap();
assert_eq!(example, include_str!("plaid_processor/expected.rs"));
assert_eq!(example, include_str!("files/plaid_watchlist_expected.rs"));
}

0 comments on commit c737728

Please sign in to comment.