ticket-booking-main/test/Spec.hs

125 lines
4.1 KiB
Haskell
Raw Permalink Normal View History

2024-05-27 14:47:43 +02:00
import Test.Hspec
import Database.PostgreSQL.Simple
import Database.PostgreSQL.Simple.FromRow
import Database.PostgreSQL.Simple.ToField
import Database.PostgreSQL.Simple.ToRow
import Database.PostgreSQL.Simple.FromField
import Database.PostgreSQL.Simple.Types
import User
import Database
import Types
import Session
import Reservation
import Control.Monad.Trans.Reader (runReaderT)
import Data.String (fromString)
import Data.Time.Calendar (fromGregorian)
import Data.ByteString.Char8 (pack)
-- Define a test suite
spec :: Spec
spec = do
describe "User registration" $ do
it "should register a new user successfully" $ do
-- Connect to the test database
conn <- connectTestDB
-- Perform any necessary initialization
initializeDB conn
-- Perform user registration
let name = "testUser"
password = "testPassword"
registerUser conn name password
-- Check if the user was successfully registered
userExists <- doesUserExist conn name
userExists `shouldBe` True
describe "User login" $ do
it "should login an existing user successfully" $ do
-- Connect to the test database
conn <- connectTestDB
-- Perform any necessary initialization
initializeDB conn
-- Register a user
let name = "testLoginUser"
password = "testLoginPassword"
registerUser conn name password
-- Attempt to login
loginResult <- loginUser conn name password
case loginResult of
Just _ -> return ()
Nothing -> error "Login failed"
it "should fail to login with incorrect password" $ do
-- Connect to the test database
conn <- connectTestDB
-- Perform any necessary initialization
initializeDB conn
-- Register a user
let name = "testFailLoginUser"
password = "testFailLoginPassword"
registerUser conn name password
-- Attempt to login with incorrect password
loginResult <- loginUser conn name "wrongPassword"
loginResult `shouldBe` Nothing
describe "Event management" $ do
it "should add an event successfully" $ do
-- Connect to the test database
conn <- connectTestDB
-- Perform any necessary initialization
initializeDB conn
-- Add an event
let eventName = "Test Event"
eventDate = fromGregorian 2024 12 31
addEvent conn (User 1 "admin" (pack "password") True) eventName eventDate
-- Check if the event was successfully added
eventExists <- doesEventExist conn 1
eventExists `shouldBe` True
describe "Reservation management" $ do
it "should add a reservation successfully" $ do
-- Connect to the test database
conn <- connectTestDB
-- Perform any necessary initialization
initializeDB conn
-- Add a user
registerUser conn "testUser" "testPassword"
-- Add an event
addEvent conn (User 1 "admin" (pack "password") True) "Test Event" (fromGregorian 2024 12 31)
-- Add a reservation
addReservation conn 1 1
-- Check if the reservation was successfully added
reservations <- getReservations conn 1
length reservations `shouldBe` 1
-- Helper function to connect to the test database
connectTestDB :: IO Connection
connectTestDB = connect defaultConnectInfo
{ connectHost = "localhost"
, connectDatabase = "postgres"
, connectUser = "postgres"
, connectPassword = "admin"
, connectPort = 5432
}
-- Helper function to check if an event exists in the database
doesEventExist :: Connection -> Int -> IO Bool
doesEventExist conn eventId = do
[Only count] <- query conn (fromString "SELECT COUNT(*) FROM events WHERE id = ?") (Only eventId) :: IO [Only Int]
return (count > 0)
-- Run the test
main :: IO ()
main = hspec spec