on dev help sign in
foreign key test
commitf920afbaab1fa6bf8315af49ffff70b308e7b0a1 authorEitan Chatav <eitan.chatav@gmail.com> dateThu 10 Aug 2017, 13:46 (9 years ago) parent9f54306
1 file changed+36
squeel-postgresql/test/Squeel/PostgreSQL/StatementSpec.hs+36
squeel-postgresql/test/Squeel/PostgreSQL/StatementSpec.hs+36
@@ -8,6 +8,7 @@
88
99module Squeel.PostgreSQL.StatementSpec where
1010
11+import Control.Category ((>>>))
1112import Data.Function
1213import Generics.SOP hiding (from)
1314import Test.Hspec
@@ -243,6 +244,41 @@ spec = do
243244 \ col2 text,\
244245 \ col3 int8 NOT NULL DEFAULT 8,\
245246 \ CHECK (col3 > 0));"
247+ let
248+ statement :: Definition '[]
249+ '[ "users" :::
250+ '[ "id" ::: 'Optional ('NotNull 'PGInt4)
251+ , "username" ::: 'Required ('NotNull 'PGText)
252+ ]
253+ , "emails" :::
254+ '[ "id" ::: 'Optional ('NotNull 'PGInt4)
255+ , "userid" ::: 'Required ('NotNull 'PGInt4)
256+ , "email" ::: 'Required ('NotNull 'PGText)
257+ ]
258+ ]
259+ statement =
260+ createTable #users
261+ (serial `As` #id :* (text & notNull) `As` #username :* Nil)
262+ [primaryKey (Column #id :* Nil)]
263+ >>>
264+ createTable #emails
265+ ( serial `As` #id :*
266+ (integer & notNull) `As` #userid :*
267+ (text & notNull) `As` #email :* Nil )
268+ [ primaryKey (Column #id :* Nil)
269+ , foreignKey (Column #userid :* Nil) #users (Column #id :* Nil)
270+ ]
271+ statement `definitionRenders`
272+ "CREATE TABLE users\
273+ \ (id serial, username text NOT NULL,\
274+ \ PRIMARY KEY (id));\
275+ \ \
276+ \CREATE TABLE emails\
277+ \ (id serial,\
278+ \ userid integer NOT NULL,\
279+ \ email text NOT NULL,\
280+ \ PRIMARY KEY (id),\
281+ \ FOREIGN KEY (userid) REFERENCES users (id));"
246282 it "should render DROP TABLE statements" $ do
247283 let
248284 statement :: Definition Tables '[]