architecturekit 0.6.0

Building blocks for DDD-based applications with CQRS and event sourcing, on top of the EventSourcingDB by the native web.
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
// These tests mirror TestSubjectScheme and TestSubjectSchemeValue of
// ArchitectureKit for Go, under the same sentences. Some do not apply to Rust:
//
// - "reports its placeholders" checks that the returned names are a copy,
//   which the borrow checker makes a given for the slice placeholders returns.
// - "refuses values with characters the database does not allow" has a case
//   for invalid UTF-8, which a &str can not hold.
// - "agrees with the database on every character" needs write, so it comes
//   with the store.
//
// Messages quote values the way Rust does, with {:?}, where Go uses %q.

use std::{
    collections::HashMap,
    panic::{UnwindSafe, catch_unwind},
};

use crate::SubjectScheme;

/// The characters that EventSourcingDB allows in a segment of a subject,
/// written out rather than taken from the code under test.
const DATABASE_CHARACTERS: &str =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";

/// Every ASCII character, and some beyond: letters outside ASCII, characters
/// that look like the ones the database allows, and the replacement
/// character.
fn some_characters() -> Vec<char> {
    (0..128_u8)
        .map(char::from)
        .chain([
            '\u{0080}',
            '\u{00fc}',
            '\u{00df}',
            '\u{00e9}',
            '\u{212a}',
            '\u{2010}',
            '\u{ff11}',
            '\u{ff21}',
            '\u{ff3f}',
            '\u{1f600}',
            '\u{fffd}',
        ])
        .collect()
}

/// Runs a function that is expected to panic, and returns the message of its
/// panic, or `None` if it did not panic.
fn panic_message(function: impl FnOnce() + UnwindSafe) -> Option<String> {
    let payload = catch_unwind(function).err()?;
    payload.downcast_ref::<String>().cloned().or_else(|| {
        payload
            .downcast_ref::<&str>()
            .map(|message| (*message).to_owned())
    })
}

fn workshops() -> SubjectScheme {
    SubjectScheme::new("/tenant/{tenant}/workshop/{workshop}")
}

#[test]
fn builds_and_matches() {
    let scheme = workshops();

    let subject = scheme.build(&["acme", "go-cqrs"]);
    assert_eq!(subject, "/tenant/acme/workshop/go-cqrs");

    let values = scheme.match_subject(&subject).expect("the subject matches");
    assert_eq!(values["tenant"], "acme");
    assert_eq!(values["workshop"], "go-cqrs");
}

#[test]
fn without_placeholders() {
    let scheme = SubjectScheme::new("/system/health");

    assert_eq!(scheme.build::<&str>(&[]), "/system/health");
    assert!(
        scheme.match_subject("/system/health").is_some(),
        "should match itself"
    );
}

#[test]
fn reports_its_placeholders() {
    assert_eq!(workshops().placeholders(), ["tenant", "workshop"]);
}

#[test]
fn rejects_subjects_that_do_not_fit() {
    let scheme = workshops();

    for (name, subject) in [
        ("no leading slash", "tenant/acme/workshop/go-cqrs"),
        ("too few segments", "/tenant/acme/workshop"),
        ("too many segments", "/tenant/acme/workshop/go-cqrs/more"),
        ("literal does not match", "/client/acme/workshop/go-cqrs"),
        ("empty placeholder value", "/tenant/acme/workshop/"),
        (
            "value with a character the database refuses",
            "/tenant/acme/workshop/go.cqrs",
        ),
    ] {
        assert!(
            scheme.match_subject(subject).is_none(),
            "{name}: {subject:?} should not match"
        );
    }
}

