private (Elector elector, ElectorExtension electorExtension, ElectorsProperty property) SaveElectorAndAssociatedEntitiesToDatabase(DateTime?birthdate = null,
                                                                                                                                           string firstName   = null, int id = 0)
        {
            var property = _fixture.Build <ElectorsProperty>()
                           .With(e => e.Uprn, _fixture.Create <int>().ToString).Create();

            ElectoralRegisterContext.Properties.Add(property);
            ElectoralRegisterContext.SaveChanges();

            var elector = _fixture.Build <Elector>()
                          .Without(e => e.ElectorsProperty)
                          .Without(e => e.ElectorExtension)
                          .With(e => e.PropertyId, property.Id)
                          .Create();

            elector.FirstName = firstName ?? elector.FirstName;
            if (id != 0)
            {
                elector.Id = id;
            }
            ElectoralRegisterContext.Electors.Add(elector);
            ElectoralRegisterContext.SaveChanges();

            var electorExtension = _fixture.Build <ElectorExtension>()
                                   .With(e => e.Id, elector.Id)
                                   .With(e => e.DateOfBirth, birthdate)
                                   .Create();

            ElectoralRegisterContext.ElectorExtensions.Add(electorExtension);
            ElectoralRegisterContext.SaveChanges();
            return(elector, electorExtension, property);
        }
 public void BaseSetup()
 {
     _factory = new MockWebApplicationFactory <TStartup>(_connection);
     Client   = _factory.CreateClient();
     ElectoralRegisterContext = new ElectoralRegisterContext(_builder.Options);
     ElectoralRegisterContext.Database.EnsureCreated();
     _transaction = ElectoralRegisterContext.Database.BeginTransaction();
 }
        public void RunBeforeAnyTests()
        {
            var builder = new DbContextOptionsBuilder();

            builder.UseNpgsql(ConnectionString.TestDatabase());
            ElectoralRegisterContext = new ElectoralRegisterContext(builder.Options);

            ElectoralRegisterContext.Database.EnsureCreated();
            _transaction = ElectoralRegisterContext.Database.BeginTransaction();
        }
示例#4
0
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            builder.ConfigureAppConfiguration(b => b.AddEnvironmentVariables())
            .UseStartup <Startup>();
            builder.ConfigureServices(services =>
            {
                var dbBuilder = new DbContextOptionsBuilder();
                dbBuilder.UseNpgsql(_connection);
                var context = new ElectoralRegisterContext(dbBuilder.Options);
                services.AddSingleton(context);

                var serviceProvider = services.BuildServiceProvider();
                var dbContext       = serviceProvider.GetRequiredService <ElectoralRegisterContext>();

                dbContext.Database.EnsureCreated();
            });
        }
        public static ResidentResponse SaveResidentsElectorRecordsToTheDatabase(ElectoralRegisterContext context, string firstName = null, string lastName = null, int id = 0)
        {
            var property = _fixture.Build <ElectorsProperty>()
                           .With(e => e.Uprn, _fixture.Create <int>().ToString).Create();

            context.Properties.Add(property);
            context.SaveChanges();

            var elector = _fixture.Build <Elector>()
                          .Without(e => e.ElectorsProperty)
                          .Without(e => e.ElectorExtension)
                          .With(e => e.PropertyId, property.Id)
                          .Create();

            elector.FirstName = firstName ?? elector.FirstName;
            elector.LastName  = lastName ?? elector.LastName;
            if (id != 0)
            {
                elector.Id = id;
            }

            context.Electors.Add(elector);
            context.SaveChanges();

            var electorExtension = _fixture.Build <ElectorExtension>()
                                   .With(e => e.Id, elector.Id)
                                   .Create();

            context.ElectorExtensions.Add(electorExtension);
            context.SaveChanges();

            var expectedResponse = new ResidentResponse
            {
                Id          = elector.Id,
                Email       = elector.Email,
                Nationality = elector.Nationality,
                Title       = elector.Title,
                Uprn        = Convert.ToInt32(property.Uprn),
                FirstName   = elector.FirstName,
                LastName    = elector.LastName,
                MiddleName  = electorExtension.MiddleName,
                DateOfBirth = electorExtension.DateOfBirth.ToString("yyyy-MM-dd")
            };

            return(expectedResponse);
        }
示例#6
0
 public ElectoralRegisterGateway(ElectoralRegisterContext electoralRegisterContext)
 {
     _electoralRegisterContext = electoralRegisterContext;
 }