Skip to content

Commit 5a4a11a

Browse files
committed
feat: fixed the tests
1 parent 7f47cd1 commit 5a4a11a

File tree

6 files changed

+49
-111
lines changed

6 files changed

+49
-111
lines changed

crates/api/src/tests/common/mod.rs

+2-36
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,14 @@ where
5858
}
5959

6060
pub fn setup() -> Router {
61-
let config = dbg!(infrastructure::config::Config::try_new().expect("valid config file"));
61+
let config = infrastructure::config::Config::try_new().expect("valid config file");
6262

6363
router(&config).expect("valid configuration")
6464
}
6565

6666
pub async fn get(mut router: Router, uri: &str) -> Result<axum::http::Response<Body>, Infallible> {
6767
router
68-
.call(
69-
Request::builder()
70-
.uri(uri)
71-
.header("x-real-ip", "127.0.0.1")
72-
.body(Body::empty())
73-
.unwrap(),
74-
)
68+
.call(Request::builder().uri(uri).body(Body::empty()).unwrap())
7569
.await
7670
}
7771

@@ -82,31 +76,3 @@ pub fn parse_date(date: &str) -> time::Date {
8276
pub fn parse_time(time: &str) -> time::Time {
8377
time::Time::parse(time, &shared::TIME_FORMAT).unwrap()
8478
}
85-
86-
pub fn parse_duration(duration: &str) -> time::Time {
87-
let mut parsed = time::parsing::Parsed::new();
88-
parsed
89-
.parse_items(duration.as_bytes(), shared::DURATION_FORMAT)
90-
.unwrap();
91-
time::Time::from_hms_nano(
92-
0,
93-
parsed.minute().unwrap_or_default(),
94-
parsed.second().unwrap_or_default(),
95-
parsed.subsecond().unwrap_or_default(),
96-
)
97-
.unwrap()
98-
}
99-
100-
pub fn parse_short_duration(duration: &str) -> time::Time {
101-
let mut parsed = time::parsing::Parsed::new();
102-
parsed
103-
.parse_items(duration.as_bytes(), shared::SHORT_DURATION_FORMAT)
104-
.unwrap();
105-
time::Time::from_hms_nano(
106-
0,
107-
parsed.minute().unwrap_or_default(),
108-
parsed.second().unwrap_or_default(),
109-
parsed.subsecond().unwrap_or_default(),
110-
)
111-
.unwrap()
112-
}

crates/api/src/tests/common/models.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use shared::prelude::*;
22

3-
use super::{parse_date, parse_duration, parse_short_duration, parse_time};
3+
use super::{parse_date, parse_time};
44

55
#[derive(Debug)]
66
pub struct StaticCircuit<'a> {
@@ -220,7 +220,7 @@ impl PartialEq<PitStop> for StaticPitStop<'_> {
220220
&& self.lap == other.lap
221221
&& self.stop == other.stop
222222
&& time == other.time
223-
&& self.duration.map(parse_short_duration) == other.duration
223+
&& self.duration == other.duration.as_deref()
224224
}
225225
}
226226

@@ -248,7 +248,7 @@ impl PartialEq<LapTiming> for StaticTiming<'_> {
248248
fn eq(&self, other: &LapTiming) -> bool {
249249
self.driver_ref == other.driver_ref
250250
&& self.position == other.position
251-
&& self.time.map(parse_duration) == other.time
251+
&& self.time == other.time.as_deref()
252252
}
253253
}
254254

