This commit is contained in:
meysamhadeli 2022-07-01 22:30:49 +04:30
parent a702916b91
commit efcf745edc
9 changed files with 29 additions and 49 deletions

View File

@ -0,0 +1 @@
<mxfile host="Electron" modified="2022-06-29T20:19:24.031Z" agent="5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/19.0.2 Chrome/102.0.5005.63 Electron/19.0.3 Safari/537.36" etag="Qjh03weoUslFN_OtWUfm" version="19.0.2" type="device"><diagram id="C5RBs43oDa-KdzZeNtuy" name="Page-1">7V1Ze6JIFP01PupXC+tjTGKSnp6Z9JieZOYlH0qpTBAMoIn966fYBUqMSgkx+JDIpaqs5XDvubcWOvhy/n7jaIvZ77ZOzA4C+nsHX3UQEgSg0n++ZB1KEEBSKJk6hh7KYCoYGr9IJASRdGnoxM0k9Gzb9IxFVji2LYuMvYxMcxz7LZtsYpvZX11oU1IQDMeaWZQ+Gro3C6UKklP5LTGms/iXoRS1eK7FiaOWuDNNt982RPi6gy8d2/bCb/P3S2L6vRf3y+Pd+tH8/iLdfPvhvmo/+789/PF3NyxssE+WpAkOsbyDi374Mb990nToPvywFo//yHfKU78Lo8F0vXXcYUSn/Rdd2o43s6e2pZnXqbQ/Xjor4pcK6YVjLy09uAL0yn0h3ngWXaS5v9v2Ikr/H/G8dQQTbenZVDTz5mZ0N+ljP//EtrxL27SdoGJ4MMD0Q+XEinEGg9/0HPuFbCSU6GcwSO7Ewx4kJuYoKH9FHM+gKKGiD/ZuNAquvXTGUVe9P/z587/+ejS++Pb2qIBb9NMgXSFM53fjBgKjMbkh9px4zpomcIipecYqC1MtQvs0SZdkvbcNWj8EokcTYhDhch0/cCBbhqc5U+JF2VJg0C8b9UhFAVz2gY7cQocPdMKBK0kXq6jqMJYBx95IUJqFBDoYzvrJz98TBDkW/BMIAMCx4Oo9+onwar15dU8cg3YOceIqfR1wiahR4Eqsb/PAJWIxA64WWKXAgmJdwCqr9kozl9EvdZBketEwZBAnvS7t+EbXDUbigiZQweI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SJzXjZQvqdqzWlRrjO/vFSWYyq5Sa6kAGKEjyBiW7uRioh2TUVHyckdOrbRvUZoF/fFE1JrWsLs/dt+8Wwpm2UvYnc4ETHBIHkBKB1IipShdNG2ZlcOK+DLf3Cf3esP8ompbPGOAuKnNLdegwaj1WTGe1/tPLbqaw2BkpkjFMsO9J578r5zaBSbvy3bAYtliTlDLmSXxzB+b2mMZw+iK/oQc0wxBMirezVfy3SGo401irIFmkt0vZAGr1M30AeJk9f5I6v/wc=</diagram></mxfile>

View File

@ -25,6 +25,7 @@
<PackageReference Include="MagicOnion.Abstractions" Version="4.4.0" />
<PackageReference Include="MagicOnion.Client" Version="4.4.0" />
<PackageReference Include="MagicOnion.Server" Version="4.4.0" />
<PackageReference Include="Mongo2Go" Version="3.1.3" />
<PackageReference Include="NSubstitute" Version="4.3.0" />
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="protobuf-net.BuildTools" Version="3.0.115">

View File

@ -1,4 +1,5 @@
using BuildingBlocks.Core;
using BuildingBlocks.Mongo;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

View File

@ -1,3 +1,4 @@
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Driver;
@ -13,12 +14,12 @@ public class MongoDbContext : IMongoDbContext
public IMongoClient MongoClient { get; }
protected readonly IList<Func<Task>> _commands;
public MongoDbContext(MongoOptions options)
public MongoDbContext(IOptions<MongoOptions> options)
{
RegisterConventions();
MongoClient = new MongoClient(options.ConnectionString);
var databaseName = options.DatabaseName;
MongoClient = new MongoClient(options.Value.ConnectionString);
var databaseName = options.Value.DatabaseName;
Database = MongoClient.GetDatabase(databaseName);
// Every command will be stored and it'll be processed at SaveChanges

View File

@ -9,7 +9,7 @@ public class BookingDbContext : AppDbContextBase
{
public const string DefaultSchema = "dbo";
public BookingDbContext(DbContextOptions options, IHttpContextAccessor httpContextAccessor) : base(options, httpContextAccessor)
public BookingDbContext(DbContextOptions<BookingDbContext> options, IHttpContextAccessor httpContextAccessor) : base(options, httpContextAccessor)
{
}

View File

@ -24,13 +24,6 @@ public class CreateBookingTests
public CreateBookingTests(IntegrationTestFixture fixture)
{
_fixture = fixture;
_fixture.RegisterTestServices(services =>
{
MockFlightGrpcServices(services);
MockPassengerGrpcServices(services);
});
_testHarness = fixture.TestHarness;
_channel = fixture.Channel;
}
@ -48,36 +41,4 @@ public class CreateBookingTests
// Assert
response.Should().BeGreaterOrEqualTo(0);
}
private void MockPassengerGrpcServices(IServiceCollection services)
{
services.Replace(ServiceDescriptor.Singleton(x =>
{
var mock = Substitute.For<IPassengerGrpcService>();
mock.GetById(Arg.Any<long>())
.Returns(new UnaryResult<PassengerResponseDto>(new FakePassengerResponseDto().Generate()));
return mock;
}));
}
private void MockFlightGrpcServices(IServiceCollection services)
{
services.Replace(ServiceDescriptor.Singleton(x =>
{
var mock = Substitute.For<IFlightGrpcService>();
mock.GetById(Arg.Any<long>())
.Returns(new UnaryResult<FlightResponseDto>(Task.FromResult(new FakeFlightResponseDto().Generate())));
mock.GetAvailableSeats(Arg.Any<long>())
.Returns(
new UnaryResult<IEnumerable<SeatResponseDto>>(Task.FromResult(FakeSeatsResponseDto.Generate())));
mock.ReserveSeat(new FakeReserveSeatRequestDto().Generate())
.Returns(new UnaryResult<SeatResponseDto>(Task.FromResult(FakeSeatsResponseDto.Generate().First())));
return mock;
}));
}
}

