-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: adding some json standard tests for CMD and CMS (#197)
- Loading branch information
Showing
27 changed files
with
813 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "integrationos-api" | ||
version = "0.1.0" | ||
version = "1.0.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
use std::{ | ||
fs::File, | ||
io::{BufReader, Read}, | ||
}; | ||
|
||
use serde::{de::DeserializeOwned, Serialize}; | ||
use serde_json::Value; | ||
|
||
pub enum CheckType { | ||
Json, | ||
Bson, | ||
} | ||
|
||
pub trait JsonChecker { | ||
/// Check if the Struct passed can be serialized and deserialized using a file | ||
/// as a reference | ||
fn check<T: Serialize + DeserializeOwned + PartialEq>( | ||
&self, | ||
value: &T, | ||
r#type: CheckType, | ||
) -> bool; | ||
|
||
/// The location where the checker will look for the file. If the folder doesn't exist, it will | ||
/// be created. It is always in relation to the current directory. | ||
fn location(&self) -> String { | ||
let current_dir = std::env::current_dir().expect("Failed to get current directory"); | ||
|
||
let json_checks_dir = current_dir.join("tests").join("resource"); | ||
|
||
if !json_checks_dir.exists() { | ||
std::fs::create_dir(&json_checks_dir).expect("Failed to create json_checks directory"); | ||
} | ||
|
||
json_checks_dir | ||
.to_str() | ||
.expect("Failed to convert path to string") | ||
.to_string() | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub struct JsonCheckerImpl { | ||
/// If true, the checker will override the file if it exists | ||
/// or create a new file if it doesn't exist | ||
r#override: bool, | ||
} | ||
|
||
impl JsonCheckerImpl { | ||
pub fn r#override(mut self) -> Self { | ||
self.r#override = true; | ||
self | ||
} | ||
} | ||
|
||
impl JsonChecker for JsonCheckerImpl { | ||
fn check<T: Serialize + DeserializeOwned + PartialEq>( | ||
&self, | ||
value: &T, | ||
r#type: CheckType, | ||
) -> bool { | ||
let type_name = std::any::type_name::<T>().to_string(); | ||
let file_path = match r#type { | ||
CheckType::Json => self.location() + &format!("/{}.json", type_name), | ||
CheckType::Bson => self.location() + &format!("/{}.bson", type_name), | ||
}; | ||
|
||
match r#type { | ||
CheckType::Json => { | ||
let serialized = | ||
serde_json::to_string_pretty(value).expect("Failed to serialize value"); | ||
|
||
let file = File::open(file_path.clone()); | ||
|
||
if self.r#override { | ||
std::fs::write(file_path, serialized).expect("Failed to write to file"); | ||
panic!("Override flag is enabled, remember to disable and commit the changes"); | ||
} | ||
|
||
if file.is_err() { | ||
return false; | ||
} | ||
|
||
let file = file.expect("Failed to open file"); | ||
let mut file = BufReader::new(file); | ||
|
||
let mut contents = String::new(); | ||
file.read_to_string(&mut contents) | ||
.expect("Failed to read file contents"); | ||
|
||
let expected = serde_json::from_str::<Value>(&contents) | ||
.expect("Failed to deserialize expect value"); | ||
|
||
let actual = serde_json::from_str::<Value>(&serialized) | ||
.expect("Failed to deserialize actual value"); | ||
|
||
expected == actual | ||
} | ||
CheckType::Bson => { | ||
let serialized = bson::to_vec(value).expect("Failed to serialize value"); | ||
|
||
let file = File::open(file_path.clone()); | ||
|
||
if self.r#override { | ||
std::fs::write(file_path, serialized).expect("Failed to write to file"); | ||
panic!("Override flag is enabled, remember to disable and commit the changes"); | ||
} | ||
|
||
if file.is_err() { | ||
return false; | ||
} | ||
|
||
let file = file.expect("Failed to open file"); | ||
let mut file = BufReader::new(file); | ||
|
||
let mut contents: Vec<u8> = vec![]; | ||
file.read_to_end(&mut contents) | ||
.expect("Failed to read file"); | ||
|
||
let expected = | ||
bson::from_slice::<T>(&contents).expect("Failed to deserialize expect value"); | ||
|
||
let actual = | ||
bson::from_slice::<T>(&serialized).expect("Failed to deserialize actual value"); | ||
|
||
expected == actual | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub mod checker; | ||
pub mod context; | ||
pub mod http; | ||
pub mod standard; | ||
pub mod worker; |
Binary file added
BIN
+1010 Bytes
integrationos-api/tests/resource/integrationos_domain::domain::connection::Connection.bson
Binary file not shown.
50 changes: 50 additions & 0 deletions
50
integrationos-api/tests/resource/integrationos_domain::domain::connection::Connection.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"_id": "conn::AAAAAAAAAAA::AAAAAAAAAAAAAAAAAAAAAA", | ||
"platformVersion": "platform-version", | ||
"connectionDefinitionId": "conn_def::AAAAAAAAAAA::AAAAAAAAAAAAAAAAAAAAAA", | ||
"type": { | ||
"api": {} | ||
}, | ||
"key": "key", | ||
"group": "group", | ||
"name": "name", | ||
"environment": "live", | ||
"platform": "platform", | ||
"secretsServiceId": "secrets-service-id", | ||
"eventAccessId": "evt_ac::AAAAAAAAAAA::AAAAAAAAAAAAAAAAAAAAAA", | ||
"accessKey": "access-key", | ||
"identity": "identity", | ||
"identityType": "user", | ||
"settings": { | ||
"parseWebhookBody": true, | ||
"showSecret": false, | ||
"allowCustomEvents": false, | ||
"oauth": false | ||
}, | ||
"throughput": { | ||
"key": "throughput-key", | ||
"limit": 100 | ||
}, | ||
"ownership": { | ||
"buildableId": "owner-id", | ||
"clientId": "client-id", | ||
"organizationId": "organization-id", | ||
"projectId": "project-id", | ||
"userId": "user-id" | ||
}, | ||
"oauth": { | ||
"enabled": { | ||
"connection_oauth_definition_id": "conn_oauth_def::AAAAAAAAAAA::AAAAAAAAAAAAAAAAAAAAAA", | ||
"expires_in": 100, | ||
"expires_at": 100 | ||
} | ||
}, | ||
"createdAt": 0, | ||
"updatedAt": 0, | ||
"updated": false, | ||
"version": "1.0.0", | ||
"lastModifiedBy": "system", | ||
"deleted": false, | ||
"active": true, | ||
"deprecated": false | ||
} |
Binary file added
BIN
+1.1 KB
...ntegrationos_domain::domain::connection::connection_definition::ConnectionDefinition.bson
Binary file not shown.
Oops, something went wrong.