Skip to content

Commit

Permalink
replacing mongoRunner with fakeMongo
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Kowalski committed Jun 16, 2014
1 parent 54f15d4 commit 59cbebb
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 136 deletions.
11 changes: 2 additions & 9 deletions README.md
Expand Up @@ -73,7 +73,6 @@ In order to build and develop on Bootzooka foundations you need the following:
* Java JDK >= 7
* [SBT](http://www.scala-sbt.org/) >= 0.13
* Node.js >= 0.10.13
* [MongoDB](http://www.mongodb.org/) >= 2.4

## How to run (development)

Expand Down Expand Up @@ -113,14 +112,8 @@ You can drop it in any servlet container (Tomcat/Jetty/JBoss/etc.)

## How to execute tests

Because some tests are using MongoDB you should have it installed on your machine. Additionally you should let SBT know
where MongoDB files are located. To do that please add one line to your ~/.sbt/0.13/local.sbt:

SettingKey[File]("mongo-directory") := file("/Users/your_user/apps/mongodb")

with proper path to your MongoDB installation directory.

After that SBT will start MongoDB instance before executing each test class that extends `FlatSpecWithMongo` trait.
Tests are using [Fakemongo](https://github.com/fakemongo/fongo) to mock out MongoDB. You don't need to have MongoDB installed on your machine.
Check out easy setup of Fakemongo in `FlatSpecWithMongo` trait.

When you issue `test` from SBT, tests for both server-side and client-side components are run. SBT integrates some Grunt
commands and executes tests for browser part via Grunt too.
Expand Down
Expand Up @@ -2,29 +2,18 @@ package com.softwaremill.bootzooka.test

import org.scalatest.{BeforeAndAfterAll, FlatSpec}
import net.liftweb.mongodb.{MongoDB, DefaultMongoIdentifier}
import com.mongodb.Mongo
import com.github.fakemongo.Fongo

trait FlatSpecWithMongo extends FlatSpec with BeforeAndAfterAll {
val mongoPort = 24567

protected var mongoRunner: MongoRunner = null
val fongo = new Fongo("mongo test server 1")

override protected def beforeAll() {
super.beforeAll()
startMongo()
}

override protected def afterAll() {
stopMongo()
super.afterAll()
}

def startMongo() {
mongoRunner = MongoRunner.run(mongoPort, verbose = true)
MongoDB.defineDb(DefaultMongoIdentifier, new Mongo("localhost", mongoPort), "bootzooka_test")
}

def stopMongo() {
mongoRunner.stop()
MongoDB.defineDb(DefaultMongoIdentifier, fongo.getMongo, "bootzooka_test")
}
}

This file was deleted.

24 changes: 6 additions & 18 deletions project/Build.scala
Expand Up @@ -7,9 +7,8 @@ import sbtassembly.Plugin._
import AssemblyKeys._

object BuildSettings {
val mongoDirectory = SettingKey[File]("mongo-directory", "The home directory of MongoDB datastore")

val buildSettings = Defaults.defaultSettings ++ Seq(mongoDirectory := file("")) ++ Seq(
val buildSettings = Defaults.defaultSettings ++ Seq(

organization := "com.softwaremill",
version := "0.0.1-SNAPSHOT",
Expand All @@ -21,21 +20,7 @@ object BuildSettings {
libraryDependencies ++= Dependencies.logging,
libraryDependencies ++= Seq(Dependencies.guava, Dependencies.googleJsr305),

parallelExecution := false, // We are starting mongo in tests.

testOptions in Test <+= mongoDirectory map {
md: File => Tests.Setup {
() =>
val mongoFile = new File(md.getAbsolutePath + "/bin/mongod")
val mongoFileWin = new File(mongoFile.getAbsolutePath + ".exe")
if (mongoFile.exists || mongoFileWin.exists) {
System.setProperty("mongo.directory", md.getAbsolutePath)
} else {
throw new RuntimeException(
"Trying to launch with MongoDB but unable to find it in 'mongo.directory' (%s). Please check your ~/.sbt/local.sbt file.".format(mongoFile.getAbsolutePath))
}
}
}
parallelExecution := true

This comment has been minimized.

Copy link
@adamw

adamw Jun 16, 2014

Member

Can the tests run in parallel? Are the Fongo "instances" exclusive per test, and isolated? What if two tests use the same collections, they will see each others data, no?

This comment has been minimized.

Copy link
@johnykov

johnykov Jun 16, 2014

Contributor

There is only one instance of Fongo and access to it is synchronized. Unfortunately we didn't gain isolated mongo instances per test.

This comment has been minimized.

Copy link
@johnykov

johnykov Jun 16, 2014

Contributor

Tests can run in parallel but it doesn't change much in execution time as access to fongo is synchronized.

This comment has been minimized.

Copy link
@adamw

adamw Jun 20, 2014

Member

How is the access synchronized? Does it exclude a possibility of two tests running in parallel, using the same collections and overwriting each others data?

This comment has been minimized.

Copy link
@johnykov

johnykov Jun 20, 2014

Contributor

All read and write operations on collections are synchronized. And as I see(Fongo & FongoDB classes) it doesn't exclude that possibility :(
I will bring back sequential execution.

This comment has been minimized.

Copy link
@adamw

adamw Jun 20, 2014

Member

Ok, in "real" mongo all write operations are synchronized as well (there's a per-collection r/w lock)

This comment has been minimized.

Copy link
@maciej

maciej Jun 26, 2014

Contributor

From what I know Fongo – different Fongo objects represent completely different fake MongoDBs, hence Fongo does not hinder the possibility of running tests in parallel.
However Lift Record does, as it requires the DB connected with it's records to be registered globally:

MongoDB.defineDb(DefaultMongoIdentifier, fongo.getMongo, "bootzooka_test")
)

}
Expand Down Expand Up @@ -93,6 +78,9 @@ object Dependencies {
val seleniumFirefox = "org.seleniumhq.selenium" % "selenium-firefox-driver" % seleniumVer % "test"
val fest = "org.easytesting" % "fest-assert" % "1.4" % "test"
val awaitility = "com.jayway.awaitility" % "awaitility-scala" % "1.3.5" % "test"
val fakeMongo = "com.github.fakemongo" % "fongo" % "1.5.1" % "test"
//lift-mongodb-record depends on older mongo-java-driver, but for fakeMongo we need newer
val mongoJava = "org.mongodb" % "mongo-java-driver" % "2.12.2" % "test"

This comment has been minimized.

Copy link
@adamw

adamw Jun 16, 2014

Member

Maybe lift-record can be updated to a newer version?

This comment has been minimized.

Copy link
@johnykov

johnykov Jun 16, 2014

Contributor

our current version is the latest stable 2.5.1 of lift-mongodb-record. All newer are type milstone


val selenium = Seq(seleniumJava, seleniumFirefox, fest)

Expand Down Expand Up @@ -158,7 +146,7 @@ object BootzookaBuild extends Build {
lazy val dao: Project = Project(
"bootzooka-dao",
file("bootzooka-dao"),
settings = buildSettings ++ Seq(libraryDependencies ++= rogue)
settings = buildSettings ++ Seq(libraryDependencies ++= (rogue ++ Seq(mongoJava, fakeMongo)))
) dependsOn(domain, common)

lazy val service: Project = Project(
Expand Down

2 comments on commit 59cbebb

@adamw
Copy link
Member

@adamw adamw commented on 59cbebb Jun 16, 2014

Choose a reason for hiding this comment

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

Awesome! Great change :)

@johnykov
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks :) Mostly my job here was to remove code :P

Please sign in to comment.