From 6e3204b813c12e733ff323cc5e1979ec33f4237c Mon Sep 17 00:00:00 2001 From: Nicolas Perriault Date: Thu, 19 Sep 2024 13:52:57 +0200 Subject: [PATCH] test: introduce failing tests for invalid country codes. --- src/Data/Textile/Query.elm | 17 +++++++++-------- tests/Server/RouteTest.elm | 28 +++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/Data/Textile/Query.elm b/src/Data/Textile/Query.elm index dbb397e1c..3a31853b5 100644 --- a/src/Data/Textile/Query.elm +++ b/src/Data/Textile/Query.elm @@ -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 @@ -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 diff --git a/tests/Server/RouteTest.elm b/tests/Server/RouteTest.elm index 950d06cd8..fb27d2947 100644 --- a/tests/Server/RouteTest.elm +++ b/tests/Server/RouteTest.elm @@ -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