Exemple #1
0
        public async Task ThenAHistoryRecordIsCreated()
        {
            MockCommitmentRespository.Setup(x => x.GetCommitmentById(123L)).ReturnsAsync(new Commitment
            {
                Id = 123L,
                EmployerAccountId = ExampleValidRequest.AccountId
            });

            var expectedOriginalApprenticeshipState = JsonConvert.SerializeObject(TestApprenticeship);

            await Handler.Handle(ExampleValidRequest);

            var expectedNewApprenticeshipState = JsonConvert.SerializeObject(TestApprenticeship);

            MockHistoryRepository.Verify(
                x =>
                x.InsertHistory(
                    It.Is <IEnumerable <HistoryItem> >(
                        y =>
                        y.First().ChangeType == ApprenticeshipChangeType.ChangeOfStatus.ToString() &&
                        y.First().CommitmentId == null &&
                        y.First().ApprenticeshipId == TestApprenticeship.Id &&
                        y.First().OriginalState == expectedOriginalApprenticeshipState &&
                        y.First().UpdatedByRole == CallerType.Employer.ToString() &&
                        y.First().UpdatedState == expectedNewApprenticeshipState &&
                        y.First().UserId == ExampleValidRequest.UserId &&
                        y.First().UpdatedByName == ExampleValidRequest.UserName)), Times.Once);
        }
        private IMigrationsScaffolder CreateMigrationScaffolder <TContext>()
            where TContext : DbContext, new()
        {
            var currentContext = new CurrentDbContext(new TContext());
            var idGenerator    = new MigrationsIdGenerator();
            var code           = new CSharpHelper();
            var reporter       = new TestOperationReporter();
            var migrationAssembly
                = new MigrationsAssembly(
                      currentContext,
                      new DbContextOptions <TContext>().WithExtension(new FakeRelationalOptionsExtension()),
                      idGenerator);
            var historyRepository = new MockHistoryRepository();

            var services = RelationalTestHelpers.Instance.CreateContextServices();

            return(new MigrationsScaffolder(
                       new MigrationsScaffolderDependencies(
                           currentContext,
                           new Model(),
                           migrationAssembly,
                           new MigrationsModelDiffer(
                               new FallbackRelationalCoreTypeMapper(
                                   TestServiceFactory.Instance.Create <CoreTypeMapperDependencies>(),
                                   TestServiceFactory.Instance.Create <RelationalTypeMapperDependencies>(),
                                   TestServiceFactory.Instance.Create <TestRelationalTypeMapper>()),
                               new MigrationsAnnotationProvider(new MigrationsAnnotationProviderDependencies()),
                               services.GetRequiredService <IChangeDetector>(),
                               services.GetRequiredService <StateManagerDependencies>(),
                               services.GetRequiredService <CommandBatchPreparerDependencies>()),
                           idGenerator,
                           new MigrationsCodeGeneratorSelector(
                               new[]
            {
                new CSharpMigrationsGenerator(
                    new MigrationsCodeGeneratorDependencies(),
                    new CSharpMigrationsGeneratorDependencies(
                        code,
                        new CSharpMigrationOperationGenerator(
                            new CSharpMigrationOperationGeneratorDependencies(code)),
                        new CSharpSnapshotGenerator(new CSharpSnapshotGeneratorDependencies(code))))
            }),
                           historyRepository,
                           reporter,
                           new MockProvider(),
                           new SnapshotModelProcessor(reporter),
                           new Migrator(
                               migrationAssembly,
                               historyRepository,
                               services.GetRequiredService <IDatabaseCreator>(),
                               services.GetRequiredService <IMigrationsSqlGenerator>(),
                               services.GetRequiredService <IRawSqlCommandBuilder>(),
                               services.GetRequiredService <IMigrationCommandExecutor>(),
                               services.GetRequiredService <IRelationalConnection>(),
                               services.GetRequiredService <ISqlGenerationHelper>(),
                               services.GetRequiredService <IDiagnosticsLogger <DbLoggerCategory.Migrations> >(),
                               services.GetRequiredService <IDatabaseProvider>()))));
        }
        private IMigrationsScaffolder CreateMigrationScaffolder <TContext>()
            where TContext : DbContext, new()
        {
            var currentContext             = new CurrentDbContext(new TContext());
            var idGenerator                = new MigrationsIdGenerator();
            var sqlServerTypeMappingSource = new SqlServerTypeMappingSource(
                TestServiceFactory.Instance.Create <TypeMappingSourceDependencies>(),
                TestServiceFactory.Instance.Create <RelationalTypeMappingSourceDependencies>());
            var sqlServerAnnotationCodeGenerator = new SqlServerAnnotationCodeGenerator(
                new AnnotationCodeGeneratorDependencies(sqlServerTypeMappingSource));
            var code     = new CSharpHelper(sqlServerTypeMappingSource);
            var reporter = new TestOperationReporter();
            var migrationAssembly
                = new MigrationsAssembly(
                      currentContext,
                      new DbContextOptions <TContext>().WithExtension(new FakeRelationalOptionsExtension()),
                      idGenerator,
                      new FakeDiagnosticsLogger <DbLoggerCategory.Migrations>());
            var historyRepository = new MockHistoryRepository();

            var services = RelationalTestHelpers.Instance.CreateContextServices();
            var model    = new Model().FinalizeModel();

            model.AddRuntimeAnnotation(RelationalAnnotationNames.RelationalModel, new RelationalModel(model));

            return(new MigrationsScaffolder(
                       new MigrationsScaffolderDependencies(
                           currentContext,
                           model,
                           migrationAssembly,
                           new MigrationsModelDiffer(
                               new TestRelationalTypeMappingSource(
                                   TestServiceFactory.Instance.Create <TypeMappingSourceDependencies>(),
                                   TestServiceFactory.Instance.Create <RelationalTypeMappingSourceDependencies>()),
                               new MigrationsAnnotationProvider(
                                   new MigrationsAnnotationProviderDependencies()),
                               services.GetRequiredService <IChangeDetector>(),
                               services.GetRequiredService <IUpdateAdapterFactory>(),
                               services.GetRequiredService <CommandBatchPreparerDependencies>()),
                           idGenerator,
                           new MigrationsCodeGeneratorSelector(
                               new[]
            {
                new CSharpMigrationsGenerator(
                    new MigrationsCodeGeneratorDependencies(
                        sqlServerTypeMappingSource,
                        sqlServerAnnotationCodeGenerator),
                    new CSharpMigrationsGeneratorDependencies(
                        code,
                        new CSharpMigrationOperationGenerator(
                            new CSharpMigrationOperationGeneratorDependencies(
                                code)),
                        new CSharpSnapshotGenerator(
                            new CSharpSnapshotGeneratorDependencies(
                                code, sqlServerTypeMappingSource, sqlServerAnnotationCodeGenerator))))
            }),
                           historyRepository,
                           reporter,
                           new MockProvider(),
                           new SnapshotModelProcessor(reporter, services.GetRequiredService <IModelRuntimeInitializer>()),
                           new Migrator(
                               migrationAssembly,
                               historyRepository,
                               services.GetRequiredService <IDatabaseCreator>(),
                               services.GetRequiredService <IMigrationsSqlGenerator>(),
                               services.GetRequiredService <IRawSqlCommandBuilder>(),
                               services.GetRequiredService <IMigrationCommandExecutor>(),
                               services.GetRequiredService <IRelationalConnection>(),
                               services.GetRequiredService <ISqlGenerationHelper>(),
                               services.GetRequiredService <ICurrentDbContext>(),
                               services.GetRequiredService <IModelRuntimeInitializer>(),
                               services.GetRequiredService <IDiagnosticsLogger <DbLoggerCategory.Migrations> >(),
                               services.GetRequiredService <IRelationalCommandDiagnosticsLogger>(),
                               services.GetRequiredService <IDatabaseProvider>()))));
        }