http://stackoverflow.com/questions/12246686/play-2-0-fakeapplication-setup-with-test-configuration とか
Last active
October 11, 2015 04:58
-
-
Save daneko/3806837 to your computer and use it in GitHub Desktop.
sample * unittest for playframework 2.0.3 + salat(mongodb) * mapReduce casbah
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
| /** | |
| * ほぼjsで書いているのようなものなので、jsで書いて動かしてから移植したほうが楽かも | |
| */ | |
| /** | |
| * type JSFunction = String となっているので、ほんとに文字列 | |
| * mapReduceを書く場合の、map部分をjsで書けばOK | |
| */ | |
| val mapF: String => JSFunction = { category => | |
| """ | |
| |function(){ | |
| | | |
| | var key = { | |
| | hoge: this.hogeId, | |
| | } | |
| | | |
| | emit( | |
| | key, | |
| | { | |
| | hogeCount: this.hogeCount | |
| | } | |
| | ); | |
| |} | |
| """.stripMargin | |
| } | |
| /** | |
| * mapReduceを書く場合の、reduce部分をjsで書けばOK | |
| */ | |
| val reduceF: JSFunction = { | |
| """ | |
| |function(key, values){ | |
| | | |
| | var result = { | |
| | hogeCount: 0 | |
| | } | |
| | | |
| | values.forEach(function(value){ | |
| | result.hogeCount += value.hogeCount; | |
| | }) | |
| | | |
| | return result; | |
| |} | |
| """.stripMargin | |
| } | |
| /** | |
| * MongoCollectionの定義がこんなの | |
| * | |
| * {{{ | |
| * val MapReduceInlineOutput = com.mongodb.casbah.map_reduce.MapReduceInlineOutput | |
| * val MapReduceMergeOutput = com.mongodb.casbah.map_reduce.MapReduceMergeOutput | |
| * val MapReduceReduceOutput = com.mongodb.casbah.map_reduce.MapReduceReduceOutput | |
| * }}} | |
| * | |
| * {{{ | |
| * def mapReduce(mapFunction: JSFunction, | |
| * reduceFunction: JSFunction, | |
| * output: MapReduceOutputTarget, | |
| * query: Option[DBObject] = None, | |
| * sort: Option[DBObject] = None, | |
| * limit: Option[Int] = None, | |
| * finalizeFunction: Option[JSFunction] = None, | |
| * jsScope: Option[String] = None, | |
| * verbose: Boolean = false) | |
| * }}} | |
| * | |
| * or | |
| * | |
| * {{{ | |
| * def mapReduce(cmd: map_reduce.MapReduceCommand) | |
| * }}} | |
| */ | |
| collection.mapReduce(mapF, reduceF, MapReduceInlineOutput).map(res => grater[ResultMapReduce].asObject(res)) | |
| /** | |
| * case classを複数に分けておけば_id/valueが複雑になってもそれなりに楽に対応できるんじゃね | |
| */ | |
| case class ResultMapReduce( | |
| @Key("_id") mapReduceId:ReduceResultId, | |
| @Key("value") mapReduceValue:ReduceResultValue | |
| ) | |
| case class ReduceResultId( | |
| hoge:... | |
| ) | |
| case class ReduceResultValue( | |
| hogeCount:Int | |
| ) |
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 com.novus.salat.dao._ | |
| import com.novus.salat.annotations._ | |
| import com.mongodb.casbah.Imports._ | |
| import com.novus.salat._ | |
| import com.mongodb.casbah.commons.conversions.scala._ | |
| import play.api.Play | |
| import play.api.Play.current | |
| package object mongoContext { | |
| RegisterConversionHelpers() | |
| RegisterJodaTimeConversionHelpers() | |
| implicit val context = { | |
| val context = new Context { | |
| val name = "global" | |
| override val typeHintStrategy = StringTypeHintStrategy(when = TypeHintFrequency.WhenNecessary, typeHint = "_t") | |
| } | |
| context.registerGlobalKeyOverride(remapThis = "id", toThisInstead = "_id") | |
| context.registerClassLoader(Play.classloader) | |
| context | |
| } | |
| } |
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.Play.current | |
| import com.novus.salat._ | |
| import com.novus.salat.dao._ | |
| import com.mongodb.casbah.Imports._ | |
| import se.radley.plugin.salat._ | |
| import salatcontext._ | |
| case class Task( | |
| id: ObjectId = new ObjectId, | |
| label: String) | |
| object Task extends ModelCompanion[Task, ObjectId] { | |
| val collection = mongoCollection("tasks") | |
| val dao = new SalatDAO[Task, ObjectId](collection = collection) {} | |
| def all = findAll.toList | |
| def create(label:String) = insert(Task(label = label)) | |
| def delete(id:ObjectId) = removeById(id) | |
| } |
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.test._ | |
| import play.api.test.Helpers._ | |
| import org.specs2.mutable._ | |
| import org.specs2.specification.Scope | |
| import org.specs2.runner.JUnitRunner | |
| import org.junit.runner.RunWith | |
| import com.mongodb.casbah.MongoConnection | |
| @RunWith(classOf[JUnitRunner]) | |
| class TaskSpecs2 extends Specification { | |
| sequential | |
| "using task model" should { | |
| "get all task" in context { | |
| running(context.fakeApp) { | |
| Task.all.size must_== 0 | |
| Task.create("hoge") | |
| Task.create("fuga") | |
| Task.all.size must_== 2 | |
| } | |
| } | |
| } | |
| object context extends BeforeAfter { | |
| lazy val dummy_db_name = "test_app_db" | |
| lazy val fakeApp = FakeApplication(additionalConfiguration = Map("mongodb.default.db" -> dummy_db_name)) | |
| def before = { | |
| implicit val mongoDB = MongoConnection()(dummy_db_name) | |
| mongoDB.dropDatabase() | |
| } | |
| def after = () | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment