private static async Task <EstateReportingContext> GetContext(String databaseName,
                                                                      TestDatabaseType databaseType = TestDatabaseType.InMemory)
        {
            EstateReportingContext context = null;

            if (databaseType == TestDatabaseType.InMemory)
            {
                DbContextOptionsBuilder <EstateReportingContext> builder = new DbContextOptionsBuilder <EstateReportingContext>().UseInMemoryDatabase(databaseName)
                                                                           .ConfigureWarnings(w => w.Ignore(InMemoryEventId.TransactionIgnoredWarning));
                context = new EstateReportingContext(builder.Options);
            }
            else if (databaseType == TestDatabaseType.SqliteInMemory)
            {
                SqliteConnection inMemorySqlite = new SqliteConnection("Data Source=:memory:");
                inMemorySqlite.Open();

                DbContextOptionsBuilder <EstateReportingContext> builder = new DbContextOptionsBuilder <EstateReportingContext>().UseSqlite(inMemorySqlite);
                context = new EstateReportingContext(builder.Options);
                await context.Database.MigrateAsync();
            }
            else
            {
                throw new NotSupportedException($"Database type [{databaseType}] not supported");
            }

            return(context);
        }
        public async Task EstateManagementRepository_GetMerchants_MerchantRetrieved(TestDatabaseType testDatabaseType)
        {
            EstateReportingContext context = await EstateManagementRepositoryTests.GetContext(Guid.NewGuid().ToString("N"), testDatabaseType);

            context.Merchants.Add(TestData.MerchantEntity);
            context.MerchantContacts.Add(TestData.MerchantContactEntity);
            context.MerchantAddresses.Add(TestData.MerchantAddressEntity);
            context.MerchantDevices.Add(TestData.MerchantDeviceEntity);
            context.MerchantOperators.Add(TestData.MerchantOperatorEntity);
            context.MerchantSecurityUsers.Add(TestData.MerchantSecurityUserEntity);

            await context.SaveChangesAsync();

            var dbContextFactory = this.GetMockDbContextFactory();
            Mock <IModelFactory> modelFactory = new Mock <IModelFactory>();

            dbContextFactory.Setup(d => d.GetContext(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(context);
            modelFactory.Setup(m => m.ConvertFrom(It.IsAny <EstateReporting.Database.Entities.Merchant>(),
                                                  It.IsAny <List <MerchantAddress> >(),
                                                  It.IsAny <List <MerchantContact> >(),
                                                  It.IsAny <List <MerchantOperator> >(),
                                                  It.IsAny <List <MerchantDevice> >(),
                                                  It.IsAny <List <MerchantSecurityUser> >()));
            EstateManagementRepository estateManagementRepository = new EstateManagementRepository(dbContextFactory.Object, modelFactory.Object);

            List <Merchant> merchantListModel = await estateManagementRepository.GetMerchants(TestData.EstateId, CancellationToken.None);

            merchantListModel.ShouldNotBeNull();
            merchantListModel.ShouldNotBeEmpty();
            merchantListModel.ShouldHaveSingleItem();
        }
        public async Task EstateManagementRepository_GetEstate_EstateRetrieved(TestDatabaseType testDatabaseType)
        {
            EstateReportingContext context = await EstateManagementRepositoryTests.GetContext(Guid.NewGuid().ToString("N"), testDatabaseType);

            context.Estates.Add(TestData.EstateEntity);
            context.EstateOperators.Add(TestData.EstateOperatorEntity);
            context.EstateSecurityUsers.Add(TestData.EstateSecurityUserEntity);
            await context.SaveChangesAsync();

            var dbContextFactory = this.GetMockDbContextFactory();
            Mock <IModelFactory> modelFactory = new Mock <IModelFactory>();

            dbContextFactory.Setup(d => d.GetContext(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(context);
            modelFactory.Setup(m => m.ConvertFrom(It.IsAny <Estate>(), It.IsAny <List <EstateOperator> >(), It.IsAny <List <EstateSecurityUser> >()))
            .Returns(new Models.Estate.Estate());
            EstateManagementRepository estateManagementRepository = new EstateManagementRepository(dbContextFactory.Object, modelFactory.Object);

            Models.Estate.Estate estateModel = await estateManagementRepository.GetEstate(TestData.EstateId, CancellationToken.None);

            estateModel.ShouldNotBeNull();
        }
        public async Task EstateManagementRepository_GetContracts_ContractsRetrieved(TestDatabaseType testDatabaseType)
        {
            EstateReportingContext context = await EstateManagementRepositoryTests.GetContext(Guid.NewGuid().ToString("N"), testDatabaseType);

            context.Contracts.Add(TestData.ContractEntity);
            context.ContractProducts.Add(TestData.ContractProductEntity);
            context.EstateOperators.Add(TestData.EstateOperatorEntity);
            await context.SaveChangesAsync();

            var dbContextFactory = this.GetMockDbContextFactory();
            Mock <IModelFactory> modelFactory = new Mock <IModelFactory>();

            dbContextFactory.Setup(d => d.GetContext(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(context);
            modelFactory.Setup(m => m.ConvertFrom(It.IsAny <Contract>(), It.IsAny <List <ContractProduct> >(), It.IsAny <List <ContractProductTransactionFee> >()))
            .Returns(TestData.ContractModel);
            EstateManagementRepository estateManagementRepository = new EstateManagementRepository(dbContextFactory.Object, modelFactory.Object);

            List <Models.Contract.Contract> contractModelList = await estateManagementRepository.GetContracts(TestData.EstateId, CancellationToken.None);

            contractModelList.ShouldNotBeNull();
            contractModelList.ShouldNotBeEmpty();
        }
        private async Task <EstateReportingGenericContext> GetContext(String databaseName, TestDatabaseType databaseType = TestDatabaseType.InMemory)
        {
            EstateReportingGenericContext context = null;

            if (databaseType == TestDatabaseType.InMemory)
            {
                DbContextOptionsBuilder <EstateReportingGenericContext> builder = new DbContextOptionsBuilder <EstateReportingGenericContext>()
                                                                                  .UseInMemoryDatabase(databaseName)
                                                                                  .ConfigureWarnings(w => w.Ignore(InMemoryEventId.TransactionIgnoredWarning));
                context = new EstateReportingSqlServerContext(builder.Options);
            }
            else
            {
                throw new NotSupportedException($"Database type [{databaseType}] not supported");
            }

            return(context);
        }
        public async Task EstateManagementRepository_GetEstate_EstateNotFound_ErrorThrown(TestDatabaseType testDatabaseType)
        {
            EstateReportingContext context = await EstateManagementRepositoryTests.GetContext(Guid.NewGuid().ToString("N"), testDatabaseType);

            var dbContextFactory = this.GetMockDbContextFactory();
            Mock <IModelFactory> modelFactory = new Mock <IModelFactory>();

            dbContextFactory.Setup(d => d.GetContext(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(context);
            modelFactory.Setup(m => m.ConvertFrom(It.IsAny <Estate>(), It.IsAny <List <EstateOperator> >(), It.IsAny <List <EstateSecurityUser> >()))
            .Returns(new Models.Estate.Estate());
            EstateManagementRepository estateManagementRepository = new EstateManagementRepository(dbContextFactory.Object, modelFactory.Object);

            Should.Throw <NotFoundException>(async() => { await estateManagementRepository.GetEstate(TestData.EstateId, CancellationToken.None); });
        }
        public async Task EstateManagementRepository_GetTransactionFeesForProduct_TransactionFeesForProductRetrieved(TestDatabaseType testDatabaseType)
        {
            EstateReportingContext context = await EstateManagementRepositoryTests.GetContext(Guid.NewGuid().ToString("N"), testDatabaseType);

            context.ContractProductTransactionFees.Add(TestData.ContractProductTransactionFeeEntity);
            await context.SaveChangesAsync();

            var dbContextFactory = this.GetMockDbContextFactory();
            Mock <IModelFactory> modelFactory = new Mock <IModelFactory>();

            dbContextFactory.Setup(d => d.GetContext(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(context);
            modelFactory.Setup(m => m.ConvertFrom(It.IsAny <List <ContractProductTransactionFee> >())).Returns(TestData.ProductTransactionFees);
            EstateManagementRepository estateManagementRepository = new EstateManagementRepository(dbContextFactory.Object, modelFactory.Object);

            List <ContractProductTransactionFeeModel> transactionFeesModel =
                await estateManagementRepository.GetTransactionFeesForProduct(TestData.EstateId,
                                                                              TestData.MerchantId,
                                                                              TestData.ContractId,
                                                                              TestData.ProductId,
                                                                              CancellationToken.None);

            transactionFeesModel.ShouldNotBeNull();
            transactionFeesModel.ShouldHaveSingleItem();
        }
        public async Task EstateManagementRepository_GetContract_ContractNotFound_ErrorThrown(TestDatabaseType testDatabaseType)
        {
            EstateReportingContext context = await EstateManagementRepositoryTests.GetContext(Guid.NewGuid().ToString("N"), testDatabaseType);

            var dbContextFactory = this.GetMockDbContextFactory();
            Mock <IModelFactory> modelFactory = new Mock <IModelFactory>();

            dbContextFactory.Setup(d => d.GetContext(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(context);
            modelFactory.Setup(m => m.ConvertFrom(It.IsAny <Contract>(), It.IsAny <List <ContractProduct> >(), It.IsAny <List <ContractProductTransactionFee> >()))
            .Returns(TestData.ContractModel);
            EstateManagementRepository estateManagementRepository = new EstateManagementRepository(dbContextFactory.Object, modelFactory.Object);

            Should.Throw <NotFoundException>(async() =>
            {
                await estateManagementRepository.GetContract(TestData.EstateId,
                                                             TestData.ContractId,
                                                             false,
                                                             false,
                                                             CancellationToken.None);
            });
        }