Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Test the expandCredits wrapper

  • Loading branch information...
commit 414e1afdafa589b69740c949e7a690a39935d1be 1 parent 985e6c9
@ocharles ocharles authored
2  musicbrainz-data
@@ -1 +1 @@
-Subproject commit 7dabcc50ee539c68c9ce2bc7882ddfb535a7c1f3
+Subproject commit 3bd97c33f43d4c9be3f6b48ac70ab37c818832f9
View
92 test/suite/Common.hs
@@ -0,0 +1,92 @@
+{-# LANGUAGE OverloadedStrings #-}
+module Common where
+
+import Control.Lens
+import Data.Maybe (fromMaybe)
+import Data.Monoid
+import Data.Text (Text)
+import Data.Text.Lens
+import MusicBrainz
+import Test.MusicBrainz
+import Text.Digestive
+
+import qualified Data.Text as Text
+import qualified MusicBrainz.Data as MB
+import qualified MusicBrainz.Data.Editor as MB
+
+--------------------------------------------------------------------------------
+registerEditor :: MusicBrainz (Entity Editor)
+registerEditor = MB.register (Editor "ocharles" "password")
+
+
+--------------------------------------------------------------------------------
+mkArtist :: Entity Editor -> Text -> MusicBrainz (Ref Artist)
+mkArtist editor name = fmap coreRef $ autoEdit $
+ MB.create (entityRef editor)
+ (ArtistTree { artistData = Artist { artistName = name
+ , artistSortName = name
+ , artistComment = ""
+ , artistBeginDate = emptyDate
+ , artistEndDate = emptyDate
+ , artistEnded = False
+ , artistGender = Nothing
+ , artistType = Nothing
+ , artistCountry = Nothing
+ }
+ , artistRelationships = mempty
+ , artistAliases = mempty
+ , artistIpiCodes = mempty
+ , artistAnnotation = ""
+ }) >>= MB.viewRevision
+
+
+--------------------------------------------------------------------------------
+mockAcFields :: [ArtistCreditName] -> [([Text], Text)]
+mockAcFields names =
+ concat [ [( ["artist-credits", "indices"] , formatIndices names) ]
+ , mkFields artistCreditField names
+ ]
+ where
+ artistCreditField i (ArtistCreditName artist name joinPhrase) =
+ [ (["artist-credits", i, "artist"], (dereference artist) ^. re mbid . packed)
+ , (["artist-credits", i, "name"], name)
+ , (["artist-credits", i, "join-phrase"], joinPhrase)
+ ]
+
+
+--------------------------------------------------------------------------------
+mockPost :: Form Text MusicBrainz a -> [([Text], Text)] -> MusicBrainz (View Text, Maybe a)
+mockPost form submissions = postForm "" form env
+ where
+ env p = return $ maybe [] (return . TextInput) $ lookup (drop 1 p) submissions
+
+
+--------------------------------------------------------------------------------
+success :: (Eq a, Show a) => Form Text MusicBrainz a -> [([Text], Text)] -> MusicBrainz a
+success form submissions = do
+ (v, actual) <- mockPost form submissions
+ return $ fromMaybe (error $ show v) actual
+
+
+--------------------------------------------------------------------------------
+expectFailure :: (Eq a, Show a) => Form Text MusicBrainz a -> [([Text], Text)] -> MusicBrainz ()
+expectFailure form submissions = do
+ (_, actual) <- mockPost form submissions
+ actual @?= Nothing
+
+
+--------------------------------------------------------------------------------
+testForm :: (Eq a, Show a) => Form Text MusicBrainz a -> [([Text], Text)] -> a -> MusicBrainz ()
+testForm form submissions expected = do
+ actual <- success form submissions
+ actual @?= expected
+
+
+--------------------------------------------------------------------------------
+formatIndices :: [a] -> Text
+formatIndices x = Text.intercalate "," $ [0..pred $ length x] ^.. traverse.to show.packed
+
+
+--------------------------------------------------------------------------------
+mkFields :: (Text -> a -> [([Text], Text)]) -> [a] -> [([Text], Text)]
+mkFields f x = concat $ zipWith f (map (view packed . show) [0..]) x
View
80 test/suite/Parsers.hs
@@ -23,6 +23,8 @@ import qualified MusicBrainz.API as API
import Test.MusicBrainz hiding (expectFailure)
+import Common
+
--------------------------------------------------------------------------------
tests :: [Test]
tests =
@@ -340,34 +342,6 @@ testOptionalAddParser form fieldName x = do
--------------------------------------------------------------------------------
-mockPost :: Form Text MusicBrainz a -> [([Text], Text)] -> MusicBrainz (View Text, Maybe a)
-mockPost form submissions = postForm "" form env
- where
- env p = return $ maybe [] (return . TextInput) $ lookup (drop 1 p) submissions
-
-
---------------------------------------------------------------------------------
-success :: (Eq a, Show a) => Form Text MusicBrainz a -> [([Text], Text)] -> MusicBrainz a
-success form submissions = do
- (v, actual) <- mockPost form submissions
- return $ fromMaybe (error $ show v) actual
-
-
---------------------------------------------------------------------------------
-expectFailure :: (Eq a, Show a) => Form Text MusicBrainz a -> [([Text], Text)] -> MusicBrainz ()
-expectFailure form submissions = do
- (_, actual) <- mockPost form submissions
- actual @?= Nothing
-
-
---------------------------------------------------------------------------------
-testForm :: (Eq a, Show a) => Form Text MusicBrainz a -> [([Text], Text)] -> a -> MusicBrainz ()
-testForm form submissions expected = do
- actual <- success form submissions
- actual @?= expected
-
-
---------------------------------------------------------------------------------
testAliases :: Test
testAliases = testCase "aliases" $
testForm API.aliases fields (Set.fromList expected)
@@ -509,55 +483,5 @@ testIpiCodes = testGroup "ipiCodes"
--------------------------------------------------------------------------------
-registerEditor :: MusicBrainz (Entity Editor)
-registerEditor = MB.register (Editor "ocharles" "password")
-
-
---------------------------------------------------------------------------------
-mkArtist :: Entity Editor -> Text -> MusicBrainz (Ref Artist)
-mkArtist editor name = fmap coreRef $ autoEdit $
- MB.create (entityRef editor)
- (ArtistTree { artistData = Artist { artistName = name
- , artistSortName = name
- , artistComment = ""
- , artistBeginDate = emptyDate
- , artistEndDate = emptyDate
- , artistEnded = False
- , artistGender = Nothing
- , artistType = Nothing
- , artistCountry = Nothing
- }
- , artistRelationships = mempty
- , artistAliases = mempty
- , artistIpiCodes = mempty
- , artistAnnotation = ""
- }) >>= MB.viewRevision
-
-
---------------------------------------------------------------------------------
-mockAcFields :: [ArtistCreditName] -> [([Text], Text)]
-mockAcFields names =
- concat [ [( ["artist-credits", "indices"] , formatIndices names) ]
- , mkFields artistCreditField names
- ]
- where
- artistCreditField i (ArtistCreditName artist name joinPhrase) =
- [ (["artist-credits", i, "artist"], (dereference artist) ^. re mbid . packed)
- , (["artist-credits", i, "name"], name)
- , (["artist-credits", i, "join-phrase"], joinPhrase)
- ]
-
-
---------------------------------------------------------------------------------
-formatIndices :: [a] -> Text
-formatIndices x = Text.intercalate "," $ [0..pred $ length x] ^.. traverse.to show.packed
-
-
---------------------------------------------------------------------------------
expandCredit :: Ref ArtistCredit -> MusicBrainz [ArtistCreditName]
expandCredit acId = (Map.! acId) <$> MB.expandCredits (Set.singleton acId)
-
-
---------------------------------------------------------------------------------
-mkFields :: (Text -> a -> [([Text], Text)]) -> [a] -> [([Text], Text)]
-mkFields f x = concat $ zipWith f (map (view packed . show) [0..]) x
View
2  test/suite/Tests.hs
@@ -3,6 +3,7 @@ module Main (main) where
import qualified Parsers
import qualified Handlers
+import qualified Wrappers
import Test.MusicBrainz
@@ -10,4 +11,5 @@ main :: IO ()
main = testRunner
[ testGroup "Parsers" Parsers.tests
, testGroup "Handlers" Handlers.tests
+ , testGroup "API Wrappers" Wrappers.tests
]
View
42 test/suite/Wrappers.hs
@@ -0,0 +1,42 @@
+{-# LANGUAGE OverloadedStrings #-}
+module Wrappers (tests) where
+
+import Control.Lens
+import Data.Text.Lens
+import MusicBrainz
+import Test.MusicBrainz
+
+import qualified Data.Map as Map
+import qualified MusicBrainz.Data.ArtistCredit as MB
+
+import qualified MusicBrainz.API.ArtistCredit as ArtistCredit
+
+import Common
+
+--------------------------------------------------------------------------------
+tests :: [Test]
+tests =
+ [ testExpandCredits ]
+
+
+--------------------------------------------------------------------------------
+testExpandCredits :: Test
+testExpandCredits = testCase "Expand credits" $ do
+ editor <- registerEditor
+
+ [a1, a2, a3] <- mapM (mkArtist editor) ["Artist 1", "Artist 2", "Artist 3"]
+
+ let ac1 = [ArtistCreditName a1 "Artist 1" " &", ArtistCreditName a2 "Artist 2" "" ]
+ let ac2 = [ArtistCreditName a3 "Artist 3" "" ]
+ [ac1_id, ac2_id] <- mapM MB.getRef [ac1, ac2]
+
+ expanded <- success ArtistCredit.expandCredits
+ [ (["indices"], "0,1")
+ , (["0"], ac1_id ^. to dereference . to show . packed)
+ , (["1"], ac2_id ^. to dereference . to show . packed)
+ ]
+
+ expanded @?= Map.fromList
+ [ (ac1_id, ac1)
+ , (ac2_id, ac2)
+ ]
Please sign in to comment.
Something went wrong with that request. Please try again.