Skip to content

Commit 22a6b1c

Browse files
committed
Move v1 corpus to files
1 parent 36bd351 commit 22a6b1c

File tree

9 files changed

+164
-116
lines changed

9 files changed

+164
-116
lines changed

src/valid/mod.rs

+43-43
Original file line numberDiff line numberDiff line change
@@ -124,46 +124,46 @@ fn is_license(v: &Value) -> Result<(), Box<dyn Error>> {
124124
Ok(())
125125
}
126126

127-
#[cfg(test)]
128-
mod tests {
129-
use super::*;
130-
use serde::{Deserialize, Serialize};
131-
use serde_json::Value;
132-
use std::error::Error;
133-
use std::io::{prelude::*, BufReader};
134-
use std::path::PathBuf;
135-
136-
#[derive(Deserialize, Serialize)]
137-
struct CorpusCase {
138-
test: String,
139-
error: Option<String>,
140-
meta: Value,
141-
}
142-
143-
#[test]
144-
fn test_validator() -> Result<(), Box<dyn Error>> {
145-
let schemas_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "schema"].iter().collect();
146-
147-
let mut validator = Validator::new(schemas_dir)?;
148-
let valid_file: PathBuf = [
149-
env!("CARGO_MANIFEST_DIR"),
150-
"tests",
151-
"corpus",
152-
"v1",
153-
"valid.txt",
154-
]
155-
.iter()
156-
.collect();
157-
let file = File::open(&valid_file)?;
158-
let reader = BufReader::new(file);
159-
for line in reader.lines() {
160-
let tc: CorpusCase = serde_json::from_str(&line?)?;
161-
if let Err(e) = validator.validate(&tc.meta) {
162-
panic!("{} failed: {e}", &tc.test);
163-
}
164-
println!("Example {} ok", &tc.test);
165-
}
166-
167-
Ok(())
168-
}
169-
}
127+
// #[cfg(test)]
128+
// mod tests {
129+
// use super::*;
130+
// use serde::{Deserialize, Serialize};
131+
// use serde_json::Value;
132+
// use std::error::Error;
133+
// use std::io::{prelude::*, BufReader};
134+
// use std::path::PathBuf;
135+
136+
// #[derive(Deserialize, Serialize)]
137+
// struct CorpusCase {
138+
// test: String,
139+
// error: Option<String>,
140+
// meta: Value,
141+
// }
142+
143+
// #[test]
144+
// fn test_validator() -> Result<(), Box<dyn Error>> {
145+
// let schemas_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "schema"].iter().collect();
146+
147+
// let mut validator = Validator::new(schemas_dir)?;
148+
// let valid_file: PathBuf = [
149+
// env!("CARGO_MANIFEST_DIR"),
150+
// "tests",
151+
// "corpus",
152+
// "v1",
153+
// "valid.txt",
154+
// ]
155+
// .iter()
156+
// .collect();
157+
// let file = File::open(&valid_file)?;
158+
// let reader = BufReader::new(file);
159+
// for line in reader.lines() {
160+
// let tc: CorpusCase = serde_json::from_str(&line?)?;
161+
// if let Err(e) = validator.validate(&tc.meta) {
162+
// panic!("{} failed: {e}", &tc.test);
163+
// }
164+
// println!("Example {} ok", &tc.test);
165+
// }
166+
167+
// Ok(())
168+
// }
169+
// }

tests/common/mod.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
use std::fs::{self, File};
2+
use std::path::PathBuf;
23
use std::{collections::HashMap, error::Error};
34

45
use boon::{Compiler, Schemas};
6+
use pgxn_meta::*;
57
use serde_json::{json, Value};
8+
use wax::Glob;
69

710
const SCHEMA_BASE: &str = "https://pgxn.org/meta/v";
811

