Skip to content

run round_trip's assertions in separate threads #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 5, 2020
2 changes: 1 addition & 1 deletion wundergraph_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ diesel = "1.4"

[dev-dependencies]
dotenv = "0.15"
insta = "0.12"
insta = { version = "0.16", features = ["backtrace"] }
tempdir = "0.3"
reqwest = "0.9"
serde_json = "1"
Expand Down
2 changes: 1 addition & 1 deletion wundergraph_cli/src/infer_schema_internals/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ fn load_table_names_returns_error_when_given_schema_name() {
let table_names = load_table_names(&conn, Some("stuff"));
match table_names {
Ok(_) => panic!("Expected load_table_names to return an error"),
Err(e) => assert!(e.description().starts_with(
Err(e) => assert!(e.to_string().starts_with(
"sqlite cannot infer \
schema for databases"
)),
Expand Down
68 changes: 40 additions & 28 deletions wundergraph_cli/src/print_schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ mod tests {
}
}

#[cfg(feature = "postgres")]
const BACKEND: &str = "postgres";

#[cfg(feature = "sqlite")]
const BACKEND: &str = "sqlite";

#[cfg(feature = "postgres")]
const MIGRATION: &[&str] = &[
"CREATE SCHEMA infer_test;",
Expand Down Expand Up @@ -151,7 +157,9 @@ mod tests {
print(&conn, None, &mut out).unwrap();

let s = String::from_utf8(out).unwrap();
insta::assert_snapshot!(&s);
insta::with_settings!({snapshot_suffix => BACKEND}, {
insta::assert_snapshot!("infer_schema", &s);
});
}

#[test]
Expand Down Expand Up @@ -311,41 +319,45 @@ mod tests {
std::thread::sleep(std::time::Duration::from_secs(1));

let query = "{\"query\": \"{ Users { id name } } \"}";
let mut r = client
.post(&format!("http://{}/graphql", listen_url))
.body(query)
.header(
reqwest::header::CONTENT_TYPE,
reqwest::header::HeaderValue::from_static("application/json"),
)
.send()
.unwrap();
insta::assert_json_snapshot!(r.json::<serde_json::Value>().unwrap());

let mutation = r#"{"query":"mutation CreateUser {\n CreateUser(NewUser: {name: \"Max\"}) {\n id\n name\n }\n}","variables":null,"operationName":"CreateUser"}"#;
let mut r = client
.post(&format!("http://{}/graphql", listen_url))
.body(mutation)
.header(
reqwest::header::CONTENT_TYPE,
reqwest::header::HeaderValue::from_static("application/json"),
)
.send()
.unwrap();
insta::assert_json_snapshot!(r.json::<serde_json::Value>().unwrap());
let t1 = request_test(&client, &listen_url, query, "round_trip_test__query_1");
let t2 = request_test(&client, &listen_url, mutation, "round_trip_test__mutation");
let t3 = request_test(&client, &listen_url, query, "round_trip_test__query_2");

child.kill().unwrap();
child.wait().unwrap();

t1.unwrap();
t2.unwrap();
t3.unwrap();
}

fn request_test(
client: &reqwest::Client,
listen_url: &str,
body: &'static str,
snapshot_name: &'static str,
) -> Result<(), String> {
fn error_mapper<T: std::fmt::Debug>(e: T) -> String {
format!("{:?}", e)
}

let mut r = client
.post(&format!("http://{}/graphql", listen_url))
.body(query)
.body(body)
.header(
reqwest::header::CONTENT_TYPE,
reqwest::header::HeaderValue::from_static("application/json"),
)
.send()
.unwrap();
insta::assert_json_snapshot!(r.json::<serde_json::Value>().unwrap());

child.kill().unwrap();
child.wait().unwrap();
.map_err(error_mapper)?;
let r = r.json::<serde_json::Value>().map_err(error_mapper)?;
std::panic::catch_unwind(|| {
insta::with_settings!({snapshot_suffix => ""}, {
insta::assert_json_snapshot!(snapshot_name, r)
})
})
.map_err(error_mapper)?;
Ok(())
}
}
11 changes: 10 additions & 1 deletion wundergraph_cli/src/print_schema/print_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,16 @@ impl<'a> Display for GraphqlInsertable<'a> {
{
let mut out = PadAdapter::new(f);
writeln!(out)?;
for c in self.table.column_data.iter().filter(|c| !c.has_default) {
for c in self.table.column_data.iter().filter(|c| {
!c.has_default
&& self
.table
.primary_key
.iter()
.filter(|x| **x == c.sql_name)
.count()
== 0
}) {
let t = GraphqlType {
sql_type: &c.ty,
allow_option: true,
Expand Down
17 changes: 0 additions & 17 deletions wundergraph_cli/src/print_schema/snapshots/tests__main-3.snap

This file was deleted.

9 changes: 0 additions & 9 deletions wundergraph_cli/src/print_schema/snapshots/tests__main-4.snap

This file was deleted.

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ wundergraph::query_object!{
#[graphql(scalar = "WundergraphScalarValue")]
#[table_name = "comments"]
pub struct NewComment {
id: i32,
post: Option<i32>,
commenter: Option<i32>,
content: String,
Expand All @@ -107,7 +106,6 @@ pub struct CommentChangeset {
#[graphql(scalar = "WundergraphScalarValue")]
#[table_name = "posts"]
pub struct NewPost {
id: i32,
author: Option<i32>,
title: String,
datetime: Option<chrono::naive::NaiveDateTime>,
Expand All @@ -130,7 +128,6 @@ pub struct PostChangeset {
#[graphql(scalar = "WundergraphScalarValue")]
#[table_name = "users"]
pub struct NewUser {
id: i32,
name: String,
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: wundergraph_cli/src/print_schema/mod.rs
expression: "r.json::<serde_json::Value>().unwrap()"
expression: r
---
{
"data": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: wundergraph_cli/src/print_schema/mod.rs
expression: "r.json::<serde_json::Value>().unwrap()"
expression: r
---
{
"data": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: wundergraph_cli/src/print_schema/mod.rs
expression: "r.json::<serde_json::Value>().unwrap()"
expression: r
---
{
"data": {
Expand Down