Skip to content

Commit a918e22

Browse files
committed
editoast: change stdcm environment date type
Signed-off-by: SarahBellaha <[email protected]>
1 parent 8e184ae commit a918e22

File tree

3 files changed

+83
-77
lines changed

3 files changed

+83
-77
lines changed

editoast/src/client/stdcm_search_env_commands.rs

+55-53
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use crate::CliError;
88
use crate::Exists;
99
use crate::Model;
1010
use crate::Retrieve;
11+
use chrono::DateTime;
1112
use chrono::Duration;
12-
use chrono::NaiveDateTime;
13+
use chrono::Utc;
1314
use clap::Args;
1415
use clap::Subcommand;
1516
use editoast_models::DbConnection;
@@ -62,10 +63,10 @@ pub struct SetSTDCMSearchEnvFromScenarioArgs {
6263
pub work_schedule_group_id: Option<i64>,
6364
/// If omitted, set to the earliest train start time in the timetable
6465
#[arg(long)]
65-
pub search_window_begin: Option<NaiveDateTime>,
66+
pub search_window_begin: Option<DateTime<Utc>>,
6667
/// If omitted, set to the latest train start time in the timetable plus one day
6768
#[arg(long)]
68-
pub search_window_end: Option<NaiveDateTime>,
69+
pub search_window_end: Option<DateTime<Utc>>,
6970
}
7071

