diff --git a/CHANGELOG.md b/CHANGELOG.md index ea2ea1535..0698a1d7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ - Added `.gitattributes` file to standardize to LF line endings and renormalized codebase - Added invalid JSON graph test coverage for `saveGraphJSON` in `app/Controllers/Graph.hs` - Converted various JS files to TypeScript +- Added static info existence test for `Database/Database.hs` ## [0.8.1] - 2026-08-10 diff --git a/app/Database/Database.hs b/app/Database/Database.hs index 185fa044b..8971f5667 100644 --- a/app/Database/Database.hs +++ b/app/Database/Database.hs @@ -4,7 +4,7 @@ -- -- The main module for parsing course information from the web and -- inserting it into the database. Run when @cabal run database@ is executed. -module Database.Database (populateCalendar, setupDatabase) where +module Database.Database (populateCalendar, setupDatabase, populateStaticInfo) where import Config (databasePath, runDb) import Control.Monad (void) @@ -13,7 +13,7 @@ import Data.Maybe (fromMaybe) import Data.Text as T (findIndex, length, reverse, take, unpack) import Database.CourseVideoSeed (seedVideos) import Database.Migrations (getDatabaseVersion) -import Database.Persist.Sqlite (insert_, runMigration, runMigrationQuiet) +import Database.Persist.Sqlite (insertMany_, runMigration, runMigrationQuiet) import Database.Tables import System.Directory (createDirectoryIfMissing) import WebParsing.ArtSciParser (parseCalendar) @@ -54,16 +54,20 @@ populateStaticInfo = do -- | Sets up the Distribution table. setupDistributionTable :: IO () setupDistributionTable = runDb $ do - insert_ $ Distribution "Humanities" - insert_ $ Distribution "Social Science" - insert_ $ Distribution "Science" + insertMany_ + [ Distribution "Humanities" + , Distribution "Social Science" + , Distribution "Science" + ] -- | Sets up the Breadth table. setupBreadthTable :: IO () setupBreadthTable = runDb $ do - insert_ $ Breadth "Creative and Cultural Representations (1)" - insert_ $ Breadth "Thought, Belief, and Behaviour (2)" - insert_ $ Breadth "Society and its Institutions (3)" - insert_ $ Breadth "Living Things and Their Environment (4)" - insert_ $ Breadth "The Physical and Mathematical Universes (5)" - insert_ $ Breadth "No Breadth" + insertMany_ + [ Breadth "Creative and Cultural Representations (1)" + , Breadth "Thought, Belief, and Behaviour (2)" + , Breadth "Society and its Institutions (3)" + , Breadth "Living Things and Their Environment (4)" + , Breadth "The Physical and Mathematical Universes (5)" + , Breadth "No Breadth" + ] diff --git a/backend-test/Database/DatabaseTests.hs b/backend-test/Database/DatabaseTests.hs new file mode 100644 index 000000000..08d7e5b1b --- /dev/null +++ b/backend-test/Database/DatabaseTests.hs @@ -0,0 +1,57 @@ +-- | +-- Description: Database module tests +-- +-- Module that contains the tests for the functions in the Database module. +module Database.DatabaseTests ( + test_database, +) where + +import Config (runDb) +import Data.List (sort) +import qualified Data.Text as T +import Database.Database (populateStaticInfo) +import Database.Persist.Sqlite (Entity, entityVal, selectList) +import Database.Tables ( + Breadth (..), + Distribution (..), + ) +import Test.Tasty (TestTree) +import Test.Tasty.HUnit (assertEqual, testCase) +import TestHelpers (clearDatabase, withDatabase) + +-- | Run test on populateStaticInfo to check that distribution and breadth tables are set up +testPopulateStaticInfo :: TestTree +testPopulateStaticInfo = + testCase "populateStaticInfo inserts tables into the database" $ do + runDb clearDatabase + populateStaticInfo + + distributions <- runDb $ selectList [] [] :: IO [Entity Distribution] + breadths <- runDb $ selectList [] [] :: IO [Entity Breadth] + + assertEqual + "Expected populateStaticInfo to insert distribution entries" + (sort $ map (distributionDescription . entityVal) distributions) + ( sort + [ T.pack "Humanities" + , T.pack "Social Science" + , T.pack "Science" + ] + ) + assertEqual + "Expected populateStaticInfo to insert breadth entries" + (sort $ map (breadthDescription . entityVal) breadths) + ( sort + [ T.pack "Creative and Cultural Representations (1)" + , T.pack "Thought, Belief, and Behaviour (2)" + , T.pack "Society and its Institutions (3)" + , T.pack "Living Things and Their Environment (4)" + , T.pack "The Physical and Mathematical Universes (5)" + , T.pack "No Breadth" + ] + ) + +-- | Test suite for Database module +test_database :: TestTree +test_database = + withDatabase "Database tests" [testPopulateStaticInfo] diff --git a/courseography.cabal b/courseography.cabal index 285b9a212..0dd465fd8 100644 --- a/courseography.cabal +++ b/courseography.cabal @@ -123,6 +123,7 @@ test-suite Tests Controllers.ProgramControllerTests, Database.BuildingTests, Database.CourseQueriesTests, + Database.DatabaseTests, Database.TablesTests, RequirementTests.ModifierTests, RequirementTests.PostParserTests,