forked from tjweir/LiftProject.g8
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMongoTestKit.scala
More file actions
85 lines (67 loc) · 2.04 KB
/
Copy pathMongoTestKit.scala
File metadata and controls
85 lines (67 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package $package$
import org.scalatest._
import net.liftweb._
import common._
import http._
import mongodb._
import util._
import util.Helpers.randomString
import com.mongodb._
// The sole mongo object for testing
object TestMongo {
val mongo = {
val uri = Props.get("mongo.default.uri", "$mongo_host$:$mongo_port$")
new MongoClient(new MongoClientURI(s"mongodb://\$uri"))
}
}
/**
* Creates a `ConnectionIdentifier` and database named after the class.
* Therefore, each Suite class shares the same database.
* Database is dropped after all tests have been run in the suite.
*/
trait MongoBeforeAndAfterAll extends BeforeAndAfterAll {
this: Suite =>
lazy val dbName = s"$name;format="norm"$_test_\${this.getClass.getName}"
.replace(".", "_")
.toLowerCase
def debug = false // db won't be dropped if this is true
lazy val Identifier = new ConnectionIdentifier {
val jndiName = dbName
}
override def beforeAll() {
// define the db
MongoDB.defineDb(Identifier, TestMongo.mongo, dbName)
}
override def afterAll() {
if (!debug) { dropDb() }
}
protected def dropDb(): Unit = MongoDB.use(Identifier) { db => db.dropDatabase }
}
/**
* Basic Mongo suite for running Mongo tests.
*/
trait MongoSuite extends TestSuiteMixin with MongoBeforeAndAfterAll {
this: TestSuite =>
def mongoIdentifier: StackableMaker[ConnectionIdentifier]
abstract override def withFixture(test: NoArgTest): Outcome = {
mongoIdentifier.doWith(Identifier) {
super.withFixture(test)
}
}
}
/**
* Mongo suite running within a LiftSession.
*/
trait MongoSessionSuite extends TestSuiteMixin with MongoBeforeAndAfterAll {
this: TestSuite =>
def mongoIdentifier: StackableMaker[ConnectionIdentifier]
// Override with `val` to share session amongst tests.
protected def session = new LiftSession("", randomString(20), Empty)
abstract override def withFixture(test: NoArgTest): Outcome = {
S.initIfUninitted(session) {
mongoIdentifier.doWith(Identifier) {
super.withFixture(test)
}
}
}
}