Skip to content

Commit 79ec0c1

Browse files
committed
Add extension schema
1 parent a6bc3ad commit 79ec0c1

File tree

2 files changed

+182
-5
lines changed

2 files changed

+182
-5
lines changed

tests/v1_schema_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,14 @@ fn test_v1_extension() -> Result<(), Box<dyn Error>> {
361361
for valid_extension in [
362362
(
363363
"required fields",
364-
json!( {
364+
json!({
365365
"file": "widget.sql",
366366
"version": "0.26.0",
367367
}),
368368
),
369369
(
370370
"with abstract",
371-
json!( {
371+
json!({
372372
"file": "widget.sql",
373373
"version": "0.26.0",
374374
"abstract": "This and that",
@@ -420,7 +420,7 @@ fn test_v1_extension() -> Result<(), Box<dyn Error>> {
420420
),
421421
(
422422
"bare x_",
423-
json!( {
423+
json!({
424424
"file": "widget.sql",
425425
"version": "0.26.0",
426426
"x_": "hi",

tests/v2_schema_test.rs

Lines changed: 179 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,11 +505,11 @@ fn test_v2_maintainers() -> Result<(), Box<dyn Error>> {
505505
("object url", json!([{"name": "hi", "url": {}}])),
506506
// Custom
507507
(
508-
"custom X_",
508+
"bare X_",
509509
json!([{"name": "x", "email": "[email protected]", "X_": true}]),
510510
),
511511
(
512-
"custom x_",
512+
"bare x_",
513513
json!([{"name": "x", "email": "[email protected]", "x_": true}]),
514514
),
515515
] {
@@ -520,3 +520,180 @@ fn test_v2_maintainers() -> Result<(), Box<dyn Error>> {
520520

521521
Ok(())
522522
}
523+
524+
#[test]
525+
fn test_v2_extension() -> Result<(), Box<dyn Error>> {
526+
// Load the schemas and compile the extension schema.
527+
let mut compiler = new_compiler("schema/v2")?;
528+
let mut schemas = Schemas::new();
529+
let id = id_for(SCHEMA_VERSION, "extension");
530+
let idx = compiler.compile(&id, &mut schemas)?;
531+
532+
for valid_extension in [
533+
(
534+
"required fields",
535+
json!({
536+
"sql": "widget.sql",
537+
"control": "widget.control",
538+
}),
539+
),
540+
(
541+
"with abstract",
542+
json!({
543+
"sql": "widget.sql",
544+
"control": "widget.control",
545+
"abstract": "This and that",
546+
}),
547+
),
548+
(
549+
"all fields",
550+
json!({
551+
"sql": "widget.sql",
552+
"control": "widget.control",
553+
"doc": "foo/bar.txt",
554+
"abstract": "This and that",
555+
"tle": true,
556+
}),
557+
),
558+
(
559+
"x field",
560+
json!({
561+
"sql": "widget.sql",
562+
"control": "widget.control",
563+
"x_hi": true,
564+
}),
565+
),
566+
(
567+
"X field",
568+
json!({
569+
"sql": "widget.sql",
570+
"control": "widget.control",
571+
"X_bar": 42,
572+
}),
573+
),
574+
] {
575+
if let Err(e) = schemas.validate(&valid_extension.1, idx) {
576+
panic!("extension {} failed: {e}", valid_extension.0);
577+
}
578+
}
579+
580+
for invalid_extension in [
581+
// Basics
582+
("array", json!([])),
583+
("string", json!("crank")),
584+
("empty string", json!("")),
585+
("true", json!(true)),
586+
("false", json!(false)),
587+
("null", json!(null)),
588+
("empty object", json!({})),
589+
(
590+
"invalid field",
591+
json!({"sql": "widget.sql", "control": "x.control", "foo": "hi", }),
592+
),
593+
(
594+
"bare x_",
595+
json!({ "sql": "widget.sql", "control": "x.control", "x_": "hi" }),
596+
),
597+
(
598+
"bare X_",
599+
json!({ "sql": "widget.sql", "control": "x.control", "X_": "hi" }),
600+
),
601+
// SQL
602+
("no sql", json!({"control": "x.control"})),
603+
("null sql", json!({"sql": null, "control": "x.control"})),
604+
("empty sql", json!({"sql": "", "control": "x.control"})),
605+
("number sql", json!({"sql": 42, "control": "x.control"})),
606+
("bool sql", json!({"sql": true, "control": "x.control"})),
607+
("array sql", json!({"sql": [], "control": "x.control"})),
608+
("object sql", json!({"sql": {}, "control": "x.control"})),
609+
// Control
610+
("no control", json!({"sql": "x.sql"})),
611+
("null control", json!({"control": null, "sql": "x.sql"})),
612+
("empty control", json!({"control": "", "sql": "x.sql"})),
613+
("number control", json!({"control": 42, "sql": "x.sql"})),
614+
("bool control", json!({"control": true, "sql": "x.sql"})),
615+
("array control", json!({"control": [], "sql": "x.sql"})),
616+
("object control", json!({"control": {}, "sql": "x.sql"})),
617+
// Doc
618+
(
619+
"empty doc",
620+
json!({"sql": "widget.sql", "control": "widget.control", "doc": ""}),
621+
),
622+
(
623+
"null doc",
624+
json!({"sql": "widget.sql", "control": "widget.control", "doc": null}),
625+
),
626+
(
627+
"number doc",
628+
json!({"sql": "widget.sql", "control": "widget.control", "doc": 42}),
629+
),
630+
(
631+
"bool doc",
632+
json!({"sql": "widget.sql", "control": "widget.control", "doc": true}),
633+
),
634+
(
635+
"array doc",
636+
json!({"sql": "widget.sql", "control": "widget.control", "doc": ["hi"]}),
637+
),
638+
(
639+
"object doc",
640+
json!({"sql": "widget.sql", "control": "widget.control", "doc": {}}),
641+
),
642+
// Abstract
643+
(
644+
"empty abstract",
645+
json!({"sql": "widget.sql", "control": "widget.control", "abstract": ""}),
646+
),
647+
(
648+
"null abstract",
649+
json!({"sql": "widget.sql", "control": "widget.control", "abstract": null}),
650+
),
651+
(
652+
"number abstract",
653+
json!({"sql": "widget.sql", "control": "widget.control", "abstract": 42}),
654+
),
655+
(
656+
"bool abstract",
657+
json!({"sql": "widget.sql", "control": "widget.control", "abstract": true}),
658+
),
659+
(
660+
"array abstract",
661+
json!({"sql": "widget.sql", "control": "widget.control", "abstract": ["hi"]}),
662+
),
663+
(
664+
"object abstract",
665+
json!({"sql": "widget.sql", "control": "widget.control", "abstract": {}}),
666+
),
667+
// TLE
668+
(
669+
"empty tle",
670+
json!({"sql": "widget.sql", "control": "widget.control", "tle": ""}),
671+
),
672+
(
673+
"tle string",
674+
json!({"sql": "widget.sql", "control": "widget.control", "tle": "true"}),
675+
),
676+
(
677+
"null tle",
678+
json!({"sql": "widget.sql", "control": "widget.control", "tle": null}),
679+
),
680+
(
681+
"number tle",
682+
json!({"sql": "widget.sql", "control": "widget.control", "tle": 42}),
683+
),
684+
(
685+
"array tle",
686+
json!({"sql": "widget.sql", "control": "widget.control", "tle": ["hi"]}),
687+
),
688+
(
689+
"object tle",
690+
json!({"sql": "widget.sql", "control": "widget.control", "tle": {}}),
691+
),
692+
] {
693+
if schemas.validate(&invalid_extension.1, idx).is_ok() {
694+
panic!("{} unexpectedly passed!", invalid_extension.0)
695+
}
696+
}
697+
698+
Ok(())
699+
}

0 commit comments

Comments
 (0)