Skip to content

Commit

Permalink
test: introduce failing tests for invalid country codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
n1k0 committed Sep 19, 2024
1 parent 262c8c0 commit 6e3204b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
17 changes: 9 additions & 8 deletions src/Data/Textile/Query.elm
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,6 @@ buildApiQuery clientUrl query =

decode : Decoder Query
decode =
let
-- Note: Using Json.Decode.Extra's optionalField here because we want
-- a failure when a Maybe decoded field value is invalid
strictOptional field decoder =
DE.andMap (DE.optionalField field decoder)
in
Decode.succeed Query
|> strictOptional "airTransportRatio" Split.decodeFloat
|> strictOptional "business" Economics.decodeBusiness
Expand Down Expand Up @@ -143,13 +137,20 @@ decode =
|> strictOptional "yarnSize" Unit.decodeYarnSize


strictOptional : String -> Decoder a -> Decoder (Maybe a -> b) -> Decoder b
strictOptional field decoder =
-- Note: Using Json.Decode.Extra's optionalField here because we want
-- a failure when a Maybe decoded field value is invalid
DE.andMap (DE.optionalField field decoder)


decodeMaterialQuery : Decoder MaterialQuery
decodeMaterialQuery =
Decode.succeed MaterialQuery
|> Pipe.optional "country" (Decode.maybe Country.decodeCode) Nothing
|> strictOptional "country" Country.decodeCode
|> Pipe.required "id" (Decode.map Material.Id Decode.string)
|> Pipe.required "share" Split.decodeFloat
|> Pipe.optional "spinning" (Decode.maybe Spinning.decode) Nothing
|> strictOptional "spinning" Spinning.decode


encode : Query -> Encode.Value
Expand Down
28 changes: 27 additions & 1 deletion tests/Server/RouteTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,33 @@ textileEndpoints db =
}
)
|> expectTextileSingleErrorContains "physicalDurability"
|> asTest "should reject invalid POST body"
|> asTest "should reject invalid physicalDurability"
, "/textile/simulator"
|> testEndpoint db
"POST"
(Query.encode
{ tShirtCotonFrance
| countrySpinning = Just (Country.Code "invalid")
}
)
|> expectTextileSingleErrorContains "materials"
|> asTest "should reject invalid spinning country"
, "/textile/simulator"
|> testEndpoint db
"POST"
(Query.encode
{ tShirtCotonFrance
| materials =
[ { country = Just (Country.Code "invalid")
, id = Material.Id "ei-coton"
, share = Split.full
, spinning = Nothing
}
]
}
)
|> expectTextileSingleErrorContains "materials"
|> asTest "should reject invalid materials country"
]
, describe "materials param checks"
[ let
Expand Down

0 comments on commit 6e3204b

Please sign in to comment.