Browse Source

"Update testcase" for play-scala

Skamander 12 years ago
parent
commit
4b88561872

+ 22 - 0
play-scala/app/controllers/Application.scala

@@ -68,4 +68,26 @@ object Application extends Controller {
       }
     }
   }
+
+  def update(queries: Int) = PredicatedAction(isDbAvailable, ServiceUnavailable) {
+    Action {
+      Async {
+        val random = ThreadLocalRandom.current()
+
+        val worlds = Future.sequence((for {
+          _ <- 1 to queries
+        } yield Future {
+            val world = World.findById(random.nextInt(TestDatabaseRows) + 1)
+            val updatedWorld = world.copy(randomNumber = random.nextInt(TestDatabaseRows) + 1)
+            World.updateRandom(updatedWorld)
+            updatedWorld
+          }(dbEc)
+        ).toList)
+
+        worlds.map {
+          w => Ok(Json.toJson(w))
+        }
+      }
+    }
+  }
 }

+ 27 - 18
play-scala/app/models/World.scala

@@ -13,33 +13,42 @@ object World {
     /**
     * Convert a World to Json object
     */
-    implicit val toJson = new Writes[World] {
-        def writes(w: World): JsValue = {
-            Json.obj(
-                "id" -> w.id.get,
-                "randomNumber" -> w.randomNumber
-            )
-        }
+  implicit val toJson = new Writes[World] {
+    def writes(w: World): JsValue = {
+      Json.obj(
+        "id" -> w.id.get,
+        "randomNumber" -> w.randomNumber
+      )
     }
+  }
 
   /**
    * Parse a World from a ResultSet
    */
-    val simpleRowParser = {
-        get[Pk[Long]]("world.id") ~
-        get[Long]("world.randomNumber") map {
-            case id~randomNumber => World(id, randomNumber)
-        }
+  val simpleRowParser = {
+    get[Pk[Long]]("world.id") ~
+    get[Long]("world.randomNumber") map {
+      case id~randomNumber => World(id, randomNumber)
     }
+  }
 
   /**
    * Retrieve a World by id.
    */
-    def findById(id: Long): World = {
-        DB.withConnection { implicit connection =>
-            SQL("SELECT * FROM World WHERE id = {id}").on(
-                "id" -> id
-            ).as(World.simpleRowParser.single)
-        }
+  def findById(id: Long): World = {
+    DB.withConnection { implicit connection =>
+      SQL("SELECT * FROM World WHERE id = {id}").on(
+          "id" -> id
+      ).as(World.simpleRowParser.single)
     }
+  }
+
+  def updateRandom(world: World) {
+    DB.withConnection { implicit connection =>
+      SQL("UPDATE World SET randomNumber = {randomNumber} WHERE id = {id}").on(
+        "id" -> world.id.get,
+        "randomNumber" -> world.randomNumber
+      ).executeUpdate()
+    }
+  }
 }

+ 1 - 0
play-scala/conf/routes

@@ -6,6 +6,7 @@
 GET     /json                           controllers.Application.json
 GET     /db                             controllers.Application.db(queries: Int ?= 1)
 GET     /fortunes                       controllers.Application.fortunes
+GET     /update                         controllers.Application.update(queries: Int ?= 1)
 
 # Map static resources from the /public folder to the /assets URL path
 GET     /assets/*file                   controllers.Assets.at(path="/public", file)