File: EmbedSpec.hs

package info (click to toggle)
haskell-persistent 2.17.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,196 kB
  • sloc: haskell: 14,076; makefile: 3
file content (169 lines) | stat: -rw-r--r-- 4,633 bytes parent folder | download | duplicates (3)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

module Database.Persist.TH.EmbedSpec where

import TemplateTestImports

import Data.Text (Text)
import qualified Data.Map as M
import qualified Data.Text as T

import Database.Persist.ImplicitIdDef
import Database.Persist.ImplicitIdDef.Internal (fieldTypeFromTypeable)
import Database.Persist.Types
import Database.Persist.Types
import Database.Persist.EntityDef
import Database.Persist.EntityDef.Internal (toEmbedEntityDef)

mkPersist sqlSettings [persistLowerCase|

Thing
    name String
    foo  String MigrationOnly

    deriving Eq Show

EmbedThing
    someThing Thing

    deriving Eq Show

SelfEmbed
    name Text
    self SelfEmbed Maybe
    deriving Eq Show

MutualEmbed
    thing MutualTarget

MutualTarget
    thing [MutualEmbed]

ModelWithList
    names [Text]

HasMap
    map (M.Map T.Text T.Text)
    deriving Show Eq Read Ord

MapIdValue
    map (M.Map T.Text (Key Thing))
    deriving Show Eq Read Ord

|]

pass :: IO ()
pass = pure ()

asIO :: IO a -> IO a
asIO = id

spec :: Spec
spec = describe "EmbedSpec" $ do
    describe "ModelWithList" $ do
        let
            edef =
                entityDef $ Proxy @ModelWithList
            [fieldDef] =
                getEntityFields edef
        it "has the right type" $ do
            fieldType fieldDef
                `shouldBe`
                    FTList (FTTypeCon Nothing "Text")
        it "has the right sqltype" $ do
            fieldSqlType fieldDef
                `shouldBe`
                    SqlString
    describe "MapIdValue" $ do
        let
            edef =
                entityDef $ Proxy @MapIdValue
            [fieldDef] =
                getEntityFields edef
        it "has the right type" $ do
            fieldType fieldDef
                `shouldBe`
                    ( FTTypeCon (Just "M") "Map"
                        `FTApp`
                        FTTypeCon (Just "T") "Text"
                        `FTApp`
                        (FTTypeCon Nothing "Key"
                            `FTApp`
                            FTTypeCon Nothing "Thing"
                        )
                    )
        it "has the right sqltype" $ do
            fieldSqlType fieldDef
                `shouldBe`
                    SqlString
    describe "HasMap" $ do
        let
            edef =
                entityDef $ Proxy @HasMap
            [fieldDef] =
                getEntityFields edef
        it "has the right type" $ do
            fieldType fieldDef
                `shouldBe`
                    ( FTTypeCon (Just "M") "Map"
                    `FTApp`
                    FTTypeCon (Just "T") "Text"
                    `FTApp`
                    FTTypeCon (Just "T") "Text"
                    )
        it "has the right sqltype" $ do
            fieldSqlType fieldDef
                `shouldBe`
                    SqlString

    describe "SomeThing" $ do
        let
            edef =
                entityDef $ Proxy @Thing
        describe "toEmbedEntityDef" $ do
            let
                embedDef =
                    toEmbedEntityDef edef
            it "should have the same field count as Haskell fields" $ do
                length (embeddedFields embedDef)
                    `shouldBe`
                        length (getEntityFields edef)

    describe "EmbedThing" $ do
        it "generates the right constructor" $ do
            let embedThing :: EmbedThing
                embedThing = EmbedThing (Thing "asdf")
            pass

    describe "SelfEmbed" $ do
        let
            edef =
                entityDef $ Proxy @SelfEmbed
        describe "fieldReference" $ do
            let
                [nameField, selfField] = getEntityFields edef
            it "has self reference" $ do
                fieldReference selfField
                    `shouldBe`
                        NoReference
        describe "toEmbedEntityDef" $ do
            let
                embedDef =
                    toEmbedEntityDef edef
            it "has the same field count as regular def" $ do
                length (getEntityFields edef)
                    `shouldBe`
                        length (embeddedFields embedDef)