Skip to content

Commit 925da09

Browse files
committed
test(json): Reduce the scope of fixtures
1 parent e316732 commit 925da09

File tree

1 file changed

+106
-16
lines changed

1 file changed

+106
-16
lines changed

tests/testsuite/file_json.rs

Lines changed: 106 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#![cfg(feature = "json")]
22

3-
use std::path::PathBuf;
4-
53
use chrono::{DateTime, TimeZone, Utc};
64
use float_cmp::ApproxEqUlps;
75
use serde_derive::Deserialize;
@@ -32,7 +30,32 @@ fn test_file() {
3230
}
3331

3432
let c = Config::builder()
35-
.add_source(File::new("tests/Settings", FileFormat::Json))
33+
.add_source(File::from_str(
34+
r#"
35+
{
36+
"debug": true,
37+
"debug_json": true,
38+
"production": false,
39+
"arr": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
40+
"place": {
41+
"name": "Torre di Pisa",
42+
"longitude": 43.7224985,
43+
"latitude": 10.3970522,
44+
"favorite": false,
45+
"reviews": 3866,
46+
"rating": 4.5,
47+
"creator": {
48+
"name": "John Smith",
49+
"username": "jsmith",
50+
"email": "jsmith@localhost"
51+
}
52+
},
53+
"FOO": "FOO should be overridden",
54+
"bar": "I am bar"
55+
}
56+
"#,
57+
FileFormat::Json,
58+
))
3659
.build()
3760
.unwrap();
3861

@@ -73,18 +96,21 @@ fn test_file() {
7396
#[test]
7497
fn test_error_parse() {
7598
let res = Config::builder()
76-
.add_source(File::new("tests/Settings-invalid", FileFormat::Json))
99+
.add_source(File::from_str(
100+
r#"
101+
{
102+
"ok": true,
103+
"error"
104+
}
105+
"#,
106+
FileFormat::Json,
107+
))
77108
.build();
78109

79-
let path_with_extension: PathBuf = ["tests", "Settings-invalid.json"].iter().collect();
80-
81110
assert!(res.is_err());
82111
assert_eq!(
83112
res.unwrap_err().to_string(),
84-
format!(
85-
"expected `:` at line 4 column 1 in {}",
86-
path_with_extension.display()
87-
)
113+
format!("expected `:` at line 5 column 1",)
88114
);
89115
}
90116

@@ -96,7 +122,7 @@ fn test_json_vec() {
96122
{
97123
"WASTE": ["example_dir1", "example_dir2"]
98124
}
99-
"#,
125+
"#,
100126
FileFormat::Json,
101127
))
102128
.build()
@@ -126,7 +152,32 @@ fn test_override_uppercase_value_for_struct() {
126152
std::env::set_var("APP_FOO", "I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE");
127153

128154
let cfg = Config::builder()
129-
.add_source(File::new("tests/Settings", FileFormat::Json))
155+
.add_source(File::from_str(
156+
r#"
157+
{
158+
"debug": true,
159+
"debug_json": true,
160+
"production": false,
161+
"arr": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
162+
"place": {
163+
"name": "Torre di Pisa",
164+
"longitude": 43.7224985,
165+
"latitude": 10.3970522,
166+
"favorite": false,
167+
"reviews": 3866,
168+
"rating": 4.5,
169+
"creator": {
170+
"name": "John Smith",
171+
"username": "jsmith",
172+
"email": "jsmith@localhost"
173+
}
174+
},
175+
"FOO": "FOO should be overridden",
176+
"bar": "I am bar"
177+
}
178+
"#,
179+
FileFormat::Json,
180+
))
130181
.add_source(config::Environment::with_prefix("APP").separator("_"))
131182
.build()
132183
.unwrap();
@@ -168,7 +219,32 @@ fn test_override_lowercase_value_for_struct() {
168219
std::env::set_var("config_foo", "I have been overridden_with_lower_case");
169220

170221
let cfg = Config::builder()
171-
.add_source(File::new("tests/Settings", FileFormat::Json))
222+
.add_source(File::from_str(
223+
r#"
224+
{
225+
"debug": true,
226+
"debug_json": true,
227+
"production": false,
228+
"arr": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
229+
"place": {
230+
"name": "Torre di Pisa",
231+
"longitude": 43.7224985,
232+
"latitude": 10.3970522,
233+
"favorite": false,
234+
"reviews": 3866,
235+
"rating": 4.5,
236+
"creator": {
237+
"name": "John Smith",
238+
"username": "jsmith",
239+
"email": "jsmith@localhost"
240+
}
241+
},
242+
"FOO": "FOO should be overridden",
243+
"bar": "I am bar"
244+
}
245+
"#,
246+
FileFormat::Json,
247+
))
172248
.add_source(config::Environment::with_prefix("config").separator("_"))
173249
.build()
174250
.unwrap();
@@ -191,7 +267,14 @@ fn test_override_uppercase_value_for_enums() {
191267
std::env::set_var("APPS_BAR", "I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE");
192268

193269
let cfg = Config::builder()
194-
.add_source(File::new("tests/Settings-enum-test", FileFormat::Json))
270+
.add_source(File::from_str(
271+
r#"
272+
{
273+
"bar": "bar is a lowercase param"
274+
}
275+
"#,
276+
FileFormat::Json,
277+
))
195278
.add_source(config::Environment::with_prefix("APPS").separator("_"))
196279
.build()
197280
.unwrap();
@@ -213,7 +296,14 @@ fn test_override_lowercase_value_for_enums() {
213296
std::env::set_var("test_bar", "I have been overridden_with_lower_case");
214297

215298
let cfg = Config::builder()
216-
.add_source(File::new("tests/Settings-enum-test", FileFormat::Json))
299+
.add_source(File::from_str(
300+
r#"
301+
{
302+
"bar": "bar is a lowercase param"
303+
}
304+
"#,
305+
FileFormat::Json,
306+
))
217307
.add_source(config::Environment::with_prefix("test").separator("_"))
218308
.build()
219309
.unwrap();
@@ -234,7 +324,7 @@ fn json() {
234324
{
235325
"json_datetime": "2017-05-10T02:14:53Z"
236326
}
237-
"#,
327+
"#,
238328
FileFormat::Json,
239329
))
240330
.build()

0 commit comments

Comments
 (0)