Best way to use Scala MongoClient in Play 2.0 - mongodb

I have just started using Play 2.0 with Scala and Casbah for connecting to MongoDB. I have been able to connect to my MongoDB instance but what I am looking for is a way to be able to access the MongoClient from all my model classes.
Is there any DependencyInjection way to inject mongoClient in all Scala models ? or
Should I have one Scala object which initialises the MongoClient and use that object to refer to MongoClient in all my models ? or
Is there a more better way to do this ?

As MongoClient uses a connection pool internally, its optimal to only have a single instance for your application and that single object can then be used by all your models.
Also, you could look at Salat which might do what you require or give you an idea on how best to implement your own models.

Related

Factory objects for Sails using sails-mongo adapter?

I'm using sails framework with sails-mongo adapter as orm, I want to test models and controllers so I want a factory to provide model objects. When I use sql databases I use factory-girl with sequelize as ORM, so I want an equivalent for mongo databases. I know there is an adapter for factory girl using mongoose, but nevertheless I read that is recommended to use sails-mongo (waterline) for sails framework instead of moongose.
Do you know a factory or how can I create objects(with associations) for this?
Thanks in advance.

alternative ways of stubbing collections

In meteor, is there any way to stub a collection for unit testing without using hwillson:stub-collections? Using this package is unfortunately not an option due to package constraints (mongo#1.0.11), and neither is using a test Mongodb instance.

Write instance to Mongo with information about class

How can I store information about class of object when I save it to Mongo? I'm using Scala and Play.
More details. Lets say, we have Trait User and 2 implementations: Admin and Member. And then we try to save them to one Mongo collection.
class UserDao {
private def collection = ReactiveMongoPlugin.db.collection[JSONCollection]("users")
def save(user:User):Future[User] = {
collection.save(user) //Fail
}
}
And we get error. Because we need Reads and Writes for Trait which are really ugly and complicated things..
Before I've written application in Java and Spring Mongo and there wasn't any problems. Spring automagically add _class field to this bson object, which is stored in Mongo. And after read from this collection spring knows which should create. And in Play there is nothing like that.
Please, help me..
I found information, that this is impossible in play reactive mongo driver and generally in play mongo. So there is only one way- write your own driver or think about different solution.

Is it possible to mock afMongo's MongoClient or ConnectionManager?

I am using afMongo for accessing a Mongo DB from a Fantom web app and I'm wondering if it is possible to mock the MongoClient or the ConnectionManager so that the Test classes don't need a Mongo DB running.
The short answer is no.
The long answer is yes, but you need to write the mock yourself and it's quite low level.
Connection is the thing to mock because it's a mixin. Internally, afMongo uses MockMongoConnection so you could try using / extending that.
Typical usage would be:
mmc = MockMongoConnection()
conMgr = ConnectionManagerLocal(mmc, ``)
mmc.reset.reply(...your map obj...)
You're probably better off wrapping your Mongo calls in your own DAO service and mocking that.

Test MongoDB Interactions in a PHP Application With Mocking

What is the best practice to work efficiently with MongoDB and PHPUnit? What should (or could) I use to mock objects that access MongoDB? PHPUnit Mocker, Mockery, Phrophecy, Phactory?
If you look at mocking data for SQL databases, there are lots of opinions here.
Some people suggest using an in-memory SQL database.
Some people just mock the ORM calls and assume that the ORM to DB portion is tested.
Some people just use a "local" DB for unit testing and just ignore the whole "mocking" concept.
Given the lack of consensus on SQL, it's even less likely that you will find consensus on the new DBs like MongoDB.
I think there are some important details to consider here.
Are you using some form of ORM / ODM? Just the driver directly?
Are you trying to mock all communications with the DB? Are you trying to mock the ODM?
If you are just trying to mock communications to DB, then the ideal solution is a "fake" implementation of the MongoDB driver. This is probably a lot of work as the driver was never written with "mockability" in mind.
If you have an ODM, then you can simply mock the ODM calls and assume the ODM is doing its job. Ideally the ODM should provide some mockable interface, but this is not always the case.
Again, this answer comes back down to what you're really planning to test and what you consider as a good unit test. Unfortunately, most of these products are still very new so there is very little guidance in this space.
Phactory provides direct support for mocking MongoDB.
Edit: Phactory is no longer maintained. However, I've found a new project called php-mongomock that seems to solve this problem:
<?php
use Helmich\MongoMock\MockCollection;
$collection = new MockCollection();
$collection->createIndex(['foo' => 1]);
$documentId = $collection->insertOne(['foo' => 'bar'])->insertedId();
$collection->updateOne(['_id' => $documentId], ['$set' => ['foo' => 'baz']]);