git.iko.soy/typeable/mandrill
on master help sign in
Online tests triggered by environment variable
commit8613a4983e42fe674581f358883fee280fac4d4e authorAlfredo Di Napoli <alfredo.dinapoli@gmail.com> dateTue 19 Aug 2014, 09:09 (12 years ago) parentf75b8d3
4 files changed+69−11
mandrill.cabal+4−1 src/Network/API/Mandrill/Types.hs+12 test/Main.hs+29−10 test/Online.hs+24
mandrill.cabal+4−1
@@ -33,6 +33,7 @@ library
3333 , aeson >= 0.7.0.3 && < 0.8
3434 , lens >= 4.1 && < 4.6
3535 , blaze-html >= 0.5.0.0
36+ , QuickCheck >= 2.6 && < 2.8
3637 , time
3738 , old-locale
3839 hs-source-dirs:
@@ -50,6 +51,7 @@ test-suite mandrill-tests
5051 other-modules:
5152 Tests
5253 RawData
54+ Online
5355 hs-source-dirs:
5456 test
5557 default-language:
@@ -59,8 +61,9 @@ test-suite mandrill-tests
5961 , base
6062 , aeson
6163 , bytestring
62− , QuickCheck >= 2.6 && < 2.8
64+ , QuickCheck
6365 , tasty >= 0.7 && < 0.9
6466 , tasty-quickcheck
6567 , tasty-hunit
6668 , raw-strings-qq < 1.2
69+ , text
src/Network/API/Mandrill/Types.hs+12
@@ -5,6 +5,7 @@
55module Network.API.Mandrill.Types where
66
77import Network.API.Mandrill.Utils
8+import Test.QuickCheck
89import Data.Char
910import Data.Time
1011import Control.Applicative
@@ -97,6 +98,8 @@ data MandrillRecipient = MandrillRecipient {
9798makeLenses ''MandrillRecipient
9899deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillRecipient
99100
101+instance Arbitrary MandrillRecipient where
102+ arbitrary = pure $ MandrillRecipient "test@example.com" Nothing Nothing
100103
101104--------------------------------------------------------------------------------
102105newtype MandrillHtml = MandrillHtml Blaze.Html
@@ -117,6 +120,8 @@ instance FromJSON MandrillHtml where
117120 parseJSON (String h) = return $ MandrillHtml (Blaze.toHtml h)
118121 parseJSON v = typeMismatch "Expecting a String for MandrillHtml" v
119122
123+instance Arbitrary MandrillHtml where
124+ arbitrary = pure $ mkMandrillHtml "<p><b>FooBar</b></p>"
120125
121126--------------------------------------------------------------------------------
122127-- | The information on the message to send
@@ -138,6 +143,13 @@ data MandrillMessage = MandrillMessage {
138143makeLenses ''MandrillMessage
139144deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMessage
140145
146+instance Arbitrary MandrillMessage where
147+ arbitrary = MandrillMessage <$> arbitrary
148+ <*> pure Nothing
149+ <*> pure "Test Subject"
150+ <*> pure "sender@example.com"
151+ <*> pure Nothing
152+ <*> resize 2 arbitrary
141153
142154--------------------------------------------------------------------------------
143155type MandrillKey = T.Text
test/Main.hs+29−10
@@ -1,20 +1,39 @@
1−
1+{-# LANGUAGE OverloadedStrings #-}
22module Main where
33
4−import Tests
5−import Test.Tasty
6−import Test.Tasty.HUnit
7−import Test.Tasty.QuickCheck
4+import System.Environment
5+import Data.Monoid
6+import Tests
7+import Online
8+import Test.Tasty
9+import Test.Tasty.HUnit
10+import Test.Tasty.QuickCheck
11+import qualified Data.Text as T
812
913----------------------------------------------------------------------
1014withQuickCheckDepth :: TestName -> Int -> [TestTree] -> TestTree
1115withQuickCheckDepth tn depth tests =
1216 localOption (QuickCheckTests depth) (testGroup tn tests)
1317
18+----------------------------------------------------------------------
19+onlineTestsIfEnabled :: IO [TestTree]
20+onlineTestsIfEnabled = do
21+ apiKey <- (return . fmap T.pack) =<< lookupEnv "MANDRILL_API_KEY"
22+ case apiKey of
23+ Nothing -> return []
24+ Just k -> return [
25+ testGroup "Mandrill online tests" [
26+ testCase "messages/send.json" (testOnlineMessagesSend k)
27+ ]]
28+
1429----------------------------------------------------------------------
1530main :: IO ()
16−main = defaultMainWithIngredients defaultIngredients $
17− testGroup "Mandrill tests" [
18− testCase "users/info.json API parsing" testUsersInfo
19− , testCase "messages/send.json API parsing" testMessagesSend
20− ]
31+main = do
32+ onlineTests <- onlineTestsIfEnabled
33+ defaultMainWithIngredients defaultIngredients $
34+ testGroup "Mandrill tests" $ onlineTests <> [
35+ testGroup "Mandrill offline tests" [
36+ testCase "users/info.json API parsing" testUsersInfo
37+ , testCase "messages/send.json API parsing" testMessagesSend
38+ ]
39+ ]
test/Online.hsnew file+24
@@ -0,0 +1,24 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Online where
3+
4+import Test.QuickCheck
5+import Test.Tasty.HUnit
6+import Text.RawString.QQ
7+import Data.Either
8+import Data.Aeson
9+import Network.API.Mandrill.Messages.Types
10+import Network.API.Mandrill.Users.Types
11+import qualified Data.ByteString.Char8 as C8
12+import RawData
13+
14+import Network.API.Mandrill.Types
15+import qualified Network.API.Mandrill.Messages as API
16+
17+
18+testOnlineMessagesSend :: MandrillKey -> Assertion
19+testOnlineMessagesSend k = do
20+ msg <- generate arbitrary
21+ res <- API.send k msg Nothing Nothing Nothing
22+ case res of
23+ MandrillSuccess _ -> return ()
24+ MandrillFailure e -> fail $ "messages/send.json " ++ show e