git.iko.soy/rowtype-yoga/purescript-yoga-http-api
on v0.1.0 help sign in
rowtype-yoga/purescript-yoga-http-api/test/MetadataValidationTest.Spec.purs
205 lines 9.1 kB history raw
1module MetadataValidationTest.Spec where
3import Prelude
5import Data.Either (Either(..), isLeft)
6import Effect (Effect)
7import Effect.Aff (Aff)
8import Type.Function (type (#))
9import Yoga.HTTP.API.Path (parseParam)
10import Yoga.HTTP.API.Route.HeaderValue (parseHeader)
11import Yoga.HTTP.API.Route.OpenAPIMetadata (Pattern, MinLength, MaxLength, Minimum, Maximum, Description, Example)
12import ViTest (ViTest, describe, test)
13import ViTest.Expect (expectToBe)
15expectToEqual :: forall a. Eq a => a -> a -> Aff Unit
16expectToEqual expected actual = expectToBe true (expected == actual)
18expectLeft :: forall a b. Either a b -> Aff Unit
19expectLeft = expectToBe true <<< isLeft
21--------------------------------------------------------------------------------
22-- Pattern Validation Tests
23--------------------------------------------------------------------------------
25testPatternValidation :: Effect ViTest
26testPatternValidation = describe "Pattern Validation" $ do
27 _ <- test "parseParam accepts matching string" do
28 let result = parseParam "hello" :: Either String (String # Pattern "^[a-z]+$")
29 expectToEqual (Right "hello") (map (const "hello") result)
31 _ <- test "parseParam rejects non-matching string" do
32 let result = parseParam "Hello123" :: Either String (String # Pattern "^[a-z]+$")
33 expectLeft result
35 _ <- test "parseParam error contains pattern" do
36 let result = parseParam "123" :: Either String (String # Pattern "^[a-z]+$")
37 case result of
38 Left err -> expectToBe true (err == "Value '123' does not match pattern: ^[a-z]+$")
39 Right _ -> expectToBe true false
41 _ <- test "parseHeader accepts matching string" do
42 let result = parseHeader "hello" :: Either String (String # Pattern "^[a-z]+$")
43 expectToEqual (Right "hello") (map (const "hello") result)
45 test "parseHeader rejects non-matching string" do
46 let result = parseHeader "Hello123" :: Either String (String # Pattern "^[a-z]+$")
47 expectLeft result
49--------------------------------------------------------------------------------
50-- MinLength Validation Tests
51--------------------------------------------------------------------------------
53testMinLengthValidation :: Effect ViTest
54testMinLengthValidation = describe "MinLength Validation" $ do
55 _ <- test "parseParam accepts string at minimum length" do
56 let result = parseParam "abc" :: Either String (String # MinLength 3)
57 expectToEqual (Right "abc") (map (const "abc") result)
59 _ <- test "parseParam accepts string above minimum length" do
60 let result = parseParam "abcdef" :: Either String (String # MinLength 3)
61 expectToEqual (Right "abcdef") (map (const "abcdef") result)
63 _ <- test "parseParam rejects string below minimum length" do
64 let result = parseParam "ab" :: Either String (String # MinLength 3)
65 expectLeft result
67 _ <- test "parseParam error message for short string" do
68 let result = parseParam "ab" :: Either String (String # MinLength 3)
69 case result of
70 Left err -> expectToBe true (err == "String length 2 is less than minimum 3")
71 Right _ -> expectToBe true false
73 test "parseHeader rejects string below minimum length" do
74 let result = parseHeader "ab" :: Either String (String # MinLength 3)
75 expectLeft result
77--------------------------------------------------------------------------------
78-- MaxLength Validation Tests
79--------------------------------------------------------------------------------
81testMaxLengthValidation :: Effect ViTest
82testMaxLengthValidation = describe "MaxLength Validation" $ do
83 _ <- test "parseParam accepts string at maximum length" do
84 let result = parseParam "abc" :: Either String (String # MaxLength 3)
85 expectToEqual (Right "abc") (map (const "abc") result)
87 _ <- test "parseParam accepts string below maximum length" do
88 let result = parseParam "ab" :: Either String (String # MaxLength 3)
89 expectToEqual (Right "ab") (map (const "ab") result)
91 _ <- test "parseParam rejects string above maximum length" do
92 let result = parseParam "abcdef" :: Either String (String # MaxLength 3)
93 expectLeft result
95 _ <- test "parseParam error message for long string" do
96 let result = parseParam "abcdef" :: Either String (String # MaxLength 3)
97 case result of
98 Left err -> expectToBe true (err == "String length 6 exceeds maximum 3")
99 Right _ -> expectToBe true false
101 test "parseHeader rejects string above maximum length" do
102 let result = parseHeader "abcdef" :: Either String (String # MaxLength 3)
103 expectLeft result
105--------------------------------------------------------------------------------
106-- Minimum Validation Tests
107--------------------------------------------------------------------------------
109testMinimumValidation :: Effect ViTest
110testMinimumValidation = describe "Minimum Validation" $ do
111 _ <- test "parseParam accepts value at minimum" do
112 let result = parseParam "1" :: Either String (Int # Minimum 1)
113 expectToEqual (Right 1) (map (const 1) result)
115 _ <- test "parseParam accepts value above minimum" do
116 let result = parseParam "50" :: Either String (Int # Minimum 1)
117 expectToEqual (Right 50) (map (const 50) result)
119 _ <- test "parseParam rejects value below minimum" do
120 let result = parseParam "0" :: Either String (Int # Minimum 1)
121 expectLeft result
123 _ <- test "parseParam error message for value below minimum" do
124 let result = parseParam "0" :: Either String (Int # Minimum 1)
125 case result of
126 Left err -> expectToBe true (err == "Value 0 is less than minimum 1")
127 Right _ -> expectToBe true false
129 _ <- test "inner parse failure propagates" do
130 let result = parseParam "abc" :: Either String (Int # Minimum 1)
131 expectLeft result
133 test "parseHeader rejects value below minimum" do
134 let result = parseHeader "0" :: Either String (Int # Minimum 1)
135 expectLeft result
137--------------------------------------------------------------------------------
138-- Maximum Validation Tests
139--------------------------------------------------------------------------------
141testMaximumValidation :: Effect ViTest
142testMaximumValidation = describe "Maximum Validation" $ do
143 _ <- test "parseParam accepts value at maximum" do
144 let result = parseParam "100" :: Either String (Int # Maximum 100)
145 expectToEqual (Right 100) (map (const 100) result)
147 _ <- test "parseParam accepts value below maximum" do
148 let result = parseParam "50" :: Either String (Int # Maximum 100)
149 expectToEqual (Right 50) (map (const 50) result)
151 _ <- test "parseParam rejects value above maximum" do
152 let result = parseParam "150" :: Either String (Int # Maximum 100)
153 expectLeft result
155 _ <- test "parseParam error message for value above maximum" do
156 let result = parseParam "150" :: Either String (Int # Maximum 100)
157 case result of
158 Left err -> expectToBe true (err == "Value 150 exceeds maximum 100")
159 Right _ -> expectToBe true false
161 test "parseHeader rejects value above maximum" do
162 let result = parseHeader "150" :: Either String (Int # Maximum 100)
163 expectLeft result
165--------------------------------------------------------------------------------
166-- Composed Validation Tests
167--------------------------------------------------------------------------------
169testComposedValidation :: Effect ViTest
170testComposedValidation = describe "Composed Validation" $ do
171 _ <- test "Int with Minimum and Maximum accepts valid value" do
172 let result = parseParam "50" :: Either String (Int # Minimum 1 # Maximum 100)
173 expectToEqual (Right 50) (map (const 50) result)
175 _ <- test "Int with Minimum and Maximum rejects below minimum" do
176 let result = parseParam "0" :: Either String (Int # Minimum 1 # Maximum 100)
177 expectLeft result
179 _ <- test "Int with Minimum and Maximum rejects above maximum" do
180 let result = parseParam "150" :: Either String (Int # Minimum 1 # Maximum 100)
181 expectLeft result
183 _ <- test "String with MinLength and MaxLength accepts valid string" do
184 let result = parseParam "hello" :: Either String (String # MinLength 3 # MaxLength 10)
185 expectToEqual (Right "hello") (map (const "hello") result)
187 _ <- test "String with MinLength and MaxLength rejects short string" do
188 let result = parseParam "ab" :: Either String (String # MinLength 3 # MaxLength 10)
189 expectLeft result
191 _ <- test "String with MinLength and MaxLength rejects long string" do
192 let result = parseParam "this is too long" :: Either String (String # MinLength 3 # MaxLength 10)
193 expectLeft result
195 _ <- test "String with Pattern and MinLength validates both" do
196 let result = parseParam "ab" :: Either String (String # Pattern "^[a-z]+$" # MinLength 3)
197 expectLeft result
199 _ <- test "documentary wrappers remain transparent" do
200 let result = parseParam "42" :: Either String (Int # Description "user id" # Example "42" # Minimum 1)
201 expectToEqual (Right 42) (map (const 42) result)
203 test "inner parse failure propagates through composed wrappers" do
204 let result = parseParam "abc" :: Either String (Int # Minimum 1 # Maximum 100)
205 expectLeft result