Created
July 12, 2015 08:25
-
-
Save damianmcdonald/2724facc813959ce1065 to your computer and use it in GitHub Desktop.
Example ScalaTest case using Embedded MongoDB with the ScalaTest EmbededMongo trait - Immutable Usage
This file contains 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
name := "embeddedmongodb-gist" | |
version := "0.1" | |
scalaVersion := "2.11.6" | |
scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8") | |
libraryDependencies ++= { | |
Seq( | |
"org.mongodb" %% "casbah" % "2.8.1" % "test", | |
"de.flapdoodle.embed" % "de.flapdoodle.embed.mongo" % "1.48.0" % "test", | |
"com.github.simplyscala" %% "scalatest-embedmongo" % "0.2.2" % "test", | |
"org.scalatest" %% "scalatest" % "2.2.4" % "test", | |
"org.slf4j" % "slf4j-api" % "1.7.12" % "test", | |
"org.slf4j" % "slf4j-simple" % "1.7.12" % "test" | |
) | |
} |
This file contains 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
import java.util.Date | |
import com.github.simplyscala.MongoEmbedDatabase | |
import com.mongodb.casbah.Imports._ | |
import com.mongodb.casbah.commons.MongoDBObject | |
import de.flapdoodle.embed.mongo.distribution.Version | |
import org.scalatest.{BeforeAndAfter, FunSpec} | |
import org.slf4j.LoggerFactory | |
class EmbeddedMongoDBImmutableTest extends FunSpec with MongoEmbedDatabase with BeforeAndAfter { | |
private val LOG = LoggerFactory.getLogger(classOf[EmbeddedMongoDBImmutableTest]) | |
/* The BSON structure that will be used for the test cases | |
{ | |
"userId": "134256", | |
"currencyFrom": "EUR", | |
"currencyTo": "GBP", | |
"amountSell": 1000, | |
"amountBuy": 747.1, | |
"rate": 0.7471, | |
"originatingCountry": "FR" | |
} | |
*/ | |
/* Default values to be used for the test cases */ | |
private val WAIT_TIME = 10000L | |
private val MONGO_PORT = 12345 | |
private val MONGO_HOST = "127.0.0.1" | |
private val MONGO_DATABASE = "currencytrade" | |
private val MONGO_COLLECTION = "trade" | |
/* The MongoDB connection - connecting to the MongoDBEmbedded instance */ | |
private lazy val collection = { | |
val connection = MongoClient(MONGO_HOST, MONGO_PORT) | |
connection(MONGO_DATABASE)(MONGO_COLLECTION) | |
} | |
/* Internal method to pre-populate the MongoDBEmbedded instance with test data */ | |
private def populateDatabase() = { | |
// drop any existing records from the collection | |
collection.drop() | |
// helper method to validate the MongoDB ObjectId of an inserted record | |
def validateObjectId(objectId: String) = { | |
"^[a-f\\d]{24}$".r findFirstIn objectId match { | |
case Some(str) => true | |
case None => fail("FAILURE >>> Invalid object id format: " + objectId) | |
} | |
} | |
/* The test data */ | |
val dbObject1 = { | |
val builder1 = MongoDBObject.newBuilder | |
builder1 += "userId" -> "lukeskywalker" | |
builder1 += "currencyFrom" -> "EUR" | |
builder1 += "currencyTo" -> "USD" | |
builder1 += "amountSell" -> 1245.67 | |
builder1 += "amountBuy" -> 978.10 | |
builder1 += "rate" -> 0.7852 | |
builder1 += "originatingCountry" -> "AU" | |
builder1 += "receptionDate" -> new Date | |
builder1.result | |
} | |
val dbObject2 = { | |
val builder2 = MongoDBObject.newBuilder | |
builder2 += "userId" -> "hansolo" | |
builder2 += "currencyFrom" -> "AUD" | |
builder2 += "currencyTo" -> "CAD" | |
builder2 += "amountSell" -> 678.33 | |
builder2 += "amountBuy" -> 333.40 | |
builder2 += "rate" -> 0.4915 | |
builder2 += "originatingCountry" -> "NZ" | |
builder2 += "receptionDate" -> new Date | |
builder2.result | |
} | |
val dbObject3 = { | |
val builder3 = MongoDBObject.newBuilder | |
builder3 += "userId" -> "darthvader" | |
builder3 += "currencyFrom" -> "HKD" | |
builder3 += "currencyTo" -> "CNY" | |
builder3 += "amountSell" -> 8763.09 | |
builder3 += "amountBuy" -> 11014.33 | |
builder3 += "rate" -> 1.2569 | |
builder3 += "originatingCountry" -> "AU" | |
builder3 += "receptionDate" -> new Date | |
builder3.result | |
} | |
val xs = List(dbObject1, dbObject2, dbObject3) | |
// add the test data to the MongoDBEmbedded instance and validate object ids | |
xs foreach (obj => { | |
collection.save(obj) | |
LOG.debug("Object added: " + obj.get("_id")) | |
obj.get("_id") match { | |
case id: ObjectId => validateObjectId(id.toString) | |
case whatever => fail("FAILURE >>> Object id not found: " + whatever) | |
} | |
}) | |
} | |
/* The test cases, executing against the MongoDBEmbedded instance */ | |
describe("A MongoBD query to find all the trades placed from Australia") { | |
it("should return a result set of 2 trades") { | |
// The mongo port and version number can be provided to the fixture | |
// The fixture will start and stop the MongoDBEmbedded instance | |
withEmbedMongoFixture(MONGO_PORT, Version.V3_1_0) { mongodProps => | |
// sleep the thread to give the MongoDBEmbedded instance time to start up | |
Thread.sleep(WAIT_TIME) | |
// prepare the database with test data | |
populateDatabase | |
val size = collection.find(MongoDBObject("originatingCountry" -> "AU")).size | |
assert(size == 2, "FAILURE >>> query should return 2 results") | |
} | |
} | |
} | |
describe("A MongoBD query to find the amountSell value of trades placed by userId hansolo") { | |
it("should return a value of 678.33") { | |
// The mongo port and version number can be provided to the fixture | |
// The fixture will start and stop the MongoDBEmbedded instance | |
withEmbedMongoFixture(MONGO_PORT, Version.V3_1_0) { mongodProps => | |
// sleep the thread to give the MongoDBEmbedded instance time to start up | |
Thread.sleep(WAIT_TIME) | |
// prepare the database with test data | |
populateDatabase | |
val result = collection.findOne(MongoDBObject("userId" -> "hansolo")).getOrElse( | |
fail("FAILURE >>> unable to find record for userid: hansolo") | |
) | |
val amountSell = result.getAsOrElse[Double]("amountSell", 0.0) | |
assert(amountSell == 678.33, "FAILURE >>> query should return 678.33") | |
} | |
} | |
} | |
} |
This file contains 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
org.slf4j.simpleLogger.defaultLogLevel = debug | |
org.slf4j.simpleLogger.showDateTime = true | |
org.slf4j.simpleLogger.dateTimeFormat = yyyy'/'MM'/'dd' 'HH':'mm':'ss'-'S | |
org.slf4j.simpleLogger.showThreadName = true | |
org.slf4j.simpleLogger.showLogName = true | |
org.slf4j.simpleLogger.showShortLogName= false | |
org.slf4j.simpleLogger.levelInBrackets = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment