ocpi-tariffs 0.53.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
use super::{walk, BuilderKind, Cardinality, Field, Object, Scalar, Schema, Warning};
use crate::json::{self, parse};

static NAME_OBJ: Object = Object {
    fields: &[
        Field::required("first", Scalar::String),
        Field::required("last", Scalar::String),
    ],
    kind: BuilderKind::Ignore,
};
static NAME_SCHEMA: Schema = Schema::Object(&NAME_OBJ);
static PERSON_OBJ: Object = Object {
    fields: &[
        Field::optional("age", Scalar::Number),
        Field::required_object("name", &NAME_OBJ),
    ],
    kind: BuilderKind::Ignore,
};
static PERSON_SCHEMA: Schema = Schema::Object(&PERSON_OBJ);
static NAMES_ARRAY_SCHEMA: Schema = Schema::Array {
    item: &NAME_SCHEMA,
    cardinality: Cardinality::ZeroOrMore,
};
static NAMES_ARRAY_ONE_OR_MORE: Schema = Schema::Array {
    item: &NAME_SCHEMA,
    cardinality: Cardinality::OneOrMore,
};

#[test]
fn empty_object_reports_missing_required() {
    let doc = parse("{}".into()).unwrap();
    let warnings = walk(&doc, &PERSON_SCHEMA).into_warnings();
    assert_eq!(warnings.len_warnings(), 1);
    assert_eq!(
        warnings.path_map()["$"],
        vec![&Warning::MissingField { name: "name" }]
    );
}

#[test]
fn unexpected_fields_reported() {
    let doc = parse(r#"{"nane": {}, "extra": 1}"#.into()).unwrap();
    let warnings = walk(&doc, &PERSON_SCHEMA).into_warnings();
    let by_path = warnings.path_map();
    assert_eq!(warnings.len_warnings(), 3);
    assert_eq!(by_path["$.nane"], vec![&Warning::UnexpectedField]);
    assert_eq!(by_path["$.extra"], vec![&Warning::UnexpectedField]);
    assert_eq!(by_path["$"], vec![&Warning::MissingField { name: "name" }]);
}

#[test]
fn valid_object_is_clean() {
    let doc = parse(r#"{"name": {"first": "John", "last": "Doe"}}"#.into()).unwrap();
    let warnings = walk(&doc, &PERSON_SCHEMA).into_warnings();
    assert!(warnings.is_empty());
}

#[test]
fn optional_field_present_does_not_warn() {
    let doc = parse(r#"{"age": 30, "name": {"first": "J", "last": "D"}}"#.into()).unwrap();
    let warnings = walk(&doc, &PERSON_SCHEMA).into_warnings();
    assert!(warnings.is_empty());
}

#[test]
fn nested_unexpected_reported_with_path() {
    let doc = parse(r#"{"name": {"first": "J", "last": "D", "middle": "X"}}"#.into()).unwrap();
    let warnings = walk(&doc, &PERSON_SCHEMA).into_warnings();
    assert_eq!(warnings.len_warnings(), 1);
    assert_eq!(
        warnings.path_map()["$.name.middle"],
        vec![&Warning::UnexpectedField]
    );
}

#[test]
fn nested_missing_required_reported_with_path() {
    let doc = parse(r#"{"name": {"first": "J"}}"#.into()).unwrap();
    let warnings = walk(&doc, &PERSON_SCHEMA).into_warnings();
    assert_eq!(warnings.len_warnings(), 1);
    assert_eq!(
        warnings.path_map()["$.name"],
        vec![&Warning::MissingField { name: "last" }]
    );
}

#[test]
fn array_items_each_validated() {
    let doc =
        parse(r#"[{"first": "A", "last": "B"}, {"first": "C", "typo": "D"}]"#.into()).unwrap();
    let warnings = walk(&doc, &NAMES_ARRAY_SCHEMA).into_warnings();
    let by_path = warnings.path_map();
    assert_eq!(warnings.len_warnings(), 2);
    assert_eq!(by_path["$[1].typo"], vec![&Warning::UnexpectedField]);
    assert_eq!(
        by_path["$[1]"],
        vec![&Warning::MissingField { name: "last" }]
    );
}

#[test]
fn any_imposes_no_kind_constraint() {
    // `Scalar::Any` constrains nothing, so arbitrary non-null content is clean.
    // (Nested `null`s are still reported - see `null_inside_any_subtree_is_reported`.)
    let doc = parse(r#"{"anything": [1, 2, {"nested": true}]}"#.into()).unwrap();
    let warnings = walk(&doc, &Schema::Scalar(Scalar::Any)).into_warnings();
    assert!(warnings.is_empty());
}

#[test]
fn non_object_against_object_schema_reported() {
    // An array where an object is expected is a shape violation.
    let doc = parse("[1, 2, 3]".into()).unwrap();
    let warnings = walk(&doc, &PERSON_SCHEMA).into_warnings();
    assert_eq!(warnings.len_warnings(), 1);
    assert_eq!(
        warnings.path_map()["$"],
        vec![&Warning::InvalidType {
            expected: json::ValueKind::Object,
            actual: json::ValueKind::Array,
        }]
    );
}

#[test]
fn scalar_where_object_expected_reported() {
    // `name` is declared as an object; a string value is a shape violation. The
    // object schema is not entered, so its required fields are not chased.
    let doc = parse(r#"{"name": "John Doe"}"#.into()).unwrap();
    let warnings = walk(&doc, &PERSON_SCHEMA).into_warnings();
    assert_eq!(warnings.len_warnings(), 1);
    assert_eq!(
        warnings.path_map()["$.name"],
        vec![&Warning::InvalidType {
            expected: json::ValueKind::Object,
            actual: json::ValueKind::String,
        }]
    );
}

#[test]
fn non_array_against_array_schema_reported() {
    // An object where an array is expected is a shape violation.
    let doc = parse(r#"{"first": "J", "last": "D"}"#.into()).unwrap();
    let warnings = walk(&doc, &NAMES_ARRAY_SCHEMA).into_warnings();
    assert_eq!(warnings.len_warnings(), 1);
    assert_eq!(
        warnings.path_map()["$"],
        vec![&Warning::InvalidType {
            expected: json::ValueKind::Array,
            actual: json::ValueKind::Object,
        }]
    );
}

#[test]
fn scalar_invalid_type_reported() {
    // `age` is declared `Scalar::Number`; a boolean value is a type mismatch.
    let doc = parse(r#"{"age": true, "name": {"first": "J", "last": "D"}}"#.into()).unwrap();
    let warnings = walk(&doc, &PERSON_SCHEMA).into_warnings();
    assert_eq!(warnings.len_warnings(), 1);
    assert_eq!(
        warnings.path_map()["$.age"],
        vec![&Warning::InvalidType {
            expected: json::ValueKind::Number,
            actual: json::ValueKind::Bool,
        }]
    );
}

#[test]
fn number_field_accepts_string_encoding() {
    // OCPI numbers may be serialized as quoted strings; that form is accepted silently
    // (no type mismatch and no warning). A linter may choose to flag it later.
    let doc = parse(r#"{"age": "30", "name": {"first": "J", "last": "D"}}"#.into()).unwrap();
    let warnings = walk(&doc, &PERSON_SCHEMA).into_warnings();
    assert!(warnings.is_empty());
}

#[test]
fn empty_one_or_more_array_reported() {
    // A `+` array must hold at least one element; an empty array is a violation.
    let doc = parse("[]".into()).unwrap();
    let warnings = walk(&doc, &NAMES_ARRAY_ONE_OR_MORE).into_warnings();
    assert_eq!(warnings.len_warnings(), 1);
    assert_eq!(warnings.path_map()["$"], vec![&Warning::Cardinality]);
}

#[test]
fn empty_zero_or_more_array_is_clean() {
    // A `*` array may be empty.
    let doc = parse("[]".into()).unwrap();
    let warnings = walk(&doc, &NAMES_ARRAY_SCHEMA).into_warnings();
    assert!(warnings.is_empty());
}

#[test]
fn non_empty_one_or_more_array_is_clean() {
    let doc = parse(r#"[{"first": "A", "last": "B"}]"#.into()).unwrap();
    let warnings = walk(&doc, &NAMES_ARRAY_ONE_OR_MORE).into_warnings();
    assert!(warnings.is_empty());
}

#[test]
fn null_inside_any_subtree_is_reported() {
    // `Scalar::Any` imposes no kind constraint, but the subtree is still walked,
    // so a nested `null` is reported at full depth.
    static OPAQUE_OBJ: Object = Object {
        fields: &[Field::optional("blob", Scalar::Any)],
        kind: BuilderKind::Ignore,
    };
    static OPAQUE_SCHEMA: Schema = Schema::Object(&OPAQUE_OBJ);
    let doc = parse(r#"{"blob": {"nested": null}}"#.into()).unwrap();
    let warnings = walk(&doc, &OPAQUE_SCHEMA).into_warnings();
    assert_eq!(warnings.len_warnings(), 1);
    assert_eq!(
        warnings.path_map()["$.blob.nested"],
        vec![&Warning::NullField]
    );
}

#[test]
fn invalid_type_subtree_is_not_walked() {
    // `name` expects an object but is given an array; the shape mismatch is
    // reported and the array is NOT walked, so the nested `null` is not seen
    // (the single warning is the mismatch).
    let doc = parse(r#"{"name": [null]}"#.into()).unwrap();
    let warnings = walk(&doc, &PERSON_SCHEMA).into_warnings();
    assert_eq!(warnings.len_warnings(), 1);
    assert_eq!(
        warnings.path_map()["$.name"],
        vec![&Warning::InvalidType {
            expected: json::ValueKind::Object,
            actual: json::ValueKind::Array,
        }]
    );
}

#[test]
fn unexpected_field_subtree_is_not_walked() {
    // An unexpected key is reported, but its subtree is NOT walked, so the nested
    // `null` is not reported (the single warning is the unexpected field).
    let doc =
        parse(r#"{"name": {"first": "J", "last": "D"}, "typo": {"x": null}}"#.into()).unwrap();
    let warnings = walk(&doc, &PERSON_SCHEMA).into_warnings();
    assert_eq!(warnings.len_warnings(), 1);
    assert_eq!(
        warnings.path_map()["$.typo"],
        vec![&Warning::UnexpectedField]
    );
}

// A synthetic enum table for exercising the walk's `Scalar::Enum` handling.
const COLOR_VALUES: &[&str] = &["RED", "GREEN", "BLUE"];
static PAINT_OBJ: Object = Object {
    fields: &[Field::required("color", Scalar::Enum(COLOR_VALUES))],
    kind: BuilderKind::Ignore,
};
static PAINT_SCHEMA: Schema = Schema::Object(&PAINT_OBJ);

#[test]
fn enum_permitted_value_is_clean() {
    let doc = parse(r#"{"color": "GREEN"}"#.into()).unwrap();
    let warnings = walk(&doc, &PAINT_SCHEMA).into_warnings();
    assert!(warnings.is_empty());
}

#[test]
fn enum_value_matched_case_insensitively() {
    // A differently-cased value matches the variant, and the case is reported: the value is
    // read, and the spec calls an enumeration case sensitive.
    let doc = parse(r#"{"color": "green"}"#.into()).unwrap();
    let warnings = walk(&doc, &PAINT_SCHEMA).into_warnings();
    assert_eq!(
        warnings.path_map()["$.color"],
        vec![&Warning::IncorrectCase {
            expected: "GREEN",
            actual: "green".to_owned(),
        }]
    );
}

#[test]
fn enum_value_matched_escape_aware_and_case_insensitively() {
    // The value is compared escape-aware and case-insensitively: the JSON
    // string `\u0067reen` decodes to "green", which matches "GREEN".
    let doc = parse(r#"{"color": "\u0067reen"}"#.into()).unwrap();
    let warnings = walk(&doc, &PAINT_SCHEMA).into_warnings();
    assert_eq!(
        warnings.path_map()["$.color"],
        vec![&Warning::IncorrectCase {
            expected: "GREEN",
            actual: "\\u0067reen".to_owned(),
        }]
    );
}

/// A value written in the spec's own case is read without comment, escapes and all.
#[test]
fn enum_value_in_the_spec_case_is_clean() {
    let doc = parse(r#"{"color": "\u0047REEN"}"#.into()).unwrap();
    let warnings = walk(&doc, &PAINT_SCHEMA).into_warnings();
    assert!(warnings.is_empty());
}

#[test]
fn enum_unknown_value_reported() {
    let doc = parse(r#"{"color": "PURPLE"}"#.into()).unwrap();
    let warnings = walk(&doc, &PAINT_SCHEMA).into_warnings();
    assert_eq!(warnings.len_warnings(), 1);
    assert_eq!(
        warnings.path_map()["$.color"],
        vec![&Warning::InvalidValue {
            expected: COLOR_VALUES,
            actual: "PURPLE".to_owned(),
        }]
    );
}

#[test]
fn enum_non_string_reported_as_invalid_type() {
    // A non-string value fails the kind check first; the value-membership check
    // is not reached, so only a single type mismatch is reported.
    let doc = parse(r#"{"color": 7}"#.into()).unwrap();
    let warnings = walk(&doc, &PAINT_SCHEMA).into_warnings();
    assert_eq!(warnings.len_warnings(), 1);
    assert_eq!(
        warnings.path_map()["$.color"],
        vec![&Warning::InvalidType {
            expected: json::ValueKind::String,
            actual: json::ValueKind::Number,
        }]
    );
}

static LABEL_OBJ: Object = Object {
    fields: &[Field::required("code", Scalar::StringMax(3))],
    kind: BuilderKind::Ignore,
};
static LABEL_SCHEMA: Schema = Schema::Object(&LABEL_OBJ);

#[test]
fn string_within_max_is_clean() {
    let doc = parse(r#"{"code": "AB"}"#.into()).unwrap();
    let warnings = walk(&doc, &LABEL_SCHEMA).into_warnings();
    assert!(warnings.is_empty());
}

#[test]
fn string_at_max_is_clean() {
    // A value exactly at the limit is permitted (the bound is inclusive).
    let doc = parse(r#"{"code": "ABC"}"#.into()).unwrap();
    let warnings = walk(&doc, &LABEL_SCHEMA).into_warnings();
    assert!(warnings.is_empty());
}

#[test]
fn string_over_max_reported() {
    let doc = parse(r#"{"code": "ABCD"}"#.into()).unwrap();
    let warnings = walk(&doc, &LABEL_SCHEMA).into_warnings();
    assert_eq!(warnings.len_warnings(), 1);
    assert_eq!(
        warnings.path_map()["$.code"],
        vec![&Warning::StringTooLong { max: 3, len: 4 }]
    );
}

#[test]
fn string_max_non_string_reported_as_invalid_type() {
    // A non-string value fails the kind check first; the length check is not
    // reached, so only a single type mismatch is reported.
    let doc = parse(r#"{"code": 7}"#.into()).unwrap();
    let warnings = walk(&doc, &LABEL_SCHEMA).into_warnings();
    assert_eq!(warnings.len_warnings(), 1);
    assert_eq!(
        warnings.path_map()["$.code"],
        vec![&Warning::InvalidType {
            expected: json::ValueKind::String,
            actual: json::ValueKind::Number,
        }]
    );
}

#[test]
fn string_max_counts_decoded_characters() {
    // The JSON value contains three escaped characters (raw form is 18 bytes) that
    // decode to "AAA". The length is measured on the decoded form, so this is clean.
    let doc = parse(r#"{"code": "\u0041\u0041\u0041"}"#.into()).unwrap();
    let warnings = walk(&doc, &LABEL_SCHEMA).into_warnings();
    assert!(warnings.is_empty());
}

#[test]
fn scalar_any_is_not_type_checked() {
    // A field declared `Scalar::Any` accepts any JSON kind, including an object.
    static OPAQUE_OBJ: Object = Object {
        fields: &[Field::optional("blob", Scalar::Any)],
        kind: BuilderKind::Ignore,
    };
    static OPAQUE_SCHEMA: Schema = Schema::Object(&OPAQUE_OBJ);
    let doc = parse(r#"{"blob": {"nested": 1}}"#.into()).unwrap();
    let warnings = walk(&doc, &OPAQUE_SCHEMA).into_warnings();
    assert!(warnings.is_empty());
}