Squadron

Squadron

  • Docs
  • Blog
  • GitHub

›Container Providers

Introduction

  • Introduction
  • Quickstart
  • Basic Concept
  • Container Options
  • Configuration
  • Multiple resources

Container Providers

  • MongoDB
  • MongoDB Replica Set
  • SQL Server
  • Elasticsearch
  • PostgreSQL
  • Redis
  • RabbitMQ
  • Azure Storage
  • Generic
  • Compose

Azure Cloud Providers

  • Setup
  • ServiceBus
Edit

MongoDB Replica Set

The MongoDB Replica Set is derived from the Mongo resource but additionally Replica Set. Some features like Transactions and Change Streams require a Replica Set to work.

Install

Install the Squadron nuget package Mongo for within your test project:

dotnet add package Squadron.Mongo

Basic usage

Inject the MongoReplicaSetResource into your test class constructor:

public class UserRepositoryWithTransactionTests
    : IClassFixture<MongoReplicaSetResource>
{
    private readonly MongoReplicaSetResource _mongoRsResource;

    public UserRepositoryWithTransactionTests(MongoReplicaSetResource mongoRsResource)
    {
        _mongoRsResource = mongoRsResource;
    }

    [Fact]
    public async Task AddUserAndAddAudit_Commit_RecordsExists()
    {
        //arrange
        var user = User.CreateSample();
        var audit = UserAudit.CreateSample(user.Id);

        IMongoDatabase db = _mongoRsResource.CreateDatabase();
        await db.CreateCollectionAsync("users");
        await db.CreateCollectionAsync("audit");

        using (IClientSessionHandle session =
            await _mongoRsResource.Client.StartSessionAsync() )
        {
            var repo = new UserRepositoryWithTransaction(db);
            session.StartTransaction();

            //act
            await repo.AddAsync(user, session);
            await repo.AddAuditAsync(audit, session);

            await session.CommitTransactionAsync();
        }


        //assert
        User createdUser = await GetUserAsync(db, user.Id);
        createdUser.Should().BeEquivalentTo(user);

        UserAudit createdAudit = await GetUserAuditAsync(db, audit.Id);
        createdAudit.Should().BeEquivalentTo(audit);
    }
}

When using transaction in mongo you need to create the collection prior starting a transaction.

More samples are available in our samples repo.

Last updated on 11/16/2019 by Philippe Birbaum
← MongoDBSQL Server →
  • Install
  • Basic usage

Community

Contributor License AgreementsCode of Conduct
Swiss Life OSSSwiss Life | OSS
Copyright © 2021 Swiss Life Developers