Skip to content

Commit 9c19f10

Browse files
committed
Move tests into own "tests" module
1 parent cde8cc8 commit 9c19f10

File tree

4 files changed

+885
-876
lines changed

4 files changed

+885
-876
lines changed

kube-core/src/schema/transform_any_of.rs

Lines changed: 132 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -2,135 +2,6 @@ use std::ops::DerefMut;
22

33
use crate::schema::{Schema, SchemaObject, SubschemaValidation};
44

5-
#[cfg(test)]
6-
#[test]
7-
fn optional_tagged_enum_with_unit_variants() {
8-
let original_schema_object_value = serde_json::json!({
9-
"anyOf": [
10-
{
11-
"description": "A very simple enum with empty variants",
12-
"oneOf": [
13-
{
14-
"type": "string",
15-
"enum": [
16-
"C",
17-
"D"
18-
]
19-
},
20-
{
21-
"description": "First variant doc-comment",
22-
"type": "string",
23-
"enum": [
24-
"A"
25-
]
26-
},
27-
{
28-
"description": "Second variant doc-comment",
29-
"type": "string",
30-
"enum": [
31-
"B"
32-
]
33-
}
34-
]
35-
},
36-
{
37-
"enum": [
38-
null
39-
],
40-
"nullable": true
41-
}
42-
]
43-
});
44-
45-
let expected_converted_schema_object_value = serde_json::json!({
46-
"description": "A very simple enum with empty variants",
47-
"nullable": true,
48-
"oneOf": [
49-
{
50-
"type": "string",
51-
"enum": [
52-
"C",
53-
"D"
54-
]
55-
},
56-
{
57-
"description": "First variant doc-comment",
58-
"type": "string",
59-
"enum": [
60-
"A"
61-
]
62-
},
63-
{
64-
"description": "Second variant doc-comment",
65-
"type": "string",
66-
"enum": [
67-
"B"
68-
]
69-
}
70-
]
71-
});
72-
73-
74-
let original_schema_object: SchemaObject =
75-
serde_json::from_value(original_schema_object_value).expect("valid JSON");
76-
let expected_converted_schema_object: SchemaObject =
77-
serde_json::from_value(expected_converted_schema_object_value).expect("valid JSON");
78-
79-
let mut actual_converted_schema_object = original_schema_object.clone();
80-
hoist_any_of_subschema_with_a_nullable_variant(&mut actual_converted_schema_object);
81-
82-
assert_json_diff::assert_json_eq!(actual_converted_schema_object, expected_converted_schema_object);
83-
}
84-
85-
#[cfg(test)]
86-
#[test]
87-
fn optional_tagged_enum_with_unit_variants_but_also_an_existing_description() {
88-
let original_schema_object_value = serde_json::json!({
89-
"description": "This comment will be lost",
90-
"anyOf": [
91-
{
92-
"description": "A very simple enum with empty variants",
93-
"type": "string",
94-
"enum": [
95-
"C",
96-
"D",
97-
"A",
98-
"B"
99-
]
100-
},
101-
{
102-
"enum": [
103-
null
104-
],
105-
"nullable": true
106-
}
107-
]
108-
});
109-
110-
let expected_converted_schema_object_value = serde_json::json!({
111-
"description": "A very simple enum with empty variants",
112-
"nullable": true,
113-
"type": "string",
114-
"enum": [
115-
"C",
116-
"D",
117-
"A",
118-
"B"
119-
]
120-
});
121-
122-
123-
let original_schema_object: SchemaObject =
124-
serde_json::from_value(original_schema_object_value).expect("valid JSON");
125-
let expected_converted_schema_object: SchemaObject =
126-
serde_json::from_value(expected_converted_schema_object_value).expect("valid JSON");
127-
128-
let mut actual_converted_schema_object = original_schema_object.clone();
129-
hoist_any_of_subschema_with_a_nullable_variant(&mut actual_converted_schema_object);
130-
131-
assert_json_diff::assert_json_eq!(actual_converted_schema_object, expected_converted_schema_object);
132-
}
133-
1345
/// Replace the schema with the anyOf subschema and set to nullable when the
1356
/// only other subschema is the nullable entry.
1367
///
@@ -216,3 +87,135 @@ pub(crate) fn hoist_any_of_subschema_with_a_nullable_variant(kube_schema: &mut S
21687
// Set the schema to nullable (as we know we matched the null variant earlier)
21788
kube_schema.extensions.insert("nullable".to_owned(), true.into());
21889
}
90+
91+
#[cfg(test)]
92+
mod tests {
93+
use super::*;
94+
95+
#[test]
96+
fn optional_tagged_enum_with_unit_variants() {
97+
let original_schema_object_value = serde_json::json!({
98+
"anyOf": [
99+
{
100+
"description": "A very simple enum with empty variants",
101+
"oneOf": [
102+
{
103+
"type": "string",
104+
"enum": [
105+
"C",
106+
"D"
107+
]
108+
},
109+
{
110+
"description": "First variant doc-comment",
111+
"type": "string",
112+
"enum": [
113+
"A"
114+
]
115+
},
116+
{
117+
"description": "Second variant doc-comment",
118+
"type": "string",
119+
"enum": [
120+
"B"
121+
]
122+
}
123+
]
124+
},
125+
{
126+
"enum": [
127+
null
128+
],
129+
"nullable": true
130+
}
131+
]
132+
});
133+
134+
let expected_converted_schema_object_value = serde_json::json!({
135+
"description": "A very simple enum with empty variants",
136+
"nullable": true,
137+
"oneOf": [
138+
{
139+
"type": "string",
140+
"enum": [
141+
"C",
142+
"D"
143+
]
144+
},
145+
{
146+
"description": "First variant doc-comment",
147+
"type": "string",
148+
"enum": [
149+
"A"
150+
]
151+
},
152+
{
153+
"description": "Second variant doc-comment",
154+
"type": "string",
155+
"enum": [
156+
"B"
157+
]
158+
}
159+
]
160+
});
161+
162+
163+
let original_schema_object: SchemaObject =
164+
serde_json::from_value(original_schema_object_value).expect("valid JSON");
165+
let expected_converted_schema_object: SchemaObject =
166+
serde_json::from_value(expected_converted_schema_object_value).expect("valid JSON");
167+
168+
let mut actual_converted_schema_object = original_schema_object.clone();
169+
hoist_any_of_subschema_with_a_nullable_variant(&mut actual_converted_schema_object);
170+
171+
assert_json_diff::assert_json_eq!(actual_converted_schema_object, expected_converted_schema_object);
172+
}
173+
174+
#[test]
175+
fn optional_tagged_enum_with_unit_variants_but_also_an_existing_description() {
176+
let original_schema_object_value = serde_json::json!({
177+
"description": "This comment will be lost",
178+
"anyOf": [
179+
{
180+
"description": "A very simple enum with empty variants",
181+
"type": "string",
182+
"enum": [
183+
"C",
184+
"D",
185+
"A",
186+
"B"
187+
]
188+
},
189+
{
190+
"enum": [
191+
null
192+
],
193+
"nullable": true
194+
}
195+
]
196+
});
197+
198+
let expected_converted_schema_object_value = serde_json::json!({
199+
"description": "A very simple enum with empty variants",
200+
"nullable": true,
201+
"type": "string",
202+
"enum": [
203+
"C",
204+
"D",
205+
"A",
206+
"B"
207+
]
208+
});
209+
210+
211+
let original_schema_object: SchemaObject =
212+
serde_json::from_value(original_schema_object_value).expect("valid JSON");
213+
let expected_converted_schema_object: SchemaObject =
214+
serde_json::from_value(expected_converted_schema_object_value).expect("valid JSON");
215+
216+
let mut actual_converted_schema_object = original_schema_object.clone();
217+
hoist_any_of_subschema_with_a_nullable_variant(&mut actual_converted_schema_object);
218+
219+
assert_json_diff::assert_json_eq!(actual_converted_schema_object, expected_converted_schema_object);
220+
}
221+
}

