コード例 #1
0
        public async Task GetProductsForUserAsync_Return_Products_For_UserGroup()
        {
            var builder = new DbContextOptionsBuilder <CoffeeCardContext>()
                          .UseInMemoryDatabase(nameof(GetProductsForUserAsync_Return_Products_For_UserGroup));

            var databaseSettings = new DatabaseSettings
            {
                SchemaName = "test"
            };
            var environmentSettings = new EnvironmentSettings()
            {
                EnvironmentType = EnvironmentType.Test
            };

            await using var context = new CoffeeCardContext(builder.Options, databaseSettings, environmentSettings);
            var p1 = new Product
            {
                Id              = 1,
                Name            = "Coffee",
                Description     = "Coffee Clip card",
                NumberOfTickets = 10,
                Price           = 10,
                ExperienceWorth = 10,
                Visible         = true
            };
            await context.AddAsync(p1);

            var p2 = new Product
            {
                Id              = 2,
                Name            = "Espresso",
                Description     = "Espresso Clip card",
                NumberOfTickets = 10,
                Price           = 20,
                ExperienceWorth = 20,
                Visible         = true
            };
            await context.AddAsync(p2);

            var p3 = new Product
            {
                Id              = 3,
                Name            = "Barista Coffee",
                Description     = "Barista Coffee Clip card",
                NumberOfTickets = 10,
                Price           = 30,
                ExperienceWorth = 10,
                Visible         = true
            };
            await context.AddAsync(p3);

            await context.SaveChangesAsync();

            await context.AddAsync(new ProductUserGroup
            {
                Product   = p1,
                UserGroup = UserGroup.Barista
            });

            await context.AddAsync(new ProductUserGroup
            {
                Product   = p2,
                UserGroup = UserGroup.Barista
            });

            await context.AddAsync(new ProductUserGroup
            {
                Product   = p3,
                UserGroup = UserGroup.Barista
            });

            await context.SaveChangesAsync();

            using (var productService = new ProductService(context))
            {
                var expected = new List <Product>
                {
                    new Product
                    {
                        Id              = 1,
                        Name            = "Coffee",
                        Description     = "Coffee Clip card",
                        NumberOfTickets = 10,
                        Price           = 10,
                        ExperienceWorth = 10,
                        Visible         = true
                    },
                    new Product
                    {
                        Id              = 2,
                        Name            = "Espresso",
                        Description     = "Espresso Clip card",
                        NumberOfTickets = 10,
                        Price           = 20,
                        ExperienceWorth = 20,
                        Visible         = true
                    },
                    new Product
                    {
                        Id              = 3,
                        Name            = "Barista Coffee",
                        Description     = "Barista Coffee Clip card",
                        NumberOfTickets = 10,
                        Price           = 30,
                        ExperienceWorth = 10,
                        Visible         = true
                    }
                };

                var user = new User
                {
                    UserGroup = UserGroup.Barista
                };

                var result = await productService.GetProductsForUserAsync(user);

                Assert.Equal(expected, result);
            }
        }