forked from yesodweb/persistent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniqueTest.hs
More file actions
76 lines (72 loc) · 2.52 KB
/
Copy pathUniqueTest.hs
File metadata and controls
76 lines (72 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
{-# LANGUAGE QuasiQuotes, TemplateHaskell, CPP, GADTs, TypeFamilies, OverloadedStrings, FlexibleContexts, EmptyDataDecls, FlexibleInstances, GeneralizedNewtypeDeriving, MultiParamTypeClasses #-}
module UniqueTest where
import Init
#ifndef WITH_NOSQL
import Control.Monad (void)
#endif
#ifdef WITH_NOSQL
mkPersist persistSettings [persistUpperCase|
#else
share [mkPersist sqlSettings, mkMigrate "uniqueMigrate"] [persistLowerCase|
#endif
TestNonNull
fieldA Int
UniqueTestNonNull fieldA
deriving Eq Show
TestNull
fieldA Int
fieldB Int Maybe
UniqueTestNull fieldA fieldB !force
deriving Eq Show
#ifndef WITH_NOSQL
TestCheckmark
name Text
value Text
active Checkmark nullable
UniqueTestCheckmark name active !force
deriving Eq Show
#endif
|]
#ifdef WITH_NOSQL
cleanDB :: (MonadIO m, PersistQuery backend, PersistEntityBackend TestNonNull ~ backend) => ReaderT backend m ()
cleanDB = do
deleteWhere ([] :: [Filter TestNonNull])
deleteWhere ([] :: [Filter TestNull])
db :: Action IO () -> Assertion
db = db' cleanDB
#endif
specs :: Spec
specs = describe "uniqueness constraints" $
#ifdef WITH_NOSQL
return ()
#else
do
it "are respected for non-nullable Ints" $ do
let ins = insert . TestNonNull
(db $ void $ ins 1 >> ins 2)
(db $ void $ ins 1 >> ins 2 >> ins 1) `shouldThrow` anyException
(db $ void $ ins 1 >>= \k -> ins 2 >> delete k >> ins 1)
it "are respected for nullable Ints" $ do
let ins a b = insert $ TestNull a b
ctx = ins 1 Nothing >> ins 1 Nothing >> ins 1 Nothing >>
ins 1 (Just 3) >> ins 1 (Just 4)
(db $ void ctx)
(db $ void $ ctx >> ins 1 (Just 3)) `shouldThrow` anyException
(db $ void $ ctx >> ins 1 (Just 4)) `shouldThrow` anyException
(db $ void $ ctx >>= \k -> delete k >> ins 1 (Just 4))
it "work for Checkmark" $ do
let ins k v a = insert $ TestCheckmark k v a
ctx = ins "name" "John" Inactive
>> ins "name" "Stewart" Inactive
>> ins "name" "Doroty" Active
>> ins "color" "blue" Inactive
(db $ void ctx)
(db $ void $ ctx >> ins "name" "Melissa" Active) `shouldThrow` anyException
(db $ void $ ctx >> ins "name" "Melissa" Inactive)
(db $ void $ ctx >>= flip update [TestCheckmarkActive =. Active])
(db $ void $ do
void ctx
updateWhere [TestCheckmarkName ==. "name"]
[TestCheckmarkActive =. Inactive]
ins "name" "Melissa" Active)
#endif