Skip to content

Commit

Permalink
test: validator test
Browse files Browse the repository at this point in the history
  • Loading branch information
apskhem committed Mar 14, 2024
1 parent bfbd1c1 commit 792167f
Show file tree
Hide file tree
Showing 25 changed files with 333 additions and 13 deletions.
11 changes: 11 additions & 0 deletions tests/dbml/validator/alias_of_duplicated_names.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Table Users as U1 {

}

Table Users as U2 {

}

Table Products as U1 {

}
7 changes: 7 additions & 0 deletions tests/dbml/validator/column_caller_type.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Table User {
id int
secret nvarbinary(MAX)
name varchar("MAX")
code varbinary('MAX')
ssid int(10)
}
18 changes: 18 additions & 0 deletions tests/dbml/validator/complex_indexes.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Table bookings {
id integer
country varchar
booking_date date
created_at timestamp

indexes {
(id, country) [pk] // composite primary key
created_at [name: 'created_at_index', note: 'Date']
booking_date
(country, booking_date) [unique]
booking_date [type: hash]
(`id*2`)
(`id*3`,`getdate()`)
(`id*3`,id)
}
}
// End of program
13 changes: 13 additions & 0 deletions tests/dbml/validator/complex_names.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Project {
Table v1.Users {
id integer
status v2.Status [default: v2.Status.new]
}

Enum v2.Status {
new
churn
active
tenant
}
}
15 changes: 15 additions & 0 deletions tests/dbml/validator/duplicate_alias_name.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Table A as A {
id int [primary key]
}

Table "B" as B {
id int [primary key]
}

Table C as "C" {
id int [primary key]
}

Table "D" as "D" {
id int [primary key]
}
5 changes: 5 additions & 0 deletions tests/dbml/validator/duplicate_columns.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Table Users {
id integer
id integer
id integer
}
5 changes: 5 additions & 0 deletions tests/dbml/validator/duplicate_enum_field.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enum status {
churn
churn
churn
}
23 changes: 23 additions & 0 deletions tests/dbml/validator/duplicate_names.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Table Users {
id integer
}

Table Users {
name char(255)
}

TableGroup Users {

}

TableGroup Users {

}

Enum Users {

}

Enum Users {

}
11 changes: 11 additions & 0 deletions tests/dbml/validator/enum.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
enum v."A" {

}

enum {
"1"
"1"
1
a
"ab"
}
25 changes: 25 additions & 0 deletions tests/dbml/validator/erroneous.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Table users as U {
id int [pk, increment]
}

Table user_role_in_diagram {
user_id int
diagram_id int
role int [note: 'Role = sum(user's available permissions bit value)']
Indexes {
(user_id, diagram_id) [pk]
}
}

Table permissions {
bit int [pk]
name varchar
}

Table diagrams {
id int [pk, increment] // auto-increment
}

Ref: "users"."id" - "user_role_in_diagram"."user_id"

Ref: "diagrams"."id" - "user_role_in_diagram"."diagram_id"
32 changes: 32 additions & 0 deletions tests/dbml/validator/invalid_args.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Table A {
id int
ssid
}

Enum E {
a1 e [note: 'abc']
a2 [note: 'abc'] e
a3 e f
a4 e
}

TableGroup G {
A a
A a a
}

Ref {
A.id > A.id [delete: cascade] a
}

Ref {
A.id > A.id a [delete: cascade]
}

Ref {
A.id > A.id a
}

Ref {
A.id > A.id a a
}
13 changes: 13 additions & 0 deletions tests/dbml/validator/nested_duplicate_names.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Project B {
Table A {

}

Table A {

}

Table B {

}
}
31 changes: 31 additions & 0 deletions tests/dbml/validator/old_undocumented_syntax.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Table "customer" {
"customer_id" SMALLINT [pk, not null, increment]
"store_id" TINYINT [not null]
"first_name" VARCHAR(45) [not null]
"last_name" VARCHAR(45) [not null, default: faLse]
"email" VARCHAR(50) [default: NULL]
"address_id" SMALLINT [not NULL]
"active" BOOLEAN [not null, default: TRUE]
"create_date" DATETIME [not null]
"last_update" TIMESTAMP [default: `CURRENT_TIMESTAMP`]
}

