-
Notifications
You must be signed in to change notification settings - Fork 20
/
LiveSubscriptionLogicSpec.scala
151 lines (145 loc) · 5.66 KB
/
LiveSubscriptionLogicSpec.scala
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package io.pager
package subscription
import io.pager.Generators._
import io.pager.client.telegram.ChatId
import io.pager.logging.Logger
import io.pager.subscription.Repository.{ Name, Version }
import zio._
import zio.test.Assertion._
import zio.test._
import TestData._
import io.pager.TestCases.TestScenarios
object LiveSubscriptionLogicSpec extends DefaultRunnableSpec(LiveSubscriptionLogicTestCases.suite)
object LiveSubscriptionLogicTestCases extends TestCases {
val specName: String = "LiveSubscriptionLogicSpec"
type RepositoryMap = UIO[Ref[Map[Name, Option[Version]]]]
type SubscriptionMap = UIO[Ref[Map[ChatId, Set[Repository.Name]]]]
private def service(
subscriptionMap: SubscriptionMap = emptyMap[ChatId, Set[Name]],
repositoryMap: RepositoryMap = emptyMap[Name, Option[Version]]
): UIO[SubscriptionLogic.Service[Any]] =
for {
repositoryMap <- repositoryMap
subscriptionMap <- subscriptionMap
} yield {
val chatStorage = ChatStorage.Test.make(subscriptionMap)
val repositoryVersionStorage = RepositoryVersionStorage.Test.make(repositoryMap)
SubscriptionLogic
.Live
.make(
logger = Logger.Test,
chatStorageService = chatStorage,
repositoryVersionStorageService = repositoryVersionStorage
)
}
val scenarios: TestScenarios = List(
testM("return empty subscriptions") {
checkM(repositoryName, chatId) {
case (name, chatId) =>
for {
service <- service()
subscriptions <- service.listSubscriptions(chatId)
subscribers <- service.listSubscribers(name)
} yield {
assert(subscriptions, isEmpty) &&
assert(subscribers, isEmpty)
}
}
},
testM("successfully subscribe to a repository") {
checkM(repositoryName, chatId) {
case (name, chatId) =>
for {
service <- service()
_ <- service.subscribe(chatId, name)
repositories <- service.listRepositories
subscriptions <- service.listSubscriptions(chatId)
subscribers <- service.listSubscribers(name)
} yield {
assert(repositories, equalTo(Map(name -> None))) &&
assert(subscriptions, equalTo(Set(name))) &&
assert(subscribers, equalTo(Set(chatId)))
}
}
},
testM("successfully subscribe to a repository twice") {
checkM(repositoryName, chatId) {
case (name, chatId) =>
for {
service <- service(mkMap(Map(chatId -> Set(name))))
_ <- service.subscribe(chatId, name)
repositories <- service.listRepositories
subscriptions <- service.listSubscriptions(chatId)
subscribers <- service.listSubscribers(name)
} yield {
assert(repositories, equalTo(Map(name -> None))) &&
assert(subscriptions, equalTo(Set(name))) &&
assert(subscribers, equalTo(Set(chatId)))
}
}
},
testM("successfully subscribe to a repository from two chats") {
checkM(repositoryName, chatIds) {
case (name, (chatId1, chatId2)) =>
for {
service <- service()
_ <- service.subscribe(chatId1, name)
_ <- service.subscribe(chatId2, name)
repositories <- service.listRepositories
subscriptions1 <- service.listSubscriptions(chatId1)
subscriptions2 <- service.listSubscriptions(chatId2)
subscribers <- service.listSubscribers(name)
} yield {
assert(repositories, equalTo(Map(name -> None))) &&
assert(subscriptions1, equalTo(Set(name))) &&
assert(subscriptions2, equalTo(Set(name))) &&
assert(subscribers, equalTo(Set(chatId1, chatId2)))
}
}
},
testM("successfully unsubscribe from non-subscribed repository") {
checkM(repositoryName, chatId) { (name, chatId) =>
for {
service <- service()
_ <- service.unsubscribe(chatId, name)
repositories <- service.listRepositories
subscriptions <- service.listSubscriptions(chatId)
subscribers <- service.listSubscribers(name)
} yield {
assert(repositories, isEmpty) &&
assert(subscriptions, isEmpty) &&
assert(subscribers, isEmpty)
}
}
},
testM("successfully unsubscribe from subscribed repository") {
checkM(repositoryName, chatId) { (name, chatId) =>
for {
service <- service(mkMap(Map(chatId -> Set(name))))
_ <- service.unsubscribe(chatId, name)
repositories <- service.listRepositories
subscriptions <- service.listSubscriptions(chatId)
subscribers <- service.listSubscribers(name)
} yield {
assert(repositories, isEmpty) &&
assert(subscriptions, isEmpty) &&
assert(subscribers, isEmpty)
}
}
},
testM("update repository version") {
checkM(repositoryName) { name =>
for {
service <- service(repositoryMap = mkMap(Map(name -> None)))
_ <- service.updateVersions(Map(name -> rcVersion))
repositories1 <- service.listRepositories
_ <- service.updateVersions(Map(name -> finalVersion))
repositories2 <- service.listRepositories
} yield {
assert(repositories1, equalTo(Map(name -> Some(rcVersion)))) &&
assert(repositories2, equalTo(Map(name -> Some(finalVersion))))
}
}
}
)
}