-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathfixtures.rs
326 lines (293 loc) · 10.3 KB
/
fixtures.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
use std::io::Cursor;
use std::ops::DerefMut;
use chrono::Utc;
use editoast_models::DbConnection;
use editoast_models::DbConnectionPool;
use editoast_models::DbConnectionPoolV2;
use editoast_schemas::infra::Direction;
use editoast_schemas::infra::DirectionalTrackRange;
use editoast_schemas::infra::InfraObject;
use editoast_schemas::infra::RailJson;
use editoast_schemas::primitives::OSRDObject;
use editoast_schemas::rolling_stock::RollingStock;
use editoast_schemas::train_schedule::TrainScheduleBase;
use postgis_diesel::types::LineString;
use serde_json::Value;
use crate::infra_cache::operation::create::apply_create_operation;
use crate::models::electrical_profiles::ElectricalProfileSet;
use crate::models::prelude::*;
use crate::models::rolling_stock_livery::RollingStockLiveryModel;
use crate::models::timetable::Timetable;
use crate::models::train_schedule::TrainSchedule;
use crate::models::work_schedules::WorkSchedule;
use crate::models::work_schedules::WorkScheduleGroup;
use crate::models::Document;
use crate::models::Infra;
use crate::models::Project;
use crate::models::RollingStockModel;
use crate::models::Scenario;
use crate::models::Study;
use crate::models::Tags;
use super::temporary_speed_limits::TemporarySpeedLimitGroup;
pub fn project_changeset(name: &str) -> Changeset<Project> {
Project::changeset()
.name(name.to_owned())
.budget(Some(0))
.creation_date(Utc::now().naive_utc())
.last_modification(Utc::now().naive_utc())
.tags(Tags::default())
}
pub async fn create_project(conn: &mut DbConnection, name: &str) -> Project {
project_changeset(name)
.create(conn)
.await
.expect("Failed to create project")
}
pub fn study_changeset(name: &str, project_id: i64) -> Changeset<Study> {
Study::changeset()
.name(name.to_owned())
.creation_date(Utc::now().naive_utc())
.creation_date(Utc::now().naive_utc())
.last_modification(Utc::now().naive_utc())
.budget(Some(0))
.tags(Tags::default())
.state("some_state".into())
.project_id(project_id)
}
pub async fn create_study(conn: &mut DbConnection, name: &str, project_id: i64) -> Study {
study_changeset(name, project_id)
.create(conn)
.await
.expect("Failed to create study")
}
pub async fn create_timetable(conn: &mut DbConnection) -> Timetable {
Timetable::create(conn)
.await
.expect("Failed to create timetable")
}
pub fn simple_train_schedule_base() -> TrainScheduleBase {
serde_json::from_str(include_str!("../tests/train_schedules/simple.json"))
.expect("Unable to parse test train schedule")
}
pub fn simple_train_schedule_changeset(timetable_id: i64) -> Changeset<TrainSchedule> {
Changeset::<TrainSchedule>::from(simple_train_schedule_base()).timetable_id(timetable_id)
}
pub async fn create_simple_train_schedule(
conn: &mut DbConnection,
timetable_id: i64,
) -> TrainSchedule {
simple_train_schedule_changeset(timetable_id)
.create(conn)
.await
.expect("Failed to create train schedule")
}
pub fn scenario_changeset(
name: &str,
study_id: i64,
timetable_id: i64,
infra_id: i64,
) -> Changeset<Scenario> {
Scenario::changeset()
.name(name.to_string())
.description("test_scenario description".to_string())
.creation_date(Utc::now().naive_utc())
.last_modification(Utc::now().naive_utc())
.tags(Tags::default())
.timetable_id(timetable_id)
.study_id(study_id)
.infra_id(infra_id)
}
pub async fn create_scenario(
conn: &mut DbConnection,
name: &str,
study_id: i64,
timetable_id: i64,
infra_id: i64,
) -> Scenario {
let scenario = scenario_changeset(name, study_id, timetable_id, infra_id);
scenario
.create(conn)
.await
.expect("Failed to create scenario")
}
pub struct ScenarioFixtureSet {
pub project: Project,
pub study: Study,
pub scenario: Scenario,
pub timetable: Timetable,
pub infra: Infra,
}
pub async fn create_scenario_fixtures_set(
conn: &mut DbConnection,
name: &str,
) -> ScenarioFixtureSet {
let project = create_project(conn, &format!("project_test_name_with_{name}")).await;
let study = create_study(conn, &format!("study_test_name_with_{name}"), project.id).await;
let infra = create_empty_infra(conn).await;
let timetable = create_timetable(conn).await;
let scenario = create_scenario(conn, name, study.id, timetable.id, infra.id).await;
ScenarioFixtureSet {
project,
study,
scenario,
timetable,
infra,
}
}
pub fn fast_rolling_stock_changeset(name: &str) -> Changeset<RollingStockModel> {
Changeset::<RollingStockModel>::from(
serde_json::from_str::<editoast_schemas::rolling_stock::RollingStock>(include_str!(
"../tests/example_rolling_stock_1.json"
))
.expect("Unable to parse example rolling stock"),
)
.name(name.to_owned())
.version(0)
}
pub async fn create_fast_rolling_stock(conn: &mut DbConnection, name: &str) -> RollingStockModel {
fast_rolling_stock_changeset(name)
.create(conn)
.await
.expect("Failed to create rolling stock")
}
pub fn rolling_stock_with_energy_sources_changeset(name: &str) -> Changeset<RollingStockModel> {
Changeset::<RollingStockModel>::from(
serde_json::from_str::<editoast_schemas::rolling_stock::RollingStock>(include_str!(
"../tests/example_rolling_stock_2_energy_sources.json"
))
.expect("Unable to parse rolling stock with energy sources"),
)
.name(name.to_owned())
.version(1)
}
pub async fn create_rolling_stock_with_energy_sources(
conn: &mut DbConnection,
name: &str,
) -> RollingStockModel {
rolling_stock_with_energy_sources_changeset(name)
.create(conn)
.await
.expect("Failed to create rolling stock with energy sources")
}
pub fn get_rolling_stock_with_invalid_effort_curves() -> &'static str {
include_str!("../tests/example_rolling_stock_3.json")
}
pub fn rolling_stock_livery_changeset(
name: &str,
rolling_stock_id: i64,
compound_image_id: i64,
) -> Changeset<RollingStockLiveryModel> {
RollingStockLiveryModel::changeset()
.name(name.to_string())
.rolling_stock_id(rolling_stock_id)
.compound_image_id(Some(compound_image_id))
}
pub async fn create_rolling_stock_livery(
conn: &mut DbConnection,
name: &str,
rolling_stock_id: i64,
compound_image_id: i64,
) -> RollingStockLiveryModel {
rolling_stock_livery_changeset(name, rolling_stock_id, compound_image_id)
.create(conn)
.await
.expect("Failed to create rolling stock livery")
}
pub async fn create_document_example(conn: &mut DbConnection) -> Document {
let img = image::open("src/tests/example_rolling_stock_image_1.gif").unwrap();
let mut img_bytes: Vec<u8> = Vec::new();
assert!(img
.write_to(&mut Cursor::new(&mut img_bytes), image::ImageFormat::Png)
.is_ok());
let changeset = Document::changeset()
.content_type(String::from("img/png"))
.data(img_bytes);
changeset
.create(conn)
.await
.expect("Failed to create document")
}
pub async fn create_rolling_stock_livery_fixture(
conn: &mut DbConnection,
name: &str,
) -> (RollingStockLiveryModel, RollingStockModel, Document) {
let rolling_stock = create_fast_rolling_stock(conn, name).await;
let document_exemple = create_document_example(conn).await;
let rs_livery =
create_rolling_stock_livery(conn, name, rolling_stock.id, document_exemple.id).await;
(rs_livery, rolling_stock, document_exemple)
}
pub async fn create_electrical_profile_set(conn: &mut DbConnection) -> ElectricalProfileSet {
let json = include_str!("../tests/electrical_profile_set.json");
serde_json::from_str::<Changeset<ElectricalProfileSet>>(json)
.expect("Unable to parse")
.create(conn)
.await
.expect("Failed to create electrical profile set")
}
pub async fn create_empty_infra(conn: &mut DbConnection) -> Infra {
Infra::changeset()
.name("empty_infra".to_owned())
.last_railjson_version()
.create(conn)
.await
.expect("Failed to create empty infra")
}
pub async fn create_infra_object<T>(
conn: &mut DbConnection,
infra_id: i64,
object: T,
) -> InfraObject
where
T: Into<InfraObject> + OSRDObject,
{
let object_type = object.get_type();
let railjson_object: InfraObject = object.into();
let result = apply_create_operation(&railjson_object, infra_id, conn).await;
assert!(result.is_ok(), "Failed to create a {object_type}");
railjson_object
}
pub async fn create_small_infra(conn: &mut DbConnection) -> Infra {
let railjson: RailJson = serde_json::from_str(include_str!(
"../../../tests/data/infras/small_infra/infra.json"
))
.unwrap();
Infra::changeset()
.name("small_infra".to_owned())
.last_railjson_version()
.persist(railjson, conn)
.await
.unwrap()
}
pub async fn create_work_schedule_group(conn: &mut DbConnection) -> WorkScheduleGroup {
WorkScheduleGroup::changeset()
.name("Empty work schedule group".to_string())
.creation_date(Utc::now().naive_utc())
.create(conn)
.await
.expect("Failed to create empty work schedule group")
}
pub async fn create_temporary_speed_limit_group(
conn: &mut DbConnection,
) -> TemporarySpeedLimitGroup {
TemporarySpeedLimitGroup::changeset()
.name("Empty temporary speed limit group".to_string())
.creation_date(Utc::now().naive_utc())
.create(conn)
.await
.expect("Failed to create empty temporary speed limit group")
}
pub async fn create_work_schedules_fixture_set(
conn: &mut DbConnection,
work_schedules: Vec<Changeset<WorkSchedule>>,
) -> (WorkScheduleGroup, Vec<WorkSchedule>) {
let work_schedule_group = create_work_schedule_group(conn).await;
let work_schedules_changesets = work_schedules
.into_iter()
.map(|ws| ws.work_schedule_group_id(work_schedule_group.id))
.collect::<Vec<_>>();
let work_schedules = WorkSchedule::create_batch(conn, work_schedules_changesets)
.await
.expect("Failed to create work test schedules");
(work_schedule_group, work_schedules)
}