#[test]
fn panics_on_malformed_patterns() {
    for (name, pattern) in [
        ("no leading slash", "workshop/{id}"),
        ("empty segment", "/workshop//{id}"),
        ("unclosed placeholder", "/workshop/{id"),
        ("unnamed placeholder", "/workshop/{}"),
        ("duplicate placeholder", "/a/{id}/b/{id}"),
        ("malformed segment", "/workshop/pre{id}"),
        ("dot in a literal", "/books.v2/{book}"),
        ("space in a literal", "/my books/{book}"),
        ("umlaut in a literal", "/b\u{00fc}cher/{book}"),
        ("dots as a literal", "/../{book}"),
        ("closing brace too many", "/{a}}"),
        ("placeholder in braces twice", "/{{a}}"),
        ("opening brace in a placeholder", "/{a{b}"),
        ("space in a placeholder", "/{a b}"),
        ("umlaut in a placeholder", "/{\u{00e4}}"),
        ("dot as a placeholder", "/{.}"),
        ("asterisk as a placeholder", "/x/{*}"),
    ] {
        assert!(
            panic_message(|| drop(SubjectScheme::new(pattern))).is_some(),
            "{name}: {pattern:?} should panic",
        );
    }
}

#[test]
fn names_what_is_wrong_with_a_literal() {
    assert_eq!(
        panic_message(|| drop(SubjectScheme::new("/books.v2/{book}"))).as_deref(),
        Some(
            r#"architecturekit: segment "books.v2" in "/books.v2/{book}" may only contain A-Z, a-z, 0-9, underscores, and hyphens"#
        ),
    );

    // A brace means a placeholder that is not the whole segment, which says
    // more than the characters.
    assert_eq!(
        panic_message(|| drop(SubjectScheme::new("/workshop/pre{id}"))).as_deref(),
        Some(r#"architecturekit: segment "pre{id}" in "/workshop/pre{id}" is malformed"#),
    );
}

#[test]
fn names_what_is_wrong_with_a_placeholder() {
    for (pattern, expected) in [
        (
            "/{a b}",
            r#"architecturekit: placeholder "a b" in "/{a b}" may only contain A-Z, a-z, 0-9, underscores, and hyphens"#,
        ),
        (
            "/{\u{00e4}}",
            "architecturekit: placeholder \"\u{00e4}\" in \"/{\u{00e4}}\" may only contain A-Z, a-z, 0-9, underscores, and hyphens",
        ),
        (
            "/{.}",
            r#"architecturekit: placeholder "." in "/{.}" may only contain A-Z, a-z, 0-9, underscores, and hyphens"#,
        ),
        (
            "/x/{*}",
            r#"architecturekit: placeholder "*" in "/x/{*}" may only contain A-Z, a-z, 0-9, underscores, and hyphens"#,
        ),
        (
            "/books/{book.id}",
            r#"architecturekit: placeholder "book.id" in "/books/{book.id}" may only contain A-Z, a-z, 0-9, underscores, and hyphens"#,
        ),
        // A brace that does not open or close the segment means a mistake in
        // the braces, which says more than the characters.
        (
            "/{a}}",
            r#"architecturekit: segment "{a}}" in "/{a}}" is malformed"#,
        ),
        (
            "/{{a}}",
            r#"architecturekit: segment "{{a}}" in "/{{a}}" is malformed"#,
        ),
        (
            "/{a{b}",
            r#"architecturekit: segment "{a{b}" in "/{a{b}" is malformed"#,
        ),
        (
            "/{a}{b}",
            r#"architecturekit: segment "{a}{b}" in "/{a}{b}" is malformed"#,
        ),
        (
            "/{}}",
            r#"architecturekit: segment "{}}" in "/{}}" is malformed"#,
        ),
        (
            "/{{}",
            r#"architecturekit: segment "{{}" in "/{{}" is malformed"#,
        ),
        (
            "/{a}b",
            r#"architecturekit: segment "{a}b" in "/{a}b" is malformed"#,
        ),
        ("/{", r#"architecturekit: segment "{" in "/{" is malformed"#),
        ("/}", r#"architecturekit: segment "}" in "/}" is malformed"#),
        (
            "/a}",
            r#"architecturekit: segment "a}" in "/a}" is malformed"#,
        ),
        (
            "/{}",
            r#"architecturekit: pattern "/{}" has an unnamed placeholder"#,
        ),
    ] {
        assert_eq!(
            panic_message(|| drop(SubjectScheme::new(pattern))).as_deref(),
            Some(expected),
            "{pattern:?}",
        );
    }
}

#[test]
fn accepts_placeholder_names_with_every_character_the_database_allows() {
    let scheme =
        SubjectScheme::new("/tenant/{Tenant_2}/agreements/{license-agreement}/{_}/{-}/{0}");

    assert_eq!(
        scheme.placeholders(),
        ["Tenant_2", "license-agreement", "_", "-", "0"]
    );

    let subject = scheme.build(&["acme", "42", "a", "b", "c"]);
    assert_eq!(subject, "/tenant/acme/agreements/42/a/b/c");

    let values = scheme.match_subject(&subject).expect("the subject matches");
    assert_eq!(
        values,
        HashMap::from([
            ("Tenant_2", "acme"),
            ("license-agreement", "42"),
            ("_", "a"),
            ("-", "b"),
            ("0", "c"),
        ]),
    );

    assert_eq!(scheme.value(&subject, "license-agreement"), Some("42"));

    let _ = SubjectScheme::new(&format!("/{{{DATABASE_CHARACTERS}}}"));
}

#[test]
fn accepts_the_placeholder_names_that_applications_use() {
    for name in [
        "user",
        "todo",
        "customer",
        "license-agreement",
        "master-agreement",
        "migration-ticket",
        "trial-license",
        "instance",
        "hour",
        "access",
        "key",
    ] {
        let scheme = SubjectScheme::new(&format!("/things/{{{name}}}"));

        assert_eq!(
            scheme.value(&scheme.build(&["42"]), name),
            Some("42"),
            "{name}"
        );
    }
}

#[test]
fn accepts_literals_with_every_character_the_database_allows() {
    let scheme = SubjectScheme::new("/Tenant_2/{tenant}/work-shop/{workshop}");

    let subject = scheme.build(&["acme", "42"]);
    assert_eq!(subject, "/Tenant_2/acme/work-shop/42");

    let values = scheme.match_subject(&subject).expect("the subject matches");
    assert_eq!(
        values,
        HashMap::from([("tenant", "acme"), ("workshop", "42")])
    );
    assert_eq!(scheme.root(), "/Tenant_2");
}

#[test]
fn panics_on_bad_values() {
    let scheme = SubjectScheme::new("/workshop/{workshop}");

    for (name, values) in [
        ("too few", vec![]),
        ("too many", vec!["a", "b"]),
        ("empty", vec![""]),
        ("contains a slash", vec!["with/slash"]),
    ] {
        assert!(
            panic_message(|| drop(scheme.build(&values))).is_some(),
            "{name} should panic",
        );
    }
}

#[test]
fn checks_values() {
    let scheme = SubjectScheme::new("/workshop/{workshop}");

    assert!(scheme.check(&["42"]).is_ok(), "values that fit should pass");

    for (name, values) in [
        ("too few", vec![]),
        ("too many", vec!["a", "b"]),
        ("empty", vec![""]),
        ("contains a slash", vec!["with/slash"]),
    ] {
        assert!(scheme.check(&values).is_err(), "{name} should be refused");
    }
}

#[test]
fn names_the_placeholder_of_a_bad_value() {
    let error = workshops()
        .check(&["acme", ""])
        .expect_err("an empty value is refused");

    assert!(error.to_string().contains(r#""workshop""#), "{error}");
}

#[test]
fn says_which_characters_a_value_may_contain() {
    let scheme = workshops();

    assert_eq!(
        scheme
            .check(&["acme", "go.cqrs"])
            .expect_err("a dot is refused")
            .to_string(),
        r#"value "go.cqrs" for "workshop" may only contain A-Z, a-z, 0-9, underscores, and hyphens"#,
    );
    assert_eq!(
        panic_message(|| drop(scheme.build(&["acme", "go.cqrs"]))).as_deref(),
        Some(concat!(
            r#"architecturekit: value "go.cqrs" for "workshop" in "/tenant/{tenant}/workshop/{workshop}" "#,
            "may only contain A-Z, a-z, 0-9, underscores, and hyphens",
        )),
    );
}

#[test]
fn names_only_the_placeholder_when_it_checks_but_the_crate_and_the_pattern_when_it_builds() {
    // What check returns may reach the caller of an API, while what build
    // panics with is for the developer.
    let scheme = workshops();

    for (name, values, checked, built) in [
        (
            "too few values",
            vec!["acme"],
            "the subject needs 2 value(s), got 1",
            r#"architecturekit: pattern "/tenant/{tenant}/workshop/{workshop}" needs 2 value(s), got 1"#,
        ),
        (
            "too many values",
            vec!["acme", "go-cqrs", "more"],
            "the subject needs 2 value(s), got 3",
            r#"architecturekit: pattern "/tenant/{tenant}/workshop/{workshop}" needs 2 value(s), got 3"#,
        ),
        (
            "an empty value",
            vec!["", "go-cqrs"],
            r#"value for "tenant" must not be empty"#,
            r#"architecturekit: value for "tenant" in "/tenant/{tenant}/workshop/{workshop}" must not be empty"#,
        ),
        (
            "a value with a character the database does not allow",
            vec!["acme", "go/cqrs"],
            r#"value "go/cqrs" for "workshop" may only contain A-Z, a-z, 0-9, underscores, and hyphens"#,
            concat!(
                r#"architecturekit: value "go/cqrs" for "workshop" in "/tenant/{tenant}/workshop/{workshop}" "#,
                "may only contain A-Z, a-z, 0-9, underscores, and hyphens",
            ),
        ),
    ] {
        let error = scheme.check(&values).expect_err(name);
        assert_eq!(error.to_string(), checked, "{name}");
        assert!(
            std::error::Error::source(&error).is_none(),
            "{name}: the error wraps nothing"
        );
        assert_eq!(
            panic_message(|| drop(scheme.build(&values))).as_deref(),
            Some(built),
            "{name}"
        );
    }
}

#[test]
fn refuses_values_with_characters_the_database_does_not_allow() {
    let scheme = SubjectScheme::new("/workshop/{workshop}");

    for (name, value) in [
        ("dot", "a.b"),
        ("only a dot", "."),
        ("only dots", ".."),
        ("space", "with space"),
        ("umlaut", "\u{00fc}mlaut"),
        ("colon", "a:b"),
        ("at sign", "a@b"),
        ("tilde", "a~b"),
        ("asterisk", "*"),
        ("question mark", "a?b"),
        ("hash", "a#b"),
        ("encoded slash", "a%2Fb"),
        ("comma", "a,b"),
        ("line feed", "a\nb"),
        ("null character", "a\0b"),
    ] {
        assert_eq!(
            scheme.check(&[value]).expect_err(name).to_string(),
            format!(
                r#"value {value:?} for "workshop" may only contain A-Z, a-z, 0-9, underscores, and hyphens"#
            ),
            "{name}",
        );
        assert_eq!(
            panic_message(|| drop(scheme.build(&[value]))),
            Some(format!(
                r#"architecturekit: value {value:?} for "workshop" in "/workshop/{{workshop}}" may only contain A-Z, a-z, 0-9, underscores, and hyphens"#
            )),
            "{name}",
        );
    }
}

#[test]
fn accepts_values_with_characters_the_database_allows() {
    let scheme = SubjectScheme::new("/workshop/{workshop}");

    for value in [
        "UPPER",
        "lower",
        "MiXeD",
        "0123456789",
        "a_b",
        "a-b",
        "_",
        "-",
        "-_-",
        DATABASE_CHARACTERS,
    ] {
        assert!(scheme.check(&[value]).is_ok(), "{value:?} should pass");

        let subject = scheme.build(&[value]);
        assert_eq!(subject, format!("/workshop/{value}"));

        let values = scheme.match_subject(&subject).expect("the subject matches");
        assert_eq!(values["workshop"], value);
    }
}

#[test]
fn allows_exactly_the_characters_the_database_allows() {
    let scheme = SubjectScheme::new("/workshop/{workshop}");

    for character in some_characters() {
        // In the middle, so that neither the first nor the last character alone
        // decides.
        let value = format!("a{character}b");
        let is_allowed = DATABASE_CHARACTERS.contains(character);

        assert_eq!(
            scheme.check(&[&value]).is_ok(),
            is_allowed,
            "check of {value:?}"
        );
        assert_eq!(
            panic_message(|| drop(scheme.build(&[&value]))).is_none(),
            is_allowed,
            "build of {value:?}",
        );
        assert_eq!(
            scheme
                .match_subject(&format!("/workshop/{value}"))
                .is_some(),
            is_allowed,
            "match of {value:?}",
        );

        // A slash in a literal separates two segments rather than being part of
        // one.
        if character == '/' {
            continue;
        }

        let literal = format!("/{value}/{{workshop}}");
        assert_eq!(
            panic_message(|| drop(SubjectScheme::new(&literal))).is_none(),
            is_allowed,
            "literal {value:?}",
        );

        let placeholder = format!("/workshop/{{{value}}}");
        assert_eq!(
            panic_message(|| drop(SubjectScheme::new(&placeholder))).is_none(),
            is_allowed,
            "placeholder {value:?}",
        );
    }
}

#[test]
fn reports_its_root() {
    for (pattern, root) in [
        ("/instances/{instance}", "/instances"),
        ("/tenant/{tenant}/workshop/{workshop}", "/tenant"),
        ("/system/tenants/{tenant}", "/system/tenants"),
        ("/{tenant}/orders/{order}", "/"),
        ("/system/health", "/system/health"),
    ] {
        assert_eq!(SubjectScheme::new(pattern).root(), root, "{pattern}");
    }
}

#[test]
fn builds_every_subject_under_its_root() {
    // A recursive read from a subject covers that subject and every one below
    // it, so this is what makes reading from the root complete.
    for scheme in [
        workshops(),
        SubjectScheme::new("/{tenant}/orders/{order}"),
        SubjectScheme::new("/system/health"),
    ] {
        let values = vec!["value"; scheme.placeholders().len()];
        let subject = scheme.build(&values);
        let root = scheme.root();

        let is_under = subject == root || root == "/" || subject.starts_with(&format!("{root}/"));
        assert!(is_under, "{subject:?} is not under {root:?}");
    }
}

#[test]
fn reports_its_pattern() {
    let pattern = "/tenant/{tenant}/workshop/{workshop}";

    assert_eq!(SubjectScheme::new(pattern).pattern(), pattern);
}

#[test]
fn takes_the_value_of_a_placeholder_out_of_a_subject() {
    let scheme = workshops();

    assert_eq!(
        scheme.value("/tenant/acme/workshop/go-cqrs", "tenant"),
        Some("acme")
    );
    assert_eq!(
        scheme.value("/tenant/acme/workshop/go-cqrs", "workshop"),
        Some("go-cqrs")
    );
}

#[test]
fn reports_none_for_a_subject_that_does_not_follow_the_pattern_as_match_subject_does() {
    let scheme = workshops();

    for subject in [
        "tenant/acme/workshop/go-cqrs",
        "/tenant/acme/workshop",
        "/client/acme/workshop/go-cqrs",
        "/tenant/acme/workshop/go.cqrs",
    ] {
        assert!(scheme.match_subject(subject).is_none(), "{subject:?}");
        assert_eq!(scheme.value(subject, "tenant"), None, "{subject:?}");
    }
}

#[test]
fn panics_for_a_placeholder_the_pattern_does_not_have() {
    let scheme = workshops();
    let message = r#"architecturekit: pattern "/tenant/{tenant}/workshop/{workshop}" has no placeholder "tennant""#;

    assert_eq!(
        panic_message(|| {
            let _ = scheme.value("/tenant/acme/workshop/go-cqrs", "tennant");
        })
        .as_deref(),
        Some(message),
    );

    // The mistake is in the code, so it is found whatever the subject is.
    assert_eq!(
        panic_message(|| {
            let _ = scheme.value("/client/acme", "tennant");
        })
        .as_deref(),
        Some(message),
    );
}

#[test]
fn panics_for_a_literal_segment_or_a_placeholder_in_braces_neither_of_which_is_the_name_of_one() {
    let books = SubjectScheme::new("/books/{book}");

    for name in ["books", "{book}", ""] {
        assert_eq!(
            panic_message(|| {
                let _ = books.value("/books/42", name);
            }),
            Some(format!(
                r#"architecturekit: pattern "/books/{{book}}" has no placeholder {name:?}"#
            )),
        );
    }
}