Skip to content

Instantly share code, notes, and snippets.

@takashima0411
Last active December 18, 2015 23:08
Show Gist options
  • Save takashima0411/5859103 to your computer and use it in GitHub Desktop.
Save takashima0411/5859103 to your computer and use it in GitHub Desktop.
Playframeword2 sample zentasksをscalikeJDBC使用に変更
package models
import play.api.db._
import play.api.Play.current
import anorm._
import anorm.SqlParser._
case class User(email: String, name: String, password: String)
import scalikejdbc._
import scalikejdbc.SQLInterpolation._
object User extends SQLSyntaxSupport[User] {
// -- Parsers
/**
* Parse a User from a ResultSet
*/
val simple = {
get[String]("user.email") ~
get[String]("user.name") ~
get[String]("user.password") map {
case email ~ name ~ password => User(email, name, password)
}
}
implicit val session = AutoSession
override val tableName = "user"
val u = User.syntax("u")
def apply(a: SyntaxProvider[User])(rs: WrappedResultSet): User = apply(a.resultName)(rs)
def apply(a: ResultName[User])(rs: WrappedResultSet): User =
new User(rs.string(u.resultName.email), rs.string(u.resultName.name), rs.string(u.resultName.password))
// -- Queries
/**
* Retrieve a User from email.
*/
def findByEmail(email: String): Option[User] = withSQL {
select
.from(User as u)
.where.eq(u.email, email)
}.map(User(u)).single.apply
/**
* Retrieve all users.
*/
def findAll(): Seq[User] = withSQL {
select.from(User as u)
}.map(User(u)).list().apply()
/**
* Authenticate a User.
*/
def authenticate(email: String, password: String) = withSQL {
select
.from(User as u)
.where.eq(u.email, email)
.and.eq(u.password, password)
}.map(User(u)).single.apply
/**
* Create a User.
*/
def create(user: User) = {
withSQL {
insert.into(User).values(user.email, user.name, user.password)
}.update.apply()
user
}
}
package models
import org.specs2.mutable._
import play.api.test._
import play.api.test.Helpers._
class UserSpec extends Specification {
"全ユーザーの取得" should {
"追加したユーザーを取得できる" in running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
User.create(User("[email protected]", "alice", "secret"))
User.create(User("[email protected]", "bob", "secret"))
User.create(User("[email protected]", "chris", "secret"))
val users = User.findAll()
users.length must equalTo(3)
}
"ユーザーを追加してない場合空リストが返る" in running (FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val users = User.findAll()
users.length must equalTo(0)
}
}
"ユーザーをメールアドレスから検索" should {
"追加したユーザーを取得できる" in running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val email = "[email protected]"
val name = "hogehoge"
User.create(User(email, name, "secret"))
val user = User.findByEmail(email).get
user.name must equalTo("hogehoge")
}
"追加していないユーザーは取得出来ない" in running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val user = User.findByEmail("[email protected]")
user must equalTo(None)
}
}
"ユーザーの認証を行う" should {
"追加したユーザーで認証が通る" in running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val email = "[email protected]"
val name = "hogehoge"
val pass = "secret"
User.create(User(email, name, pass))
val user = User.authenticate(email, pass)
user must equalTo(Some(User(email, name, pass)))
}
"追加したユーザーでパスワードが間違っていると認証を通らない" in running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val email = "[email protected]"
val name = "hogehoge"
val pass = "secret"
User.create(User(email, name, pass))
val user = User.authenticate(email, "another password")
user must equalTo(None)
}
"追加していないユーザーで認証を通らない" in running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val email = "[email protected]"
val name = "hogehoge"
val pass = "secret"
val user = User.authenticate(email, pass)
user must equalTo(None)
}
}
}
@takashima0411
Copy link
Author

applyでの列名呼び出しを文字列からメソッドに変更
anorm用メソッドの削除

@takashima0411
Copy link
Author

テストの追加

@takashima0411
Copy link
Author

不要コメントの削除

@seratch
Copy link

seratch commented Jun 26, 2013

zentasks はここにサンプルありますよ。

https://github.com/seratch/scalikejdbc/tree/develop/scalikejdbc-play-plugin/test/zentasks

あと ScalikeJDBC のテストは model 部分だけは Play2 非依存でテストできるので、こちらを参考にしてみてください。

https://github.com/seratch/devteam-app/blob/master/src/test/scala/devteam/model/CompanySpec.scala

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment