-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathPostgresSpec.hs
More file actions
121 lines (102 loc) · 3.83 KB
/
Copy pathPostgresSpec.hs
File metadata and controls
121 lines (102 loc) · 3.83 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
module Service.EventStore.PostgresSpec where
import Core
import Service.EventStore.Postgres qualified as Postgres
import Service.EventStore.Postgres.Internal qualified as Internal
import Service.EventStore.Postgres.Internal.Core qualified as PostgresCore
import Service.EventStore.Postgres.Internal.Sessions qualified as Sessions
import Task qualified
import Test
import Test.Service.EntityFetcher qualified as EntityFetcher
import Test.Service.EntityFetcher.Core qualified as EntityFetcherCore
import Test.Service.EventStore qualified as EventStore
import Test.Service.EventStore.Core (BankAccountEvent)
import Var qualified
spec :: Spec Unit
spec = do
describe "PostgresEventStore" do
let config =
Postgres.Config
{ host = "localhost",
databaseName = "neohaskell",
user = "neohaskell",
password = "neohaskell",
port = 5432
}
describe "new method" do
it "acquires the connection" \_ -> do
(ops, observe) <- mockNewOps
Internal.new @BankAccountEvent ops config
|> Task.mapError toText
|> discard
observe.acquireCalls
|> varContents shouldBe 1
it "initializes the table" \_ -> do
(ops, observe) <- mockNewOps
Internal.new @BankAccountEvent ops config
|> Task.mapError toText
|> discard
observe.initializeTableCalls
|> varContents shouldBe 1
it "initializes the subscriptions" \_ -> do
(ops, observe) <- mockNewOps
Internal.new @BankAccountEvent ops config
|> Task.mapError toText
|> discard
observe.initializeSubscriptionsCalls
|> varContents shouldBe 1
let newStore = do
let ops = Internal.defaultOps @BankAccountEvent
dropPostgres ops config
Postgres.new config |> Task.mapError toText
EventStore.spec newStore
let newStoreAndFetcher = do
store <- newStore
fetcher <- EntityFetcherCore.newFetcher store |> Task.mapError toText
Task.yield (store, fetcher)
EntityFetcher.spec newStoreAndFetcher
data NewObserve = NewObserve
{ acquireCalls :: Var Int,
initializeTableCalls :: Var Int,
initializeSubscriptionsCalls :: Var Int
}
dropPostgres :: Internal.Ops eventType -> Postgres.Config -> Task Text Unit
dropPostgres ops config =
ops
|> Internal.withConnection
config
( \connection -> do
res <-
Sessions.dropEventsTableSession
|> Sessions.run connection
|> Task.asResult
case res of
Ok _ -> Task.yield unit
Err err ->
Task.throw (err |> PostgresCore.SessionError)
)
|> Task.mapError (toText)
mockNewOps :: Task Text (Internal.Ops eventType, NewObserve)
mockNewOps = do
acquireCalls <- Var.new 0
initializeTableCalls <- Var.new 0
initializeSubscriptionsCalls <- Var.new 0
releaseCalls <- Var.new (0 :: Int)
let acquire :: Internal.Config -> Task Text Internal.Connection
acquire _ = do
Var.increment acquireCalls
Task.yield Internal.MockConnection
let initializeTable :: Internal.Connection -> Task Text Unit
initializeTable _ = do
Var.increment initializeTableCalls
Task.yield unit
let initializeSubscriptions :: Internal.SubscriptionStore eventType -> Internal.Config -> Task Text Unit
initializeSubscriptions _ _ = do
Var.increment initializeSubscriptionsCalls
Task.yield unit
let release :: Internal.Connection -> Task Text Unit
release _ = do
Var.increment releaseCalls
Task.yield unit
let newObserver = NewObserve {acquireCalls, initializeTableCalls, initializeSubscriptionsCalls}
let ops = Internal.Ops {acquire, initializeTable, initializeSubscriptions, release}
Task.yield (ops, newObserver)