crates/shared/src/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,3 @@ pub const DATE_FORMAT: &[time::format_description::BorrowedFormatItem<'_>] =
1313
time::macros::format_description!("[year]-[month]-[day]");
1414
pub const TIME_FORMAT: &[time::format_description::BorrowedFormatItem<'_>] =
1515
time::macros::format_description!("[hour]:[minute]:[second]");
16-
pub const DURATION_FORMAT: &[time::format_description::BorrowedFormatItem<'_>] =
17-
time::macros::format_description!("[minute padding:none]:[second].[subsecond digits:3]");
18-
pub const SHORT_DURATION_FORMAT: &[time::format_description::BorrowedFormatItem<'_>] =
19-
time::macros::format_description!("[second].[subsecond digits:3]");

crates/shared/src/models.rs

+2-58
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ pub struct Lap {
111111
pub driver_ref: String,
112112
pub lap: i32,
113113
pub position: Option<i32>,
114-
#[mysql(with = "try_as_duration")]
115-
pub time: Option<time::Time>,
114+
pub time: Option<String>,
116115
}
117116

118117
#[derive(FromRow, Debug)]
@@ -146,8 +145,7 @@ pub struct PitStop {
146145
pub stop: i32,
147146
pub lap: i32,
148147
pub time: time::Time,
149-
#[mysql(with = "try_as_short_duration")]
150-
pub duration: Option<time::Time>,
148+
pub duration: Option<String>,
151149
}
152150

153151
#[derive(FromRow, Debug)]
@@ -197,57 +195,3 @@ pub struct Status {
197195
pub status: String,
198196
pub count: i32,
199197
}
200-
201-
fn try_as_duration(
202-
value: mysql_common::Value,
203-
) -> Result<Option<time::Time>, mysql_common::FromValueError> {
204-
use mysql_common::Value::*;
205-
206-
match value {
207-
Bytes(bytes) => {
208-
// SAFETY: values are stored this way in the database so it should
209-
// never panic
210-
let mut parsed = time::parsing::Parsed::new();
211-
parsed.parse_items(&bytes, super::DURATION_FORMAT).unwrap();
212-
Ok(Some(
213-
time::Time::from_hms_nano(
214-
0,
215-
parsed.minute().unwrap_or_default(),
216-
parsed.second().unwrap_or_default(),
217-
parsed.subsecond().unwrap_or_default(),
218-
)
219-
.unwrap(),
220-
))
221-
}
222-
NULL => Ok(None),
223-
_ => Err(mysql_common::FromValueError(value)),
224-
}
225-
}
226-
227-
fn try_as_short_duration(
228-
value: mysql_common::Value,
229-
) -> Result<Option<time::Time>, mysql_common::FromValueError> {
230-
use mysql_common::Value::*;
231-
232-
match value {
233-
Bytes(bytes) => {
234-
// SAFETY: values are stored this way in the database so it should
235-
// never panic
236-
let mut parsed = time::parsing::Parsed::new();
237-
parsed
238-
.parse_items(&bytes, super::SHORT_DURATION_FORMAT)
239-
.unwrap();
240-
Ok(Some(
241-
time::Time::from_hms_nano(
242-
0,
243-
parsed.minute().unwrap_or_default(),
244-
parsed.second().unwrap_or_default(),
245-
parsed.subsecond().unwrap_or_default(),
246-
)
247-
.unwrap(),
248-
))
249-
}
250-
NULL => Ok(None),
251-
_ => Err(mysql_common::FromValueError(value)),
252-
}
253-
}

crates/shared/src/responses.rs

+38-10
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ use time::serde;
44
use crate::error;
55
use crate::error::Result;
66
use crate::parameters::Series;
7-
use crate::{DATE_FORMAT, DURATION_FORMAT, SHORT_DURATION_FORMAT, TIME_FORMAT};
7+
use crate::{DATE_FORMAT, TIME_FORMAT};
88

99
serde::format_description!(date_format, Date, DATE_FORMAT);
1010
serde::format_description!(time_format, Time, TIME_FORMAT);
11-
serde::format_description!(duration_format, Time, DURATION_FORMAT);
12-
serde::format_description!(short_duration_format, Time, SHORT_DURATION_FORMAT);
1311

1412
#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Eq)]
1513
pub struct Pagination {
@@ -24,7 +22,7 @@ pub struct Response<T> {
2422
pub data: T,
2523

2624
#[serde(flatten)]
27-
#[serde(skip_serializing_if = "Option::is_none")]
25+
#[serde(skip_serializing_if = "Option::is_none", default)]
2826
pub pagination: Option<Pagination>,
2927
pub series: Series,
3028
}
@@ -114,10 +112,12 @@ pub enum Standings {
114112

115113
#[derive(Debug, Serialize, Deserialize)]
116114
pub struct LapsResponse {
115+
#[serde(skip_serializing_if = "Option::is_none", default)]
117116
pub url: Option<String>,
118117
pub race_name: String,
119118
#[serde(with = "date_format")]
120119
pub date: time::Date,
120+
#[serde(skip_serializing_if = "Option::is_none", default)]
121121
#[serde(with = "time_format::option")]
122122
pub time: Option<time::Time>,
123123

@@ -128,10 +128,12 @@ pub struct LapsResponse {
128128

129129
#[derive(Debug, Serialize, Deserialize)]
130130
pub struct PitStopsResponse {
131+
#[serde(skip_serializing_if = "Option::is_none", default)]
131132
pub url: Option<String>,
132133
pub race_name: String,
133134
#[serde(with = "date_format")]
134135
pub date: time::Date,
136+
#[serde(skip_serializing_if = "Option::is_none", default)]
135137
#[serde(with = "time_format::option")]
136138
pub time: Option<time::Time>,
137139

@@ -147,37 +149,59 @@ pub struct Race {
147149
pub name: String,
148150
#[serde(with = "date_format")]
149151
pub date: time::Date,
150-
#[serde(with = "time_format::option")]
152+
#[serde(
153+
skip_serializing_if = "Option::is_none",
154+
with = "time_format::option",
155+
default
156+
)]
151157
pub time: Option<time::Time>,
158+
#[serde(skip_serializing_if = "Option::is_none", default)]
152159
pub url: Option<String>,
160+
#[serde(skip_serializing_if = "Option::is_none", default)]
153161
pub fp1: Option<DateAndTime>,
162+
#[serde(skip_serializing_if = "Option::is_none", default)]
154163
pub fp2: Option<DateAndTime>,
164+
#[serde(skip_serializing_if = "Option::is_none", default)]
155165
pub fp3: Option<DateAndTime>,
166+
#[serde(skip_serializing_if = "Option::is_none", default)]
156167
pub quali: Option<DateAndTime>,
168+
#[serde(skip_serializing_if = "Option::is_none", default)]
157169
pub sprint: Option<DateAndTime>,
158170
}
159171

160172
#[derive(Debug, Serialize, Deserialize, PartialEq)]
161173
pub struct Circuit {
162174
pub circuit_ref: String,
163175
pub name: String,
176+
#[serde(skip_serializing_if = "Option::is_none", default)]
164177
pub location: Option<String>,
178+
#[serde(skip_serializing_if = "Option::is_none", default)]
165179
pub country: Option<String>,
180+
#[serde(skip_serializing_if = "Option::is_none", default)]
166181
pub lat: Option<f32>,
182+
#[serde(skip_serializing_if = "Option::is_none", default)]
167183
pub lng: Option<f32>,
184+
#[serde(skip_serializing_if = "Option::is_none", default)]
168185
pub alt: Option<i32>,
169186
pub url: String,
170187
}
171188

172189
#[derive(Debug, Serialize, Deserialize)]
173190
pub struct Driver {
174191
pub driver_ref: String,
192+
#[serde(skip_serializing_if = "Option::is_none", default)]
175193
pub number: Option<i32>,
194+
#[serde(skip_serializing_if = "Option::is_none", default)]
176195
pub code: Option<String>,
177196
pub forename: String,
178197
pub surname: String,
179-
#[serde(with = "date_format::option")]
198+
#[serde(
199+
skip_serializing_if = "Option::is_none",
200+
with = "date_format::option",
201+
default
202+
)]
180203
pub dob: Option<time::Date>,
204+
#[serde(skip_serializing_if = "Option::is_none", default)]
181205
pub nationality: Option<String>,
182206
pub url: String,
183207
}
@@ -186,14 +210,17 @@ pub struct Driver {
186210
pub struct Constructor {
187211
pub constructor_ref: String,
188212
pub name: String,
213+
#[serde(skip_serializing_if = "Option::is_none", default)]
189214
pub nationality: Option<String>,
190215
pub url: String,
191216
}
192217

193218
#[derive(Debug, Serialize, Deserialize)]
194219
pub struct Standing {
195220
pub points: f32,
221+
#[serde(skip_serializing_if = "Option::is_none", default)]
196222
pub position: Option<i32>,
223+
#[serde(skip_serializing_if = "Option::is_none", default)]
197224
pub position_text: Option<String>,
198225
pub wins: i32,
199226
}
@@ -215,9 +242,10 @@ pub struct Lap {
215242
#[derive(Debug, Serialize, Deserialize)]
216243
pub struct LapTiming {
217244
pub driver_ref: String,
245+
#[serde(skip_serializing_if = "Option::is_none", default)]
218246
pub position: Option<i32>,
219-
#[serde(with = "duration_format::option")]
220-
pub time: Option<time::Time>,
247+
#[serde(skip_serializing_if = "Option::is_none", default)]
248+
pub time: Option<String>,
221249
}
222250

223251
#[derive(Debug, Serialize, Deserialize)]
@@ -227,8 +255,8 @@ pub struct PitStop {
227255
pub stop: i32,
228256
#[serde(with = "time_format")]
229257
pub time: time::Time,
230-
#[serde(with = "short_duration_format::option")]
231-
pub duration: Option<time::Time>,
258+
#[serde(skip_serializing_if = "Option::is_none", default)]
259+
pub duration: Option<String>,
232260
}
233261

234262
#[derive(Debug, Serialize, Deserialize)]

docker-compose.yml

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ services:
55
context: resources/dockerfiles/database
66
command: --default-authentication-plugin=mysql_native_password
77
restart: always
8+
ports:
9+
- "3306:3306"
810
environment:
911
MYSQL_DATABASE: ${MYSQL_DATABASE}
1012
MYSQL_USER: ${MYSQL_USER}
@@ -20,6 +22,8 @@ services:
2022
memlock: -1
2123
volumes:
2224
- dragonflydata:/data
25+
ports:
26+
- "6379:6379"
2327
healthcheck:
2428
test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
2529
timeout: 20s

0 commit comments

Comments
 (0)