Skip to content

Commit 09d1e31

Browse files
committed
Added test cases for not null and unique
1 parent 265db55 commit 09d1e31

File tree

7 files changed

+109
-19
lines changed

7 files changed

+109
-19
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ stack run -- test.sql -o erd.svg
7474
- [ ] Add Additional Examples
7575
- [ ] Add more parsing functions
7676
- [ ] Add more documentation
77+
- [x] Adding test cases
78+
- [ ] Adding support for interval data type
79+
- [x] GENERATED constraint
80+
- [ ] Parse 2D array
7781

7882
See the [open issues](https://github.com/tusharad/sql2er/issues) for a full list of proposed features (and known issues).
7983

@@ -93,7 +97,7 @@ See the [open issues](https://github.com/tusharad/sql2er/issues) for a full list
9397
- tablespace
9498
- not valid
9599
- validate
96-
- begin/commit
100+
- interval data type
97101

98102
<!-- ACKNOWLEDGMENTS -->
99103
## Acknowledgments
@@ -117,4 +121,4 @@ See the [open issues](https://github.com/tusharad/sql2er/issues) for a full list
117121
[linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=for-the-badge&logo=linkedin&colorB=555
118122
[linkedin-url]: https://linkedin.com/in/tushar-adhatrao
119123
[Haskell]: https://img.shields.io/badge/Haskell-5e5086?style=for-the-badge&logo=haskell&logoColor=white
120-
[Haskell-url]: https://www.haskell.org/
124+
[Haskell-url]: https://www.haskell.org/

example/test.sql

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,22 @@ CREATE TABLE query_categories (
7676
updated_at TIMESTAMP DEFAULT NOW()
7777
);
7878

79+
CREATE TABLE films (
80+
code char(5) CONSTRAINT firstkey PRIMARY KEY,
81+
title varchar(40) NOT NULL,
82+
did integer NOT NULL,
83+
date_prod date,
84+
kind varchar(10)
85+
);
86+
7987
CREATE TABLE faqs (
80-
faq_id SERIAL PRIMARY KEY,
88+
faq_id SERIAL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
8189
faq TEXT NOT NULL,
8290
-- freq INTEGER NOT NULL DEFAULT 0 CONSTRAINT chk_positive CHECK (freq >= 0),
8391
coverage_percentage Integer,
8492
coverage_description text,
8593
created_at timestamp DEFAULT now(),
86-
updated_at timestamp DEFAULT now()
94+
updated_at timestamp DEFAULT now() GENERATED BY DEFAULT AS IDENTITY
8795
);
8896

8997
CREATE TABLE sop_gap_coverage (

src/Sql2er/Common/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ data SqlType
3030
| PGboolean
3131
| PGbox
3232
| PGbytea
33-
| PGchar
33+
| PGchar (Maybe Int)
3434
| PGvarchar (Maybe Int)
3535
| PGdate
3636
| PGinteger

src/Sql2er/Parser/Common.hs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ parseReferenceForCol = do
119119
*> takeWhile1P Nothing (`notElem` (" \t\n;,)" :: String))
120120
return (ReferencesColumn refTable refCol)
121121

122+
ignoreConstraints :: Parser ()
123+
ignoreConstraints = do
124+
_ <-
125+
lexeme (string "generated")
126+
*> lexeme (takeWhile1P Nothing (`notElem` (",);" :: String)))
127+
return ()
128+
122129
parseSqlType :: Parser SqlType
123130
parseSqlType =
124131
choice $
@@ -134,9 +141,13 @@ parseSqlType =
134141
, PGbytea
135142
<$ string "bytea"
136143
, PGchar
137-
<$ string "char"
144+
<$> ( string "char"
145+
*> optional (space *> lexeme (char '(') *> decimal <* char ')')
146+
)
138147
, PGvarchar
139-
<$> (string "varchar" *> optional (space *> lexeme (char '(') *> decimal <* char ')'))
148+
<$> ( string "varchar"
149+
*> optional (space *> lexeme (char '(') *> decimal <* char ')')
150+
)
140151
, PGdate
141152
<$ string "date"
142153
, PGinteger

src/Sql2er/Parser/CreateTable.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ parseColumn = do
2929
, parseReferenceForCol
3030
, parseNullForCol
3131
]))
32+
void $ optional ignoreConstraints
3233
return
3334
Column
3435
{ columnName = cName

test/ExamplesForTests.hs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,54 @@ simpleTable1String =
1313
\did integer NOT NULL,\
1414
\date_prod date,\
1515
\kind varchar(10),\
16-
\len interval hour to minute\
1716
\);"
1817

18+
uniqueConstraint :: Table
19+
uniqueConstraint =
20+
Table
21+
{ tableName = "x"
22+
, columns =
23+
[ Column
24+
{ columnName = "y"
25+
, columnType = PGvarchar (Just 3)
26+
, cConstraints = [Unique]
27+
}
28+
, Column
29+
{ columnName = "z"
30+
, columnType = PGinteger
31+
, cConstraints = []
32+
}
33+
]
34+
, tableConstraints = [UniqueConstraint ["z"]]
35+
}
36+
37+
columnNullNotNull :: Table
38+
columnNullNotNull =
39+
Table
40+
{ tableName = "x"
41+
, columns =
42+
[ Column
43+
{ columnName = "y"
44+
, columnType = PGvarchar (Just 3)
45+
, cConstraints = [NotNull]
46+
}
47+
, Column
48+
{ columnName = "z"
49+
, columnType = PGinteger
50+
, cConstraints = [Null]
51+
}
52+
]
53+
, tableConstraints = []
54+
}
55+
1956
constraintNamePK :: Table
2057
constraintNamePK =
2158
Table
2259
{ tableName = "x"
2360
, columns =
2461
[ Column
2562
{ columnName = "y"
26-
, columnType = PGinteger
63+
, columnType = PGvarchar (Just 3)
2764
, cConstraints = [PrimaryKey]
2865
}
2966
, Column

test/Spec.hs

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,25 @@ import Data.Text qualified as T
66
import Data.Text.IO qualified as T
77
import ExamplesForTests
88
import ExamplesForTests qualified as Ex
9+
import Sql2er.Common.Types
910
import Sql2er.Common.Utils (begin, commit, rollback)
1011
import Sql2er.Parser
1112
import Sql2er.Parser.CreateTable (parseCreateTable)
1213
import Test.Tasty
1314
import Test.Tasty.HUnit
1415
import Text.Megaparsec
1516

17+
testParse :: Text -> Parser Table -> Table -> String -> Assertion
18+
testParse inputText parsingFunc expectedOp failureMsg = do
19+
let eRes =
20+
runParser
21+
parsingFunc
22+
""
23+
inputText
24+
case eRes of
25+
Left _ -> assertFailure failureMsg
26+
Right r -> expectedOp @=? r
27+
1628
testBeginCommit :: TestTree
1729
testBeginCommit =
1830
testGroup
@@ -47,19 +59,36 @@ testColumnConstraint :: TestTree
4759
testColumnConstraint =
4860
testGroup
4961
"column constraints"
50-
[ testCase "column with constraint name" $ do
51-
let eRes =
52-
runParser
53-
parseCreateTable
54-
""
62+
[ testCase "column with constraint name" $
63+
testParse
64+
( T.toLower
65+
"create or replace \
66+
\table x (y varchar(3) CONSTRAINT \
67+
\cName primary key, z int)")
68+
parseCreateTable
69+
Ex.constraintNamePK
70+
"parsing failed for column constraint"
71+
, testCase "column with Null and Not Null constraints" $
72+
testParse
5573
( T.toLower
5674
"create or replace \
57-
\table x (y int CONSTRAINT \
58-
\cName primary key, z int)"
75+
\table x (y varchar(3) Not NULL\
76+
\, z int NULL)"
5977
)
60-
case eRes of
61-
Left _ -> assertFailure "parsing failed for column constraint"
62-
Right r -> Ex.constraintNamePK @=? r
78+
parseCreateTable
79+
Ex.columnNullNotNull
80+
"parsing failed for column NULL, not NULL"
81+
, testCase "column with unique constraint" $
82+
testParse
83+
( T.toLower
84+
"create or replace \
85+
\table x (y varchar(3) unique\
86+
\, z int\
87+
\, constraint zUnique unique (z))"
88+
)
89+
parseCreateTable
90+
Ex.uniqueConstraint
91+
"parsing failed for unique column constraint"
6392
]
6493

6594
createStatementTests :: TestTree

0 commit comments

Comments
 (0)