-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example protocol unit testing (#49)
* Add BookingCommandProtocolSuite * Update documentation with mentions of protocol unit testing * Set compatibility to full Co-authored-by: Jonas Chapuis <[email protected]>
- Loading branch information
Showing
5 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
example/src/test/scala/endless/example/protocol/BookingCommandProtocolSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package endless.example.protocol | ||
|
||
import cats.Id | ||
import endless.\/ | ||
import endless.example.algebra.BookingAlg | ||
import endless.example.data.Booking | ||
import endless.example.logic.Generators | ||
import org.scalacheck.Prop.forAll | ||
import cats.syntax.functor._ | ||
|
||
//#example | ||
class BookingCommandProtocolSuite extends munit.ScalaCheckSuite with Generators { | ||
val protocol = new BookingCommandProtocol | ||
|
||
test("place booking") { | ||
forAll { (booking: Booking, reply: BookingAlg.BookingAlreadyExists \/ Unit) => | ||
val outgoingCommand = protocol.client.place( | ||
booking.id, | ||
booking.passengerCount, | ||
booking.origin, | ||
booking.destination | ||
) | ||
val incomingCommand = protocol.server[Id].decode(outgoingCommand.payload) | ||
val encodedReply = incomingCommand | ||
.runWith(new TestBookingAlg { | ||
override def place( | ||
bookingID: Booking.BookingID, | ||
passengerCount: Int, | ||
origin: Booking.LatLon, | ||
destination: Booking.LatLon | ||
): Id[BookingAlg.BookingAlreadyExists \/ Unit] = reply | ||
}) | ||
.map(incomingCommand.replyEncoder.encode(_)) | ||
assertEquals(outgoingCommand.replyDecoder.decode(encodedReply), reply) | ||
} | ||
} | ||
//#example | ||
|
||
test("get booking") { | ||
forAll { (reply: BookingAlg.BookingUnknown.type \/ Booking) => | ||
val outgoingCommand = protocol.client.get | ||
val incomingCommand = protocol.server[Id].decode(outgoingCommand.payload) | ||
val encodedReply = incomingCommand | ||
.runWith(new TestBookingAlg { | ||
override def get: Id[BookingAlg.BookingUnknown.type \/ Booking] = reply | ||
}) | ||
.map(incomingCommand.replyEncoder.encode(_)) | ||
assertEquals(outgoingCommand.replyDecoder.decode(encodedReply), reply) | ||
} | ||
} | ||
|
||
test("change origin") { | ||
forAll { (newOrigin: Booking.LatLon, reply: BookingAlg.BookingUnknown.type \/ Unit) => | ||
val outgoingCommand = protocol.client.changeOrigin(newOrigin) | ||
val incomingCommand = protocol.server[Id].decode(outgoingCommand.payload) | ||
val encodedReply = incomingCommand | ||
.runWith(new TestBookingAlg { | ||
override def changeOrigin( | ||
origin: Booking.LatLon | ||
): Id[BookingAlg.BookingUnknown.type \/ Unit] = reply | ||
}) | ||
.map(incomingCommand.replyEncoder.encode(_)) | ||
assertEquals(outgoingCommand.replyDecoder.decode(encodedReply), reply) | ||
} | ||
} | ||
|
||
test("change destination") { | ||
forAll { (newDestination: Booking.LatLon, reply: BookingAlg.BookingUnknown.type \/ Unit) => | ||
val outgoingCommand = protocol.client.changeDestination(newDestination) | ||
val incomingCommand = protocol.server[Id].decode(outgoingCommand.payload) | ||
val encodedReply = incomingCommand | ||
.runWith(new TestBookingAlg { | ||
override def changeDestination( | ||
destination: Booking.LatLon | ||
): Id[BookingAlg.BookingUnknown.type \/ Unit] = reply | ||
}) | ||
.map(incomingCommand.replyEncoder.encode(_)) | ||
assertEquals(outgoingCommand.replyDecoder.decode(encodedReply), reply) | ||
} | ||
} | ||
|
||
test("change origin and destination") { | ||
forAll { | ||
( | ||
newOrigin: Booking.LatLon, | ||
newDestination: Booking.LatLon, | ||
reply: BookingAlg.BookingUnknown.type \/ Unit | ||
) => | ||
val outgoingCommand = protocol.client.changeOriginAndDestination(newOrigin, newDestination) | ||
val incomingCommand = protocol.server[Id].decode(outgoingCommand.payload) | ||
val encodedReply = incomingCommand | ||
.runWith(new TestBookingAlg { | ||
override def changeOriginAndDestination( | ||
origin: Booking.LatLon, | ||
destination: Booking.LatLon | ||
): Id[BookingAlg.BookingUnknown.type \/ Unit] = reply | ||
}) | ||
.map(incomingCommand.replyEncoder.encode(_)) | ||
assertEquals(outgoingCommand.replyDecoder.decode(encodedReply), reply) | ||
} | ||
} | ||
|
||
test("cancel") { | ||
forAll { (reply: BookingAlg.BookingUnknown.type \/ Unit) => | ||
val outgoingCommand = protocol.client.cancel | ||
val incomingCommand = protocol.server[Id].decode(outgoingCommand.payload) | ||
val encodedReply = incomingCommand | ||
.runWith(new TestBookingAlg { | ||
override def cancel: Id[BookingAlg.BookingUnknown.type \/ Unit] = reply | ||
}) | ||
.map(incomingCommand.replyEncoder.encode(_)) | ||
assertEquals(outgoingCommand.replyDecoder.decode(encodedReply), reply) | ||
} | ||
} | ||
|
||
trait TestBookingAlg extends BookingAlg[Id] { | ||
def place( | ||
bookingID: Booking.BookingID, | ||
passengerCount: Int, | ||
origin: Booking.LatLon, | ||
destination: Booking.LatLon | ||
): Id[BookingAlg.BookingAlreadyExists \/ Unit] = throw new RuntimeException( | ||
"not supposed to be called" | ||
) | ||
def get: Id[BookingAlg.BookingUnknown.type \/ Booking] = throw new RuntimeException( | ||
"not supposed to be called" | ||
) | ||
def changeOrigin(newOrigin: Booking.LatLon): Id[BookingAlg.BookingUnknown.type \/ Unit] = | ||
throw new RuntimeException("not supposed to be called") | ||
def changeDestination( | ||
newDestination: Booking.LatLon | ||
): Id[BookingAlg.BookingUnknown.type \/ Unit] = throw new RuntimeException( | ||
"not supposed to be called" | ||
) | ||
def changeOriginAndDestination( | ||
newOrigin: Booking.LatLon, | ||
newDestination: Booking.LatLon | ||
): Id[BookingAlg.BookingUnknown.type \/ Unit] = throw new RuntimeException( | ||
"not supposed to be called" | ||
) | ||
def cancel: Id[BookingAlg.BookingUnknown.type \/ Unit] = throw new RuntimeException( | ||
"not supposed to be called" | ||
) | ||
} | ||
} |