Skip to content

Commit

Permalink
add: parsing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
apskhem committed Mar 22, 2023
1 parent 7ff3222 commit 5a595b4
Show file tree
Hide file tree
Showing 23 changed files with 6,427 additions and 41 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/target
/src/main.rs

makefile
log.txt
makefile
67 changes: 39 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0"
repository = "https://github.com/apskhem/dbml-rs"
readme = "README.md"
authors = ["Apisit Ritreungroj <[email protected]>"]
keywords = ["compiler", "dbml"]
keywords = ["parser", "parse", "dbml"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
6 changes: 3 additions & 3 deletions tests/dbml/array_type.in.dbml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Table sal_emp {
name text
pay_by_quarter int[] [not null]
schedule text[][] [null]
pay_by_quarter "int[]" [not null]
schedule "text[][]" [null]
}

Table tictactoe {
squares integer[3][3]
squares "integer[3][3]"
}
2 changes: 1 addition & 1 deletion tests/dbml/default_tables.in.dbml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Table orders {
id int [pk, default: 123]
user_id int [not null, unique]
status varchar [default: "Completed"]
status varchar [default: 'Completed']
created_at varchar [default: `now()`]
}

Expand Down
10 changes: 5 additions & 5 deletions tests/dbml/enum_tables.in.dbml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
Enum job_status {
created [note: "Job created and pending"]
running [note: "Waiting for warehouse to process"]
created [note: 'Job created and pending']
running [note: 'Waiting for warehouse to process']
done
failed
"wait for validation" [note: "Enum label that has white spaces"]
"wait for validation" [note: 'Enum label that has white spaces']
}

Table jobs {
id integer [pk]
status job_status [note: "This is a column note"]
status job_status [note: 'This is a column note']
}

Enum "order status" {
created [note: "Order created"]
created [note: 'Order created']
pending
processing
completed
Expand Down
51 changes: 51 additions & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::{path::{Path, PathBuf}, fs, io::Result};

const DBML_DIR: &str = "tests/dbml";
const OUT_DIR: &str = "tests/out";

fn read_dbml_dir<P: AsRef<Path>>(dir_path: P) -> Result<Vec<PathBuf>> {
let mut out = vec![];
let entries = fs::read_dir(dir_path)?;

for entry in entries {
let file_path = entry?.path();

if file_path.is_file() {
out.push(file_path);
}
}

Ok(out)
}

fn create_out_dir() -> Result<()> {
if !fs::metadata(OUT_DIR).is_ok() {
fs::create_dir(OUT_DIR)?;
}

Ok(())
}

#[test]
fn parse_dbml() -> Result<()> {
create_out_dir()?;

let testing_dbml_files = read_dbml_dir(DBML_DIR)?;

for path in testing_dbml_files {
let content = fs::read_to_string(&path)?;
let parsed = dbml_rs::parser::parse(&content)
.unwrap_or_else(|err| panic!("{:?} {}", path, err));

let out_content = format!("{:#?}", parsed);

let mut out_file_path = path.clone();
out_file_path.set_extension("ron");
let out_file_name = out_file_path.file_name().unwrap().to_str().unwrap();
let out_file_path = format!("{}/{}", OUT_DIR, out_file_name);

fs::write(out_file_path, out_content)?;
}

Ok(())
}
Loading

0 comments on commit 5a595b4

Please sign in to comment.