1
0
mirror of https://github.com/bspeice/dtparse synced 2024-12-22 04:18:09 -05:00

Fuzzy tests are failing already...

This commit is contained in:
Bradlee Speice 2018-07-02 22:49:05 -04:00
parent d7b3c33567
commit 79ac26e07f
2 changed files with 106 additions and 0 deletions

View File

@ -80,6 +80,9 @@ tests = {
'1994-11-05T08:15:30Z', '1976-07-04T00:01:02Z', '1994-11-05T08:15:30Z', '1976-07-04T00:01:02Z',
'Tue Apr 4 00:22:12 PDT 1995' 'Tue Apr 4 00:22:12 PDT 1995'
], ],
'test_fuzzy': [
'Today is 25 of September of 2003, exactly at 10:49:41 with timezone -03:00.'
],
'test_parse_default_ignore': [ 'test_parse_default_ignore': [
], ],
} }
@ -149,6 +152,12 @@ def test_parse_default_ignore(i, s):
return TEST_PARSE_DEFAULT_IGNORE.format(i=i, d=d, s=s) return TEST_PARSE_DEFAULT_IGNORE.format(i=i, d=d, s=s)
def test_fuzzy(i, s):
d = parse(s, fuzzy=True)
return TEST_FUZZY.format(i=i, d=d, s=s)
# Here lies all the ugly junk. # Here lies all the ugly junk.
TEST_HEADER = ''' TEST_HEADER = '''
extern crate chrono; extern crate chrono;
@ -224,6 +233,42 @@ fn parse_and_assert_simple(
assert_eq!(pdt.tzo, rs_parsed.1.map(|u| u.local_minus_utc()), "Timezone Offset mismatch for {}", s); assert_eq!(pdt.tzo, rs_parsed.1.map(|u| u.local_minus_utc()), "Timezone Offset mismatch for {}", s);
} }
fn parse_fuzzy_and_assert(
pdt: PyDateTime,
ptokens: Option<Vec<String>>,
info: ParserInfo,
s: &str,
dayfirst: Option<bool>,
yearfirst: Option<bool>,
fuzzy: bool,
fuzzy_with_tokens: bool,
default: Option<&NaiveDateTime>,
ignoretz: bool,
tzinfos: HashMap<String, i32>,
) {
let mut parser = Parser::new(info);
let rs_parsed = parser.parse(
s,
dayfirst,
yearfirst,
fuzzy,
fuzzy_with_tokens,
default,
ignoretz,
tzinfos).expect(&format!("Unable to parse date in Rust '{}'", s));
assert_eq!(pdt.year, rs_parsed.0.year(), "Year mismatch for '{}'", s);
assert_eq!(pdt.month, rs_parsed.0.month(), "Month mismatch for '{}'", s);
assert_eq!(pdt.day, rs_parsed.0.day(), "Day mismatch for '{}'", s);
assert_eq!(pdt.hour, rs_parsed.0.hour(), "Hour mismatch for '{}'", s);
assert_eq!(pdt.minute, rs_parsed.0.minute(), "Minute mismatch f'or' {}", s);
assert_eq!(pdt.second, rs_parsed.0.second(), "Second mismatch for '{}'", s);
assert_eq!(pdt.micros, rs_parsed.0.timestamp_subsec_micros(), "Microsecond mismatch for {}", s);
assert_eq!(pdt.tzo, rs_parsed.1.map(|u| u.local_minus_utc()), "Timezone Offset mismatch for {}", s);
assert_eq!(ptokens, rs_parsed.2, "Fuzzy mismatch for {}", s);
}
macro_rules! rs_tzinfo_map { macro_rules! rs_tzinfo_map {
() => ({ () => ({
let mut h = HashMap::new(); let mut h = HashMap::new();
@ -364,6 +409,19 @@ fn test_parse_default_ignore{i}() {{
Some(default_rsdate), false, HashMap::new()); Some(default_rsdate), false, HashMap::new());
}}\n''' }}\n'''
TEST_FUZZY = '''
#[test]
fn test_fuzzy{i}() {{
let info = ParserInfo::default();
let pdt = PyDateTime {{
year: {d.year}, month: {d.month}, day: {d.day},
hour: {d.hour}, minute: {d.minute}, second: {d.second},
micros: {d.microsecond}, tzo: None
}};
parse_fuzzy_and_assert(pdt, None, info, "{s}", None, None, true, false,
None, false, HashMap::new());
}}\n'''
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -72,6 +72,42 @@ fn parse_and_assert_simple(
assert_eq!(pdt.tzo, rs_parsed.1.map(|u| u.local_minus_utc()), "Timezone Offset mismatch for {}", s); assert_eq!(pdt.tzo, rs_parsed.1.map(|u| u.local_minus_utc()), "Timezone Offset mismatch for {}", s);
} }
fn parse_fuzzy_and_assert(
pdt: PyDateTime,
ptokens: Option<Vec<String>>,
info: ParserInfo,
s: &str,
dayfirst: Option<bool>,
yearfirst: Option<bool>,
fuzzy: bool,
fuzzy_with_tokens: bool,
default: Option<&NaiveDateTime>,
ignoretz: bool,
tzinfos: HashMap<String, i32>,
) {
let mut parser = Parser::new(info);
let rs_parsed = parser.parse(
s,
dayfirst,
yearfirst,
fuzzy,
fuzzy_with_tokens,
default,
ignoretz,
tzinfos).expect(&format!("Unable to parse date in Rust '{}'", s));
assert_eq!(pdt.year, rs_parsed.0.year(), "Year mismatch for '{}'", s);
assert_eq!(pdt.month, rs_parsed.0.month(), "Month mismatch for '{}'", s);
assert_eq!(pdt.day, rs_parsed.0.day(), "Day mismatch for '{}'", s);
assert_eq!(pdt.hour, rs_parsed.0.hour(), "Hour mismatch for '{}'", s);
assert_eq!(pdt.minute, rs_parsed.0.minute(), "Minute mismatch f'or' {}", s);
assert_eq!(pdt.second, rs_parsed.0.second(), "Second mismatch for '{}'", s);
assert_eq!(pdt.micros, rs_parsed.0.timestamp_subsec_micros(), "Microsecond mismatch for {}", s);
assert_eq!(pdt.tzo, rs_parsed.1.map(|u| u.local_minus_utc()), "Timezone Offset mismatch for {}", s);
assert_eq!(ptokens, rs_parsed.2, "Fuzzy mismatch for {}", s);
}
macro_rules! rs_tzinfo_map { macro_rules! rs_tzinfo_map {
() => ({ () => ({
let mut h = HashMap::new(); let mut h = HashMap::new();
@ -1697,3 +1733,15 @@ fn test_parse_ignoretz7() {
parse_and_assert(pdt, info, "Tue Apr 4 00:22:12 PDT 1995", None, None, false, false, parse_and_assert(pdt, info, "Tue Apr 4 00:22:12 PDT 1995", None, None, false, false,
None, true, HashMap::new()); None, true, HashMap::new());
} }
#[test]
fn test_fuzzy0() {
let info = ParserInfo::default();
let pdt = PyDateTime {
year: 2003, month: 9, day: 25,
hour: 10, minute: 49, second: 41,
micros: 0, tzo: None
};
parse_fuzzy_and_assert(pdt, None, info, "Today is 25 of September of 2003, exactly at 10:49:41 with timezone -03:00.", None, None, true, false,
None, false, HashMap::new());
}