sydtest-amqp (empty) → 0.1.0.0
raw patch · 5 files changed
+216/−0 lines, 5 filesdep +aesondep +amqpdep +async
Dependencies added: aeson, amqp, async, base, bytestring, network, path, path-io, port-utils, process, stm, sydtest, sydtest-amqp, sydtest-rabbitmq, sydtest-typed-process, text, typed-process
Files
- LICENSE.md +5/−0
- src/Test/Syd/AMQP.hs +57/−0
- sydtest-amqp.cabal +75/−0
- test/Spec.hs +1/−0
- test/Test/Syd/AMQPSpec.hs +78/−0
+ LICENSE.md view
@@ -0,0 +1,5 @@+# Sydtest License++Copyright (c) 2020-2021 Tom Sydney Kerckhove++See the Sydtest License at https://github.com/NorfairKing/sydtest/blob/master/sydtest/LICENSE.md for the full license text.
+ src/Test/Syd/AMQP.hs view
@@ -0,0 +1,57 @@+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE TypeOperators #-}++module Test.Syd.AMQP+ ( amqpSpec,+ amqpConnectionSetupFunc,+ )+where++import Network.AMQP as AMQP+import Test.Syd+import Test.Syd.RabbitMQ++-- | Run a rabbitmq server around a group of test, and provide a clean connection to each individual test+--+-- Example usage+--+-- > spec :: Spec+-- > spec =+-- > describe "amqpSpec" $ do+-- > it "can write and read a message" $ \conn -> do+-- > chan <- openChannel conn+-- >+-- > -- declare a queue, exchange and binding+-- > _ <- declareQueue chan newQueue {queueName = "myQueue"}+-- > declareExchange chan newExchange {exchangeName = "myExchange", exchangeType = "direct"}+-- > bindQueue chan "myQueue" "myExchange" "myKey"+-- >+-- > -- publish a message to our new exchange+-- > let body = "hello world"+-- > _ <-+-- > publishMsg+-- > chan+-- > "myExchange"+-- > "myKey"+-- > newMsg+-- > { msgBody = body,+-- > msgDeliveryMode = Just Persistent+-- > }+-- >+-- > mMesg <- getMsg chan Ack "myQueue"+-- > case mMesg of+-- > Nothing -> expectationFailure "Should have received a message"+-- > Just (m, e) -> do+-- > msgBody m `shouldBe` body+-- > ackEnv e+amqpSpec :: TestDefM (RabbitMQHandle ': outers) AMQP.Connection result -> TestDefM outers inner result+amqpSpec = rabbitMQSpec . setupAroundWith' (\serverHandle _ -> amqpConnectionSetupFunc serverHandle)++-- | Setup function for a connection to a given rabbitmq server+amqpConnectionSetupFunc :: RabbitMQHandle -> SetupFunc Connection+amqpConnectionSetupFunc h = do+ let opts = defaultConnectionOpts {coServers = [("localhost", rabbitMQHandlePort h)]}+ let acquire = openConnection'' opts+ let release = closeConnection+ bracketSetupFunc acquire release
+ sydtest-amqp.cabal view
@@ -0,0 +1,75 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.34.4.+--+-- see: https://github.com/sol/hpack++name: sydtest-amqp+version: 0.1.0.0+synopsis: An amqp companion library for sydtest+category: Testing+homepage: https://github.com/NorfairKing/sydtest#readme+bug-reports: https://github.com/NorfairKing/sydtest/issues+author: Tom Sydney Kerckhove+maintainer: syd@cs-syd.eu+copyright: Copyright (c) 2020-2021 Tom Sydney Kerckhove+license: OtherLicense+license-file: LICENSE.md+build-type: Simple++source-repository head+ type: git+ location: https://github.com/NorfairKing/sydtest++flag sydtest_integration_tests+ description: Whether to allow building integration tests+ manual: False+ default: True++library+ exposed-modules:+ Test.Syd.AMQP+ other-modules:+ Paths_sydtest_amqp+ hs-source-dirs:+ src+ build-depends:+ aeson+ , amqp+ , async+ , base >=4.7 && <5+ , bytestring+ , network+ , path+ , path-io+ , port-utils+ , process+ , stm+ , sydtest+ , sydtest-rabbitmq+ , sydtest-typed-process+ , text+ , typed-process+ default-language: Haskell2010++test-suite sydtest-amqp-test+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ other-modules:+ Test.Syd.AMQPSpec+ Paths_sydtest_amqp+ hs-source-dirs:+ test+ ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall+ build-tool-depends:+ sydtest-discover:sydtest-discover+ build-depends:+ amqp+ , base >=4.7 && <5+ , sydtest+ , sydtest-amqp+ if flag(sydtest_integration_tests)+ buildable: True+ else+ buildable: False+ default-language: Haskell2010
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF sydtest-discover #-}
+ test/Test/Syd/AMQPSpec.hs view
@@ -0,0 +1,78 @@+{-# LANGUAGE OverloadedStrings #-}++module Test.Syd.AMQPSpec (spec) where++import Network.AMQP+import Test.Syd+import Test.Syd.AMQP++spec :: Spec+spec = do+ amqpSpec $+ doNotRandomiseExecutionOrder $ do+ describe "amqpSpec" $ do+ it "can write and read a message" $ \conn -> do+ chan <- openChannel conn++ -- declare a queue, exchange and binding+ _ <- declareQueue chan newQueue {queueName = "myQueue"}+ declareExchange chan newExchange {exchangeName = "myExchange", exchangeType = "direct"}+ bindQueue chan "myQueue" "myExchange" "myKey"++ -- publish a message to our new exchange+ let body = "hello world"+ _ <-+ publishMsg+ chan+ "myExchange"+ "myKey"+ newMsg+ { msgBody = body,+ msgDeliveryMode = Just Persistent+ }++ mMesg <- getMsg chan Ack "myQueue"+ case mMesg of+ Nothing -> expectationFailure "Should have received a message"+ Just (m, e) -> do+ msgBody m `shouldBe` body+ ackEnv e+ it "can write a message (to make sure the next test cannot read it)" $ \conn -> do+ chan <- openChannel conn++ -- declare a queue, exchange and binding+ _ <- declareQueue chan newQueue {queueName = "myQueue"}+ declareExchange chan newExchange {exchangeName = "myExchange", exchangeType = "direct"}+ bindQueue chan "myQueue" "myExchange" "myKey"++ -- publish a message to our new exchange+ let body = "hello world"+ _ <-+ publishMsg+ chan+ "myExchange"+ "myKey"+ newMsg+ { msgBody = body,+ msgDeliveryMode = Just Persistent+ }+ pure ()++ it "cannot read a message if none have been published" $ \conn -> do+ chan <- openChannel conn++ -- declare a queue, exchange and binding+ _ <- declareQueue chan newQueue {queueName = "myQueue"}+ declareExchange chan newExchange {exchangeName = "myExchange", exchangeType = "direct"}+ bindQueue chan "myQueue" "myExchange" "myKey"++ -- Don't publish anything+ mMesg <- getMsg chan Ack "myQueue"+ case mMesg of+ Nothing -> pure ()+ Just (m, _) ->+ expectationFailure $+ unlines+ [ "Should not have been able to read any message, but read this one:",+ show m+ ]