ocpi-tariffs 0.55.0

OCPI tariff calculations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#![allow(clippy::indexing_slicing, reason = "tests are allowed to panic")]
#![allow(
    clippy::unwrap_in_result,
    reason = "A test can unwrap wherever it wants"
)]
#![allow(
    clippy::panic_in_result_fn,
    reason = "A test can unwrap wherever it wants"
)]
use std::assert_matches;

use super::{Source, find_or_infer};
use crate::{
    Verdict, Version, cdr, schema, test, timezone::Warning, warning::test::VerdictTestExt as _,
};

#[test]
fn should_find_timezone() {
    const JSON: &str = r#"{
    "country_code": "NL",
    "cdr_location": {
        "time_zone": "Europe/Amsterdam"
    }
}"#;

    test::setup();
    let timezone = parse_expect_v221_and_time_zone_field(JSON)
        .unwrap()
        .unwrap();

    assert_matches!(timezone, Source::Found(ref tz) if tz.iana_name() == Some("Europe/Amsterdam"));
}

#[test]
fn should_find_timezone_but_warn_about_use_of_location_for_v221_cdr() {
    const JSON: &str = r#"{
    "country_code": "NL",
    "location": {
        "time_zone": "Europe/Amsterdam"
    }
}"#;

    test::setup();
    let (cdr, mut warnings) =
        cdr::check(crate::json::parse_object(JSON).unwrap(), Version::V221).into_parts();
    // The CDR carries the fields the code under test reads and omits the rest, so the
    // spec's required fields are largely absent.
    warnings.remove_missing_fields();

    // The schema reports the `location` object as non-spec for v221 and reads it anyway. Its
    // contents are not reported: the object as a whole is already flagged.
    test::assert_schema_warnings(&warnings, &[("$.location", &["non_spec_field"])]);

    let (timezone_source, warnings) = find_or_infer(&cdr).unwrap().into_parts();
    let warnings = warnings.into_path_as_str_map();
    let warnings = &warnings["$"];

    assert_matches!(
        timezone_source,
        Source::Found(ref tz) if tz.iana_name() == Some("Europe/Amsterdam")
    );
    // And the `find_or_infer` fn will warn about a v221 CDR having a `location` field.
    assert_matches!(warnings.as_slice(), [Warning::V221CdrHasLocationField]);
}

#[test]
fn should_find_timezone_without_cdr_country() {
    const JSON: &str = r#"{
    "cdr_location": {
        "time_zone": "Europe/Amsterdam"
    }
}"#;

    test::setup();
    let timezone = parse_expect_v221_and_time_zone_field(JSON)
        .unwrap()
        .unwrap();

    assert_matches!(timezone, Source::Found(ref tz) if tz.iana_name() == Some("Europe/Amsterdam"));
}

#[test]
fn should_infer_timezone_when_the_time_zone_is_null() {
    const JSON: &str = r#"{
    "country_code": "NL",
    "cdr_location": {
        "time_zone": null,
        "country": "BEL"
    }
}"#;

    test::setup();
    let (cdr, schema_warnings) =
        cdr::check(crate::json::parse_object(JSON).unwrap(), Version::V221).into_parts();

    // A `null` `time_zone` is the schema's to report; `find_or_infer` just carries on to the
    // country.
    let schema_warnings = schema_warnings.into_path_as_str_map();
    assert_matches!(
        schema_warnings["$.cdr_location.time_zone"].as_slice(),
        [schema::Warning::NonSpecField, schema::Warning::NullField]
    );

    let (timezone, warnings) = find_or_infer(&cdr).unwrap().into_parts();

    assert_matches!(
        timezone,
        Source::Inferred(ref tz) if tz.iana_name() == Some("Europe/Brussels")
    );
    assert!(warnings.is_empty(), "{:?}", warnings.path_id_map());
}

#[test]
fn should_find_timezone_and_warn_about_invalid_type() {
    const JSON: &str = r#"{
    "country_code": "NL",
    "cdr_location": {
        "time_zone": "Europe/Hamsterdam",
        "country": "BEL"
    }
}"#;

    test::setup();
    let (timezone, warnings) = parse_expect_v221_and_time_zone_field(JSON)
        .unwrap()
        .into_parts();

    assert_matches!(
        timezone,
        Source::Inferred(ref tz) if tz.iana_name() == Some("Europe/Brussels")
    );
    let warnings = warnings.into_path_as_str_map();
    let warnings = &warnings["$.cdr_location.time_zone"];
    assert_matches!(warnings.as_slice(), [Warning::InvalidTimezone]);
}

