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); }; }
public void Add(Option newOption) { if (string.IsNullOrEmpty(newOption.Text)) { throw new ArgumentNullException(nameof(newOption.Text)); } List <PropertyInfo> props = typeof(Option) .GetProperties() .Where(p => p.GetCustomAttributes(typeof(MaxLengthAttribute), true).Any()).ToList(); MaxLengthAttribute max = props.First(p => p.Name == nameof(newOption.Text)) .GetCustomAttributes(true) .Where(attr => attr.GetType() == typeof(MaxLengthAttribute)) .FirstOrDefault() as MaxLengthAttribute; if (max != null) { if (max.Length < newOption.Text.Length) { throw new ArgumentOutOfRangeException(nameof(newOption.Text)); } } _context.Options.Add(newOption); _context.SaveChanges(); }
public Award Add(decimal successValue, decimal failValue, decimal quitValue, LevelEnum level) { Award award = new Award { Success = successValue, Fail = failValue, Quit = quitValue, Level = level }; _context.Awards.Add(award); _context.SaveChanges(); return(award); }
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 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 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 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 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 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 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 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 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_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 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 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 void Add(Question question) { _context.Add(question); _context.SaveChanges(); }