Skip to content

Commit

Permalink
Libs(Rust): add a "kitchen sink" test to verify null serialization
Browse files Browse the repository at this point in the history
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
svix-onelson committed Oct 17, 2024
1 parent 1b88cf5 commit 7912eb9
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
3 changes: 3 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ url = "2.2"
tokio = { version = "1.38.0", features = ["time"] }
serde_with = { version = "^3.8", default-features = false, features = ["base64", "std", "macros"] }

[dev-dependencies]
tokio = { version = "1.38.0", features = ["macros"] }

[package.metadata.cargo-public-api-crates]
allowed = [
"http",
Expand Down
150 changes: 150 additions & 0 deletions rust/tests/kitchen_sink.rs
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();
}

0 comments on commit 7912eb9

Please sign in to comment.