public async Task AddAsync(TModel entity) { await _mongoContext.AddCommand(async() => { await _mongoCollection.InsertOneAsync(entity); }); }
public async Task GivenTransactionsAreNotSupported_WhenSaveChangesCalled_TheCommandsAreActionedWithoutATransaction() { // arrange var target = new MongoContext(clientProvider, loggerFactory); Mock.Get(client) .Setup(x => x.CanSupportTransactions) .Returns(false) .Verifiable(); bool commandHasRun = false; target.AddCommand(() => Task.Run(() => commandHasRun = true)); // act await target.SaveChangesAsync(); // assert Assert.True(commandHasRun); Mock.Get(client).Verify(x => x.StartSessionAsync(null, default), Times.Never); }
public async Task GivenTransactionsAreSupported_WhenSaveChangesCalled_TheCommandsAreActionedWithinATransaction() { // arrange var target = new MongoContext(clientProvider, loggerFactory); var sessionHandle = Mock.Of <IClientSessionHandle>(); Mock.Get(client) .Setup(x => x.CanSupportTransactions) .Returns(true) .Verifiable(); Mock.Get(sessionHandle) .Setup(x => x.StartTransaction(null)) .Verifiable(); Mock.Get(sessionHandle) .Setup(x => x.CommitTransactionAsync(default(CancellationToken))) .Returns(Task.FromResult(0)) .Verifiable(); Mock.Get(client) .Setup(x => x.StartSessionAsync(null, default(CancellationToken))) .ReturnsAsync(sessionHandle) .Verifiable(); bool commandHasRun = false; target.AddCommand(() => Task.Run(() => commandHasRun = true)); // act await target.SaveChangesAsync(); // assert Assert.True(commandHasRun); Mock.Verify(Mock.Get(sessionHandle)); }
public virtual void Add(TEntity obj) => _context.AddCommand(async() => await _dbSet.InsertOneAsync(obj));