Giter Club home page Giter Club logo

mongo2go's Introduction

Mongo2Go - MongoDB for integration tests & local debugging

Logo

Build Status Linux Build (Ubuntu Trusty)
Build status Windows Build (Windows Server 2016)

Mongo2Go is a managed wrapper around the latest MongoDB binaries. It targets .NET Standard 1.6 (and .NET 4.6 for legacy environments) and works with Windows, Linux and macOS. This Nuget package contains the executables of mongod, mongoimport and mongoexport for Windows, Linux and macOS .

Brought to you by Johannes Hoppe, follow him on Twitter.

Mongo2Go has two use cases:

  1. Providing multiple, temporary and isolated MongoDB databases for unit tests (or to be precise: integration tests)
  2. Providing a quick to set up MongoDB database for a local developer environment

Unit Test / Integration test

With each call of the static method MongoDbRunner.Start() a new MongoDB instance will be set up. A free port will be used (starting with port 27018) and a corresponding data directory will be created. The method returns an instance of MongoDbRunner, which implements IDisposable. As soon as the MongoDbRunner is disposed (or if the Finalizer is called by the GC), the wrapped MongoDB process will be killed and all data in the data directory will be deleted.

Local debugging

In this mode a single MongoDB instance will be started on the default port (27017). No data will be deleted and the MongoDB instance won’t be killed automatically. Multiple calls to MongoDbRunner.StartForDebugging() will return an instance with the State “AlreadyRunning”. You can ignore the IDisposable interface, as it won’t have any effect. I highly recommend to not use this mode on productive machines! Here you should set up a MongoDB as it is described in the manual. For you convenience the MongoDbRunner also exposes mongoexport and mongoimport which allow you to quickly set up a working environment.

Single server replica set mode to enable transactions

MongoDbRunner.Start() can be set up to take in an optional boolean parameter called singleNodeReplSet. When passed in with the value true - (MongoDbRunner.Start(singleNodeReplSet: true))

  • a single node mongod instance will be started as a replica set with the name singleNodeReplSet. Replica set mode is required for transactions to work in MongoDB 4.0 or greater

Additional mongod arguments

MongoDbRunner.Start() can be set up to consume additional mongod arguments. This can be done using the string parameter called additionalMongodArguments. The list of additional arguments cannot contain arguments already defined internally by Mongo2Go. An ArgumentException will be thrown in this case, specifying which additional arguments are required to be discarded. Example of usage of the additional mongod arguments: MongoDbRunner.Start(additionalMongodArguments: "--quiet")

Installation

The Mongo2Go Nuget package can be found at https://nuget.org/packages/Mongo2Go/

Search for „Mongo2Go“ in the Manage NuGet Packages dialog box or run:

PM> Install-Package Mongo2Go

or run for the legacy .NET 4.6 package:

PM> Install-Package Mongo2Go -Version 1.1.0

in the Package Manager Console.

  • The new 2.x branch targets .NET Standard 1.6, please use the latest packages if possible.
  • The old 1.x branch targets good-old classic .NET 4.6.1. This is for legacy environments only. No new features will be added, only bugfixes will be made.

Examples

Example: Integration Test (here: Machine.Specifications & Fluent Assertions)

[Subject("Runner Integration Test")]
public class when_using_the_inbuild_serialization : MongoIntegrationTest
{
    static TestDocument findResult;
    
    Establish context = () =>
        {
            CreateConnection();
            _collection.Insert(TestDocument.DummyData1());
        };

    Because of = () => findResult = _collection.FindOneAs<TestDocument>();

    It should_return_a_result = () => findResult.ShouldNotBeNull();
    It should_hava_expected_data = () => findResult.ShouldHave().AllPropertiesBut(d => d.Id).EqualTo(TestDocument.DummyData1());

    Cleanup stuff = () => _runner.Dispose();
}

public class MongoIntegrationTest
{
    internal static MongoDbRunner _runner;
    internal static MongoCollection<TestDocument> _collection;

    internal static void CreateConnection()
    {
        _runner = MongoDbRunner.Start();
        
        MongoServer server = MongoServer.Create(_runner.ConnectionString);
        MongoDatabase database = server.GetDatabase("IntegrationTest");
        _collection = database.GetCollection<TestDocument>("TestCollection");
    }
}    

