Beispiel #1
0
        public async Task GetAllByManufacturerAsync_WithNullManufacturerId_ReturnsNull()
        {
            // Arrange
            var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>()
                                              .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                              .Options);

            context.CarModels.Add(new CarModel
            {
                Model        = "OtherModel",
                Manufacturer = new CarManufacturer
                {
                    Name = "OtherManufacturer"
                }
            });

            context.SaveChanges();

            var modelsService = new ModelsService(new EfRepository <CarModel>(context));

            // Act
            var result = await modelsService.GetAllByManufacturerAsync(null);

            // Assert
            Assert.Null(result);
        }
Beispiel #2
0
        public async Task GetAllByManufacturerAsync_WithData_WorksCorrectly()
        {
            // Arrange
            var expectedResults = new[] { "Model1", "Model2", "Model3" }
            .OrderBy(m => m)
            .ToArray();

            var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>()
                                              .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                              .Options);

            var manufacturer = new CarManufacturer
            {
                Name = "TestManufacturer"
            };

            foreach (var model in expectedResults)
            {
                context.CarModels.Add(new CarModel
                {
                    Model        = model,
                    Manufacturer = manufacturer
                });
            }

            context.CarModels.Add(new CarModel
            {
                Model        = "OtherModel",
                Manufacturer = new CarManufacturer
                {
                    Name = "OtherManufacturer"
                }
            });

            context.SaveChanges();

            var modelsService = new ModelsService(new EfRepository <CarModel>(context));

            // Act
            var actualResults = (await modelsService.GetAllByManufacturerAsync(manufacturer.Id))
                                .Select(m => m.Model)
                                .OrderBy(m => m)
                                .ToArray();

            // Assert
            Assert.Equal(expectedResults, actualResults);
        }
Beispiel #3
0
        public async Task GetAllByManufacturerAsync_WithNonExistentManufacturer_ReturnsEmptyEnumerable()
        {
            // Arrange
            var testId = Guid.NewGuid().ToString();

            var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>()
                                              .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                              .Options);

            var modelsService = new ModelsService(new EfRepository <CarModel>(context));

            // Act
            var actualResult = (await modelsService.GetAllByManufacturerAsync(testId)).Count();

            // Assert
            const int expectedResult = 0;

            Assert.Equal(expectedResult, actualResult);
        }