11{-# LANGUAGE OverloadedStrings #-}
22{-# LANGUAGE NamedFieldPuns #-}
33
4- import Control.Monad (unless )
5- -- import Data.WithLocation (WithLocation)
64import Test.Hspec
7- import Test.Hspec.Expectations (expectationFailure )
8- -- import Test.Hspec.Wai (with)
95import Database.PostgreSQL.Simple.Bind.Representation
106import Data.Text ()
117
@@ -14,7 +10,7 @@ main = hspec spec
1410
1511spec :: Spec
1612spec = do
17- describe " simple signatures " $ do
13+ describe " parsePGFunction " $ do
1814 it " works with simple signatures" $ do
1915 (parsePGFunction " function f(x bigint) returns bigint" ) `shouldBe`
2016 (PGFunction " " " f" [PGArgument " x" " bigint" False ] (PGSingle " bigint" ))
@@ -29,3 +25,103 @@ spec = do
2925 (parsePGFunction " FUNCTION H(Z VARCHAR) RETURNS BIGINT" ) `shouldBe`
3026 (PGFunction " " " h" [PGArgument " z" " varchar" False ] (PGSingle " bigint" ))
3127
28+
29+ 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+
45+
46+ it " works with return value types" $ do
47+ (parsePGFunction " function f() returns setof bigint" ) `shouldBe`
48+ (PGFunction " " " f" [] (PGSetOf " bigint" ))
49+
50+ (parsePGFunction " function f() returns SETOF bigint" ) `shouldBe`
51+ (PGFunction " " " f" [] (PGSetOf " bigint" ))
52+
53+ (parsePGFunction " function f() returns table (x bigint, y varchar)" ) `shouldBe`
54+ (PGFunction " " " f" [] (PGTable [PGColumn " x" " bigint" , PGColumn " y" " varchar" ]))
55+
56+
57+ it " works with schema names" $ do
58+ (parsePGFunction " function public.f() returns bigint" ) `shouldBe`
59+ (PGFunction " public" " f" [] (PGSingle " bigint" ))
60+
61+ (parsePGFunction " function Test.f() returns bigint" ) `shouldBe`
62+ (PGFunction " test" " f" [] (PGSingle " bigint" ))
63+
64+
65+
66+ 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" ))
70+
71+
72+ 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" ))
75+
76+ (parsePGFunction " function f(x bit (32)) returns void" ) `shouldBe`
77+ (PGFunction " " " f" [PGArgument " x" " bit" False ] (PGSingle " void" ))
78+
79+
80+ 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" ))
85+
86+
87+ 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" ))
90+
91+ (parsePGFunction " function f(x numeric(10)) returns void" ) `shouldBe`
92+ (PGFunction " " " f" [PGArgument " x" " numeric" False ] (PGSingle " void" ))
93+
94+ (parsePGFunction " function f(x numeric(10,3)) returns void" ) `shouldBe`
95+ (PGFunction " " " f" [PGArgument " x" " numeric" False ] (PGSingle " void" ))
96+
97+
98+ it " works for time types" $ do
99+ (parsePGFunction " function f(x time) returns void" ) `shouldBe`
100+ (PGFunction " " " f" [PGArgument " x" " time" False ] (PGSingle " void" ))
101+
102+ (parsePGFunction " function f(x time (6)) returns void" ) `shouldBe`
103+ (PGFunction " " " f" [PGArgument " x" " time" False ] (PGSingle " void" ))
104+
105+ (parsePGFunction " function f(x time (6) with time zone) returns void" ) `shouldBe`
106+ (PGFunction " " " f" [PGArgument " x" " time with time zone" False ] (PGSingle " void" ))
107+
108+ (parsePGFunction " function f(x time with time zone) returns void" ) `shouldBe`
109+ (PGFunction " " " f" [PGArgument " x" " time with time zone" False ] (PGSingle " void" ))
110+
111+ (parsePGFunction " function f(x time without time zone) returns void" ) `shouldBe`
112+ (PGFunction " " " f" [PGArgument " x" " time without time zone" False ] (PGSingle " void" ))
113+
114+ (parsePGFunction " function f(x timestamp with time zone) returns void" ) `shouldBe`
115+ (PGFunction " " " f" [PGArgument " x" " timestamp with time zone" False ] (PGSingle " void" ))
116+
117+
118+ it " works for intervals" $ do
119+ (parsePGFunction " function f(x interval) returns void" ) `shouldBe`
120+ (PGFunction " " " f" [PGArgument " x" " interval" False ] (PGSingle " void" ))
121+
122+ (parsePGFunction " function f(x interval month) returns void" ) `shouldBe`
123+ (PGFunction " " " f" [PGArgument " x" " interval month" False ] (PGSingle " void" ))
124+
125+ (parsePGFunction " function f(x interval minute to second (4)) returns void" ) `shouldBe`
126+ (PGFunction " " " f" [PGArgument " x" " interval minute to second" False ] (PGSingle " void" ))
127+
0 commit comments