More tests can be found at https://github.com/Mongo2Go/Mongo2Go/tree/master/src/Mongo2GoTests/Runner

Example: Exporting seed data

using (MongoDbRunner runner = MongoDbRunner.StartForDebugging()) {

    runner.Export("TestDatabase", "TestCollection", @"..\..\App_Data\test.json");
}

Example: Importing for local debugging (compatible with ASP.NET MVC 4 Web API as well as ASP.NET Core)

public class WebApiApplication : System.Web.HttpApplication
{
    private MongoDbRunner _runner;

    protected void Application_Start()
    {
        _runner = MongoDbRunner.StartForDebugging();
        _runner.Import("TestDatabase", "TestCollection", @"..\..\App_Data\test.json", true);

        MongoServer server = MongoServer.Create(_runner.ConnectionString);
        MongoDatabase database = server.GetDatabase("TestDatabase");
        MongoCollection<TestObject> collection = database.GetCollection<TestObject>("TestCollection");

        /* happy coding! */
    }

    protected void Application_End()
    {
        _runner.Dispose();
    }
}

Example: Transactions (New feature since v2.2.8)

Full integration test with transaction handling (click to show)
 public class when_transaction_completes : MongoTransactionTest
    {
        private static TestDocument mainDocument;
        private static TestDocument dependentDocument;
        Establish context = () =>

        {
            _runner = MongoDbRunner.Start(singleNodeReplSet: true);
             client = new MongoClient(_runner.ConnectionString);
            database = client.GetDatabase(_databaseName);
            _mainCollection = database.GetCollection<TestDocument>(_mainCollectionName);
            _dependentCollection = database.GetCollection<TestDocument>(_dependentCollectionName);
            _mainCollection.InsertOne(TestDocument.DummyData2());
            _dependentCollection.InsertOne(TestDocument.DummyData2());
        };

        private Because of = () =>
        {
            var filter = Builders<TestDocument>.Filter.Where(x => x.IntTest == 23);
            var update = Builders<TestDocument>.Update.Inc(i => i.IntTest, 10);

            using (var sessionHandle = client.StartSession())
            {
                try
                {
                    var i = 0;
                    while (i < 10)
                    {
                        try
                        {
                            i++;
                            sessionHandle.StartTransaction(new TransactionOptions(
                                readConcern: ReadConcern.Local,
                                writeConcern: WriteConcern.W1)); 
                            try
                            {
                                var first = _mainCollection.UpdateOne(sessionHandle, filter, update);
                                var second = _dependentCollection.UpdateOne(sessionHandle, filter, update);
                            }
                            catch (Exception e)
                            {
                                sessionHandle.AbortTransaction();
                                throw;
                            }

                            var j = 0;
                            while (j < 10)
                            {
                                try
                                {
                                    j++;
                                    sessionHandle.CommitTransaction();
                                    break;
                                }
                                catch (MongoException e)
                                {
                                    if (e.HasErrorLabel("UnknownTransactionCommitResult"))
                                        continue;
                                    throw;
                                }
                            }
                            break;
                        }
                        catch (MongoException e)
                        {
                            if (e.HasErrorLabel("TransientTransactionError"))
                                continue;
                            throw;
                        }
                    }
                }
                catch (Exception e)
                {
                    //failed after multiple attempts so log and do what is appropriate in your case
                }
            }

             mainDocument = _mainCollection.FindSync(Builders<TestDocument>.Filter.Empty).FirstOrDefault();
             dependentDocument = _dependentCollection.FindSync(Builders<TestDocument>.Filter.Empty).FirstOrDefault();
        };
        
        It main_should_be_33 = () => mainDocument.IntTest.Should().Be(33);
        It dependent_should_be_33 = () => dependentDocument.IntTest.Should().Be(33);
        Cleanup cleanup = () => _runner.Dispose();
    }

Changelog

