Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: scala
scala:
- "2.10.4"
- "2.11.1"
- "2.11.7"

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ You can change thow default values:

Modify `project/Build.scala` and add

`"com.github.athieriot" %% "specs2-embedmongo" % "0.7.0"`
`"com.github.athieriot" %% "specs2-embedmongo" % "0.8.1"`


[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/athieriot/specs2-embedmongo/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
Expand Down
24 changes: 9 additions & 15 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import sbt.Keys._

object ProjectBuild extends Build {

lazy val buildVersion = "0.7.0"
lazy val buildVersion = "0.8.1"

lazy val root = Project(id = "specs2-embedmongo", base = file("."), settings = Project.defaultSettings).settings(
organization := "com.github.athieriot",
description := "Specs2 helper to configure a EmbedMongo based instance",
version := buildVersion,
scalaVersion := "2.10.4",
crossScalaVersions := Seq("2.10.4", "2.11.1"),
scalaVersion := "2.11.7",
crossScalaVersions := Seq("2.10.4", "2.11.7"),

resolvers += "Typesafe Releases" at "http://repo.typesafe.com/typesafe/releases/",
resolvers += "Typesafe Snapshots" at "http://repo.typesafe.com/typesafe/snapshots/",
Expand All @@ -19,11 +19,10 @@ object ProjectBuild extends Build {
resolvers += "Sonatype releases" at "https://oss.sonatype.org/content/repositories/releases/",

libraryDependencies <++= scalaVersion(sv => Seq(
"de.flapdoodle.embed" % "de.flapdoodle.embed.mongo" % "1.46.0",
"org.specs2" %% "specs2" % "2.3.12",
"org.mongodb" %% "casbah-core" % "2.7.2" % "provided",
"com.novus" %% "salat-core" % "1.9.8" % "test",
"junit" % "junit" % "4.11" % "test"
"de.svenkubiak" % "embedded-mongodb" % "4.2.9",
"org.specs2" %% "specs2-core" % "3.8.2" % "provided",
"org.reactivemongo" %% "reactivemongo" % "0.11.8" % "provided",
"junit" % "junit" % "4.12" % "test"
)),

parallelExecution in Test := false,
Expand Down Expand Up @@ -52,12 +51,7 @@ object ProjectBuild extends Build {
</developer>
</developers>
),
publishTo <<= version { version: String =>
val nexus = "https://oss.sonatype.org/"
if (version.trim.endsWith("SNAPSHOT"))
Some("snapshots" at nexus + "content/repositories/snapshots")
else
Some("releases" at nexus + "service/local/staging/deploy/maven2")
}
publishTo := Some("cody" at "http://cody:8082/nexus/content/repositories/releases"),
credentials += Credentials(Path.userHome / ".sbt" / "sonatype.credentials")
)
}
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=0.13.0
sbt.version=0.13.8
22 changes: 15 additions & 7 deletions src/main/scala/com/github/athieriot/CleanAfterExample.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package com.github.athieriot

import scala.collection.JavaConversions._
import com.mongodb.{ MongoClient, ServerAddress }
import org.specs2.specification.AfterExample
import org.specs2.specification.AfterEach
import reactivemongo.api.MongoDriver

trait CleanAfterExample extends AfterExample {
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

trait CleanAfterExample extends AfterEach {
self: EmbedConnection =>

lazy val mongoClient = new MongoClient(new ServerAddress(network.getServerAddress(), network.getPort()));
def getConn = {
val driver = new MongoDriver
val db = driver.connection(List(s"127.0.0.1:${network.getPort}"))("testdb")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling "apply" is deprecated, better use "database" method.
Hardcoding the name "testdb" is creating a restriction on database name. It seems like there's no way of getting database names in ReactiveMongo, I couldn't find any API through brief googling. Maybe we should use MongoClient here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReactiveMongo gives an opportunity to run a custom command, so there is a way to get a list of all databases. I've made a pull request with an implementation of such a command.

Await.ready(db.connection.waitForPrimary(10 seconds), 11 seconds)
db
}

def after() {
mongoClient.getDatabaseNames().map { mongoClient.getDB(_) }.foreach { _.dropDatabase() }
def after = {
Await.ready(getConn.drop(), 1 second)
}

}
29 changes: 15 additions & 14 deletions src/main/scala/com/github/athieriot/EmbedConnection.scala
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package com.github.athieriot

import de.flapdoodle.embed._
import process.runtime.Network
import mongo._
import config._
import distribution._
import org.specs2.specification._
import org.specs2.mutable.SpecificationLike
import de.flapdoodle.embed.mongo._
import de.flapdoodle.embed.mongo.config._
import de.flapdoodle.embed.mongo.distribution._
import de.flapdoodle.embed.process.runtime.Network
import org.specs2.main.Arguments
import org.specs2.mutable.{Specification, SpecificationLike}
import org.specs2.specification.core.{Fragment, Fragments}

trait EmbedConnection extends FragmentsBuilder {
trait EmbedConnection extends Specification {
self: SpecificationLike =>
isolated

def sequentialyIsolated: Arguments = args(isolated = true, sequential = true)

override def sequential: Arguments = args(isolated = false, sequential = true)

override def isolated: Arguments = args(isolated = true, sequential = false)

//Override this method to personalize testing port
def embedConnectionPort(): Int = { 12345 }
def embedConnectionPort: Int = 12345

//Override this method to personalize MongoDB version
def embedMongoDBVersion(): Version.Main = { Version.Main.PRODUCTION }
def embedMongoDBVersion: Version.Main = Version.Main.PRODUCTION

lazy val network = new Net(embedConnectionPort, Network.localhostIsIPv6)

Expand All @@ -36,11 +37,11 @@ trait EmbedConnection extends FragmentsBuilder {

override def map(fs: => Fragments) = startMongo ^ fs ^ stoptMongo

private def startMongo() = {
Example("Start Mongo", { mongodExecutable.start; success })
private def startMongo = {
step(mongodExecutable.start)
}

private def stoptMongo() = {
Example("Stop Mongo", { mongodExecutable.stop; success })
private def stoptMongo = {
step(mongodExecutable.stop())
}
}
37 changes: 23 additions & 14 deletions src/test/scala/com/github/athieriot/CleanAfterExampleSpec.scala
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
package com.github.athieriot

import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner
import org.specs2.mutable.Specification
import reactivemongo.bson.BSONDocument

@RunWith(classOf[JUnitRunner])
class CleanAfterExampleSpec extends Specification with EmbedConnection with CleanAfterExample {
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

class CleanAfterExampleSpec extends Specification with EmbedConnection with MongoInit with CleanAfterExample {

sequentialyIsolated

"Embed database" should {
"be able to save a Model I" in {
Model.save(Model(name = "test"))
Model.count() must be_==(1)
val coll = getColl
Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second)
Await.result(coll.count(None), 5 second) must be_==(1)
}

"be able to save a Model II" in {
Model.save(Model(name = "test"))
Model.count() must be be_== (1)
val coll = getColl
Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second)
Await.result(coll.count(None), 5 second) must be_==(1)
}

"be able to save a Model III" in {
Model.save(Model(name = "test"))
Model.count() must be be_== (1)
val coll = getColl
Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second)
Await.result(coll.count(None), 5 second) must be_==(1)
}

"be able to save a Model IV" in {
Model.save(Model(name = "test"))
Model.count() must be be_== (1)
val coll = getColl
Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second)
Await.result(coll.count(None), 5 second) must be_==(1)
}

"be able to save a Model V" in {
Model.save(Model(name = "test"))
Model.count() must be be_== (1)
val coll = getColl
Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second)
Await.result(coll.count(None), 5 second) must be_==(1)
}
}
}
34 changes: 20 additions & 14 deletions src/test/scala/com/github/athieriot/EmbedConnectionSpec.scala
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
package com.github.athieriot

import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner
import org.specs2.mutable.Specification
import reactivemongo.bson.BSONDocument

@RunWith(classOf[JUnitRunner])
class EmbedConnectionSpec extends Specification with EmbedConnection {
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
class EmbedConnectionSpec extends Specification with EmbedConnection with MongoInit {
sequential

"Embed database" should {
"be able to save a Model I" in {
Model.save(Model(name = "test"))
Model.count() must be_==(1)
val coll = getColl
Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second)
Await.result(coll.count(None), 5 second) must be_==(1)
}

"be able to save a Model II" in {
Model.save(Model(name = "test"))
Model.count() must be be_== (2)
val coll = getColl
Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second)
Await.result(coll.count(None), 5 second) must be_==(2)
}

"be able to save a Model III" in {
Model.save(Model(name = "test"))
Model.count() must be be_== (3)
val coll = getColl
Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second)
Await.result(coll.count(None), 5 second) must be_==(3)
}

"be able to save a Model IV" in {
Model.save(Model(name = "test"))
Model.count() must be be_== (4)
val coll = getColl
Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second)
Await.result(coll.count(None), 5 second) must be_==(4)
}

"be able to save a Model V" in {
Model.save(Model(name = "test"))
Model.count() must be be_== (5)
val coll = getColl
Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second)
Await.result(coll.count(None), 5 second) must be_==(5)
}
}
}
Expand Down
11 changes: 0 additions & 11 deletions src/test/scala/com/github/athieriot/Model.scala

This file was deleted.

16 changes: 16 additions & 0 deletions src/test/scala/com/github/athieriot/MongoInit.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.athieriot

import reactivemongo.api.MongoDriver
import reactivemongo.api.collections.bson.BSONCollection
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

trait MongoInit {
def getColl: BSONCollection = {
val driver = new MongoDriver
val db = driver.connection(List("localhost:12345"))("testdb")
Await.ready(db.connection.waitForPrimary(10 seconds), 11 seconds)
db("testcoll")
}
}