Exemple #1
0
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    var context = services.
                                  GetRequiredService <ProjectContext>();
                    context.Database.EnsureCreated();
                    SeedProjectTechnician.Initialize(services);
                    SeedIdentityData.Initialize(services);
                    context.Database.Migrate();
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
            }

            host.Run();
        }
        // Creates and seeds an ProjectContext with test data; then calls  test action.
        private async Task PrepareTestContext(TestAction testAction)
        {
            // Database is in memory as long the connection is open
            var connection = new SqliteConnection("DataSource=:memory:");

            try
            {
                connection.Open();

                var options = new DbContextOptionsBuilder <ProjectContext>()
                              .UseSqlite(connection)
                              .Options;

                // Create the schema in the database and seeds with test data
                using (var context = new ProjectContext(options))
                {
                    context.Database.EnsureCreated();
                    SeedProjectTechnician.Initialize(context);

                    await testAction(context);
                }
            }
            finally
            {
                connection.Close();
            }
        }
        public async Task OnGetAsyncPopulatesPageModel()
        {
            // Arrange: seed database with test data
            await PrepareTestContext(async(context) =>
            {
                var expectedTechnicians = SeedProjectTechnician.GetSeedingTechnicians();

                // Act: retrieve Technicians
                var pageModel = new IndexModel(context);
                await pageModel.OnGetAsync();

                // Assert: seeded and retrieved Technicians match
                var actualMessages = Assert.IsAssignableFrom <List <Technician> >(pageModel.Technician);
                Assert.Equal(
                    expectedTechnicians.Select(a => a.Name),
                    actualMessages.Select(a => a.Name));
            });
        }
Exemple #4
0
      public async Task OnGetAsyncPopulatesPageModel()
      {
          // Arrange: seed database with test data
          await PrepareTestContext(async(context) =>
            {
                var expectedProjects = SeedProjectTechnician.GetSeedingProjects();

                // Act: retrieve Projects
                var pageModel = new IndexModel(context);
                await pageModel.OnGetAsync(id, tech);

                // Assert: seeded and retrieved Projects match
                //var actualMessages = Assert.IsAssignableFrom<List<Project>>(pageModel.Project);
                //Assert.Equal(
                //  expectedProjects.Select(a => a.Title),
                //actualMessages.Select(a => a.Title));
            });
      }
        public async Task ProjectsAreRetrievedTest()
        {
            using (var db = new ProjectContext(Utilities.TestDbContextOptions()))
            {
                // Arrange: seed database with Technicians

                var expectedProjects = SeedProjectTechnician.GetSeedingProjects();
                await db.AddRangeAsync(expectedProjects);

                await db.SaveChangesAsync();

                // Act: retrieve seeded Projects from database
                var result = await db.GetProjectsAsync();

                // Assert: seeded and retrieved Projects match
                var actualProjects = Assert.IsAssignableFrom <List <Project> >(result);
                Assert.Equal(
                    expectedProjects.OrderBy(a => a.ProjectID).Select(a => a.Title),
                    actualProjects.OrderBy(a => a.ProjectID).Select(a => a.Title));
            }
        }