private void GenerateMockQuestions() { var cs = Configuration.GetConnectionString("SqlServer"); var options = new DbContextOptionsBuilder <BitcoinShowDBContext>() .UseSqlServer(cs) .Options; using (var context = new BitcoinShowDBContext(options)) { context.Database.Migrate(); context.Database.EnsureCreated(); context.Options.ToList().ForEach(o => { o.Question = null; o.QuestionId = null; context.Options.Update(o); }); context.SaveChanges(); context.Questions.RemoveRange(context.Questions); context.Options.RemoveRange(context.Options); context.SaveChanges(); teste(context, LevelEnum.Easy, 2); teste(context, LevelEnum.Medium, 2); teste(context, LevelEnum.Hard, 2); teste(context, LevelEnum.VeryHard, 2); }; }
private void InitializeContainer(IApplicationBuilder app) { // Add application presentation components: container.RegisterMvcControllers(app); container.RegisterMvcViewComponents(app); // Add application services. For instance: container.Register <IQuestionService, QuestionService>(Lifestyle.Scoped); container.Register <IQuestionRepository, QuestionRepository>(Lifestyle.Scoped); container.Register <BitcoinShowDBContext>(() => { var cs = Configuration.GetConnectionString("SqlServer"); var options = new DbContextOptionsBuilder <BitcoinShowDBContext>() .UseSqlServer(cs) .Options; var context = new BitcoinShowDBContext(options); context.Database.Migrate(); context.Database.EnsureCreated(); return(context); }, Lifestyle.Scoped); //Cross-wire ASP.NET services (if any). For instance: container.CrossWire <ILoggerFactory>(app); container.Verify(); // NOTE: Do prevent cross-wired instances as much as possible. // See: https://simpleinjector.org/blog/2016/07/ }
public void Update_Option_Success() { var options = new DbContextOptionsBuilder <BitcoinShowDBContext>() .UseInMemoryDatabase(System.Guid.NewGuid().ToString()) .Options; var context = new BitcoinShowDBContext(options); var newOption = new Option { Text = "New option" }; context.Options.Add(newOption); context.SaveChanges(); OptionRepository repository = new OptionRepository(context); Option expected = new Option(); expected.Id = newOption.Id; expected.Text = "Update option"; Option actual = new Option(); actual.Id = newOption.Id; actual.Text = "Update option"; repository.Update(actual); Assert.Equal(expected, actual); }
public void Update_Option_With_Text_Greater_Than_Max_Size_Error() { var options = new DbContextOptionsBuilder <BitcoinShowDBContext>() .UseInMemoryDatabase(System.Guid.NewGuid().ToString()) .Options; var context = new BitcoinShowDBContext(options); var newOption = new Option { Text = "New option" }; context.Options.Add(newOption); context.SaveChanges(); OptionRepository repository = new OptionRepository(context); Option updatedOption = new Option(); updatedOption.Id = newOption.Id; updatedOption.Text = new String('B', 201); ArgumentOutOfRangeException ex = Assert.Throws <ArgumentOutOfRangeException>(() => repository.Update(updatedOption)); Assert.NotNull(ex); Assert.Equal(nameof(updatedOption.Text), ex.ParamName); }
public void Delete_Question_Not_Found_Error() { BitcoinShowDBContext context = DbContextFactory.GetContext(); QuestionRepository repository = new QuestionRepository(context); Exception ex = Assert.Throws <Exception>(() => repository.Delete(99999999)); Assert.NotNull(ex); Assert.Equal("The current Question does not exist.", ex.Message); }
public void Add_Question_Without_Title_Error() { BitcoinShowDBContext context = DbContextFactory.GetContext(); QuestionRepository repository = new QuestionRepository(context); Question option = new Question(); ArgumentNullException ex = Assert.Throws <ArgumentNullException>(() => repository.Add(option)); Assert.NotNull(ex); Assert.Equal(nameof(option.Title), ex.ParamName); }
public void Get_Option_By_Id_Not_Found() { var options = new DbContextOptionsBuilder <BitcoinShowDBContext>() .UseInMemoryDatabase(System.Guid.NewGuid().ToString()) .Options; var context = new BitcoinShowDBContext(options); OptionRepository repository = new OptionRepository(context); var option = repository.Get(100); Assert.Null(option); }
public void Add_Option_Without_Text_Error() { var options = new DbContextOptionsBuilder <BitcoinShowDBContext>() .UseInMemoryDatabase(System.Guid.NewGuid().ToString()) .Options; var context = new BitcoinShowDBContext(options); OptionRepository repository = new OptionRepository(context); Option option = new Option(); ArgumentNullException ex = Assert.Throws <ArgumentNullException>(() => repository.Add(option)); Assert.NotNull(ex); Assert.Equal(nameof(option.Text), ex.ParamName); }
public void Add_Question_With_Title_Greater_Than_Max_Error() { BitcoinShowDBContext context = DbContextFactory.GetContext(); QuestionRepository repository = new QuestionRepository(context); Question option = new Question(); option.Title = new String('a', 201); ArgumentOutOfRangeException ex = Assert.Throws <ArgumentOutOfRangeException>(() => repository.Add(option)); Assert.NotNull(ex); Assert.Equal(nameof(option.Title), ex.ParamName); }
public void Add_Question_Without_Answer_Error() { BitcoinShowDBContext context = DbContextFactory.GetContext(); QuestionRepository repository = new QuestionRepository(context); Question question = new Question(); question.Title = "How many times do you test your code?"; ArgumentNullException ex = Assert.Throws <ArgumentNullException>(() => repository.Add(question)); Assert.NotNull(ex); Assert.Equal(nameof(question.Answer), ex.ParamName); }
public void Delete_Option_Not_Found_Error() { var options = new DbContextOptionsBuilder <BitcoinShowDBContext>() .UseInMemoryDatabase(System.Guid.NewGuid().ToString()) .Options; var context = new BitcoinShowDBContext(options); OptionRepository repository = new OptionRepository(context); Exception ex = Assert.Throws <DbUpdateException>(() => repository.Delete(1)); Assert.NotNull(ex); Assert.Equal("The current option does not exists.", ex.Message); }
public void Get_Random_Question_By_Level() { BitcoinShowDBContext context = DbContextFactory.GetContext(); #region mock data int[] easyIds = new int[5]; for (int i = 0; i < 4; i++) { Question easyQuestion = new Question(); easyQuestion.Answer = new Option { Text = "Option 1 easyQuestion ${easyQuestion.Id}", QuestionId = easyQuestion.Id }; easyQuestion.Title = "Question 1"; easyQuestion.Level = LevelEnum.Easy; var options = RandomOptions(4).ToList(); options.ForEach(o => { context.Options.Add(o); }); context.SaveChanges(); easyQuestion.Answer = options[0]; context.Questions.Add(easyQuestion); easyQuestion.Options = options; options.ForEach(o => { o.Question = easyQuestion; context.Options.Update(o); }); context.SaveChanges(); easyIds[i] = easyQuestion.Id; } #endregion mock data QuestionRepository repository = new QuestionRepository(context); Question question = repository.GetByLevel(LevelEnum.Easy, easyIds); Assert.Null(question); question = repository.GetByLevel(LevelEnum.Easy, new int[] { easyIds[0], easyIds[4] }); Assert.NotNull(question); var filter = easyIds .Select((value, index) => new { value, index }) .Where(item => item.index == 0 || item.index == 4) .Select(item => item.value); Assert.True(easyIds.Contains(question.Id)); Assert.False(filter.Contains(question.Id)); }
public void Delete_Question_Success() { var question = new Question { Title = "Delete_Question_Success" }; BitcoinShowDBContext context = DbContextFactory.GetContext(); context.Questions.Add(question); context.SaveChanges(); int questionId = question.Id; QuestionRepository repository = new QuestionRepository(context); repository.Delete(questionId); Assert.Null(context.Questions.Find(questionId)); }
public void Delete_Option_Success() { var options = new DbContextOptionsBuilder <BitcoinShowDBContext>() .UseInMemoryDatabase(System.Guid.NewGuid().ToString()) .Options; var context = new BitcoinShowDBContext(options); Option deleteOption = new Option(); deleteOption.Text = "delete"; context.Options.Add(deleteOption); context.SaveChanges(); OptionRepository repository = new OptionRepository(context); repository.Delete(deleteOption.Id); }
public void Add_Option_With_Text_Greater_Than_Max_Size_Error() { var options = new DbContextOptionsBuilder <BitcoinShowDBContext>() .UseInMemoryDatabase(System.Guid.NewGuid().ToString()) .Options; var context = new BitcoinShowDBContext(options); OptionRepository repository = new OptionRepository(context); Option option = new Option(); option.Text = new String('A', 201); ArgumentOutOfRangeException ex = Assert.Throws <ArgumentOutOfRangeException>(() => repository.Add(option)); Assert.NotNull(ex); Assert.Equal(nameof(option.Text), ex.ParamName); Assert.Equal(0, context.Options.Count()); }
public void Update_Question_Success() { var option = new Option { Text = "Update_Question_Success Option" }; var option2 = new Option { Text = "Update_Question_Success Option2" }; var question = new Question { Title = "Update_Question_Success", Answer = option, Level = LevelEnum.Easy }; BitcoinShowDBContext context = DbContextFactory.GetContext(); context.Questions.Add(question); context.Options.Add(option2); context.SaveChanges(); var expected = new Question { Id = question.Id, Title = "Updated", Answer = question.Answer, Level = LevelEnum.Hard }; QuestionRepository repository = new QuestionRepository(context); question.Title = "Updated"; question.Level = LevelEnum.Hard; repository.Update(question); var actual = context.Questions.Find(question.Id); Assert.Equal(expected, actual); expected.Answer = option2; question.Answer = option2; repository.Update(question); actual = context.Questions.Find(question.Id); Assert.Equal(expected, actual); }
public void Add_Option_Success() { string text = "Add_Option_Success"; var options2 = new DbContextOptionsBuilder <BitcoinShowDBContext>() .UseInMemoryDatabase("asd") .Options; var context2 = new BitcoinShowDBContext(options2); OptionRepository repository = new OptionRepository(context2); Option actual = new Option(); actual.Text = text; repository.Add(actual); Assert.NotNull(actual); Assert.True(0 < actual.Id); Assert.Equal(text, actual.Text); Assert.Equal(1, context2.Options.Count()); }
public void Get_Option_By_Id_Success() { var options = new DbContextOptionsBuilder <BitcoinShowDBContext>() .UseInMemoryDatabase(System.Guid.NewGuid().ToString()) .Options; var context = new BitcoinShowDBContext(options); var expected = new Option { Text = "New option" }; context.Options.Add(expected); context.SaveChanges(); OptionRepository repository = new OptionRepository(context); var actual = repository.Get(expected.Id); Assert.NotNull(actual); Assert.Equal(expected, actual); }
public void GetAll_Questions_Success() { BitcoinShowDBContext context = DbContextFactory.GetContext(); Option option = new Option { Text = "Option" }; context.Options.Add(option); context.SaveChanges(); List <Option> optionsList = new List <Option>(); optionsList.Add(option); Question question = new Question { Title = $"Random Question {1}", Answer = new Option { Text = $"Random Option {1}" }, Level = LevelEnum.Medium, }; question.Options = new List <Option>(); optionsList[0].QuestionId = question.Id; question.Options.Add(optionsList[0]); context.Questions.Add(question); context.SaveChanges(); QuestionRepository repository = new QuestionRepository(context); repository.GetAll().ForEach(q => { Assert.NotNull(q); Assert.True(q.Id > 0); Assert.False(String.IsNullOrEmpty(q.Title)); Assert.NotNull(q.Answer); Assert.True(q.Answer.Id > 0); Assert.False(String.IsNullOrEmpty(q.Answer.Text)); Assert.Equal(q.Level, LevelEnum.Medium); Assert.Equal(optionsList, q.Options); }); }
private void teste(BitcoinShowDBContext context, LevelEnum level, int quantidade) { for (int i = 0; i < quantidade; i++) { Question easyQuestion = new Question(); easyQuestion.Title = $"{level.GetEnumDisplayName()} Question {i}"; easyQuestion.Level = level; List <Option> optionsList = new List <Option> { new Option { Text = $"{level.GetEnumDisplayName()} Question {i} option A" }, new Option { Text = $"{level.GetEnumDisplayName()} Question {i} option B" }, new Option { Text = $"{level.GetEnumDisplayName()} Question {i} option C" }, new Option { Text = $"{level.GetEnumDisplayName()} Question {i} option D" }, }; optionsList.ForEach(o => { context.Options.Add(o); }); context.SaveChanges(); easyQuestion.Answer = optionsList[0]; context.Questions.Add(easyQuestion); easyQuestion.Options = optionsList; optionsList.ForEach(o => { o.Question = easyQuestion; context.Options.Update(o); }); context.SaveChanges(); } }
public void Add_Question_Success() { BitcoinShowDBContext context = DbContextFactory.GetContext(); var options = RandomOptions(4).ToList(); options.ForEach(o => { context.Options.Add(o); }); context.SaveChanges(); QuestionRepository repository = new QuestionRepository(context); Question question = new Question(); question.Answer = context.Options.First(); question.Title = "Test question"; question.Level = LevelEnum.Hard; repository.Add(question); Assert.True(question.Id > 0); }
public void Get_Question_Not_Found_Error() { BitcoinShowDBContext context = DbContextFactory.GetContext(); for (int i = 0; i < 10; i++) { context.Questions.Add(new Question { Title = $"Random Question {i + 1}", Answer = new Option { Id = i, Text = $"Random Option {i}" } }); } context.SaveChanges(); QuestionRepository repository = new QuestionRepository(context); Question actual = repository.Get(50); Assert.Null(actual); }
public void Update_Question_Without_Answer_Error() { var option = new Option { Text = "Update_Question_Without_Answer_Error Option" }; var question = new Question { Title = "Update_Question_Without_Answer_Error", Answer = option }; BitcoinShowDBContext context = DbContextFactory.GetContext(); context.Questions.Add(question); context.SaveChanges(); QuestionRepository repository = new QuestionRepository(context); question.Answer = null; ArgumentNullException ex = Assert.Throws <ArgumentNullException>(() => repository.Update(question)); Assert.NotNull(ex); Assert.Equal(nameof(question.Answer), ex.ParamName); }
public void Update_Question_With_Title_Greater_Than_Max_Error() { var option = new Option { Text = "Update_Question_With_Title_Greater_Than_Max_Error Option" }; var question = new Question { Title = "Update_Question_With_Title_Greater_Than_Max_Error", Answer = option }; BitcoinShowDBContext context = DbContextFactory.GetContext(); context.Questions.Add(question); context.SaveChanges(); QuestionRepository repository = new QuestionRepository(context); question.Title = new String('a', 201); ArgumentOutOfRangeException ex = Assert.Throws <ArgumentOutOfRangeException>(() => repository.Update(question)); Assert.NotNull(ex); Assert.Equal(nameof(question.Title), ex.ParamName); }
public void Get_Question_Success() { BitcoinShowDBContext context = DbContextFactory.GetContext(); for (int i = 0; i < 98; i++) { context.Questions.Add(new Question { Title = $"Random Question {i + 1}", Answer = new Option { Id = i, Text = $"Random Option {i}" }, Level = LevelEnum.VeryHard }); } context.SaveChanges(); QuestionRepository repository = new QuestionRepository(context); Question actual = repository.Get(50); Assert.NotNull(actual); Assert.NotNull(actual.Answer); }
public OptionRepository(BitcoinShowDBContext context) { _context = context; }
public AwardRepository(BitcoinShowDBContext context) { _context = context; }
public QuestionRepository(BitcoinShowDBContext context) { this._context = context; }