Guest User

Untitled

a guest
Jan 15th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. case class Player(id: UUID, name: String, email: String)
  2.  
  3. class PlayerSpec extends Specification {
  4. "Player" should {
  5.  
  6. val application = FakeApplication(additionalConfiguration = inMemoryDatabase("test"))
  7.  
  8. "insert a player" in {
  9. running(application) {
  10.  
  11. val uuid = UUID.randomUUID()
  12.  
  13. Player.insert(Player(uuid, "Joe Person", "joe.player@someplace.mail")) must beEqualTo(1)
  14. }
  15. }
  16.  
  17. "Find a player by id" in {
  18. running(application) {
  19. val uuid = UUID.randomUUID()
  20.  
  21. Player.insert(Player(uuid, "Jane Person", "jane.player@someplace.mail"))
  22.  
  23. Player.findById(uuid) must beEqualTo(Some(Player(uuid, "Jane Person", "jane.player@someplace.mail")))
  24. }
  25. }
  26.  
  27. }
  28. }
  29.  
  30. [info] PlayerSpec
  31. [info]
  32. [info] Player should
  33. [info] + insert a player
  34. [error] ! Find a player by id
  35. [error] anon$1: Configuration error [Cannot connect to database [default]] Configuration.scala:258)
  36. ....
  37.  
  38. case class Player(id: UUID, name: String, email: String)
  39.  
  40. object Player {
  41.  
  42. private val fullPlayer = {
  43. get[UUID]("player.id")(Utils.rowToUuid) ~
  44. get[String]("player.name") ~
  45. get[String]("player.email") map {
  46. case id~name~email => Player(id, name, email)
  47. }
  48. }
  49.  
  50. def insert(player: Player) = {
  51. DB.withConnection { implicit c =>
  52.  
  53. SQL(
  54. """
  55. insert into player (id, name, email) values (
  56. {id}, {name}, {email}
  57. )
  58. """
  59. ).on(
  60. 'id -> player.id.toString,
  61. 'name -> player.name,
  62. 'email -> player.email
  63. ).executeUpdate()
  64.  
  65. }
  66. }
  67.  
  68. def findById(id: UUID) : Option[Player] = {
  69. DB.withConnection { implicit c =>
  70. SQL("select * from player where id = {id}").on('id -> id).as(fullPlayer.singleOpt)
  71. }
  72. }
  73. }
  74.  
  75. object Utils {
  76. implicit def rowToUuid: Column[UUID] = Column.nonNull {
  77. (value, meta) =>
  78. val MetaDataItem(qualified, nullable, clazz) = meta
  79. value match {
  80. case uuid: UUID => Right(uuid)
  81. case _ => Left(TypeDoesNotMatch("Cannot convert " + value + ":" + value.asInstanceOf[AnyRef].getClass + " to UUID for column " + qualified))
  82. }
  83. }
  84. }
  85.  
  86. val application = FakeApplication(additionalConfiguration = inMemoryDatabase("test"))
  87.  
  88. def application() = FakeApplication(additionalConfiguration = inMemoryDatabase("test"))
Add Comment
Please, Sign In to add comment