Skip to content

Commit

Permalink
[#500] OrgRegistry-Tests: Move utility functions into mirza-test-util…
Browse files Browse the repository at this point in the history
…s-haskel so that they can be used across services.
  • Loading branch information
a-stacey committed Jul 31, 2019
1 parent 8b88b58 commit 8738625
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ library
, beam-postgres
, bytestring
, hspec-expectations
, http-types
, mtl
, network
, postgresql-simple
Expand Down
29 changes: 29 additions & 0 deletions projects/mirza-test-utils-haskell/test/Mirza/Common/Tests/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module Mirza.Common.Tests.Utils
( within1Second
, betweenInclusive
, shouldSatisfyIO
, checkField
, checkFailureStatus
, checkFailureMessage
, unsafeMkEmailAddress
, DatabaseName (..)
, DatabaseConnectionString (..)
Expand All @@ -20,8 +23,14 @@ import Data.ByteString as BS hiding (putStrLn, reverse,
unpack)
import qualified Data.ByteString.Char8 as C8 (unpack)

import qualified Data.ByteString.Lazy as BSL

import Test.Hspec.Expectations (Expectation, shouldSatisfy)

import Servant.Client

import qualified Network.HTTP.Types.Status as NS

import Data.Foldable (forM_)
import Data.String (fromString)
import qualified Data.Text as T (unpack)
Expand Down Expand Up @@ -68,6 +77,26 @@ betweenInclusive bound1 bound2 x = (bound1 `comparator` x) && (x `comparator` bo
shouldSatisfyIO :: (HasCallStack, Show a, Eq a) => IO a -> (a -> Bool) -> Expectation
action `shouldSatisfyIO` p = action >>= (`shouldSatisfy` p)


-- Test helper function that enables a predicate to be run on the result of a
-- test call.
checkField :: (a -> b) -> (b -> Bool) -> Either c a -> Bool
checkField accessor predicate = either (const False) (predicate . accessor)


checkFailureStatus :: NS.Status -> Either ServantError a -> Bool
checkFailureStatus = checkFailureField responseStatusCode


checkFailureMessage :: BSL.ByteString -> Either ServantError a -> Bool
checkFailureMessage = checkFailureField responseBody


checkFailureField :: (Eq a) => (Response -> a) -> a -> Either ServantError b -> Bool
checkFailureField accessor x (Left (FailureResponse failure)) = x == (accessor failure)
checkFailureField _ _ _ = False


--------------------------------------------------------------------------------
-- Database Utils
--------------------------------------------------------------------------------
Expand Down
20 changes: 0 additions & 20 deletions projects/or_scs/test/Mirza/OrgRegistry/Tests/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -753,26 +753,6 @@ clientSpec = do
]



-- Test helper function that enables a predicate to be run on the result of a
-- test call.
checkField :: (a -> b) -> (b -> Bool) -> Either c a -> Bool
checkField accessor predicate = either (const False) (predicate . accessor)


checkFailureStatus :: NS.Status -> Either ServantError a -> Bool
checkFailureStatus = checkFailureField responseStatusCode


checkFailureMessage :: ByteString -> Either ServantError a -> Bool
checkFailureMessage = checkFailureField responseBody


checkFailureField :: (Eq a) => (Response -> a) -> a -> Either ServantError b -> Bool
checkFailureField accessor x (Left (FailureResponse failure)) = x == (accessor failure)
checkFailureField _ _ _ = False


partialNewOrgToOrgResponse :: GS1CompanyPrefix -> PartialNewOrg -> OrgResponse
partialNewOrgToOrgResponse gs1CompanyPrefix partialNewOrg = OrgResponse
{ orgResponseGS1CompanyPrefix = gs1CompanyPrefix
Expand Down

0 comments on commit 8738625

Please sign in to comment.