#[test]
fn should_find_timezone_and_warn_about_escape_codes_and_invalid_type() {
    const JSON: &str = r#"{
    "country_code": "NL",
    "cdr_location": {
        "time_zone": "Europe\/Hamsterdam",
        "country": "BEL"
    }
}"#;

    test::setup();
    let (timezone, warnings) = parse_expect_v221_and_time_zone_field(JSON)
        .unwrap()
        .into_parts();

    assert_matches!(
        timezone,
        Source::Inferred(ref tz) if tz.iana_name() == Some("Europe/Brussels")
    );

    let groups = warnings.into_path_as_str_map();
    let warnings = &groups["$.cdr_location.time_zone"];
    assert_matches!(
        warnings.as_slice(),
        [Warning::ContainsEscapeCodes, Warning::InvalidTimezone]
    );
}

#[test]
fn should_find_timezone_and_warn_about_escape_codes() {
    const JSON: &str = r#"{
    "country_code": "NL",
    "cdr_location": {
        "time_zone": "Europe\/Amsterdam",
        "country": "BEL"
    }
}"#;

    test::setup();
    let (timezone, warnings) = parse_expect_v221_and_time_zone_field(JSON)
        .unwrap()
        .into_parts();

    assert_matches!(timezone, Source::Found(ref tz) if tz.iana_name() == Some("Europe/Amsterdam"));

    let groups = warnings.into_path_as_str_map();
    let warnings = &groups["$.cdr_location.time_zone"];
    assert_matches!(warnings.as_slice(), [Warning::ContainsEscapeCodes]);
}

#[test]
fn should_infer_timezone_and_warn_about_an_escape_that_fails_to_decode() {
    const JSON: &str = r#"{
    "country_code": "NL",
    "cdr_location": {
        "time_zone": "Europe\qAmsterdam",
        "country": "BEL"
    }
}"#;

    test::setup();
    let (timezone, warnings) = parse_expect_v221_and_time_zone_field(JSON)
        .unwrap()
        .into_parts();

    assert_matches!(
        timezone,
        Source::Inferred(ref tz) if tz.iana_name() == Some("Europe/Brussels")
    );

    // A string that fails to decode is passed on as written, so the IANA lookup fails
    // too and `find_or_infer` falls back to the location's `country`.
    let groups = warnings.into_path_as_str_map();
    let warnings = &groups["$.cdr_location.time_zone"];
    assert_matches!(
        warnings.as_slice(),
        [
            Warning::Decode(crate::json::decode::Warning::InvalidEscape(_)),
            Warning::InvalidTimezone
        ]
    );
}

#[test]
fn should_infer_timezone_from_location_country() {
    const JSON: &str = r#"{
    "country_code": "NL",
    "cdr_location": {
        "country": "BEL"
    }
}"#;

    test::setup();
    let timezone = parse_expect_v221(JSON).unwrap().unwrap();

    assert_matches!(
        timezone,
        Source::Inferred(ref tz) if tz.iana_name() == Some("Europe/Brussels")
    );
}

/// A caller reads the inferred zone's name, so inference names a country's zone by its
/// current IANA name rather than by a legacy alias with the same offsets, such as `Europe/Kiev`.
#[test]
fn should_infer_current_iana_name_rather_than_legacy_alias() {
    const JSON: &str = r#"{
    "country_code": "NL",
    "cdr_location": {
        "country": "UKR"
    }
}"#;

    test::setup();
    let timezone = parse_expect_v221(JSON).unwrap().unwrap();

    assert_matches!(
        timezone,
        Source::Inferred(ref tz) if tz.iana_name() == Some("Europe/Kyiv")
    );
}

#[test]
fn should_find_timezone_but_report_alpha2_location_country_code() {
    const JSON: &str = r#"{
    "country_code": "NL",
    "cdr_location": {
        "country": "BE"
    }
}"#;

    test::setup();
    let (timezone, warnings) = parse_expect_v221(JSON).unwrap().into_parts();

    assert_matches!(
        timezone,
        Source::Inferred(ref tz) if tz.iana_name() == Some("Europe/Brussels")
    );

    let groups = warnings.into_path_as_str_map();
    let warnings = &groups["$.cdr_location.country"];
    assert_matches!(
        warnings.as_slice(),
        [Warning::LocationCountryShouldBeAlpha3,]
    );
}