Table cities {
id integer [primary key]
name e [default: "hello"]
country_id integer
note: "sasasa"
}

Table country {
id integer
cities string[]
}

Table citites {
id integer
name string
indexes {
id name
}
}
7 changes: 7 additions & 0 deletions tests/dbml/validator/public_schema.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Enum status {

}

Enum public.status {

}
14 changes: 14 additions & 0 deletions tests/dbml/validator/redefined_note.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Table A {
Note: ''' This is a local note '''
Note {
'This is another local note'
}
}

Note {
'This is a global note'
}

Note: '''
This is another global note
'''
1 change: 1 addition & 0 deletions tests/dbml/validator/ref.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ref a: Users.id < Products.uid [update: no action]
16 changes: 16 additions & 0 deletions tests/dbml/validator/ref_in_table.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Table A {
id integer
code number
}

Table B {
id integer
code number
}

Table C {
id integer
code number
Ref: id > A.id
Ref: code > B.code
}
40 changes: 40 additions & 0 deletions tests/dbml/validator/sticky_notes.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Table users [headercolor: #3498DB] {
id integer [primary key]
username varchar(255) [not null, unique]
}

Note note2 {
'One line note'
}

Note note2 {
'One line note'
}

Note note3 {
'One line note'
}

Note "note3" {
'One line note'
}

Note schema.note3[] {
'''
# Title
body
'''
}
Note "schema.note4" {
'''
# Title
body
'''
}

Note "schema"."note5" [headercolor: #3457DB] {
'''
# Title
body
'''
}
20 changes: 20 additions & 0 deletions tests/dbml/validator/table_settings.in.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Table Users [
headercolor: #123,
note: 'This is a note',
note: 'This is a duplicate note'
] {
id integer [primary key] // pk
name string [not null] /* not null */
age integer [default: 18, note: 'This is a note', note: 'This is another note']
/* -----
----- */
status v2.status [default: v2.status.new]
dob char(255) [null]
}

Enum v2.status {
churn
new
active
tenant
}
21 changes: 17 additions & 4 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use std::path::{
PathBuf,
};

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

fn read_dbml_dir<P: AsRef<Path>>(dir_path: P) -> Result<Vec<PathBuf>> {
let mut out = vec![];
Expand Down Expand Up @@ -36,9 +34,9 @@ fn create_out_dir() -> Result<()> {
fn parse_dbml_unchecked() -> Result<()> {
create_out_dir()?;

let testing_dbml_files = read_dbml_dir(DBML_DIR)?;
let testing_dbml_paths = read_dbml_dir("tests/dbml")?;

for path in testing_dbml_files {
for path in testing_dbml_paths {
let content = fs::read_to_string(&path)?;
let parsed =
dbml_rs::parse_dbml_unchecked(&content).unwrap_or_else(|err| panic!("{}", err.with_path(path.to_str().unwrap())));
Expand All @@ -55,6 +53,21 @@ fn parse_dbml_unchecked() -> Result<()> {

Ok(())
}

#[test]
fn parse_dbml_validator() -> Result<()> {
let testing_dbml_paths = read_dbml_dir("tests/dbml/validator")?;

for path in testing_dbml_paths {
let content = fs::read_to_string(&path)?;

if let Ok(_) = dbml_rs::parse_dbml(&content) {
panic!("{:?}: validation unexpected", path)
}
}

Ok(())
}

#[test]
fn parse_dbml_checked_tmp() -> Result<()> {
Expand Down
Loading

0 comments on commit 792167f

Please sign in to comment.