-
Notifications
You must be signed in to change notification settings - Fork 16
/
DomainSpec.hs
84 lines (65 loc) · 3.18 KB
/
DomainSpec.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module DomainSpec where
import Data.Time.Calendar
import Domain.ReservationDomain
import GHC.Natural (Natural (..), naturalFromInteger)
import Test.Hspec
import Test.QuickCheck
main :: IO ()
main = hspec spec
instance Arbitrary Natural where
arbitrary = do
NonNegative nonNegative <- arbitrary
return $ naturalFromInteger nonNegative
instance Arbitrary Reservation where
arbitrary = do
natural <- arbitrary
return $ Reservation day "Jupp0" "jupp@jupp.com" natural
day :: Day
day = fromGregorian 2020 2 29
res1 :: Reservation
res1 = Reservation day "Andrew M. Jones" "amjones@example.com" 4
res2 :: Reservation
res2 = Reservation day "Thomas Miller" "tm@example.com" 3
reservations :: [Reservation]
reservations = [res1, res2]
totalCapacity :: Natural
totalCapacity = 20
spec :: Spec
spec =
describe "Domain Logic" $ do
it "testing works with arbitrary reservation on a single day" $
property $ \res -> date res == day && name res == "Jupp0" && email res == "jupp@jupp.com"
it "computes the used capacity for an empty list of reservations" $
usedCapacity [] `shouldBe` 0
it "computes the used capacity for a list of reservations" $
property $ \reservations -> usedCapacity reservations `shouldBe` sum (map quantity reservations)
it "computes the available seats for a list of reservations" $
property $
\reservations maxCapacity ->
availableSeats maxCapacity reservations
`shouldBe` if maxCapacity >= usedCapacity reservations
then maxCapacity - usedCapacity reservations
else 0
it "can check if a reservation is possible on a given day" $
property $ \res reservationsOnDay maxCapacity ->
isReservationPossible res reservationsOnDay maxCapacity
`shouldBe` availableSeats maxCapacity reservationsOnDay >= quantity res
it "can check if a reservation is possible on a day with no bookings" $
property $ \res maxCapacity -> isReservationPossible res [] maxCapacity `shouldBe` quantity res <= maxCapacity
it "will accept all reservations up to available seats on a day with no bookings" $
property $ \reservation available -> isReservationPossible reservation [] available `shouldBe` quantity reservation <= available
it "detects if a reservation is not possible on a given day" $
isReservationPossible (Reservation day "name" "mail@mail.com" 15) reservations totalCapacity `shouldBe` False
it "can add a reservation to a list of reservations" $ do
property $ \quantity reservations ->
let res = Reservation day "x" "mail@mail.com" quantity
added = addReservation res reservations
in length added == 1 + length reservations
&& notElem res reservations
&& elem res added
&& usedCapacity added == usedCapacity reservations + quantity
it "can cancel a reservation" $ do
cancelReservation res1 reservations `shouldBe` [res2]
cancelReservation res1 [] `shouldBe` []
cancelReservation res2 reservations `shouldBe` [res1]
cancelReservation res2 (cancelReservation res1 reservations) `shouldBe` []