示例#1
0
        public async void Should_Get_Category_By_Id()
        {
            var category = new Category {
                Id = 3, Title = "MMORPG"
            };
            var category_2 = new Category {
                Id = 4, Title = "FPS"
            };

            var options = new DbContextOptionsBuilder <GServerDbContext>()
                          .UseInMemoryDatabase(databaseName: "test2")
                          .Options;

            using (var context = new GServerDbContext(options))
            {
                var repository = new CategoryRepository(context);
                await repository.AddAsync(category);

                await repository.AddAsync(category_2);
            }

            using (var context = new GServerDbContext(options))
            {
                var repository = new CategoryRepository(context);
                var _category  = await repository.GetByIdAsync(category.Id);

                var _category_null = await repository.GetByIdAsync(5);

                Assert.IsType <Category>(_category);
                Assert.Equal("MMORPG", _category.Title);
                Assert.Null(_category_null);
                Assert.Equal(3, _category.Id);
            }
        }
示例#2
0
        public async void Should_Save_Category_To_Db()
        {
            var category = new Category {
                Id = 1, Title = "MMORPG"
            };
            var category_2 = new Category {
                Id = 2, Title = "FPS"
            };

            var options = new DbContextOptionsBuilder <GServerDbContext>()
                          .UseInMemoryDatabase(databaseName: "test1")
                          .Options;

            using (var context = new GServerDbContext(options)) {
                var repository = new CategoryRepository(context);
                await repository.AddAsync(category);

                await repository.AddAsync(category_2);
            }

            using (var context = new GServerDbContext(options))
            {
                var repository     = new CategoryRepository(context);
                var all_categories = await repository.ListAllAsync();

                Assert.NotNull(all_categories);
            }
        }
示例#3
0
        public async void Should_Update_Category_Data()
        {
            var category = new Category {
                Id = 1, Title = "FPS"
            };

            var options = new DbContextOptionsBuilder <GServerDbContext>()
                          .UseInMemoryDatabase(databaseName: "test4")
                          .Options;

            using (var context = new GServerDbContext(options))
            {
                var repository = new CategoryRepository(context);
                await repository.AddAsync(category);
            }

            using (var context = new GServerDbContext(options))
            {
                var repository = new CategoryRepository(context);
                var _category  = await repository.GetByIdAsync(category.Id);

                _category.Title = "MMORPG";
                await repository.UpdateAsync(_category);

                _category = await repository.GetByIdAsync(category.Id);

                Assert.Equal("MMORPG", _category.Title);
            }
        }
示例#4
0
        private void AddCategory()
        {
            var categoryRepository = new CategoryRepository(DbContext);
            var category           = new CategoryEntity
            {
                Id   = CategoryId,
                Name = "ASP.NET Core"
            };

            categoryRepository.AddAsync(category).GetAwaiter().GetResult();
        }
示例#5
0
        public async void Should_Remove_Category_Data_By_Id()
        {
            var category = new Category {
                Id = 3, Title = "MMORPG"
            };
            var category_2 = new Category {
                Id = 4, Title = "FPS"
            };
            var category_3 = new Category {
                Id = 5, Title = "FPS"
            };

            var options = new DbContextOptionsBuilder <GServerDbContext>()
                          .UseInMemoryDatabase(databaseName: "test3")
                          .Options;

            using (var context = new GServerDbContext(options))
            {
                var repository = new CategoryRepository(context);
                await repository.AddAsync(category);

                await repository.AddAsync(category_2);

                await repository.AddAsync(category_3);
            }

            using (var context = new GServerDbContext(options))
            {
                var repository = new CategoryRepository(context);
                await repository.DeleteAsync(category);
            }


            using (var context = new GServerDbContext(options))
            {
                var repository     = new CategoryRepository(context);
                var all_categories = await repository.ListAllAsync();

                Assert.Equal(2, all_categories.Count);
            }
        }
示例#6
0
        public async Task CategoryAddTestMethodAsync()
        {
            var context    = new DataContext();
            var repository = new CategoryRepository(context);
            var data       = new Category
            {
                Name = "Test"
            };
            var response = await repository.AddAsync(data);

            Assert.IsTrue(response != null);
        }
示例#7
0
        public async Task CategoryRepo_Add_AddsOneCategory()
        {
            var repository = new CategoryRepository(_context);

            var expected = new Category {
                CategoryId = 6, Name = "NewCategory"
            };
            await repository.AddAsync(expected);

            _context.SaveChanges();

            var result = (await repository.GetAllAsync()).Last();

            Assert.Equal(expected.CategoryId, result.CategoryId);
            Assert.Equal(expected.Name, result.Name);
        }