kube-core/src/schema/transform_one_of.rs

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,6 @@ use std::ops::DerefMut;
22

33
use crate::schema::{Schema, SchemaObject, SubschemaValidation};
44

5-
#[cfg(test)]
6-
#[test]
7-
fn tagged_enum_with_unit_variants() {
8-
let original_schema_object_value = serde_json::json!({
9-
"description": "A very simple enum with unit variants",
10-
"oneOf": [
11-
{
12-
"type": "string",
13-
"enum": [
14-
"C",
15-
"D"
16-
]
17-
},
18-
{
19-
"description": "First variant doc-comment",
20-
"type": "string",
21-
"enum": [
22-
"A"
23-
]
24-
},
25-
{
26-
"description": "Second variant doc-comment",
27-
"type": "string",
28-
"enum": [
29-
"B"
30-
]
31-
},
32-
]
33-
});
34-
35-
let expected_converted_schema_object_value = serde_json::json!({
36-
"description": "A very simple enum with unit variants",
37-
"type": "string",
38-
"enum": [
39-
"C",
40-
"D",
41-
"A",
42-
"B"
43-
]
44-
});
45-
46-
47-
let original_schema_object: SchemaObject =
48-
serde_json::from_value(original_schema_object_value).expect("valid JSON");
49-
let expected_converted_schema_object: SchemaObject =
50-
serde_json::from_value(expected_converted_schema_object_value).expect("valid JSON");
51-
52-
let mut actual_converted_schema_object = original_schema_object.clone();
53-
hoist_one_of_enum_with_unit_variants(&mut actual_converted_schema_object);
54-
55-
assert_json_diff::assert_json_eq!(actual_converted_schema_object, expected_converted_schema_object);
56-
}
57-
58-
595
/// Merge oneOf subschema enums and consts into a schema level enum.
606
///
617
/// Used for correcting the schema for tagged enums with unit variants.
@@ -143,3 +89,60 @@ pub(crate) fn hoist_one_of_enum_with_unit_variants(kube_schema: &mut SchemaObjec
14389
// Clear the oneOf subschemas
14490
subschemas.one_of = None;
14591
}
92+
93+
#[cfg(test)]
94+
mod tests {
95+
use super::*;
96+
97+
#[test]
98+
fn tagged_enum_with_unit_variants() {
99+
let original_schema_object_value = serde_json::json!({
100+
"description": "A very simple enum with unit variants",
101+
"oneOf": [
102+
{
103+
"type": "string",
104+
"enum": [
105+
"C",
106+
"D"
107+
]
108+
},
109+
{
110+
"description": "First variant doc-comment",
111+
"type": "string",
112+
"enum": [
113+
"A"
114+
]
115+
},
116+
{
117+
"description": "Second variant doc-comment",
118+
"type": "string",
119+
"enum": [
120+
"B"
121+
]
122+
},
123+
]
124+
});
125+
126+
let expected_converted_schema_object_value = serde_json::json!({
127+
"description": "A very simple enum with unit variants",
128+
"type": "string",
129+
"enum": [
130+
"C",
131+
"D",
132+
"A",
133+
"B"
134+
]
135+
});
136+
137+
138+
let original_schema_object: SchemaObject =
139+
serde_json::from_value(original_schema_object_value).expect("valid JSON");
140+
let expected_converted_schema_object: SchemaObject =
141+
serde_json::from_value(expected_converted_schema_object_value).expect("valid JSON");
142+
143+
let mut actual_converted_schema_object = original_schema_object.clone();
144+
hoist_one_of_enum_with_unit_variants(&mut actual_converted_schema_object);
145+
146+
assert_json_diff::assert_json_eq!(actual_converted_schema_object, expected_converted_schema_object);
147+
}
148+
}

0 commit comments

Comments
 (0)