7172
async fn set_stdcm_search_env_from_scenario(
@@ -126,10 +127,10 @@ pub struct SetSTDCMSearchEnvFromScratchArgs {
126127
#[arg(long)]
127128
/// If omitted, set to the earliest train start time in the timetable
128129
#[arg(long)]
129-
pub search_window_begin: Option<NaiveDateTime>,
130+
pub search_window_begin: Option<DateTime<Utc>>,
130131
/// If omitted, set to the latest train start time in the timetable plus one day
131132
#[arg(long)]
132-
pub search_window_end: Option<NaiveDateTime>,
133+
pub search_window_end: Option<DateTime<Utc>>,
133134
}
134135

135136
async fn set_stdcm_search_env_from_scratch(
@@ -178,10 +179,10 @@ async fn set_stdcm_search_env_from_scratch(
178179

179180
async fn resolve_search_window(
180181
timetable_id: i64,
181-
search_window_begin: Option<NaiveDateTime>,
182-
search_window_end: Option<NaiveDateTime>,
182+
search_window_begin: Option<DateTime<Utc>>,
183+
search_window_end: Option<DateTime<Utc>>,
183184
conn: &mut DbConnection,
184-
) -> Result<(NaiveDateTime, NaiveDateTime), Box<dyn Error + Send + Sync>> {
185+
) -> Result<(DateTime<Utc>, DateTime<Utc>), Box<dyn Error + Send + Sync>> {
185186
let (begin, end) = if let (Some(begin), Some(end)) = (search_window_begin, search_window_end) {
186187
(begin, end)
187188
} else {
@@ -193,8 +194,8 @@ async fn resolve_search_window(
193194
return Err(Box::new(CliError::new(1, error_msg)));
194195
};
195196

196-
let begin = search_window_begin.unwrap_or(min.naive_utc());
197-
let end = search_window_end.unwrap_or(max.naive_utc() + Duration::days(1));
197+
let begin = search_window_begin.unwrap_or(*min);
198+
let end = search_window_end.unwrap_or(*max + Duration::days(1));
198199
(begin, end)
199200
};
200201

@@ -235,22 +236,23 @@ mod tests {
235236
};
236237

237238
use super::*;
238-
use chrono::NaiveDateTime;
239+
use chrono::DateTime;
240+
use chrono::Utc;
239241
use editoast_models::{DbConnection, DbConnectionPoolV2};
240242
use rstest::rstest;
241243

242-
fn make_naive_datetime(s: &str) -> NaiveDateTime {
243-
NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S").unwrap()
244+
fn make_datetime(s: &str) -> DateTime<Utc> {
245+
DateTime::parse_from_rfc3339(s).unwrap().to_utc()
244246
}
245247

246248
async fn create_train_schedules_from_start_times(
247-
start_times: Vec<NaiveDateTime>,
249+
start_times: Vec<DateTime<Utc>>,
248250
timetable_id: i64,
249251
conn: &mut DbConnection,
250252
) {
251253
for start_time in start_times {
252254
simple_train_schedule_changeset(timetable_id)
253-
.start_time(start_time.and_utc())
255+
.start_time(start_time)
254256
.create(conn)
255257
.await
256258
.expect("Should be able to create train schedules");
@@ -261,45 +263,45 @@ mod tests {
261263
#[case::both_none(
262264
None,
263265
None,
264-
make_naive_datetime("2000-01-01 11:59:59"),
265-
make_naive_datetime("2000-02-03 00:00:01")
266+
make_datetime("2000-01-01 11:59:59Z"),
267+
make_datetime("2000-02-03 00:00:01Z")
266268
)]
267269
#[case::begin_none(
268270
None,
269-
Some(make_naive_datetime("2000-02-01 00:00:00")),
270-
make_naive_datetime("2000-01-01 11:59:59"),
271-
make_naive_datetime("2000-02-01 00:00:00")
271+
Some(make_datetime("2000-02-01 00:00:00Z")),
272+
make_datetime("2000-01-01 11:59:59Z"),
273+
make_datetime("2000-02-01 00:00:00Z")
272274
)]
273275
#[case::end_none(
274-
Some(make_naive_datetime("2000-02-01 08:00:00")),
276+
Some(make_datetime("2000-02-01 08:00:00Z")),
275277
None,
276-
make_naive_datetime("2000-02-01 08:00:00"),
277-
make_naive_datetime("2000-02-03 00:00:01")
278+
make_datetime("2000-02-01 08:00:00Z"),
279+
make_datetime("2000-02-03 00:00:01Z")
278280
)]
279281
#[case::both_some(
280-
Some(make_naive_datetime("2000-02-01 08:00:00")),
281-
Some(make_naive_datetime("2000-05-22 09:00:50")),
282-
make_naive_datetime("2000-02-01 08:00:00"),
283-
make_naive_datetime("2000-05-22 09:00:50")
282+
Some(make_datetime("2000-02-01 08:00:00Z")),
283+
Some(make_datetime("2000-05-22 09:00:50Z")),
284+
make_datetime("2000-02-01 08:00:00Z"),
285+
make_datetime("2000-05-22 09:00:50Z")
284286
)]
285287
async fn test_resolve_search_window(
286-
#[case] search_window_begin: Option<NaiveDateTime>,
287-
#[case] search_window_end: Option<NaiveDateTime>,
288-
#[case] expected_begin: NaiveDateTime,
289-
#[case] expected_end: NaiveDateTime,
288+
#[case] search_window_begin: Option<DateTime<Utc>>,
289+
#[case] search_window_end: Option<DateTime<Utc>>,
290+
#[case] expected_begin: DateTime<Utc>,
291+
#[case] expected_end: DateTime<Utc>,
290292
) {
291293
let db_pool = DbConnectionPoolV2::for_tests();
292294
let conn = &mut db_pool.get_ok();
293295

294296
let timetable = create_timetable(conn).await;
295297

296298
let start_times = vec![
297-
make_naive_datetime("2000-01-01 12:00:00"),
298-
make_naive_datetime("2000-02-02 00:00:00"),
299-
make_naive_datetime("2000-01-01 11:59:59"), // earliest
300-
make_naive_datetime("2000-01-15 08:59:59"),
301-
make_naive_datetime("2000-02-02 00:00:01"), // latest
302-
make_naive_datetime("2000-01-19 17:00:00"),
299+
make_datetime("2000-01-01 12:00:00Z"),
300+
make_datetime("2000-02-02 00:00:00Z"),
301+
make_datetime("2000-01-01 11:59:59Z"), // earliest
302+
make_datetime("2000-01-15 08:59:59Z"),
303+
make_datetime("2000-02-02 00:00:01Z"), // latest
304+
make_datetime("2000-01-19 17:00:00Z"),
303305
];
304306

305307
create_train_schedules_from_start_times(start_times, timetable.id, conn).await;
@@ -327,23 +329,23 @@ mod tests {
327329

328330
#[rstest]
329331
#[case::both_some(
330-
Some(make_naive_datetime("2000-02-01 08:00:00")),
331-
Some(make_naive_datetime("2000-02-01 00:00:00"))
332+
Some(make_datetime("2000-02-01 08:00:00Z")),
333+
Some(make_datetime("2000-02-01 00:00:00Z"))
332334
)]
333-
#[case::end_none(Some(make_naive_datetime("2000-03-01 00:00:00")), None)]
334-
#[case::begin_none(None, Some(make_naive_datetime("2000-01-01 08:00:00")))]
335+
#[case::end_none(Some(make_datetime("2000-03-01 00:00:00Z")), None)]
336+
#[case::begin_none(None, Some(make_datetime("2000-01-01 08:00:00Z")))]
335337
async fn test_resolve_search_window_incompatible_dates(
336-
#[case] search_window_begin: Option<NaiveDateTime>,
337-
#[case] search_window_end: Option<NaiveDateTime>,
338+
#[case] search_window_begin: Option<DateTime<Utc>>,
339+
#[case] search_window_end: Option<DateTime<Utc>>,
338340
) {
339341
let db_pool = DbConnectionPoolV2::for_tests();
340342
let conn = &mut db_pool.get_ok();
341343

342344
let timetable = create_timetable(conn).await;
343345

344346
let start_times = vec![
345-
make_naive_datetime("2000-01-01 12:00:00"),
346-
make_naive_datetime("2000-02-02 00:00:01"),
347+
make_datetime("2000-01-01 12:00:00Z"),
348+
make_datetime("2000-02-02 00:00:01Z"),
347349
];
348350

349351
create_train_schedules_from_start_times(start_times, timetable.id, conn).await;
@@ -366,8 +368,8 @@ mod tests {
366368
let work_schedule_group = create_work_schedule_group(conn).await;
367369

368370
let start_times = vec![
369-
make_naive_datetime("2000-01-01 12:00:00"),
370-
make_naive_datetime("2000-02-02 08:00:00"),
371+
make_datetime("2000-01-01 12:00:00Z"),
372+
make_datetime("2000-02-02 08:00:00Z"),
371373
];
372374

373375
create_train_schedules_from_start_times(
@@ -394,11 +396,11 @@ mod tests {
394396

395397
assert_eq!(
396398
search_env.search_window_begin,
397-
make_naive_datetime("2000-01-01 12:00:00")
399+
make_datetime("2000-01-01 12:00:00Z")
398400
);
399401
assert_eq!(
400402
search_env.search_window_end,
401-
make_naive_datetime("2000-02-03 08:00:00")
403+
make_datetime("2000-02-03 08:00:00Z")
402404
);
403405
}
404406

@@ -413,8 +415,8 @@ mod tests {
413415
let electrical_profile_set = create_electrical_profile_set(conn).await;
414416

415417
let start_times = vec![
416-
make_naive_datetime("2000-01-01 12:00:00"),
417-
make_naive_datetime("2000-02-02 08:00:00"),
418+
make_datetime("2000-01-01 12:00:00Z"),
419+
make_datetime("2000-02-02 08:00:00Z"),
418420
];
419421

420422
create_train_schedules_from_start_times(start_times, timetable.id, conn).await;
@@ -438,11 +440,11 @@ mod tests {
438440

439441
assert_eq!(
440442
search_env.search_window_begin,
441-
make_naive_datetime("2000-01-01 12:00:00")
443+
make_datetime("2000-01-01 12:00:00Z")
442444
);
443445
assert_eq!(
444446
search_env.search_window_end,
445-
make_naive_datetime("2000-02-03 08:00:00")
447+
make_datetime("2000-02-03 08:00:00Z")
446448
);
447449
}
448450
}

editoast/src/models/stdcm_search_environment.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use chrono::NaiveDateTime;
1+
use chrono::DateTime;
2+
use chrono::Utc;
23
use diesel::ExpressionMethods;
34
use diesel::QueryDsl;
45
use diesel_async::RunQueryDsl;
@@ -28,8 +29,8 @@ pub struct StdcmSearchEnvironment {
2829
#[serde(skip_serializing_if = "Option::is_none")]
2930
pub work_schedule_group_id: Option<i64>,
3031
pub timetable_id: i64,
31-
pub search_window_begin: NaiveDateTime,
32-
pub search_window_end: NaiveDateTime,
32+
pub search_window_begin: DateTime<Utc>,
33+
pub search_window_end: DateTime<Utc>,
3334
#[schema(nullable = false)]
3435
#[serde(skip_serializing_if = "Option::is_none")]
3536
pub temporary_speed_limit_group_id: Option<i64>,
@@ -65,7 +66,8 @@ impl StdcmSearchEnvironmentChangeset {
6566

6667
#[cfg(test)]
6768
pub mod tests {
68-
use chrono::NaiveDate;
69+
use chrono::TimeZone;
70+
use chrono::Utc;
6971
use pretty_assertions::assert_eq;
7072
use rstest::rstest;
7173

@@ -127,11 +129,11 @@ pub mod tests {
127129
.work_schedule_group_id(Some(work_schedule_group.id))
128130
.temporary_speed_limit_group_id(Some(temporary_speed_limit_group.id))
129131
.timetable_id(timetable.id)
130-
.search_window_begin(NaiveDate::from_ymd_opt(2024, 1, 1).unwrap().into())
131-
.search_window_end(NaiveDate::from_ymd_opt(2024, 1, 15).unwrap().into());
132+
.search_window_begin(Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap())
133+
.search_window_end(Utc.with_ymd_and_hms(2024, 1, 15, 0, 0, 0).unwrap());
132134

133-
let begin = NaiveDate::from_ymd_opt(2024, 1, 16).unwrap().into();
134-
let end = NaiveDate::from_ymd_opt(2024, 1, 31).unwrap().into();
135+
let begin = Utc.with_ymd_and_hms(2024, 1, 16, 0, 0, 0).unwrap();
136+
let end = Utc.with_ymd_and_hms(2024, 1, 13, 0, 0, 0).unwrap();
135137

136138
let changeset_2 = changeset_1
137139
.clone()
@@ -190,16 +192,16 @@ pub mod tests {
190192
.work_schedule_group_id(Some(work_schedule_group.id))
191193
.temporary_speed_limit_group_id(Some(temporary_speed_limit_group.id))
192194
.timetable_id(timetable.id)
193-
.search_window_begin(NaiveDate::from_ymd_opt(2024, 1, 1).unwrap().into())
194-
.search_window_end(NaiveDate::from_ymd_opt(2024, 1, 15).unwrap().into());
195+
.search_window_begin(Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap())
196+
.search_window_end(Utc.with_ymd_and_hms(2024, 1, 15, 0, 0, 0).unwrap());
195197

196198
let too_young = too_old
197199
.clone()
198-
.search_window_begin(NaiveDate::from_ymd_opt(2024, 1, 16).unwrap().into())
199-
.search_window_end(NaiveDate::from_ymd_opt(2024, 1, 31).unwrap().into());
200+
.search_window_begin(Utc.with_ymd_and_hms(2024, 1, 16, 0, 0, 0).unwrap())
201+
.search_window_end(Utc.with_ymd_and_hms(2024, 1, 31, 0, 0, 0).unwrap());
200202

201-
let begin = NaiveDate::from_ymd_opt(2024, 1, 7).unwrap().into();
202-
let end = NaiveDate::from_ymd_opt(2024, 1, 31).unwrap().into();
203+
let begin = Utc.with_ymd_and_hms(2024, 1, 7, 0, 0, 0).unwrap();
204+
let end = Utc.with_ymd_and_hms(2024, 1, 31, 0, 0, 0).unwrap();
203205

204206
let the_best = too_old
205207
.clone()

editoast/src/views/stdcm_search_environment.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use axum::http::StatusCode;
44
use axum::response::IntoResponse;
55
use axum::response::Response;
66
use axum::Extension;
7-
use chrono::NaiveDateTime;
7+
use chrono::DateTime;
8+
use chrono::Utc;
89
use editoast_authz::BuiltinRole;
910
use editoast_models::DbConnectionPoolV2;
1011
use serde::de::Error as SerdeError;
@@ -42,8 +43,8 @@ struct StdcmSearchEnvironmentCreateForm {
4243
work_schedule_group_id: Option<i64>,
4344
temporary_speed_limit_group_id: Option<i64>,
4445
timetable_id: i64,
45-
search_window_begin: NaiveDateTime, // TODO: move to DateTime<Utc>
46-
search_window_end: NaiveDateTime,
46+
search_window_begin: DateTime<Utc>,
47+
search_window_end: DateTime<Utc>,
4748
}
4849

4950
impl<'de> Deserialize<'de> for StdcmSearchEnvironmentCreateForm {
@@ -59,8 +60,8 @@ impl<'de> Deserialize<'de> for StdcmSearchEnvironmentCreateForm {
5960
work_schedule_group_id: Option<i64>,
6061
temporary_speed_limit_group_id: Option<i64>,
6162
timetable_id: i64,
62-
search_window_begin: NaiveDateTime,
63-
search_window_end: NaiveDateTime,
63+
search_window_begin: DateTime<Utc>,
64+
search_window_end: DateTime<Utc>,
6465
}
6566
let internal = Internal::deserialize(deserializer)?;
6667

@@ -156,7 +157,8 @@ async fn retrieve_latest(
156157
#[cfg(test)]
157158
pub mod tests {
158159
use axum::http::StatusCode;
159-
use chrono::NaiveDate;
160+
use chrono::TimeZone;
161+
use chrono::Utc;
160162
use pretty_assertions::assert_eq;
161163
use rstest::rstest;
162164

@@ -185,8 +187,8 @@ pub mod tests {
185187
work_schedule_group_id: Some(work_schedule_group.id),
186188
temporary_speed_limit_group_id: Some(temporary_speed_limit_group.id),
187189
timetable_id: timetable.id,
188-
search_window_begin: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap().into(),
189-
search_window_end: NaiveDate::from_ymd_opt(2024, 1, 15).unwrap().into(),
190+
search_window_begin: Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap(),
191+
search_window_end: Utc.with_ymd_and_hms(2024, 1, 15, 0, 0, 0).unwrap(),
190192
};
191193

192194
let request = app.post("/stdcm/search_environment").json(&form);
@@ -224,8 +226,8 @@ pub mod tests {
224226
electrical_profile_set,
225227
) = stdcm_search_env_fixtures(&mut pool.get_ok()).await;
226228

227-
let begin = NaiveDate::from_ymd_opt(2024, 1, 1).unwrap().into();
228-
let end = NaiveDate::from_ymd_opt(2024, 1, 15).unwrap().into();
229+
let begin = Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap();
230+
let end = Utc.with_ymd_and_hms(2024, 1, 15, 0, 0, 0).unwrap();
229231

230232
let _ = StdcmSearchEnvironment::changeset()
231233
.infra_id(infra.id)

0 commit comments

Comments
 (0)