示例#8
0
        public async Task <CategoryViewModel> AddCategory(CategoryViewModel category)
        {
            try
            {
                if (category == null)
                {
                    throw new NullReferenceException("Missing category to add!");
                }
                var addedCategory = await _rep.AddAsync(category.ToEntity());

                return(addedCategory.ToViewModel());
            }
            catch (NullReferenceException e)
            {
                _logger.Log(e.Message + "\n" + e.StackTrace);
            }
            return(null);
        }
示例#9
0
        public bool CreateDefaults()
        {
            //bool AnswerRepositoryIsEmpty = AnswerRepository.Select().Count() == 0;
            //bool AnswerRepositoryIsEmpty = AnswerRepository.FindBy(item=>item.Description=="zzzz").Count() == 0;
            bool AnswerRepositoryIsEmpty = AnswerRepository.FindAllAsync().GetAwaiter().GetResult().Count() == 0;

            if (AnswerRepositoryIsEmpty == false)
            {
                return(false);
            }
            bool CategoryRepositoryIsEmpty = CategoryRepository.FindAllAsync().GetAwaiter().GetResult().Count() == 0;
            bool EarningRepositoryIsEmpty  = EarningRepository.FindAllAsync().GetAwaiter().GetResult().Count() == 0;
            bool QuestionRepositoryIsEmpty = QuestionRepository.FindAllAsync().GetAwaiter().GetResult().Count() == 0;
            bool TagRepositoryIsEmpty      = TagRepository.FindAllAsync().GetAwaiter().GetResult().Count() == 0;
            //bool ApplicationUserRepositoryIsEmpty = ApplicationUserRepository.All().Count() == 0;
            bool ApplicationUserRepositoryIsEmpty = ApplicationUserRepository.FindAllAsync().GetAwaiter().GetResult().Count() == 0;

            ////aboutIsEmpty = false;
            //if (CategoryRepositoryIsEmpty == false) return false;
            if (CategoryRepositoryIsEmpty)
            {
                try
                {
                    string[] categoryNames = new string[] { "Categoryname1", "Categoryname2" };
                    foreach (var item in categoryNames)
                    {
                        var newItem = new Category
                        {
                            CreatedDate = DateTime.Now,
                            UpdatedDate = DateTime.Now,
                            Name        = item,
                            ImageOrIcon = "https://img.icons8.com/nolan/64/bug.png"
                        };
                        CategoryRepository.AddAsync(newItem).GetAwaiter().GetResult();
                    }
                    Commit();
                }
                catch (Exception e)
                {
                    throw e;
                    Console.WriteLine("{0} Exception caught.", e.Message);
                }
            }
            if (TagRepositoryIsEmpty)
            {
                try
                {
                    string[] tagNames = new string[] { "tag1", "tag2", "tag3", "tag4" };
                    foreach (var item in tagNames)
                    {
                        var newItem = new Tag
                        {
                            CreatedDate = DateTime.Now,
                            UpdatedDate = DateTime.Now,
                            Name        = item
                        };
                        TagRepository.AddAsync(newItem).GetAwaiter().GetResult();
                    }
                    Commit();
                }
                catch (Exception e)
                {
                    throw e;
                    Console.WriteLine("{0} Exception caught.", e.Message);
                }
            }
            if (ApplicationUserRepositoryIsEmpty)
            {
                try
                {
                    string[] userNames = new string[] { "User1", "User2", "User3", "User4" };
                    foreach (var item in userNames)
                    {
                        var newItem = new ApplicationUser
                        {
                            Id                 = Guid.NewGuid().ToString(),
                            CreatedDate        = DateTime.Now,
                            UpdatedDate        = DateTime.Now,
                            FacebookName       = item,
                            ProfilePicture     = "SeedPicture",
                            ProfilePictureWeb  = "https://img.icons8.com/nolan/64/user.png",
                            ConcurrencyStamp   = Guid.NewGuid().ToString(),
                            SecurityStamp      = Guid.NewGuid().ToString("N").ToUpper(),
                            NormalizedEmail    = item.ToLower() + "@asqme.com",
                            Email              = item.ToLower() + "@asqme.com",
                            NormalizedUserName = item.ToLower() + "@asqme.com",
                            UserName           = item.ToLower() + "@asqme.com",
                        };
                        //var newItem2 = new ApplicationUser
                        //{
                        //    CreatedDate = DateTime.Now,
                        //    UpdatedDate = DateTime.Now,
                        //    FacebookName = "FacebookName",
                        //    ProfilePicture = "SeedPicture",
                        //    ProfilePictureWeb = "https://img.icons8.com/nolan/64/user.png",
                        //    ConcurrencyStamp = Guid.NewGuid().ToString(),
                        //    SecurityStamp = Guid.NewGuid().ToString(),
                        //    NormalizedEmail = AppSettings.Value.AdminEmail,
                        //    Email = AppSettings.Value.AdminEmail,
                        //    NormalizedUserName = AppSettings.Value.AdminEmail,
                        //    UserName = AppSettings.Value.AdminEmail,
                        //};
                        ApplicationUserRepository.AddAsync(newItem).GetAwaiter().GetResult();
                        //UserManager.CreateAsync(newItem2).GetAwaiter().GetResult();
                    }
                    Commit();
                }
                catch (Exception e)
                {
                    throw e;
                    Console.WriteLine("{0} Exception caught.", e.Message);
                }
            }
            if (QuestionRepositoryIsEmpty)
            {
                try
                {
                    var users      = ApplicationUserRepository.FindAllAsync().GetAwaiter().GetResult().ToList();
                    var tags       = TagRepository.FindAllAsync().GetAwaiter().GetResult().ToList();
                    var categories = CategoryRepository.FindAllAsync().GetAwaiter().GetResult().ToList();
                    foreach (var item in tags)
                    {
                        var newItem = new Question
                        {
                            CreatedDate       = DateTime.Now,
                            UpdatedDate       = DateTime.Now,
                            Title             = "Question Title",
                            Description       = "Question Description",
                            ApplicationUserId = users[new Random().Next(0, users.Count() - 1)].Id,
                            SolverAnswerId    = int.MinValue,
                            CategoryId        = categories[new Random().Next(0, categories.Count() - 1)].Id
                        };
                        QuestionRepository.AddAsync(newItem).GetAwaiter().GetResult();
                        //Commit();
                        //Id = QuestionTagRepository.All().Count() + 1,
                        //CreatedDate = DateTime.Now,
                        //UpdatedDate = DateTime.Now,
                        var newItem2 = new QuestionTag
                        {
                            QuestionId = newItem.Id,
                            TagId      = tags[new Random().Next(0, tags.Count() - 1)].Id
                        };
                        QuestionTagRepository.AddAsync(newItem2).GetAwaiter().GetResult();
                    }
                    Commit();
                }
                catch (Exception e)
                {
                    throw e;
                    Console.WriteLine("{0} Exception caught.", e.Message);
                }
            }
            if (AnswerRepositoryIsEmpty)
            {
                var users     = ApplicationUserRepository.FindAllAsync().GetAwaiter().GetResult().ToList();
                var questions = QuestionRepository.FindAllAsync().GetAwaiter().GetResult().ToList();
                try
                {
                    foreach (var item in questions)
                    {
                        var newItem = new Answer
                        {
                            CreatedDate       = DateTime.Now,
                            UpdatedDate       = DateTime.Now,
                            QuestionId        = item.Id,
                            ApplicationUserId = users[new Random().Next(0, users.Count() - 1)].Id,
                            Description       = "Answer Description"
                        };
                        AnswerRepository.AddAsync(newItem).GetAwaiter().GetResult();
                        Commit();
                        item.SolverAnswerId = newItem.Id;
                        QuestionRepository.UpdateAsync(item).GetAwaiter().GetResult();
                        Commit();
                    }
                }
                catch (Exception e)
                {
                    throw e;
                    Console.WriteLine("{0} Exception caught.", e.Message);
                }
            }
            if (EarningRepositoryIsEmpty)
            {
                try
                {
                    var users = ApplicationUserRepository.FindAllAsync().GetAwaiter().GetResult().ToList();
                    //var earningsTypes = new string[] { "Received", "Sent" };
                    var earningsTypes = Enum.GetValues(typeof(Earning.EarningType));
                    foreach (var item in users)
                    {
                        foreach (var item2 in users)
                        {
                            var newItem = new Earning
                            {
                                CreatedDate  = DateTime.Now,
                                UpdatedDate  = DateTime.Now,
                                EarningsType = (Earning.EarningType)earningsTypes.GetValue(new Random().Next(earningsTypes.Length)),
                                Amount       = 1m,
                                SenderId     = users.Where(u => u.Id != item.Id).ToList()
                                               [new Random().Next(0, users.Count() - 2)].Id,
                                ApplicationUserId = item.Id
                            };
                            EarningRepository.AddAsync(newItem).GetAwaiter().GetResult();
                        }
                        Commit();
                    }
                }
                catch (Exception e)
                {
                    throw e;
                    Console.WriteLine("{0} Exception caught.", e.Message);
                }
            }
            //Commit();
            if (CategoryRepositoryIsEmpty || TagRepositoryIsEmpty || ApplicationUserRepositoryIsEmpty ||
                QuestionRepositoryIsEmpty || AnswerRepositoryIsEmpty || EarningRepositoryIsEmpty)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#10
0
        public async Task CategoryRepositoryAsyncAllMethodTest()
        {
            // DbContextOptions<T> Object Creation
            var options = new DbContextOptionsBuilder <DotNetSaleCoreDbContext>()
                          .UseInMemoryDatabase(databaseName: $"DotNetSaleCore{Guid.NewGuid()}").Options;
            //.UseSqlServer("server=(localdb)\\mssqllocaldb;database=DotNetSaleCore;integrated security=true;").Options;

            // ILoggerFactory Object Creation
            var serviceProvider = new ServiceCollection().AddLogging().BuildServiceProvider();
            var factory         = serviceProvider.GetService <ILoggerFactory>();

            //[1] AddAsync() Method Test
            using (var context = new DotNetSaleCoreDbContext(options))
            {
                // Repository Object Creation
                //[!] Arrange
                var repository = new CategoryRepository(context, factory);
                var model      = new Category {
                    CategoryName = "[1] 카테고리이름"
                };

                //[!] Act
                await repository.AddAsync(model);

                await context.SaveChangesAsync();
            }
            using (var context = new DotNetSaleCoreDbContext(options))
            {
                //[!] Assert
                Assert.AreEqual(1, await context.Categories.CountAsync());
                var model = await context.Categories.Where(m => m.CategoryId == 1).SingleOrDefaultAsync();

                Assert.AreEqual("[1] 카테고리이름", model?.CategoryName);
            }

            //[2] GetAllAsync() Method Test
            using (var context = new DotNetSaleCoreDbContext(options))
            {
                //// 트랜잭션 관련 코드는 InMemoryDatabase 공급자에서는 지원 X
                ////using (var transaction = context.Database.BeginTransaction()) { transaction.Commit(); }
                var repository = new CategoryRepository(context, factory);
                var model      = new Category {
                    CategoryName = "[2] 책"
                };
                await context.Categories.AddAsync(model);

                await context.SaveChangesAsync(); //[1]

                await context.Categories.AddAsync(new Category { CategoryName = "[3] 강의" });

                await context.SaveChangesAsync(); //[2]
            }
            using (var context = new DotNetSaleCoreDbContext(options))
            {
                var repository = new CategoryRepository(context, factory);
                var models     = await repository.GetAllAsync();

                Assert.AreEqual(3, models.Count);
            }

            //[3] GetByIdAsync() Method Test
            using (var context = new DotNetSaleCoreDbContext(options))
            {
                // Empty
            }
            using (var context = new DotNetSaleCoreDbContext(options))
            {
                var repository = new CategoryRepository(context, factory);
                var model      = await repository.GetByIdAsync(2);

                Assert.IsTrue(model.CategoryName.Contains("책"));
                Assert.AreEqual("[2] 책", model.CategoryName);
            }

            //[4] GetEditAsync() Method Test
            using (var context = new DotNetSaleCoreDbContext(options))
            {
                // Empty
            }
            using (var context = new DotNetSaleCoreDbContext(options))
            {
                var repository = new CategoryRepository(context, factory);
                var model      = await repository.GetByIdAsync(2);

                model.CategoryName = "[2] 컴퓨터";
                await repository.EditAsync(model);

                await context.SaveChangesAsync();

                Assert.AreEqual("[2] 컴퓨터",
                                (await context.Categories.Where(m => m.CategoryId == 2).SingleOrDefaultAsync()).CategoryName);
            }

            //[5] GetDeleteAsync() Method Test
            using (var context = new DotNetSaleCoreDbContext(options))
            {
                // Empty
            }
            using (var context = new DotNetSaleCoreDbContext(options))
            {
                var repository = new CategoryRepository(context, factory);
                await repository.DeleteAsync(2);

                await context.SaveChangesAsync();

                Assert.AreEqual(2, await context.Categories.CountAsync());
                Assert.IsNull(await repository.GetByIdAsync(2));
            }

            //[6] PagingAsync() Method Test
            using (var context = new DotNetSaleCoreDbContext(options))
            {
                // Empty
            }
            using (var context = new DotNetSaleCoreDbContext(options))
            {
                int pageIndex = 0;
                int pageSize  = 1;

                var repository = new CategoryRepository(context, factory);
                var models     = await repository.GetAllAsync(pageIndex, pageSize);

                Assert.AreEqual("[3] 강의", models.Records.FirstOrDefault().CategoryName);
                Assert.AreEqual(2, models.TotalRecords);
            }
        }