Skip to content

Commit

Permalink
Added 1 test case for mermaid
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharad committed Nov 14, 2024
1 parent 193866c commit 95dde44
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 37 deletions.
30 changes: 30 additions & 0 deletions test/ExamplesForTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,36 @@ module ExamplesForTests where
import Data.Text (Text)
import Sql2er.Common.Types

foreginKeyStatement :: Table
foreginKeyStatement =
Table
{ tableName = "summaries"
, columns =
[ Column
{ columnName = "summary_id"
, columnType = PGserial
, cConstraints = [PrimaryKey]
}
, Column
{ columnName = "thread_id"
, columnType = PGinteger
, cConstraints = [NotNull, ReferencesColumn "threads" (Just "thread_id")]
}
, Column {columnName = "summary_content", columnType = PGtext, cConstraints = [NotNull]}
, Column
{ columnName = "summary_created_at"
, columnType = PGtimestamp
, cConstraints = [Default "now()"]
}
, Column
{ columnName = "summary_modified_at"
, columnType = PGtimestamp
, cConstraints = [Default "now()"]
}
]
, tableConstraints = []
}

partitionConstraint :: Table
partitionConstraint =
Table
Expand Down
113 changes: 76 additions & 37 deletions test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ExamplesForTests
import ExamplesForTests qualified as Ex
import Sql2er.Common.Types
import Sql2er.Common.Utils (begin, commit, rollback)
import Sql2er.Mermaid (generateMermaidERD)
import Sql2er.Parser
import Sql2er.Parser.CreateTable (parseCreateTable)
import Test.Tasty
Expand Down Expand Up @@ -61,23 +62,24 @@ testColumnConstraint =
"column constraints"
[ testCase "column with constraint name" $
testParse
( T.toLower
"create \
\table if not exists x (y varchar(3) CONSTRAINT \
\cName primary key, z int)")
parseCreateTable
Ex.constraintNamePK
"parsing failed for column constraint"
( T.toLower
"create \
\table if not exists x (y varchar(3) CONSTRAINT \
\cName primary key, z int)"
)
parseCreateTable
Ex.constraintNamePK
"parsing failed for column constraint"
, testCase "column with Null and Not Null constraints" $
testParse
( T.toLower
"create \
\table if not exists x (y varchar(3) Not NULL\
\, z int NULL)"
)
parseCreateTable
Ex.columnNullNotNull
"parsing failed for column NULL, not NULL"
( T.toLower
"create \
\table if not exists x (y varchar(3) Not NULL\
\, z int NULL)"
)
parseCreateTable
Ex.columnNullNotNull
"parsing failed for column NULL, not NULL"
, testCase "column with unique constraint" $
testParse
( T.toLower
Expand All @@ -87,7 +89,7 @@ testColumnConstraint =
\, constraint zUnique unique (z))"
)
parseCreateTable
Ex.uniqueConstraint
Ex.uniqueConstraint
"parsing failed for unique column constraint"
]

Expand All @@ -102,12 +104,11 @@ testColumnConstraint =

testTableConstraint :: TestTree
testTableConstraint =
testGroup
"table constraints"
[
testCase "table constraints" $
testParse
( T.toLower
testGroup
"table constraints"
[ testCase "table constraints" $
testParse
( T.toLower
"create table if not exists x (y varchar(3) unique\
\, z int, constraint zunique unique (z),\
\constraint asd primary key (y), constraint fKey foreign key (z) references\
Expand All @@ -116,24 +117,44 @@ testTableConstraint =
)
parseCreateTable
Ex.tableConstraint
"Parsing failed for table constraints"
"Parsing failed for table constraints"
]

testPartitionConstraint :: TestTree
testPartitionConstraint =
testGroup "partition constraint" [
testCase "paition with multiple parenthesis" $
testParse
(T.toLower
"CREATE TABLE cities (\
\city_id int not null,\
\name text not null,\
\population bigint\
\) PARTITION BY LIST (left(lower(name), 1))"
)
parseCreateTable
Ex.partitionConstraint
"Parsing failed for table with partition"
testGroup
"partition constraint"
[ testCase "paition with multiple parenthesis" $
testParse
( T.toLower
"CREATE TABLE cities (\
\city_id int not null,\
\name text not null,\
\population bigint\
\) PARTITION BY LIST (left(lower(name), 1))"
)
parseCreateTable
Ex.partitionConstraint
"Parsing failed for table with partition"
]

foreignKeyStatements :: TestTree
foreignKeyStatements =
testGroup
"Foreign key statements"
[ testCase "paition with multiple parenthesis" $
testParse
( T.toLower
"CREATE TABLE summaries (\
\summary_id SERIAL PRIMARY KEY,\
\thread_id INTEGER NOT NULL REFERENCES threads (thread_id),\
\summary_content TEXT NOT NULL,\
\summary_created_at TIMESTAMP DEFAULT NOW(),\
\summary_modified_at TIMESTAMP DEFAULT NOW())"
)
parseCreateTable
Ex.foreginKeyStatement
"Parsing failed for table with partition"
]

createStatementTests :: TestTree
Expand All @@ -144,12 +165,29 @@ createStatementTests =
, testColumnConstraint
, testTableConstraint
, testPartitionConstraint
, foreignKeyStatements
]

mermaidColumnReferenceTable :: TestTree
mermaidColumnReferenceTable =
testGroup
"Column reference table"
[ testCase "column reference table 1" $ do
"erDiagram\nsummaries {\n serial summary_id\n integer thread_id\n text summary_content\n timestamp summary_created_at\n timestamp summary_modified_at\n}\nsummaries ||--o{ threads : thread_id\n\n"
@=? generateMermaidERD [foreginKeyStatement]
]

mermaidDiagramTest :: TestTree
mermaidDiagramTest =
testGroup
"Mermaid diagram tests"
[ mermaidColumnReferenceTable
]

testSqlScripts :: Text -> TestTree
testSqlScripts t = do
let eRes = runParser parseSqlScript "" t
testCase "parsing script" $ do
testCase "parsing script" $ do
assertBool "" (isRight eRes)

main :: IO ()
Expand All @@ -161,4 +199,5 @@ main = do
[ testSqlScripts testScriptContent
, ignoreStatementTests
, createStatementTests
, mermaidDiagramTest
]

0 comments on commit 95dde44

Please sign in to comment.