11{-# LANGUAGE OverloadedStrings #-}
2- {-# LANGUAGE NamedFieldPuns #-}
32
43import Test.Hspec
54import Database.PostgreSQL.Simple.Bind.Representation
@@ -10,124 +9,203 @@ main = hspec spec
109
1110spec :: Spec
1211spec = do
12+ let test = \ declaration result -> parsePGFunction declaration >>= shouldBe result
13+
1314 describe " parsePGFunction" $ do
1415 it " works with simple signatures" $ do
15- (parsePGFunction " function f(x bigint) returns bigint" ) >>= shouldBe
16- (PGFunction " " " f" [PGArgument " x" " bigint" False ] (PGSingle " bigint" ))
17-
18- (parsePGFunction " function g(x bigint, y varchar) returns void" ) >>= shouldBe
19- (PGFunction " " " g" [PGArgument " x" " bigint" False , PGArgument " y" " varchar" False ]
20- (PGSingle " void" ))
21-
22- (parsePGFunction " function h() returns varchar" ) >>= shouldBe
23- (PGFunction " " " h" [] (PGSingle " varchar" ))
24-
25- (parsePGFunction " FUNCTION H(Z VARCHAR) RETURNS BIGINT" ) >>= shouldBe
26- (PGFunction " " " h" [PGArgument " z" " varchar" False ] (PGSingle " bigint" ))
16+ let r = PGFunction {
17+ pgfSchema = " "
18+ , pgfName = " "
19+ , pgfArguments = []
20+ , pgfResult = PGSingle " "
21+ }
22+
23+ test " function f(x bigint) returns bigint" r {
24+ pgfName = " f"
25+ , pgfResult = PGSingle " bigint"
26+ , pgfArguments = [
27+ PGArgument { pgaName = " x" , pgaType = " bigint" , pgaOptional = False }
28+ ]
29+ }
30+
31+ test " function g(x bigint, y varchar) returns void" r {
32+ pgfName = " g"
33+ , pgfArguments = [
34+ PGArgument { pgaName = " x" , pgaType = " bigint" , pgaOptional = False }
35+ , PGArgument { pgaName = " y" , pgaType = " varchar" , pgaOptional = False }
36+ ]
37+ , pgfResult = PGSingle " void"
38+ }
39+
40+ test " function h() returns varchar" r {
41+ pgfName = " h"
42+ , pgfArguments = []
43+ , pgfResult = PGSingle " varchar"
44+ }
45+
46+ test " FUNCTION H(Z VARCHAR) RETURNS BIGINT" r {
47+ pgfName = " h"
48+ , pgfArguments = [
49+ PGArgument { pgaName = " z" , pgaType = " varchar" , pgaOptional = False }
50+ ]
51+ , pgfResult = PGSingle " bigint"
52+ }
2753
2854
2955 it " works with default values" $ do
30- (parsePGFunction " function f(x bigint default 0::bigint) returns void" ) >>= shouldBe
31- (PGFunction " " " f" [PGArgument " x" " bigint" True ] (PGSingle " void" ))
32-
33- (parsePGFunction " function f(x bigint, y bigint default 0::bigint) returns void" ) >>= shouldBe
34- (PGFunction " " " f"
35- [PGArgument " x" " bigint" False , PGArgument " y" " bigint" True ] (PGSingle " void" ))
36-
37- (parsePGFunction
38- " function f(x bigint, y bigint = 0::bigint, z bigint = 0::bigint) returns void" ) >>= shouldBe
39- (PGFunction " " " f" [
40- PGArgument " x" " bigint" False
41- , PGArgument " y" " bigint" True
42- , PGArgument " z" " bigint" True
43- ] (PGSingle " void" ))
44-
56+ let r = PGFunction {
57+ pgfSchema = " "
58+ , pgfName = " f"
59+ , pgfArguments = []
60+ , pgfResult = PGSingle " void"
61+ }
62+
63+ test " function f(x bigint default 0::bigint) returns void" r {
64+ pgfArguments = [
65+ PGArgument { pgaName = " x" , pgaType = " bigint" , pgaOptional = True }
66+ ]
67+ }
68+
69+ test " function f(x bigint, y bigint default 0::bigint) returns void" r {
70+ pgfArguments = [
71+ PGArgument { pgaName = " x" , pgaType = " bigint" , pgaOptional = False }
72+ , PGArgument { pgaName = " y" , pgaType = " bigint" , pgaOptional = True }
73+ ]
74+ }
75+
76+ test " function f(x bigint, y bigint = 0::bigint, z bigint = 0::bigint) returns void" r {
77+ pgfArguments = [
78+ PGArgument { pgaName = " x" , pgaType = " bigint" , pgaOptional = False }
79+ , PGArgument { pgaName = " y" , pgaType = " bigint" , pgaOptional = True }
80+ , PGArgument { pgaName = " z" , pgaType = " bigint" , pgaOptional = True }
81+ ]
82+ }
4583
4684 it " works with return value types" $ do
47- (parsePGFunction " function f() returns setof bigint" ) >>= shouldBe
48- (PGFunction " " " f" [] (PGSetOf " bigint" ))
85+ let r = PGFunction {
86+ pgfSchema = " "
87+ , pgfName = " f"
88+ , pgfArguments = []
89+ , pgfResult = PGSetOf " "
90+ }
4991
50- (parsePGFunction " function f() returns SETOF bigint" ) >>= shouldBe
51- (PGFunction " " " f" [] (PGSetOf " bigint" ))
92+ test " function f() returns setof bigint" r { pgfResult = PGSetOf " bigint" }
5293
53- (parsePGFunction " function f() returns table (x bigint, y varchar)" ) >>= shouldBe
54- (PGFunction " " " f" [] (PGTable [PGColumn " x" " bigint" , PGColumn " y" " varchar" ]))
94+ test " function f() returns SETOF bigint" r { pgfResult = PGSetOf " bigint" }
5595
96+ test " function f() returns table (x bigint, y varchar)" r {
97+ pgfResult = PGTable [
98+ PGColumn { pgcName = " x" , pgcType = " bigint" }
99+ , PGColumn { pgcName = " y" , pgcType = " varchar" }
100+ ]
101+ }
56102
57103 it " works with schema names" $ do
58- (parsePGFunction " function public.f() returns bigint" ) >>= shouldBe
59- (PGFunction " public" " f" [] (PGSingle " bigint" ))
104+ let r = PGFunction {
105+ pgfSchema = " "
106+ , pgfName = " f"
107+ , pgfArguments = []
108+ , pgfResult = PGSingle " bigint"
109+ }
60110
61- (parsePGFunction " function Test.f() returns bigint" ) >>= shouldBe
62- (PGFunction " test" " f" [] (PGSingle " bigint" ))
111+ test " function public.f() returns bigint" r { pgfSchema = " public" }
63112
113+ test " function Test.f() returns bigint" r { pgfSchema = " test" }
64114
65115
66116 describe " parsing a function with specific type" $ do
67- it " works for multiple word types" $ do
68- (parsePGFunction " function f(x double precision) returns void" ) >>= shouldBe
69- (PGFunction " " " f" [PGArgument " x" " double precision" False ] (PGSingle " void" ))
117+ let r = PGFunction {
118+ pgfSchema = " "
119+ , pgfName = " f"
120+ , pgfArguments = []
121+ , pgfResult = PGSingle " void"
122+ }
70123
124+ let x = PGArgument { pgaName = " x" , pgaType = " " , pgaOptional = False }
125+
126+ it " works for multiple word types" $ do
127+ test " function f(x double precision) returns void" r {
128+ pgfArguments = [ x { pgaType = " double precision" } ]
129+ }
71130
72131 it " works for types with precisions" $ do
73- (parsePGFunction " function f(x bit) returns void" ) >>= shouldBe
74- (PGFunction " " " f" [PGArgument " x" " bit" False ] (PGSingle " void" ))
132+ test " function f(x bit) returns void" r {
133+ pgfArguments = [ x { pgaType = " bit" } ]
134+ }
75135
76- (parsePGFunction " function f(x bit (32)) returns void" ) >>= shouldBe
77- (PGFunction " " " f" [PGArgument " x" " bit" False ] (PGSingle " void" ))
136+ test " function f(x bit (32)) returns void" r {
137+ pgfArguments = [ x { pgaType = " bit" } ]
138+ }
78139
79140
80141 it " works for types with precisions and multiple words" $ do
81- (parsePGFunction " function f(x character varying) returns void" ) >>= shouldBe
82- (PGFunction " " " f" [PGArgument " x" " character varying" False ] (PGSingle " void" ))
83- (parsePGFunction " function f(x character varying (256)) returns void" ) >>= shouldBe
84- (PGFunction " " " f" [PGArgument " x" " character varying" False ] (PGSingle " void" ))
142+ test " function f(x character varying) returns void" r {
143+ pgfArguments = [ x { pgaType = " character varying" } ]
144+ }
145+
146+ test " function f(x character varying (256)) returns void" r {
147+ pgfArguments = [ x { pgaType = " character varying" } ]
148+ }
85149
86150
87151 it " works for types with multiple precisions" $ do
88- (parsePGFunction " function f(x numeric) returns void" ) >>= shouldBe
89- (PGFunction " " " f" [PGArgument " x" " numeric" False ] (PGSingle " void" ))
152+ test " function f(x numeric) returns void" r {
153+ pgfArguments = [ x { pgaType = " numeric" } ]
154+ }
90155
91- (parsePGFunction " function f(x numeric(10)) returns void" ) >>= shouldBe
92- (PGFunction " " " f" [PGArgument " x" " numeric" False ] (PGSingle " void" ))
156+ test " function f(x numeric(10)) returns void" r {
157+ pgfArguments = [ x { pgaType = " numeric" } ]
158+ }
93159
94- (parsePGFunction " function f(x numeric(10,3)) returns void" ) >>= shouldBe
95- (PGFunction " " " f" [PGArgument " x" " numeric" False ] (PGSingle " void" ))
160+ test " function f(x numeric(10,3)) returns void" r {
161+ pgfArguments = [ x { pgaType = " numeric" } ]
162+ }
96163
97- (parsePGFunction " function f(x user_defined_type(1,2,3,4)) returns void" ) >>= shouldBe
98- (PGFunction " " " f" [PGArgument " x" " user_defined_type" False ] (PGSingle " void" ))
164+ test " function f(x user_defined_type(1,2,3,4)) returns void" r {
165+ pgfArguments = [ x { pgaType = " user_defined_type" } ]
166+ }
99167
100168
101169 it " works for time types" $ do
102- (parsePGFunction " function f(x time) returns void" ) >>= shouldBe
103- (PGFunction " " " f" [PGArgument " x" " time" False ] (PGSingle " void" ))
170+ test " function f(x time) returns void" r {
171+ pgfArguments = [ x { pgaType = " time" } ]
172+ }
104173
105- (parsePGFunction " function f(x time (6)) returns void" ) >>= shouldBe
106- (PGFunction " " " f" [PGArgument " x" " time" False ] (PGSingle " void" ))
174+ test " function f(x time (6)) returns void" r {
175+ pgfArguments = [ x { pgaType = " time" } ]
176+ }
107177
108- (parsePGFunction " function f(x time (6) with time zone) returns void" ) >>= shouldBe
109- (PGFunction " " " f" [PGArgument " x" " time with time zone" False ] (PGSingle " void" ))
178+ test " function f(x time (6) with time zone) returns void" r {
179+ pgfArguments = [ x { pgaType = " time with time zone" } ]
180+ }
110181
111- (parsePGFunction " function f(x time with time zone) returns void" ) >>= shouldBe
112- (PGFunction " " " f" [PGArgument " x" " time with time zone" False ] (PGSingle " void" ))
182+ test " function f(x time with time zone) returns void" r {
183+ pgfArguments = [ x { pgaType = " time with time zone" } ]
184+ }
113185
114- (parsePGFunction " function f(x time without time zone) returns void" ) >>= shouldBe
115- (PGFunction " " " f" [PGArgument " x" " time without time zone" False ] (PGSingle " void" ))
186+ test " function f(x time without time zone) returns void" r {
187+ pgfArguments = [ x { pgaType = " time without time zone" } ]
188+ }
116189
117- (parsePGFunction " function f(x timestamp with time zone) returns void" ) >>= shouldBe
118- (PGFunction " " " f" [PGArgument " x" " timestamp with time zone" False ] (PGSingle " void" ))
190+ test " function f(x timestamp with time zone) returns void" r {
191+ pgfArguments = [ x { pgaType = " timestamp with time zone" } ]
192+ }
119193
120- (parsePGFunction " function f(x timestamptz) returns void" ) >>= shouldBe
121- (PGFunction " " " f" [PGArgument " x" " timestamptz" False ] (PGSingle " void" ))
194+ test " function f(x timestamptz) returns void" r {
195+ pgfArguments = [ x { pgaType = " timestamptz" } ]
196+ }
122197
123198
124199 it " works for intervals" $ do
125- (parsePGFunction " function f(x interval) returns void" ) >>= shouldBe
126- (PGFunction " " " f" [PGArgument " x" " interval" False ] (PGSingle " void" ))
200+ test " function f(x interval) returns void" r {
201+ pgfArguments = [ x { pgaType = " interval" } ]
202+ }
127203
128- (parsePGFunction " function f(x interval month) returns void" ) >>= shouldBe
129- (PGFunction " " " f" [PGArgument " x" " interval month" False ] (PGSingle " void" ))
204+ test " function f(x interval month) returns void" r {
205+ pgfArguments = [ x { pgaType = " interval month" } ]
206+ }
130207
131- (parsePGFunction " function f(x interval minute to second (4)) returns void" ) >>= shouldBe
132- (PGFunction " " " f" [PGArgument " x" " interval minute to second" False ] (PGSingle " void" ))
208+ test " function f(x interval minute to second (4)) returns void" r {
209+ pgfArguments = [ x { pgaType = " interval minute to second" } ]
210+ }
133211
0 commit comments