/
AppTest.scala
165 lines (135 loc) · 5.45 KB
/
AppTest.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.github.morotsman.investigate_finagle_service.candy_finch
import cats.effect.IO
import cats.effect.concurrent.Ref
import com.twitter.finagle.http.Status
import io.circe.generic.auto._
import io.finch._
import io.finch.circe._
import io.finch.internal.DummyExecutionContext
import org.scalacheck.{Arbitrary, Gen}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.scalacheck.Checkers._
class AppTest extends AnyFlatSpec with Matchers {
private case class AppState(id: Int, store: Map[Int, MachineState])
private case class TestApp(
id: Ref[IO, Int],
store: Ref[IO, Map[Int, MachineState]]
) extends App(id, store)(IO.contextShift(DummyExecutionContext)) {
def state: IO[AppState] = for {
i <- id.get
s <- store.get
} yield AppState(i, s)
}
private case class MachineWithoutId(locked: Boolean, candies: Int, coins: Int) {
def withId(id: Int): MachineState = MachineState(id, locked, candies, coins)
}
private val genMachineWithoutId = for {
locked <- Gen.oneOf(true, false)
candies <- Gen.choose(0, 3)
coins <- Gen.choose(0, 1000)
} yield MachineWithoutId(locked, candies, coins)
private def genTestApp: Gen[TestApp] =
Gen.listOf(genMachineWithoutId).map { machines =>
val id = machines.length
val store = machines.zipWithIndex.map { case (m, i) => i -> m.withId(i) }
TestApp(Ref.unsafe[IO, Int](id), Ref.unsafe[IO, Map[Int, MachineState]](store.toMap))
}
private implicit def arbitraryTodoWithoutId: Arbitrary[MachineWithoutId] = Arbitrary(genMachineWithoutId)
private implicit def arbitraryApp: Arbitrary[TestApp] = Arbitrary(genTestApp)
it should "create a machine" in {
check { (app: TestApp, machine: MachineWithoutId) =>
val input = Input.post("/machine").withBody[Application.Json](machine)
val shouldBeTrue: IO[Boolean] = for {
prev <- app.state
newMachine <- app.createMachine(input).output.get
next <- app.state
} yield prev.id + 1 == next.id &&
prev.store + (prev.id -> newMachine.value) == next.store &&
newMachine.value == machine.withId(prev.id)
shouldBeTrue.unsafeRunSync()
}
}
it should "give back the state of all the machines" in {
check { (app: TestApp) =>
val input = Input.get("/machine")
val shouldBeTrue = for {
prev <- app.state
machines <- app.getMachines(input).output.get
next <- app.state
} yield
stateUnChanged(prev, next) && machines.value == prev.store.values.toList.sortBy(_.id)
shouldBeTrue.unsafeRunSync()
}
}
private def stateUnChanged(prev: AppState, next: AppState): Boolean =
sameId(prev, next) && storeSame(prev, next)
private def sameId(prev: AppState, next: AppState): Boolean =
prev.id == next.id
private def storeSame(prev: AppState, next: AppState): Boolean =
prev.store == next.store
it should "accept coins" in {
check { (app: TestApp) =>
val id = 0
val input = Input.put(s"/machine/$id/coin")
val shouldBeTrue = for {
prev <- app.state
result <- app.insertCoin(input).output.get
next <- app.state
} yield result.status match {
case Status.NotFound =>
stateUnChanged(prev, next) && machineUnknown(id, prev)
case Status.BadRequest =>
stateUnChanged(prev, next) && machineInWrongState(id, prev, Coin)
case Status.Ok =>
isUnlocked(id, prev, next)
case _ => false
}
shouldBeTrue.unsafeRunSync()
}
}
def machineUnknown(id: Int, prev: AppState): Boolean =
prev.store.get(id).isEmpty
def machineInWrongState(id: Int, prev: AppState, command: Input): Boolean = command match {
case Turn =>
prev.store(id).locked || prev.store(id).candies <= 0
case Coin =>
!prev.store(id).locked || prev.store(id).candies <= 0
}
def isUnlocked(id: Int, prevState: AppState, nextState: AppState): Boolean = (for {
prev <- prevState.store.get(id)
next <- nextState.store.get(id)
if (prev.locked && !next.locked)
if (prev.candies > 0 && next.candies == prev.candies && next.coins == prev.coins + 1)
if sameId(prevState, nextState)
if (prevState.store.filter(kv => kv._1 != id) == nextState.store.filter(kv => kv._1 != id))
} yield true).getOrElse(false)
it should "turn" in {
check { (app: TestApp) =>
val id = 0
val input = Input.put(s"/machine/$id/turn")
val shouldBeTrue = for {
prev <- app.state
result <- app.turn(input).output.get
next <- app.state
} yield result.status match {
case Status.NotFound =>
stateUnChanged(prev, next) && machineUnknown(id, prev)
case Status.BadRequest =>
stateUnChanged(prev, next) && machineInWrongState(id, prev, Turn)
case Status.Ok =>
returnsCandy(id, prev, next)
case _ => false
}
shouldBeTrue.unsafeRunSync()
}
}
def returnsCandy(id: Int, prevState: AppState, nextState: AppState): Boolean = (for {
prev <- prevState.store.get(id)
next <- nextState.store.get(id)
if (!prev.locked && next.locked)
if (prev.candies - 1 == next.candies && prev.coins == next.coins)
if sameId(prevState, nextState)
if (prevState.store.filter(kv => kv._1 != id) == nextState.store.filter(kv => kv._1 != id))
} yield true).getOrElse(false)
}