#[test]
fn should_not_find_timezone_due_to_no_location() {
    const JSON: &str = r#"{ "country_code": "BE" }"#;

    test::setup();
    let error = parse_expect_v221(JSON).unwrap_only_error();

    assert_eq!(error.element().path.as_str(), "$");
    assert_matches!(error.warning(), Warning::NoLocation);
}

#[test]
fn should_not_find_timezone_due_to_no_country() {
    // The `$.country_code` field is not used at all when inferring the timezone.
    const JSON: &str = r#"{
    "country_code": "BE",
    "cdr_location": {}
}"#;

    test::setup();
    let error = parse_expect_v221(JSON).unwrap_only_error();

    assert_eq!(error.element().path.as_str(), "$.cdr_location");
    assert_matches!(error.warning(), Warning::NoLocationCountry);
}

#[test]
fn should_not_find_timezone_due_to_country_having_many_timezones() {
    const JSON: &str = r#"{
    "country_code": "BE",
    "cdr_location": {
        "country": "CHN"
    }
}"#;

    test::setup();
    let error = parse_expect_v221(JSON).unwrap_only_error();

    assert_eq!(error.element().path.as_str(), "$.cdr_location.country");
    assert_matches!(error.warning(), Warning::CantInferTimezoneFromCountry("CN"));
}

#[test]
fn should_fail_due_to_json_not_being_object() {
    const JSON: &str = r#"["not_a_cdr"]"#;

    test::setup();

    // A CDR that is not a JSON object is now rejected up front by `json::parse_object`
    // rather than later during timezone inference.
    let error = crate::json::parse_object(JSON).unwrap_err();

    assert_matches!(error, crate::json::ParseError::ShouldBeAnObject);
}

/// Parse CDR and infer the timezone.
#[track_caller]
fn parse_expect_v221(json: &str) -> Verdict<Source, Warning> {
    let json = crate::json::parse_object(json).unwrap();
    let (cdr, mut warnings) = cdr::check(json, Version::V221).into_parts();
    // The CDR JSON's used in this test suite are minimal to test specific issues without noise.
    // We don't care if a field is required by the spec and missing from test CDR.
    warnings.remove_missing_fields();

    test::assert_schema_warnings(&warnings, &[]);

    find_or_infer(&cdr)
}

/// A miscased `time_zone` still names one IANA time zone, so `find_or_infer` returns
/// it as found rather than inferring a zone from the country, and warns that the case is wrong.
#[test]
fn should_find_miscased_timezone_and_warn() {
    const JSON: &str = r#"{
    "country_code": "NL",
    "cdr_location": {
        "time_zone": "europe/amsterdam"
    }
}"#;

    test::setup();
    let (timezone, warnings) = parse_expect_v221_and_time_zone_field(JSON)
        .unwrap()
        .into_parts();
    let warnings = warnings.into_path_as_str_map();

    assert_matches!(timezone, Source::Found(ref tz) if tz.iana_name() == Some("Europe/Amsterdam"));
    assert_matches!(
        warnings.get("$.cdr_location.time_zone").map(Vec::as_slice),
        Some([Warning::MiscasedTimezone])
    );
}

/// `find_or_infer` warns whenever the name jiff returns differs from the input,
/// so it relies on jiff returning an alias such as `Europe/Kiev` as itself, not as `Europe/Kyiv`.
#[test]
fn should_find_legacy_alias_timezone_without_warning() {
    const JSON: &str = r#"{
    "country_code": "UA",
    "cdr_location": {
        "time_zone": "Europe/Kiev"
    }
}"#;

    test::setup();
    let timezone = parse_expect_v221_and_time_zone_field(JSON)
        .unwrap()
        .unwrap();

    assert_matches!(timezone, Source::Found(ref tz) if tz.iana_name() == Some("Europe/Kiev"));
}

/// Parse a CDR whose `cdr_location` carries the non-spec `time_zone` field, and infer the
/// timezone.
#[track_caller]
fn parse_expect_v221_and_time_zone_field(json: &str) -> Verdict<Source, Warning> {
    let (cdr, mut warnings) =
        cdr::check(crate::json::parse_object(json).unwrap(), Version::V221).into_parts();
    // The CDR carries the fields the code under test reads and omits the rest, so the
    // spec's required fields are largely absent.
    warnings.remove_missing_fields();

    // `time_zone` is a v211 `Location` field that v221 dropped. The schema reads it anyway
    // and says so.
    test::assert_schema_warnings(
        &warnings,
        &[("$.cdr_location.time_zone", &["non_spec_field"])],
    );

    find_or_infer(&cdr)
}