Mongo2Go 2.2.10, May 10 2019

  • allows additional custom MongoDB arguments (PR #68, fixes #68 - many thanks to José Mira)

Mongo2Go 2.2.9, February 04 2019

  • fixes a file path issue on Linux if you run on an SDK version beyond .NET Standard 1.6 (PR #63, fixes #62 and #61) - many thanks to Jeroen Vannevel)
  • continuous integration runs on Linux (Travis CI) and Windows (AppVeyor) now

Mongo2Go 2.2.8, October 12 2018

  • updated MongoDB binaries to 4.0.2 to support tests leveraging transaction across different collections and databases
  • updated MongoDB C# driver to 2.7.0 to be compatible with MongoDB 4.0
  • adds singleNodeReplSet paramter to MongoDbRunner.Start which allows mongod instance to be started as a replica set to enable transaction support (PR #57 - many thanks to Mahi Satyanarayana)
  • fixes port lookup for UnixPortWatcher (PR #58 - many thanks to Viktor Kolybaba)

Mongo2Go 2.2.7, August 13 2018

  • updates the MongoBinaryLocator to look for binaries in the nuget cache if they are not found in the project directory.
    • this will make Mongo2Go compatible with projects using the nuget PackageReference option. (PR #56, fixes #39 and #55)
  • adds the binariesSearchDirectory parameter to MongoDbRunner.Start which allows an additional binaries search directory to be provided.
    • this will make the db runner more flexible if someone decides to use it in some unpredictable way.
  • many thanks to Nicholas Markkula

Mongo2Go 2.2.6, July 20 2018

  • fixes broken linux support (fixes #47)

Mongo2Go 2.2.5, July 19 2018

Mongo2Go 2.2.4, June 06 2018

  • better support for TeamCity: removed MaxLevelOfRecursion limitation when searching for MongoDb binaries (PR #50, fixes #39)
  • many thanks to Stanko Culaja

Mongo2Go 2.2.2, June 05 2018

  • includes mongod, mongoimport and mongoexport v3.6.1 for Windows, Linux and macOS via PR #46, which fixes #45
  • many thanks to Joe Chan

Mongo2Go 2.2.1, November 23 2017

  • no MongoDB binaries changed, still .NET Standard 1.6
  • feature: uses temporary directory instead of good-old windows style C:\data\db by default (PR #42) - MongoDbRunner.Start() and MongoDbRunner.StartForDebugging() will now work without any extra parameters for Linux/macOS
  • bugfix: runs again on Linux/macOS, by making the binaries executable (PR #42, which fixes #37 and might also fix #43)
  • internal: Unit Tests are running again (PR #44, which fixes #31, #40)
  • internal: No hardcoded path passed to MongoDbRunner constructor (fixes 41)
  • many thanks to Per Liedman

Mongo2Go 2.2.0, August 17 2017

  • includes mongod, mongoimport and mongoexport v3.4.7 for Windows, Linux and macOS
  • targets .NET Standard 1.6 (can be used with .NET Core 1.0 / 1.1 / 2.0)
  • many thanks to Aviram Fireberger

Mongo2Go 2.1.0, March 10 2017

  • skips v2.0 to have same numbers as v1.x.
  • no MongoDB binaries changed since 2.0.0-alpha1 (still MongoDB v3.2.7 for Windows, Linux and macOS)
  • targets .NET Standard 1.6 (can be used with .NET Core 1.0 / 1.1)
  • bugfix: prevent windows firewall popup (PR #30, which fixes #21)
  • many thanks to kubal5003

Mongo2Go 1.1.0, March 10 2017 (legacy branch!)

  • no MongoDB binaries changed since v1.0 (still MongoDB v3.2.7 for Windows, Linux and macOS)
  • targets .NET 4.6.1
  • bugfix: prevent windows firewall popup (PR #29, which fixes #21)
  • many thanks to kubal5003

Mongo2Go 2.0.0-alpha1, December 19 2016

  • this version has no support for .NET Framework 4.6, please continue to use the stable package v.1.0.0
  • NEW: first support of .NET Standard 1.6 (#25)
    • many thanks to Hassaan Ahmed
    • see the Wiki for more information about .NET Core 1.0 / .NET Standard 1.6
Changelog v0.1.0 to v1.0.0 (click to show)

Mongo2Go 1.0.0, November 14 2016

  • v1.0 finally marked as stable
  • no changes to 1.0.0-beta4
  • changes since last stable version (0.2):
    • includes mongod, mongoimport and mongoexport v3.2.7 for Windows, Linux and macOS
    • support for Windows, Linux and macOS
    • uses MongoDB.Driver 2.3.0
    • requires .NET 4.6
    • various small bugfixes and improvements

Mongo2Go 1.0.0-beta4, October 24 2016

  • update to MongoDB.Driver 2.3.0 (#23)
  • upgraded to .NET 4.6
  • internal change: update MSpec as well and add MSTest Adapter for MSpec (ReSharper console runner doesn't support 4.6)
  • many thanks to Alexander Zeitler
  • please report any kind of issues here on github so that we can mark 1.0.0 as stable!

Mongo2Go 1.0.0-beta3, August 22 2016

  • feature: process windows are hidden now (#20)
  • bugfix: random folders are used for storing databases (#18)
  • many thanks to Matt Kocaj
  • please report any kind of issues here on github so that we can mark 1.0.0 as stable!

Mongo2Go 1.0.0-beta2, July 29 2016

  • fixes for bugs that were introduced by the big rewrite for cross-platform support
  • changes from pull request #14, which fixes #12, #13 and #15, many thanks to Mitch Ferrer
  • please report any kind of issues here on github so that we can mark 1.0.0 as stable!

Mongo2Go 1.0.0-beta, July 24 2016

  • 🎉 NEW: support for Linux and macOS 🎉
  • many thanks to Kristofer Linnestjerna from netclean.com for the new cross-platform support
  • includes mongod, mongoimport and mongoexport v3.2.7 for Windows, Linux and macOS
  • changes from pull request #8, #10, #11 which fixes #9
  • please report any kind of issues here on github so that we can mark 1.0.0 as stable!

Mongo2Go 0.2, May 30 2016

Mongo2Go 0.1.8, March 13 2016

  • includes mongod, mongoimport and mongoexport v3.0.10 (32bit)
  • changes from pull request #5, thanks to Aristarkh Zagorodnikov

Mongo2Go 0.1.6, July 21 2015

  • includes mongod, mongoimport and mongoexport v3.0.4 (32bit)
  • bug fix #4:
    Sometimes the runner tries to delete the database directory before the mongod process has been stopped, this throws an IOException. Now the runner waits until the mongod process has been stopped before the database directory will be deleted.
  • Thanks Sergey Zwezdin

Mongo2Go 0.1.5, July 08 2015

  • includes mongod, mongoimport and mongoexport v2.6.6 (32bit)
  • changes from pull request #3
  • new: Start and StartForDebugging methods accept an optional parameter to specify a different data directory (default is "C:\data\db")
  • many thanks to Marc

Mongo2Go 0.1.4, January 26 2015

  • includes mongod, mongoimport and mongoexport v2.6.6 (32bit)
  • changes from pull request #2
  • internal updates for testing the package (not part of the release)
    • updated MSpec package so that it would work with the latest VS and R# test runner
    • updated Mongo C# Driver, Fluent Assertions, and Moq packages to latest versions
    • fixed date handling for mongoimport and mongoexport to pass tests
  • many thanks to Jesse Sweetland

Mongo2Go 0.1.3, September 20 2012

  • includes mongod, mongoimport and mongoexport v2.2.0 (32bit)

Mongo2Go 0.1.2, August 20 2012

  • stable version
  • includes mongod, mongoimport and mongoexport v2.2.0-rc1 (32bit)

Mongo2Go 0.1.1, August 16 2012

  • second alpha version
  • includes mongod, mongoimport and mongoexport v2.2.0-rc1 (32bit)

Mongo2Go 0.1.0, August 15 2012

  • first alpha version
  • includes mongod, mongoimport and mongoexport v2.2.0-rc1 (32bit)

How to contribute

Just fork the project, make your changes send us a PR.
You can compile the project with Visual Studio 2017 and/or the .NET Core 2.0 CLI!

In the root folder, just run:

dotnet restore
dotnet build
dotnet test src/Mongo2GoTests

mongo2go's People

Contributors

alexzeitler avatar avrum avatar bannerflow-hassaan avatar cottsak avatar g3n7 avatar johanneshoppe avatar jsweetland-ar avatar krippz avatar kubal5003 avatar mahisatyanarayana avatar marcpiechura avatar marioleed avatar onyxmaster avatar stanko-culaja-kolotree avatar vannevelj avatar vikkol avatar zmira avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.