Last active
December 18, 2015 23:08
-
-
Save takashima0411/5859103 to your computer and use it in GitHub Desktop.
Playframeword2 sample zentasksをscalikeJDBC使用に変更
This file contains hidden or 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
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 | |
} | |
} |
This file contains hidden or 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
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) | |
} | |
} | |
} |
テストの追加
不要コメントの削除
zentasks はここにサンプルありますよ。
https://github.com/seratch/scalikejdbc/tree/develop/scalikejdbc-play-plugin/test/zentasks
あと ScalikeJDBC のテストは model 部分だけは Play2 非依存でテストできるので、こちらを参考にしてみてください。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
applyでの列名呼び出しを文字列からメソッドに変更
anorm用メソッドの削除