public InstrumentsServiceTests() { // Setup an actual in-memory Sqlite for db mocking var optionsBuilder = new DbContextOptionsBuilder <ForexMinerHeimdallrDbContext>(); optionsBuilder.UseSqlite("Filename=:memory:"); _dbContext = new ForexMinerHeimdallrDbContext(optionsBuilder.Options); _dbContext.Database.OpenConnection(); _dbContext.Database.Migrate(); // Auto mapper var contractContract = new ContractContractMappings(); var databaseContract = new DatabaseContractMappings(); var oandaContract = new OandaContractMappings(); var configuration = new MapperConfiguration(cfg => { cfg.AddProfile(contractContract); cfg.AddProfile(databaseContract); cfg.AddProfile(oandaContract); }); _mapper = new Mapper(configuration); // Class under test _instrumentService = new InstrumentService(_dbContext, _mapper); }
public ConnectionServiceTests() { // Setup an actual in-memory Sqlite for db mocking var optionsBuilder = new DbContextOptionsBuilder <ForexMinerHeimdallrDbContext>(); optionsBuilder.UseSqlite("Filename=:memory:"); _dbContext = new ForexMinerHeimdallrDbContext(optionsBuilder.Options); _dbContext.Database.OpenConnection(); _dbContext.Database.Migrate(); // Auto mapper var contractContract = new ContractContractMappings(); var databaseContract = new DatabaseContractMappings(); var oandaContract = new OandaContractMappings(); var configuration = new MapperConfiguration(cfg => { cfg.AddProfile(contractContract); cfg.AddProfile(databaseContract); cfg.AddProfile(oandaContract); }); _mapper = new Mapper(configuration); // Mocks _connectionsSecretServiceMock = new Mock <IConnectionsSecretService>(); _oandaApiFactoryMock = new Mock <IOandaApiConnectionFactory>(); // Class under test _connectionService = new ConnectionService(_dbContext, _connectionsSecretServiceMock.Object, _mapper, _oandaApiFactoryMock.Object); }
public static void MigrateDatabase(this ForexMinerHeimdallrDbContext dbContext, bool isDevEnvironment) { try { if (dbContext.Database.GetPendingMigrations().Count() > 0) { dbContext.Database.Migrate(); } } catch (SqlException ex) { // Swallow migration race condition exception on dev docker containers if (!isDevEnvironment) { throw ex; } else { // Retry in 5 sec for dev // as healthchecks doesn't stop it failing Thread.Sleep(5000); if (dbContext.Database.GetPendingMigrations().Count() > 0) { dbContext.Database.Migrate(); } } } }
public ConnectionService(ForexMinerHeimdallrDbContext dbContext, IConnectionsSecretService connectionsSecretService, IMapper mapper, IOandaApiConnectionFactory oandaApiConnectionFactory) { _dbContext = dbContext; _connectionsSecretService = connectionsSecretService; _mapper = mapper; _oandaApiConnectionFactory = oandaApiConnectionFactory; }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, ForexMinerHeimdallrDbContext dbContext) { // Custom app.UseConnectionsApiServices(_environment, dbContext); // System app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
/// <summary> /// Instruments history service constructor /// Sets up the required services and objects /// </summary> /// <param name="configuration">Configuration object</param> /// <param name="httpClientFactory">Http client factory</param> /// <param name="dbContext">Database context</param> /// <param name="instrumentStorageService">Instrument storage service</param> /// <param name="mapper">Auto mapper service</param> public InstrumentHistoryService( IConfiguration configuration, IHttpClientFactory httpClientFactory, ForexMinerHeimdallrDbContext dbContext, IInstrumentStorageService instrumentStorageService, IMapper mapper, IOandaApiConnectionFactory oandaApiConnectionFactory ) { _configuration = configuration; _httpClientFactory = httpClientFactory; _retryPolicy = Policy.Handle <HttpRequestException>().RetryAsync(int.Parse(_configuration["forex-miner-thor:Max-Retries"])); _dbContext = dbContext; _instrumentStorageService = instrumentStorageService; _mapper = mapper; _oandaApiConnectionFactory = oandaApiConnectionFactory; }
public InstrumentHistoryServiceTests() { // Setup an actual in-memory Sqlite for db mocking var optionsBuilder = new DbContextOptionsBuilder <ForexMinerHeimdallrDbContext>(); optionsBuilder.UseSqlite("Filename=:memory:"); _dbContext = new ForexMinerHeimdallrDbContext(optionsBuilder.Options); _dbContext.Database.OpenConnection(); _dbContext.Database.Migrate(); // Auto mapper var contractContract = new ContractContractMappings(); var databaseContract = new DatabaseContractMappings(); var oandaContract = new OandaContractMappings(); var configuration = new MapperConfiguration(cfg => { cfg.AddProfile(contractContract); cfg.AddProfile(databaseContract); cfg.AddProfile(oandaContract); }); _mapper = new Mapper(configuration); // Mocks _configurationMock = new Mock <IConfiguration>(); _httpClientFactoryMock = new Mock <IHttpClientFactory>(); _instrumentStorageServiceMock = new Mock <IInstrumentStorageService>(); _oandaApiConnectionFactoryMock = new Mock <IOandaApiConnectionFactory>(); _configurationMock.SetupGet(c => c[It.Is <string>(cv => cv == "forex-miner-thor:Max-Retries")]).Returns("5"); _configurationMock.SetupGet(c => c[It.Is <string>(cv => cv == "forex-miner-thor:Name")]).Returns("forex-miner-thor"); _configurationMock.SetupGet(c => c[It.Is <string>(cv => cv == "forex-miner-thor:Content-Type")]).Returns("application/json"); _configurationMock.SetupGet(c => c[It.Is <string>(cv => cv == "Oanda-MasterToken")]).Returns("FakeToken"); // Class under test _instrumentHistoryService = new InstrumentHistoryService( _configurationMock.Object, _httpClientFactoryMock.Object, _dbContext, _instrumentStorageServiceMock.Object, _mapper, _oandaApiConnectionFactoryMock.Object ); }
public static void UseConnectionsApiServices(this IApplicationBuilder app, IWebHostEnvironment environment, ForexMinerHeimdallrDbContext dbContext) { // CORS app.UseCorsPolicy(); // ProblemDetails app.UseProblemDetails(environment.IsDevelopment()); }
/// <summary> /// Instrument service implementation constructor. /// Sets up the database context. /// </summary> /// <param name="dbContext"></param> public InstrumentService(ForexMinerHeimdallrDbContext dbContext, IMapper mapper) { _dbContext = dbContext; _mapper = mapper; }
/// <summary> /// Use the added services /// </summary> /// <param name="app">The application builder</param> /// <param name="environment">The environment</param> /// <param name="dbContext">The database context</param> /// <param name="seedService">The entity seed service</param> public static void UseUsersApiServices(this IApplicationBuilder app, IWebHostEnvironment environment, ForexMinerHeimdallrDbContext dbContext, IEntitySeedService seedService) { // CORS app.UseCorsPolicy(); // ProblemDetails app.UseProblemDetails(environment.IsDevelopment()); // Database migration dbContext.MigrateDatabase(environment.IsDevelopment()); // Seeding seedService.Seed(); }
/// <summary> /// Initialize user service /// /// Setups the database context, the object auto mapper and the configuration /// </summary> /// <param name="dbContext"></param> /// <param name="mapper"></param> /// <param name="configuration"></param> public UserService(ForexMinerHeimdallrDbContext dbContext, IMapper mapper, IConfiguration configuration) { _dbContext = dbContext; _mapper = mapper; _configuration = configuration; }