Skip to content

Commit 1dc2c1b

Browse files
committed
Tests for extern_enums
1 parent c66cf3a commit 1dc2c1b

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed

graphql_client/tests/extern_enums.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use graphql_client::*;
2+
use serde::Deserialize;
3+
4+
/*
5+
* Enums under test
6+
*
7+
* They rename the fields to use SCREAMING_SNAKE_CASE for deserialization, as it is the standard for GraphQL enums.
8+
*/
9+
#[derive(Deserialize, Debug, PartialEq)]
10+
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
11+
pub enum Direction {
12+
North,
13+
East,
14+
South,
15+
West,
16+
}
17+
18+
#[derive(Deserialize, Debug, PartialEq)]
19+
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
20+
pub enum DistanceUnit {
21+
Meter,
22+
Feet,
23+
SomethingElseWithMultipleWords,
24+
}
25+
26+
/* Queries */
27+
28+
// Minimal setup using extern enum.
29+
#[derive(GraphQLQuery)]
30+
#[graphql(
31+
schema_path = "tests/extern_enums/schema.graphql",
32+
query_path = "tests/extern_enums/single_extern_enum_query.graphql",
33+
extern_enums("DistanceUnit")
34+
)]
35+
pub struct SingleExternEnumQuery;
36+
37+
// Tests using multiple externally defined enums. Also covers mixing with derived traits and with nullable GraphQL enum values.
38+
#[derive(GraphQLQuery)]
39+
#[graphql(
40+
schema_path = "tests/extern_enums/schema.graphql",
41+
query_path = "tests/extern_enums/multiple_extern_enums_query.graphql",
42+
response_derives = "Debug, PartialEq",
43+
extern_enums("Direction", "DistanceUnit")
44+
)]
45+
pub struct MultipleExternEnumsQuery;
46+
47+
/* Tests */
48+
49+
#[test]
50+
fn single_extern_enum() {
51+
const RESPONSE: &str = include_str!("extern_enums/single_extern_enum_response.json");
52+
53+
println!("{:?}", RESPONSE);
54+
let response_data: single_extern_enum_query::ResponseData =
55+
serde_json::from_str(RESPONSE).unwrap();
56+
57+
println!("{:?}", response_data.unit);
58+
59+
let expected = single_extern_enum_query::ResponseData {
60+
unit: DistanceUnit::Meter,
61+
};
62+
63+
assert_eq!(response_data.unit, expected.unit);
64+
}
65+
66+
#[test]
67+
fn multiple_extern_enums() {
68+
const RESPONSE: &str = include_str!("extern_enums/multiple_extern_enums_response.json");
69+
70+
println!("{:?}", RESPONSE);
71+
let response_data: multiple_extern_enums_query::ResponseData =
72+
serde_json::from_str(RESPONSE).unwrap();
73+
74+
println!("{:?}", response_data);
75+
76+
let expected = multiple_extern_enums_query::ResponseData {
77+
distance: 100,
78+
direction: Some(Direction::North),
79+
unit: DistanceUnit::SomethingElseWithMultipleWords,
80+
};
81+
82+
assert_eq!(response_data, expected);
83+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
query MultipleExternEnumsQuery {
2+
distance
3+
unit
4+
direction
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"distance": 100,
3+
"unit": "SOMETHING_ELSE_WITH_MULTIPLE_WORDS",
4+
"direction": "NORTH"
5+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
schema {
2+
query: ExternEnumQueryRoot
3+
}
4+
5+
enum Direction {
6+
NORTH
7+
EAST
8+
SOUTH
9+
WEST
10+
}
11+
12+
enum DistanceUnit {
13+
METER
14+
FEET
15+
SOMETHING_ELSE_WITH_MULTIPLE_WORDS
16+
}
17+
18+
type ExternEnumQueryRoot {
19+
distance: Int!
20+
unit: DistanceUnit!
21+
direction: Direction
22+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query SingleExternEnumQuery {
2+
unit
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"unit": "METER"
3+
}

0 commit comments

Comments
 (0)