packages feed

dag-migrations-0.1.0.0 / test / Database / DagMigrations / GraphSpec.hs

module Database.DagMigrations.GraphSpec (spec) whereimport Data.Either (isRight)import Data.List qualified as Listimport Data.List.NonEmpty (NonEmpty (..))import Data.List.NonEmpty qualified as NonEmptyimport Data.Map.Strict qualified as Mapimport Data.Maybe (listToMaybe)import Data.Set qualified as Setimport Data.Text (Text)import Database.DagMigrations.Error (ValidationError (..))import Database.DagMigrations.Genimport Database.DagMigrations.Graphimport Hedgehog.Gen qualified as Genimport Test.Hspecimport Test.Hspec.Hedgehog (assert, forAll, hedgehog, (===))spec :: Specspec = do  describe "validate" $ do    it "accepts an empty registry" $      fmap linearOrder (validate ([] :: [TestMigration])) `shouldBe` Right []    it "reports a duplicate identifier" $      errorsOf [testMigration "a" [], testMigration "a" []]        `shouldBe` Just (DuplicateIdentifier "a" :| [])    it "reports an unknown dependency" $      errorsOf [testMigration "a" ["missing"]]        `shouldBe` Just (UnknownDependency "a" "missing" :| [])    it "reports a migration that depends on itself as a cycle of length 1" $      errorsOf [testMigration "a" ["a"]]        `shouldBe` Just (DependencyCycle ("a" :| []) :| [])    it "reports a cycle starting from its least member, in dependency order" $      errorsOf [testMigration "a" ["b"], testMigration "b" ["c"], testMigration "c" ["a"]]        `shouldBe` Just (DependencyCycle ("a" :| ["b", "c"]) :| [])    it "reports each of several disjoint cycles" $      errorsOf [testMigration "a" ["b"], testMigration "b" ["a"], testMigration "c" ["d"], testMigration "d" ["c"]]        `shouldBe` Just (DependencyCycle ("a" :| ["b"]) :| [DependencyCycle ("c" :| ["d"])])    it "reports a shortest cycle through the least member" $      errorsOf [testMigration "a" ["b", "c"], testMigration "b" ["d"], testMigration "d" ["a"], testMigration "c" ["a"]]        `shouldBe` Just (DependencyCycle ("a" :| ["c"]) :| [])    it "reports a cycle that only one of two duplicate registrations closes" $      errorsOf [testMigration "a" ["b"], testMigration "a" [], testMigration "b" ["a"]]        `shouldBe` Just (DuplicateIdentifier "a" :| [DependencyCycle ("a" :| ["b"])])    it "does not report a migration that only depends on a cycle" $      errorsOf [testMigration "a" ["b"], testMigration "b" ["a"], testMigration "c" ["a"]]        `shouldBe` Just (DependencyCycle ("a" :| ["b"]) :| [])    it "reports every problem at once, in ascending order" $      errorsOf        [ testMigration "a" ["x"],          testMigration "a" [],          testMigration "b" ["c"],          testMigration "c" ["b"]        ]        `shouldBe` Just          ( DuplicateIdentifier "a"              :| [UnknownDependency "a" "x", DependencyCycle ("b" :| ["c"])]          )    it "accepts every generated acyclic registry" $ hedgehog $ do      specs <- forAll genRegistry      assert (isRight (validate (toRegistry specs)))    it "reports only real cycles after an edge closes one" $ hedgehog $ do      specs <- forAll genRegistry      graph <- validated (toRegistry specs)      case linearOrder graph of        [] -> pure ()        ids -> do          from <- forAll (Gen.element ids)          -- Every member of upSet depends on 'from', so a dependency of          -- 'from' on any of them closes a cycle.          to <- forAll (Gen.element (Set.toList (upSet graph from)))          let mutated =                [ (name, if name == from then to : dependencies else dependencies)                | (name, dependencies) <- specs                ]              edges = dependencyMap mutated              isEdge (a, b) = maybe False (Set.member b) (Map.lookup a edges)              cyclesOf errors = [c | DependencyCycle c <- NonEmpty.toList errors]          case validate (toRegistry mutated) of            Right _ -> fail "expected a cycle"            Left errors -> do              assert (not (null (cyclesOf errors)))              let consecutive (c :| rest) = zip (c : rest) (rest <> [c])              assert (all (all isEdge . consecutive) (cyclesOf errors))              -- Each cycle is simple, and starts with its least member.              assert (all (\c -> length c == Set.size (Set.fromList (NonEmpty.toList c))) (cyclesOf errors))              assert (all (\c -> NonEmpty.head c == minimum c) (cyclesOf errors))  describe "linearOrder" $ do    it "is the lexicographically least linear extension" $      fmap linearOrder (validate [testMigration "1" ["3"], testMigration "2" [], testMigration "3" []])        `shouldBe` Right ["2", "3", "1"]    it "places, at each position, the least migration whose dependencies are all placed" $ hedgehog $ do      specs <- forAll genRegistry      graph <- validated (toRegistry specs)      let edges = dependencyMap specs          order = linearOrder graph          available placed =            [ name            | (name, dependencies) <- Map.toAscList edges,              not (Set.member name placed),              dependencies `Set.isSubsetOf` placed            ]          expected = List.unfoldr (\placed -> fmap (\next -> (next, Set.insert next placed)) (listToMaybe (available placed))) Set.empty      order === expected    it "does not depend on the order of registration" $ hedgehog $ do      specs <- forAll genRegistry      shuffled <- forAll (Gen.shuffle specs)      graph <- validated (toRegistry specs)      graph' <- validated (toRegistry shuffled)      linearOrder graph === linearOrder graph'    it "keeps the old order when a maximal migration is added" $ hedgehog $ do      specs <- forAll genRegistry      graph <- validated (toRegistry specs)      dependencies <- forAll (Gen.subsequence (linearOrder graph))      -- "m00" sorts before every generated name.      graph' <- validated (toRegistry (("m00", dependencies) : specs))      filter (/= "m00") (linearOrder graph') === linearOrder graph  describe "downSet and upSet" $ do    it "contain the identifier and its transitive dependencies or dependents" $      case validate [testMigration "a" [], testMigration "b" ["a"], testMigration "c" ["b"]] of        Left errors -> expectationFailure (show errors)        Right graph -> do          downSet graph "b" `shouldBe` Set.fromList ["a", "b"]          upSet graph "b" `shouldBe` Set.fromList ["b", "c"]          downSet graph "missing" `shouldBe` Set.empty  where    errorsOf :: [TestMigration] -> Maybe (NonEmpty (ValidationError Text))    errorsOf = either Just (const Nothing) . validate