Skip to content

Commit 8ef3f08

Browse files
committed
chore: improve test coverage
1 parent 4b5535f commit 8ef3f08

File tree

2 files changed

+161
-20
lines changed

2 files changed

+161
-20
lines changed

src/evalutate.rs

Lines changed: 154 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::unix_timestamp;
12
use crate::user::FPUser;
23
use crate::FPError;
34
use byteorder::{BigEndian, ReadBytesExt};
@@ -310,7 +311,7 @@ impl Condition {
310311
ConditionType::Segment => self.match_segment(user, &self.predicate, segment_repo),
311312
ConditionType::Number => self.match_ordering::<f64>(user, &self.predicate),
312313
ConditionType::SemVer => self.match_ordering::<Version>(user, &self.predicate),
313-
ConditionType::Date => self.match_timestamp(user, &self.predicate),
314+
ConditionType::Date => self.match_timestamp(&self.predicate),
314315
_ => false,
315316
}
316317
}
@@ -382,23 +383,16 @@ impl Condition {
382383
false
383384
}
384385

385-
fn match_timestamp(&self, user: &FPUser, predicate: &str) -> bool {
386-
if let Some(c) = user.get(&self.subject) {
387-
let c: u64 = match c.parse() {
388-
Ok(v) => v,
389-
Err(_) => return false,
390-
};
391-
return match predicate {
392-
">=" => self.do_match::<u64>(&c, |c, o| c.ge(o)),
393-
"<" => self.do_match::<u64>(&c, |c, o| c.lt(o)),
394-
_ => {
395-
info!("unknown predicate {}", predicate);
396-
false
397-
}
398-
};
399-
}
400-
info!("user attr missing: {}", self.subject);
401-
false
386+
fn match_timestamp(&self, predicate: &str) -> bool {
387+
let c = unix_timestamp() / 1000;
388+
return match predicate {
389+
"after" => self.do_match::<u128>(&c, |c, o| c.ge(o)),
390+
"before" => self.do_match::<u128>(&c, |c, o| c.lt(o)),
391+
_ => {
392+
info!("unknown predicate {}", predicate);
393+
false
394+
}
395+
};
402396
}
403397

404398
fn do_match<T: FromStr>(&self, t: &T, f: fn(&T, &T) -> bool) -> bool {
@@ -725,7 +719,7 @@ mod distribution_tests {
725719
}
726720

727721
#[cfg(test)]
728-
mod string_condition_tests {
722+
mod condition_tests {
729723
use super::*;
730724
use std::fs;
731725
use std::path::PathBuf;
@@ -1071,7 +1065,7 @@ mod string_condition_tests {
10711065
}
10721066

10731067
#[test]
1074-
fn test_segment_condition() {
1068+
fn test_segment_deserilize() {
10751069
let json_str = r#"
10761070
{
10771071
"type":"segment",
@@ -1084,4 +1078,144 @@ mod string_condition_tests {
10841078
.map_err(|e| FPError::JsonError(e.to_string()));
10851079
assert!(segment.is_ok())
10861080
}
1081+
1082+
#[test]
1083+
fn test_semver_condition() {
1084+
let mut condition = Condition {
1085+
r#type: ConditionType::SemVer,
1086+
subject: "version".to_owned(),
1087+
objects: vec!["1.0.0".to_owned(), "2.0.0".to_owned()],
1088+
predicate: "=".to_owned(),
1089+
};
1090+
1091+
let user = FPUser::new("user").with("version".to_owned(), "1.0.0".to_owned());
1092+
assert!(condition.meet(&user, None));
1093+
let user = FPUser::new("user").with("version".to_owned(), "2.0.0".to_owned());
1094+
assert!(condition.meet(&user, None));
1095+
let user = FPUser::new("user").with("version".to_owned(), "3.0.0".to_owned());
1096+
assert!(!condition.meet(&user, None));
1097+
1098+
condition.predicate = "!=".to_owned();
1099+
let user = FPUser::new("user").with("version".to_owned(), "1.0.0".to_owned());
1100+
assert!(!condition.meet(&user, None));
1101+
let user = FPUser::new("user").with("version".to_owned(), "2.0.0".to_owned());
1102+
assert!(!condition.meet(&user, None));
1103+
let user = FPUser::new("user").with("version".to_owned(), "0.1.0".to_owned());
1104+
assert!(condition.meet(&user, None));
1105+
1106+
condition.predicate = ">".to_owned();
1107+
let user = FPUser::new("user").with("version".to_owned(), "2.0.0".to_owned());
1108+
assert!(condition.meet(&user, None));
1109+
let user = FPUser::new("user").with("version".to_owned(), "3.0.0".to_owned());
1110+
assert!(condition.meet(&user, None));
1111+
let user = FPUser::new("user").with("version".to_owned(), "0.1.0".to_owned());
1112+
assert!(!condition.meet(&user, None));
1113+
1114+
condition.predicate = ">=".to_owned();
1115+
let user = FPUser::new("user").with("version".to_owned(), "1.0.0".to_owned());
1116+
assert!(condition.meet(&user, None));
1117+
let user = FPUser::new("user").with("version".to_owned(), "2.0.0".to_owned());
1118+
assert!(condition.meet(&user, None));
1119+
let user = FPUser::new("user").with("version".to_owned(), "3.0.0".to_owned());
1120+
assert!(condition.meet(&user, None));
1121+
let user = FPUser::new("user").with("version".to_owned(), "0.1.0".to_owned());
1122+
assert!(!condition.meet(&user, None));
1123+
1124+
condition.predicate = "<".to_owned();
1125+
let user = FPUser::new("user").with("version".to_owned(), "1.0.0".to_owned()); // < 2.0.0
1126+
assert!(condition.meet(&user, None));
1127+
let user = FPUser::new("user").with("version".to_owned(), "2.0.0".to_owned());
1128+
assert!(!condition.meet(&user, None));
1129+
let user = FPUser::new("user").with("version".to_owned(), "3.0.0".to_owned());
1130+
assert!(!condition.meet(&user, None));
1131+
1132+
condition.predicate = "<=".to_owned();
1133+
let user = FPUser::new("user").with("version".to_owned(), "1.0.0".to_owned());
1134+
assert!(condition.meet(&user, None));
1135+
let user = FPUser::new("user").with("version".to_owned(), "2.0.0".to_owned());
1136+
assert!(condition.meet(&user, None));
1137+
let user = FPUser::new("user").with("version".to_owned(), "0.1.0".to_owned());
1138+
assert!(condition.meet(&user, None));
1139+
1140+
let user = FPUser::new("user").with("version".to_owned(), "a".to_owned());
1141+
assert!(!condition.meet(&user, None));
1142+
}
1143+
1144+
#[test]
1145+
fn test_number_condition() {
1146+
let mut condition = Condition {
1147+
r#type: ConditionType::Number,
1148+
subject: "price".to_owned(),
1149+
objects: vec!["10".to_owned(), "100".to_owned()],
1150+
predicate: "=".to_owned(),
1151+
};
1152+
1153+
let user = FPUser::new("user").with("price".to_owned(), "10".to_owned());
1154+
assert!(condition.meet(&user, None));
1155+
let user = FPUser::new("user").with("price".to_owned(), "100".to_owned());
1156+
assert!(condition.meet(&user, None));
1157+
let user = FPUser::new("user").with("price".to_owned(), "0".to_owned());
1158+
assert!(!condition.meet(&user, None));
1159+
1160+
condition.predicate = "!=".to_owned();
1161+
let user = FPUser::new("user").with("price".to_owned(), "10".to_owned());
1162+
assert!(!condition.meet(&user, None));
1163+
let user = FPUser::new("user").with("price".to_owned(), "100".to_owned());
1164+
assert!(!condition.meet(&user, None));
1165+
let user = FPUser::new("user").with("price".to_owned(), "0".to_owned());
1166+
assert!(condition.meet(&user, None));
1167+
1168+
condition.predicate = ">".to_owned();
1169+
let user = FPUser::new("user").with("price".to_owned(), "11".to_owned());
1170+
assert!(condition.meet(&user, None));
1171+
let user = FPUser::new("user").with("price".to_owned(), "10".to_owned());
1172+
assert!(!condition.meet(&user, None));
1173+
1174+
condition.predicate = ">=".to_owned();
1175+
let user = FPUser::new("user").with("price".to_owned(), "10".to_owned());
1176+
assert!(condition.meet(&user, None));
1177+
let user = FPUser::new("user").with("price".to_owned(), "11".to_owned());
1178+
assert!(condition.meet(&user, None));
1179+
let user = FPUser::new("user").with("price".to_owned(), "100".to_owned());
1180+
assert!(condition.meet(&user, None));
1181+
let user = FPUser::new("user").with("price".to_owned(), "0".to_owned());
1182+
assert!(!condition.meet(&user, None));
1183+
1184+
condition.predicate = "<".to_owned();
1185+
let user = FPUser::new("user").with("price".to_owned(), "1".to_owned());
1186+
assert!(condition.meet(&user, None));
1187+
let user = FPUser::new("user").with("price".to_owned(), "10".to_owned()); // < 100
1188+
assert!(condition.meet(&user, None));
1189+
let user = FPUser::new("user").with("price".to_owned(), "100".to_owned()); // < 100
1190+
assert!(!condition.meet(&user, None));
1191+
1192+
condition.predicate = "<=".to_owned();
1193+
let user = FPUser::new("user").with("price".to_owned(), "1".to_owned());
1194+
assert!(condition.meet(&user, None));
1195+
let user = FPUser::new("user").with("price".to_owned(), "10".to_owned()); // < 100
1196+
assert!(condition.meet(&user, None));
1197+
let user = FPUser::new("user").with("price".to_owned(), "100".to_owned()); // < 100
1198+
assert!(condition.meet(&user, None));
1199+
1200+
let user = FPUser::new("user").with("price".to_owned(), "a".to_owned());
1201+
assert!(!condition.meet(&user, None));
1202+
}
1203+
1204+
#[test]
1205+
fn test_date_condition() {
1206+
let now_ts = unix_timestamp() / 1000;
1207+
let mut condition = Condition {
1208+
r#type: ConditionType::Date,
1209+
subject: "".to_owned(),
1210+
objects: vec![format!("{}", now_ts)],
1211+
predicate: "after".to_owned(),
1212+
};
1213+
1214+
let user = FPUser::new("user");
1215+
assert!(condition.meet(&user, None));
1216+
1217+
condition.predicate = "before".to_owned();
1218+
condition.objects = vec![format!("{}", now_ts + 2)];
1219+
assert!(condition.meet(&user, None))
1220+
}
10871221
}

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ impl Header for SdkAuthorization {
7070
}
7171
}
7272

73+
pub fn unix_timestamp() -> u128 {
74+
std::time::SystemTime::now()
75+
.duration_since(std::time::UNIX_EPOCH)
76+
.expect("Time went backwards!")
77+
.as_millis()
78+
}
79+
7380
#[cfg(test)]
7481
mod tests {
7582
use super::*;

0 commit comments

Comments
 (0)