@@ -237,3 +240,25 @@ pub fn test_schema_version(version: u8) -> Result<(), Box<dyn Error>> {
237240

238241
Ok(())
239242
}
243+
244+
pub fn test_corpus(version: u8) -> Result<(), Box<dyn Error>> {
245+
let schemas_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "schema"].iter().collect();
246+
let mut validator = Validator::new(schemas_dir)?;
247+
let v_dir = format!("v{version}");
248+
249+
let dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "tests", "corpus", &v_dir]
250+
.iter()
251+
.collect();
252+
let glob = Glob::new("*.json")?;
253+
254+
for path in glob.walk(dir) {
255+
let path = path?.into_path();
256+
let meta: Value = serde_json::from_reader(File::open(&path)?)?;
257+
if let Err(e) = validator.validate(&meta) {
258+
panic!("{v_dir}/{:?} failed: {e}", path.file_name().unwrap());
259+
}
260+
println!("Example {v_dir}/{:?} ok", path.file_name().unwrap());
261+
}
262+
263+
Ok(())
264+
}

tests/corpus/v1/howto.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "pair",
3+
"abstract": "A key/value pair data type",
4+
"version": "0.1.0",
5+
"maintainer": "David E. Wheeler <[email protected]>",
6+
"license": "postgresql",
7+
"provides": {
8+
"pair": {
9+
"abstract": "A key/value pair data type",
10+
"file": "sql/pair.sql",
11+
"docfile": "doc/pair.md",
12+
"version": "0.1.0"
13+
}
14+
},
15+
"meta-spec": {
16+
"version": "1.0.0",
17+
"url": "https://pgxn.org/meta/spec.txt"
18+
}
19+
}

tests/corpus/v1/howto2.json

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "pair",
3+
"abstract": "A key/value pair data type",
4+
"description": "This library contains a single PostgreSQL extension, a key/value pair data type called “pair”, along with a convenience function for constructing key/value pairs.",
5+
"version": "0.1.4",
6+
"maintainer": ["David E. Wheeler <[email protected]>"],
7+
"license": "postgresql",
8+
"provides": {
9+
"pair": {
10+
"abstract": "A key/value pair data type",
11+
"file": "sql/pair.sql",
12+
"docfile": "doc/pair.md",
13+
"version": "0.1.0"
14+
}
15+
},
16+
"resources": {
17+
"bugtracker": {
18+
"web": "https://github.com/theory/kv-pair/issues/"
19+
},
20+
"repository": {
21+
"url": "git://github.com/theory/kv-pair.git",
22+
"web": "https://github.com/theory/kv-pair/",
23+
"type": "git"
24+
}
25+
},
26+
"generated_by": "David E. Wheeler",
27+
"meta-spec": {
28+
"version": "1.0.0",
29+
"url": "https://pgxn.org/meta/spec.txt"
30+
},
31+
"tags": [
32+
"variadic function",
33+
"ordered pair",
34+
"pair",
35+
"key value",
36+
"key value pair",
37+
"data type"
38+
]
39+
}

tests/corpus/v1/invalid.txt

-1
This file was deleted.

tests/corpus/v1/valid.txt

-3
This file was deleted.

tests/corpus/v1/widget.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "widget",
3+
"abstract": "Widget for PostgreSQL",
4+
"description": "¿A widget is just thing thing, yoŭ know?",
5+
"version": "0.2.5",
6+
"maintainer": ["David E. Wheeler <[email protected]>"],
7+
"license": {
8+
"PostgreSQL": "https://www.postgresql.org/about/licence"
9+
},
10+
"prereqs": {
11+
"runtime": {
12+
"requires": {
13+
"plpgsql": 0,
14+
"PostgreSQL": "8.0.0"
15+
},
16+
"recommends": {
17+
"PostgreSQL": "8.4.0"
18+
}
19+
}
20+
},
21+
"provides": {
22+
"widget": {
23+
"file": "sql/widget.sql.in",
24+
"version": "0.2.5"
25+
}
26+
},
27+
"resources": {
28+
"homepage": "http://widget.example.org/"
29+
},
30+
"generated_by": "theory",
31+
"meta-spec": {
32+
"version": "1.0.0",
33+
"url": "https://pgxn.org/meta/spec.txt"
34+
},
35+
"tags": ["widget", "gadget", "full text search"]
36+
}

tests/v1_schema_test.rs

+1-47
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use std::error::Error;
2-
use std::fs::File;
3-
use std::io::{prelude::*, BufReader};
42

53
use boon::Schemas;
64
use serde::{Deserialize, Serialize};
@@ -28,51 +26,7 @@ struct CorpusCase {
2826

2927
#[test]
3028
fn test_corpus_v1_valid() -> Result<(), Box<dyn Error>> {
31-
// Load the schemas and compile the root schema.
32-
let mut compiler = new_compiler("schema/v1")?;
33-
let mut schemas = Schemas::new();
34-
let id = id_for(SCHEMA_VERSION, "distribution");
35-
let index = compiler.compile(&id, &mut schemas)?;
36-
37-
// Test each meta JSON in the corpus.
38-
let file = File::open("tests/corpus/v1/valid.txt")?;
39-
let reader = BufReader::new(file);
40-
for line in reader.lines() {
41-
let tc: CorpusCase = serde_json::from_str(&line?)?;
42-
43-
if let Err(e) = schemas.validate(&tc.meta, index) {
44-
panic!("{} failed: {e}", &tc.test);
45-
}
46-
println!("Example {} ok", &tc.test);
47-
}
48-
49-
Ok(())
50-
}
51-
52-
#[test]
53-
fn test_corpus_v1_invalid() -> Result<(), Box<dyn Error>> {
54-
// Load the schemas and compile the root schema.
55-
let mut compiler = new_compiler("schema/v1")?;
56-
let mut schemas = Schemas::new();
57-
let id = id_for(SCHEMA_VERSION, "distribution");
58-
let index = compiler.compile(&id, &mut schemas)?;
59-
60-
// Test each meta JSON in the corpus.
61-
let file = File::open("tests/corpus/v1/invalid.txt")?;
62-
let reader = BufReader::new(file);
63-
for line in reader.lines() {
64-
let tc: CorpusCase = serde_json::from_str(&line?)?;
65-
match schemas.validate(&tc.meta, index) {
66-
Ok(_) => panic!("{} unexpectedly passed!", &tc.test),
67-
Err(e) => assert!(
68-
e.to_string().contains(&tc.error.unwrap()),
69-
"{} error: {e}",
70-
&tc.test,
71-
),
72-
}
73-
}
74-
75-
Ok(())
29+
test_corpus(1)
7630
}
7731

7832
#[test]

tests/v2_schema_test.rs

+1-22
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
use std::error::Error;
2-
use std::fs::File;
3-
use std::path::PathBuf;
42

53
use boon::Schemas;
64
use serde::{Deserialize, Serialize};
75
use serde_json::{json, Map, Value};
8-
use wax::Glob;
96

107
// importing common module.
118
mod common;
129
use common::*;
1310

1411
use pgxn_meta::valid::_test_support::*;
15-
use pgxn_meta::*;
1612

1713
const SCHEMA_VERSION: u8 = 2;
1814

@@ -3348,22 +3344,5 @@ struct CorpusCase {
33483344

33493345
#[test]
33503346
fn test_corpus_v2_valid() -> Result<(), Box<dyn Error>> {
3351-
let schemas_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "schema"].iter().collect();
3352-
let mut validator = Validator::new(schemas_dir)?;
3353-
3354-
let dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "tests", "corpus", "v2"]
3355-
.iter()
3356-
.collect();
3357-
let glob = Glob::new("*.json")?;
3358-
3359-
for path in glob.walk(dir) {
3360-
let path = path?.into_path();
3361-
let meta: Value = serde_json::from_reader(File::open(&path)?)?;
3362-
if let Err(e) = validator.validate(&meta) {
3363-
panic!("{:?} failed: {e}", path.file_name().unwrap());
3364-
}
3365-
println!("Example {:?} ok", path.file_name().unwrap());
3366-
}
3367-
3368-
Ok(())
3347+
test_corpus(2)
33693348
}

0 commit comments

Comments
 (0)