From 39328479034c684fc1cbc0b6f6b2679db189f434 Mon Sep 17 00:00:00 2001 From: Edgar Date: Wed, 14 Oct 2020 10:48:46 +0200 Subject: [PATCH] reorganize tests --- src/lib.rs | 87 +++++++++++++++++++++++++++++++++++++++++++--------- src/tests.rs | 51 ------------------------------ 2 files changed, 73 insertions(+), 65 deletions(-) delete mode 100644 src/tests.rs diff --git a/src/lib.rs b/src/lib.rs index edf9ef2..716a1db 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,14 +12,9 @@ #![deny(missing_docs)] -#[cfg(test)] -mod tests; - -extern crate chrono; - +pub mod common; pub mod errors; pub mod invoice; -pub mod common; pub mod orders; use reqwest::header; @@ -81,6 +76,7 @@ pub struct Client { /// /// Note: You can avoid most fields by the Default impl like so: /// ``` +/// use paypal_rs::Query; /// let query = Query { count: Some(40), ..Default::default() }; /// ``` #[derive(Debug, Default, Serialize)] @@ -173,15 +169,21 @@ impl Client { /// # Examples /// /// ``` - /// let clientid = std::env::var("PAYPAL_CLIENTID").unwrap(); - /// let secret = std::env::var("PAYPAL_SECRET").unwrap(); + /// use paypal_rs::Client; /// - /// let mut client = paypal_rs::Client::new( - /// clientid, - /// secret, - /// true, - /// ); - /// client.get_access_token().await.unwrap(); + /// #[tokio::main] + /// async fn main() { + /// # dotenv::dotenv().ok(); + /// let clientid = std::env::var("PAYPAL_CLIENTID").unwrap(); + /// let secret = std::env::var("PAYPAL_SECRET").unwrap(); + /// + /// let mut client = Client::new( + /// clientid, + /// secret, + /// true, + /// ); + /// client.get_access_token().await.unwrap(); + /// } /// ``` pub fn new>(client_id: S, secret: S, sandbox: bool) -> Client { Client { @@ -290,3 +292,60 @@ impl Client { } } } + +#[cfg(test)] +mod tests { + use crate::{orders::*, Client, HeaderParams, Prefer}; + use std::env; + + async fn create_client() -> Client { + dotenv::dotenv().ok(); + let clientid = env::var("PAYPAL_CLIENTID").unwrap(); + let secret = env::var("PAYPAL_SECRET").unwrap(); + + let mut client = Client::new(clientid, secret, true); + + assert_eq!(client.get_access_token().await.is_err(), false, "should not error"); + client + } + + #[tokio::test] + async fn test_order() { + let client = create_client().await; + + let order = OrderPayload::new(Intent::Authorize, vec![PurchaseUnit::new(Amount::new("EUR", "10.0"))]); + + let ref_id = format!( + "TEST-{:?}", + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs() + ); + + let order_created = client + .create_order( + order, + HeaderParams { + prefer: Some(Prefer::Representation), + request_id: Some(ref_id.clone()), + ..Default::default() + }, + ) + .await + .unwrap(); + + assert_ne!(order_created.id, ""); + assert_eq!(order_created.status, OrderStatus::Created); + assert_eq!(order_created.links.len(), 4); + + client + .update_order( + order_created.id, + Some(Intent::Capture), + Some(order_created.purchase_units.expect("to exist")), + ) + .await + .unwrap(); + } +} diff --git a/src/tests.rs b/src/tests.rs deleted file mode 100644 index 0a52c8a..0000000 --- a/src/tests.rs +++ /dev/null @@ -1,51 +0,0 @@ -use crate::{ - orders::*, - Client, HeaderParams, Prefer, -}; -use std::env; - -#[tokio::test] -async fn it_works() { - dotenv::dotenv().ok(); - let clientid = env::var("PAYPAL_CLIENTID").unwrap(); - let secret = env::var("PAYPAL_SECRET").unwrap(); - - let mut client = Client::new(clientid, secret, true); - - assert_eq!(client.get_access_token().await.is_err(), false, "should not error"); - - let order = OrderPayload::new(Intent::Authorize, vec![PurchaseUnit::new(Amount::new("EUR", "10.0"))]); - - let ref_id = format!( - "TEST-{:?}", - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap() - .as_secs() - ); - - let order_created = client - .create_order( - order, - HeaderParams { - prefer: Some(Prefer::Representation), - request_id: Some(ref_id.clone()), - ..Default::default() - }, - ) - .await - .unwrap(); - - assert!(order_created.id != "", "order id is not empty"); - assert_eq!(order_created.status, OrderStatus::Created, "order status is created"); - assert_eq!(order_created.links.len(), 4, "order links exist"); - - client - .update_order( - order_created.id, - Some(Intent::Capture), - Some(order_created.purchase_units.expect("to exist")), - ) - .await - .unwrap(); -}