@@ -145,9 +145,22 @@ mod event {
145
145
static IMPRESSION_STRING : Lazy < String > = Lazy :: new ( || EventType :: Impression . to_string ( ) ) ;
146
146
static CLICK_STRING : Lazy < String > = Lazy :: new ( || EventType :: Click . to_string ( ) ) ;
147
147
148
- #[ derive( Debug , Display , FromStr , Serialize , Deserialize , Hash , Ord , Eq , PartialEq , PartialOrd , Clone , Copy ) ]
148
+ #[ derive(
149
+ Debug ,
150
+ Display ,
151
+ FromStr ,
152
+ Serialize ,
153
+ Deserialize ,
154
+ Hash ,
155
+ Ord ,
156
+ Eq ,
157
+ PartialEq ,
158
+ PartialOrd ,
159
+ Clone ,
160
+ Copy ,
161
+ ) ]
149
162
#[ display( style = "SNAKE_CASE" ) ]
150
- #[ serde( rename_all= "SCREAMING_SNAKE_CASE" ) ]
163
+ #[ serde( rename_all = "SCREAMING_SNAKE_CASE" ) ]
151
164
pub enum EventType {
152
165
Impression ,
153
166
Click ,
@@ -300,7 +313,7 @@ pub struct FetchedAnalytics {
300
313
}
301
314
302
315
/// The value of the requested analytics [`Metric`].
303
- #[ derive( Debug , Serialize , Deserialize , Clone , PartialEq , Eq ) ]
316
+ #[ derive( Debug , Serialize , Deserialize , Clone , Copy , PartialEq , Eq ) ]
304
317
#[ serde( untagged) ]
305
318
pub enum FetchedMetric {
306
319
Count ( u32 ) ,
@@ -355,7 +368,11 @@ impl<Tz: TimeZone, Tz2: TimeZone> PartialEq<DateHour<Tz2>> for DateHour<Tz> {
355
368
356
369
impl < Tz : TimeZone > Ord for DateHour < Tz > {
357
370
fn cmp ( & self , other : & DateHour < Tz > ) -> Ordering {
358
- self . date . cmp ( & other. date )
371
+ match self . date . cmp ( & other. date ) {
372
+ // Only if the two dates are equal, compare the hours too!
373
+ Ordering :: Equal => self . hour . cmp ( & other. hour ) ,
374
+ ordering => ordering,
375
+ }
359
376
}
360
377
}
361
378
@@ -365,11 +382,14 @@ impl<Tz: TimeZone, Tz2: TimeZone> PartialOrd<DateHour<Tz2>> for DateHour<Tz> {
365
382
/// See [`DateTime`] implementation of `PartialOrd<DateTime<Tz2>>` for more details.
366
383
fn partial_cmp ( & self , other : & DateHour < Tz2 > ) -> Option < Ordering > {
367
384
if self . date == other. date {
368
- if self . hour > other. hour {
369
- Some ( Ordering :: Greater )
370
- } else {
371
- Some ( Ordering :: Less )
372
- }
385
+ self . hour . partial_cmp ( & other. hour )
386
+ // if self.hour > other.hour {
387
+ // Some(Ordering::Greater)
388
+ // } else if self.hour == other.hour {
389
+ // Some(Ordering::Equal)
390
+ // } else {
391
+ // Some(Ordering::Less)
392
+ // }
373
393
} else {
374
394
self . date . naive_utc ( ) . partial_cmp ( & other. date . naive_utc ( ) )
375
395
}
@@ -922,7 +942,8 @@ pub mod campaign_create {
922
942
#[ cfg( feature = "postgres" ) ]
923
943
mod postgres {
924
944
use super :: {
925
- Analytics , DateHour , FetchedAnalytics , FetchedMetric , MessageResponse , ValidatorMessage , EventType ,
945
+ Analytics , DateHour , EventType , FetchedAnalytics , FetchedMetric , MessageResponse ,
946
+ ValidatorMessage ,
926
947
} ;
927
948
use crate :: {
928
949
analytics:: { AnalyticsQuery , Metric } ,
@@ -1072,7 +1093,7 @@ mod postgres {
1072
1093
raw : & ' a [ u8 ] ,
1073
1094
) -> Result < Self , Box < dyn std:: error:: Error + Sync + Send > > {
1074
1095
let event_string = <& str as FromSql >:: from_sql ( ty, raw) ?;
1075
-
1096
+
1076
1097
Ok ( event_string. parse ( ) ?)
1077
1098
}
1078
1099
accepts ! ( VARCHAR , TEXT ) ;
@@ -1129,7 +1150,7 @@ mod test {
1129
1150
use serde_json:: { json, Value } ;
1130
1151
1131
1152
#[ test]
1132
- pub fn de_serialize_events ( ) {
1153
+ pub fn test_de_serialize_events ( ) {
1133
1154
let click = Event :: Click {
1134
1155
publisher : ADDRESSES [ "publisher" ] ,
1135
1156
ad_unit : Some ( DUMMY_IPFS [ 0 ] ) ,
@@ -1152,7 +1173,7 @@ mod test {
1152
1173
}
1153
1174
1154
1175
#[ test]
1155
- fn datehour_subtract_timeframe ( ) {
1176
+ fn test_datehour_subtract_timeframe ( ) {
1156
1177
// test with End of year
1157
1178
{
1158
1179
let datehour = DateHour :: from_ymdh ( 2021 , 12 , 31 , 22 ) ;
@@ -1175,7 +1196,7 @@ mod test {
1175
1196
}
1176
1197
1177
1198
#[ test]
1178
- fn datehour_de_serialize_partial_ord_and_eq ( ) {
1199
+ fn test_datehour_de_serialize_partial_ord_and_eq ( ) {
1179
1200
let earlier = DateHour :: from_ymdh ( 2021 , 12 , 1 , 16 ) ;
1180
1201
let later = DateHour :: from_ymdh ( 2021 , 12 , 31 , 16 ) ;
1181
1202
@@ -1189,16 +1210,46 @@ mod test {
1189
1210
assert_eq ! ( Some ( Ordering :: Greater ) , later. partial_cmp( & earlier) ) ;
1190
1211
1191
1212
// Serialize & deserialize
1192
- let json_datetime = Value :: String ( "2021-12-1T16 :00:00+02:00" . into ( ) ) ;
1213
+ let json_datetime = Value :: String ( "2021-12-01T16 :00:00+02:00" . into ( ) ) ;
1193
1214
let datehour: DateHour < Utc > =
1194
1215
serde_json:: from_value ( json_datetime. clone ( ) ) . expect ( "Should deserialize" ) ;
1195
1216
assert_eq ! (
1196
- DateHour :: from_ymdh( 2021 , 12 , 1 , 12 ) ,
1217
+ DateHour :: from_ymdh( 2021 , 12 , 1 , 14 ) ,
1197
1218
datehour,
1198
1219
"Deserialized DateHour should be 2 hours earlier to match UTC +0"
1199
1220
) ;
1200
1221
1201
1222
let serialized_value = serde_json:: to_value ( datehour) . expect ( "Should serialize DateHour" ) ;
1202
- assert_eq ! ( json_datetime, serialized_value) ;
1223
+ assert_eq ! (
1224
+ Value :: String ( "2021-12-01T14:00:00Z" . into( ) ) ,
1225
+ serialized_value,
1226
+ "Json value should always be serialized with Z (UTC+0) timezone"
1227
+ ) ;
1228
+ }
1229
+
1230
+ #[ test]
1231
+ fn test_partial_eq_with_same_datehour_but_different_zones ( ) {
1232
+ // Eq & PartialEq with different timezones
1233
+ let json = json ! ( {
1234
+ "UTC+0" : "2021-12-16T14:00:00+00:00" ,
1235
+ "UTC+2" : "2021-12-16T16:00:00+02:00" ,
1236
+ } ) ;
1237
+
1238
+ let map: HashMap < String , DateHour < Utc > > =
1239
+ serde_json:: from_value ( json) . expect ( "Should deserialize" ) ;
1240
+
1241
+ assert_eq ! (
1242
+ Some ( Ordering :: Equal ) ,
1243
+ map[ "UTC+0" ] . partial_cmp( & map[ "UTC+2" ] )
1244
+ ) ;
1245
+
1246
+ assert_eq ! (
1247
+ map[ "UTC+0" ] , map[ "UTC+2" ] ,
1248
+ "DateHour should be the same after the second one is made into UTC+0"
1249
+ ) ;
1250
+ assert ! (
1251
+ map[ "UTC+0" ] >= map[ "UTC+2" ] ,
1252
+ "UTC+0 value should be equal to UTC+2"
1253
+ ) ;
1203
1254
}
1204
1255
}
0 commit comments