Skip to content

Commit ffbb883

Browse files
authored
Merge pull request #53 from p-alik/issue-50
run round_trip's assertions in separate threads
2 parents 6c8cb7b + ad775a3 commit ffbb883

12 files changed

+55
-72
lines changed

wundergraph_cli/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ diesel = "1.4"
1717

1818
[dev-dependencies]
1919
dotenv = "0.15"
20-
insta = "0.12"
20+
insta = { version = "0.16", features = ["backtrace"] }
2121
tempdir = "0.3"
2222
reqwest = "0.9"
2323
serde_json = "1"

wundergraph_cli/src/infer_schema_internals/sqlite.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ fn load_table_names_returns_error_when_given_schema_name() {
277277
let table_names = load_table_names(&conn, Some("stuff"));
278278
match table_names {
279279
Ok(_) => panic!("Expected load_table_names to return an error"),
280-
Err(e) => assert!(e.description().starts_with(
280+
Err(e) => assert!(e.to_string().starts_with(
281281
"sqlite cannot infer \
282282
schema for databases"
283283
)),

wundergraph_cli/src/print_schema/mod.rs

+40-28
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ mod tests {
8282
}
8383
}
8484

85+
#[cfg(feature = "postgres")]
86+
const BACKEND: &str = "postgres";
87+
88+
#[cfg(feature = "sqlite")]
89+
const BACKEND: &str = "sqlite";
90+
8591
#[cfg(feature = "postgres")]
8692
const MIGRATION: &[&str] = &[
8793
"CREATE SCHEMA infer_test;",
@@ -151,7 +157,9 @@ mod tests {
151157
print(&conn, None, &mut out).unwrap();
152158

153159
let s = String::from_utf8(out).unwrap();
154-
insta::assert_snapshot!(&s);
160+
insta::with_settings!({snapshot_suffix => BACKEND}, {
161+
insta::assert_snapshot!("infer_schema", &s);
162+
});
155163
}
156164

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

313321
let query = "{\"query\": \"{ Users { id name } } \"}";
314-
let mut r = client
315-
.post(&format!("http://{}/graphql", listen_url))
316-
.body(query)
317-
.header(
318-
reqwest::header::CONTENT_TYPE,
319-
reqwest::header::HeaderValue::from_static("application/json"),
320-
)
321-
.send()
322-
.unwrap();
323-
insta::assert_json_snapshot!(r.json::<serde_json::Value>().unwrap());
324-
325322
let mutation = r#"{"query":"mutation CreateUser {\n CreateUser(NewUser: {name: \"Max\"}) {\n id\n name\n }\n}","variables":null,"operationName":"CreateUser"}"#;
326-
let mut r = client
327-
.post(&format!("http://{}/graphql", listen_url))
328-
.body(mutation)
329-
.header(
330-
reqwest::header::CONTENT_TYPE,
331-
reqwest::header::HeaderValue::from_static("application/json"),
332-
)
333-
.send()
334-
.unwrap();
335-
insta::assert_json_snapshot!(r.json::<serde_json::Value>().unwrap());
323+
let t1 = request_test(&client, &listen_url, query, "round_trip_test__query_1");
324+
let t2 = request_test(&client, &listen_url, mutation, "round_trip_test__mutation");
325+
let t3 = request_test(&client, &listen_url, query, "round_trip_test__query_2");
326+
327+
child.kill().unwrap();
328+
child.wait().unwrap();
329+
330+
t1.unwrap();
331+
t2.unwrap();
332+
t3.unwrap();
333+
}
334+
335+
fn request_test(
336+
client: &reqwest::Client,
337+
listen_url: &str,
338+
body: &'static str,
339+
snapshot_name: &'static str,
340+
) -> Result<(), String> {
341+
fn error_mapper<T: std::fmt::Debug>(e: T) -> String {
342+
format!("{:?}", e)
343+
}
336344

337345
let mut r = client
338346
.post(&format!("http://{}/graphql", listen_url))
339-
.body(query)
347+
.body(body)
340348
.header(
341349
reqwest::header::CONTENT_TYPE,
342350
reqwest::header::HeaderValue::from_static("application/json"),
343351
)
344352
.send()
345-
.unwrap();
346-
insta::assert_json_snapshot!(r.json::<serde_json::Value>().unwrap());
347-
348-
child.kill().unwrap();
349-
child.wait().unwrap();
353+
.map_err(error_mapper)?;
354+
let r = r.json::<serde_json::Value>().map_err(error_mapper)?;
355+
std::panic::catch_unwind(|| {
356+
insta::with_settings!({snapshot_suffix => ""}, {
357+
insta::assert_json_snapshot!(snapshot_name, r)
358+
})
359+
})
360+
.map_err(error_mapper)?;
361+
Ok(())
350362
}
351363
}

wundergraph_cli/src/print_schema/print_helper.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,16 @@ impl<'a> Display for GraphqlInsertable<'a> {
475475
{
476476
let mut out = PadAdapter::new(f);
477477
writeln!(out)?;
478-
for c in self.table.column_data.iter().filter(|c| !c.has_default) {
478+
for c in self.table.column_data.iter().filter(|c| {
479+
!c.has_default
480+
&& self
481+
.table
482+
.primary_key
483+
.iter()
484+
.filter(|x| **x == c.sql_name)
485+
.count()
486+
== 0
487+
}) {
479488
let t = GraphqlType {
480489
sql_type: &c.ty,
481490
allow_option: true,

wundergraph_cli/src/print_schema/snapshots/tests__main-3.snap

-17
This file was deleted.

wundergraph_cli/src/print_schema/snapshots/tests__main-4.snap

-9
This file was deleted.

wundergraph_cli/src/print_schema/snapshots/tests__round_trip.snap

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ wundergraph::query_object!{
8686
#[graphql(scalar = "WundergraphScalarValue")]
8787
#[table_name = "comments"]
8888
pub struct NewComment {
89-
id: i32,
9089
post: Option<i32>,
9190
commenter: Option<i32>,
9291
content: String,
@@ -107,7 +106,6 @@ pub struct CommentChangeset {
107106
#[graphql(scalar = "WundergraphScalarValue")]
108107
#[table_name = "posts"]
109108
pub struct NewPost {
110-
id: i32,
111109
author: Option<i32>,
112110
title: String,
113111
datetime: Option<chrono::naive::NaiveDateTime>,
@@ -130,7 +128,6 @@ pub struct PostChangeset {
130128
#[graphql(scalar = "WundergraphScalarValue")]
131129
#[table_name = "users"]
132130
pub struct NewUser {
133-
id: i32,
134131
name: String,
135132
}
136133

wundergraph_cli/src/print_schema/snapshots/tests__round_trip-2.snap wundergraph_cli/src/print_schema/snapshots/wundergraph_cli__print_schema__tests__round_trip_test__mutation.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: wundergraph_cli/src/print_schema/mod.rs
3-
expression: "r.json::<serde_json::Value>().unwrap()"
3+
expression: r
44
---
55
{
66
"data": {

wundergraph_cli/src/print_schema/snapshots/tests__main-2.snap wundergraph_cli/src/print_schema/snapshots/wundergraph_cli__print_schema__tests__round_trip_test__query_1.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: wundergraph_cli/src/print_schema/mod.rs
3-
expression: "r.json::<serde_json::Value>().unwrap()"
3+
expression: r
44
---
55
{
66
"data": {

wundergraph_cli/src/print_schema/snapshots/tests__round_trip-3.snap wundergraph_cli/src/print_schema/snapshots/wundergraph_cli__print_schema__tests__round_trip_test__query_2.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: wundergraph_cli/src/print_schema/mod.rs
3-
expression: "r.json::<serde_json::Value>().unwrap()"
3+
expression: r
44
---
55
{
66
"data": {

0 commit comments

Comments
 (0)