55 lines
2.0 KiB
Haskell
55 lines
2.0 KiB
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
import Test.Hspec
|
|
import UserManager
|
|
import Database.SQLite.Simple
|
|
import Control.Exception (bracket_)
|
|
import System.Directory (removeFile, doesFileExist)
|
|
|
|
main :: IO ()
|
|
main = hspec $ do
|
|
describe "UserManager" $ do
|
|
beforeAll_ setupDB $
|
|
afterAll_ cleanupDB $ do
|
|
it "initializes the database" $ do
|
|
conn <- open "test_users.db"
|
|
tables <- query_ conn "SELECT name FROM sqlite_master WHERE type='table' AND name='users';" :: IO [Only String]
|
|
close conn
|
|
tables `shouldBe` [Only "users"]
|
|
|
|
it "adds a user" $ do
|
|
addUser "test_user" "test@example.com" "password"
|
|
users <- listUsers
|
|
users `shouldBe` [User "test_user" "test@example.com" "password"]
|
|
|
|
it "lists users" $ do
|
|
addUser "test_user1" "test1@example.com" "password1"
|
|
addUser "test_user2" "test2@example.com" "password2"
|
|
users <- listUsers
|
|
users `shouldBe` [User "test_user1" "test1@example.com" "password1", User "test_user2" "test2@example.com" "password2"]
|
|
|
|
it "finds users by name" $ do
|
|
addUser "unique_user" "unique@example.com" "password"
|
|
users <- findUsers "unique_user"
|
|
users `shouldBe` [User "unique_user" "unique@example.com" "password"]
|
|
|
|
it "finds users by email" $ do
|
|
addUser "another_user" "another@example.com" "password"
|
|
users <- findUsers "another@example.com"
|
|
users `shouldBe` [User "another_user" "another@example.com" "password"]
|
|
|
|
it "deletes a user" $ do
|
|
addUser "delete_user" "delete@example.com" "password"
|
|
deleteUser "delete_user"
|
|
users <- listUsers
|
|
users `shouldSatisfy` all (\user -> userName user /= "delete_user")
|
|
|
|
setupDB :: IO ()
|
|
setupDB = do
|
|
conn <- open "test_users.db"
|
|
execute_ conn "CREATE TABLE users (name TEXT PRIMARY KEY, email TEXT, password TEXT)"
|
|
close conn
|
|
|
|
cleanupDB :: IO ()
|
|
cleanupDB = removeFile "test_users.db"
|