View File

@ -4,6 +4,7 @@ using System.Threading.Tasks;
using Booking.Data;
using BuildingBlocks.Core.Model;
using BuildingBlocks.MassTransit;
using BuildingBlocks.Mongo;
using BuildingBlocks.Web;
using Grpc.Net.Client;
using MassTransit;
@ -16,6 +17,8 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Mongo2Go;
using NSubstitute;
using Respawn;
using Serilog;
@ -33,6 +36,8 @@ public class IntegrationTestFixture : IAsyncLifetime
{
private WebApplicationFactory<Program> _factory;
public Checkpoint Checkpoint { get; set; }
private MongoDbRunner _mongoRunner;
public Action<IServiceCollection>? TestRegistrationServices { get; set; }
public IServiceProvider ServiceProvider => _factory.Services;
public IConfiguration Configuration => _factory.Services.GetRequiredService<IConfiguration>();
@ -67,11 +72,13 @@ public class IntegrationTestFixture : IAsyncLifetime
});
Checkpoint = new Checkpoint {TablesToIgnore = new[] {"__EFMigrationsHistory"}};
TestRegistrationServices?.Invoke(services);
});
});
// _mongoRunner = MongoDbRunner.Start();
// var mongoOptions = _factory.Services.GetRequiredService<IOptions<MongoOptions>>();
// if (mongoOptions is not null)
// mongoOptions.Value.ConnectionString = _mongoRunner.ConnectionString;
return Task.CompletedTask;
}

View File

@ -8,7 +8,7 @@ namespace Flight.Data;
public class FlightReadDbContext : MongoDbContext
{
public FlightReadDbContext(IOptions<MongoOptions> options) : base(options.Value)
public FlightReadDbContext(IOptions<MongoOptions> options) : base(options)
{
Flight = GetCollection<FlightReadModel>(nameof(Flight).Underscore());
}

View File

@ -5,7 +5,9 @@ using BuildingBlocks.Core.Model;
using BuildingBlocks.EFCore;
using BuildingBlocks.MassTransit;
using BuildingBlocks.MessageProcessor;
using BuildingBlocks.Mongo;
using BuildingBlocks.Web;
using DotNetCore.CAP.MongoDB;
using Flight.Data;
using FluentAssertions.Common;
using Grpc.Net.Client;
@ -20,6 +22,8 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Mongo2Go;
using NSubstitute;
using Respawn;
using Serilog;
@ -36,6 +40,8 @@ public class FixtureCollection : ICollectionFixture<IntegrationTestFixture>
public class IntegrationTestFixture : IAsyncLifetime
{
private WebApplicationFactory<Program> _factory;
private MongoDbRunner _mongoRunner;
public Checkpoint Checkpoint { get; set; }
public Action<IServiceCollection>? TestRegistrationServices { get; set; }
public IServiceProvider ServiceProvider => _factory.Services;
@ -67,7 +73,6 @@ public class IntegrationTestFixture : IAsyncLifetime
builder.UseEnvironment("test");
builder.ConfigureServices(services =>
{
TestRegistrationServices?.Invoke(services);
services.AddMassTransitTestHarness(x =>
{
x.UsingRabbitMq((context, cfg) =>
@ -85,11 +90,13 @@ public class IntegrationTestFixture : IAsyncLifetime
});
Checkpoint = new Checkpoint {TablesToIgnore = new[] {"__EFMigrationsHistory"}};
TestRegistrationServices?.Invoke(services);
});
});
_mongoRunner = MongoDbRunner.Start();
var mongoOptions = _factory.Services.GetRequiredService<IOptions<MongoOptions>>();
mongoOptions.Value.ConnectionString = _mongoRunner.ConnectionString;
return Task.CompletedTask;
}
@ -99,6 +106,7 @@ public class IntegrationTestFixture : IAsyncLifetime
await Checkpoint.Reset(Configuration?.GetConnectionString("DefaultConnection"));
await _factory.DisposeAsync();
_mongoRunner.Dispose();
}
public async Task ExecuteScopeAsync(Func<IServiceProvider, Task> action)