-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Libs(Rust): add a "kitchen sink" test to verify null serialization
Just adding a little something to check how fields typed as nullable collections are serialized. New in this version of codegen are the "double Option" fields, allowing us to set fields to null explicitly during PATCH requests! I didn't test that, however. I tested that we're not sending "empty" values when we shouldn't be since those would result in 422 responses for clients who are otherwise following the rules.
- Loading branch information
1 parent
1b88cf5
commit 7912eb9
Showing
2 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
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,150 @@ | ||
use std::collections::HashSet; | ||
use svix::{ | ||
api::{ApplicationIn, EndpointIn, EndpointPatch, EventTypeIn, Svix, SvixOptions}, | ||
error::Error, | ||
}; | ||
|
||
fn get_test_client() -> Svix { | ||
let token = std::env::var("SVIX_TOKEN").expect("SVIX_TOKEN is required to run this test"); | ||
let server_url = | ||
std::env::var("SVIX_SERVER_URL").expect("SVIX_SERVER_URL is required to run this test"); | ||
Svix::new( | ||
token, | ||
Some(SvixOptions { | ||
debug: false, | ||
server_url: Some(server_url), | ||
timeout: None, | ||
}), | ||
) | ||
} | ||
|
||
fn check_for_conflict(e: Error) { | ||
match e { | ||
Error::Http(e) => { | ||
assert_eq!( | ||
e.status, | ||
http02::StatusCode::CONFLICT, | ||
"conflicts are expected but other statuses are not" | ||
); | ||
} | ||
_ => panic!("unexpected error: {e}"), | ||
} | ||
} | ||
|
||
// Opt-in with `cargo test --ignored` | ||
#[ignore] | ||
#[tokio::test] | ||
async fn test_endpoint_crud() { | ||
let client = get_test_client(); | ||
|
||
let app = client | ||
.application() | ||
.create( | ||
ApplicationIn { | ||
name: "app".to_string(), | ||
..Default::default() | ||
}, | ||
None, | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
if let Err(e) = client | ||
.event_type() | ||
.create( | ||
EventTypeIn { | ||
name: String::from("event.started"), | ||
description: String::from("Something started"), | ||
..Default::default() | ||
}, | ||
None, | ||
) | ||
.await | ||
{ | ||
check_for_conflict(e); | ||
} | ||
|
||
if let Err(e) = client | ||
.event_type() | ||
.create( | ||
EventTypeIn { | ||
name: String::from("event.ended"), | ||
description: String::from("Something ended"), | ||
..Default::default() | ||
}, | ||
None, | ||
) | ||
.await | ||
{ | ||
check_for_conflict(e); | ||
} | ||
|
||
let ep = client | ||
.endpoint() | ||
.create( | ||
app.id.clone(), | ||
EndpointIn { | ||
channels: Some(Some(vec![String::from("ch0"), String::from("ch1")])), | ||
url: String::from("https://example.svix.com/"), | ||
..Default::default() | ||
}, | ||
None, | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let want_channels: HashSet<_> = [String::from("ch0"), String::from("ch1")] | ||
.into_iter() | ||
.collect(); | ||
let got_channels = ep.channels.clone().unwrap().unwrap().into_iter().collect(); | ||
assert_eq!(want_channels, got_channels); | ||
assert_eq!( | ||
0, | ||
ep.filter_types | ||
.unwrap_or_default() | ||
.unwrap_or_default() | ||
.len() | ||
); | ||
|
||
let ep_patched = client | ||
.endpoint() | ||
.patch( | ||
app.id.clone(), | ||
ep.id.clone(), | ||
EndpointPatch { | ||
filter_types: Some(Some(vec![ | ||
String::from("event.started"), | ||
String::from("event.ended"), | ||
])), | ||
..Default::default() | ||
}, | ||
None, | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let want_filter_types: HashSet<_> = | ||
[String::from("event.started"), String::from("event.ended")] | ||
.into_iter() | ||
.collect(); | ||
let got_channels = ep_patched | ||
.channels | ||
.clone() | ||
.unwrap() | ||
.unwrap() | ||
.into_iter() | ||
.collect(); | ||
let got_filter_types = ep_patched | ||
.filter_types | ||
.clone() | ||
.unwrap() | ||
.unwrap() | ||
.into_iter() | ||
.collect(); | ||
assert_eq!(want_channels, got_channels); | ||
assert_eq!(want_filter_types, got_filter_types); | ||
|
||
// Should complete without error if the deserialization handles empty bodies | ||
// correctly. | ||
client.endpoint().delete(app.id, ep.id).await.unwrap(); | ||
} |