Skip to content

Commit

Permalink
Merge pull request #34 from italopessoa/develop
Browse files Browse the repository at this point in the history
merge to release
  • Loading branch information
italopessoa committed Mar 3, 2018
2 parents f4c5da9 + 91dc258 commit 9ae8e32
Show file tree
Hide file tree
Showing 15 changed files with 324 additions and 81 deletions.
38 changes: 38 additions & 0 deletions BitcoinShow.Test/Facade/BitcoinShowFacadeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using AutoMapper;
using BitcoinShow.Web.Facade;
using BitcoinShow.Web.Facade.Interface;
using BitcoinShow.Web.Models;
using BitcoinShow.Web.Repositories.Interface;
using BitcoinShow.Web.Services.Interface;
using Moq;
using Xunit;

namespace BitcoinShow.Test.Facade
{
public class BitcoinShowFacadeTest
{
public BitcoinShowFacadeTest()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile<BitcoinShowProfile>();
});
}
[Fact]
public void QuestionService_GetRandom_Question_By_Level()
{
Mock<IQuestionService> mockQuestionService = new Mock<IQuestionService>(MockBehavior.Strict);
mockQuestionService.Setup(s => s.GetByLevel(LevelEnum.Easy, null)).Returns(new Question());
mockQuestionService.Setup(s => s.GetByLevel(LevelEnum.Hard, new [] { 1 })).Returns(new Question());

IBitcoinShowFacade facade = new BitcoinShowFacade(mockQuestionService.Object, null, null);
QuestionViewModel question = facade.GetRandomQuestionByLevel(LevelEnum.Easy, null);
QuestionViewModel question2 = facade.GetRandomQuestionByLevel(LevelEnum.Hard, new [] { 1 });
Assert.NotNull(question);
Assert.NotNull(question2);

mockQuestionService.Verify(s => s.GetByLevel(LevelEnum.Easy, null), Times.Once());
mockQuestionService.Verify(s => s.GetByLevel(LevelEnum.Hard, new [] { 1 }), Times.Once());
}
}
}
88 changes: 68 additions & 20 deletions BitcoinShow.Test/Repositories/QuestionRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Add_Question_With_Title_Greater_Than_Max_Error()

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);
Expand All @@ -47,7 +47,7 @@ public void Add_Question_Without_Answer_Error()

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);
Expand All @@ -59,7 +59,7 @@ public void Add_Question_Success()
BitcoinShowDBContext context = DbContextFactory.GetContext();

var options = RandomOptions(4).ToList();
options.ForEach(o =>
options.ForEach(o =>
{
context.Options.Add(o);
});
Expand Down Expand Up @@ -88,9 +88,9 @@ public void GetAll_Questions_Success()
List<Option> optionsList = new List<Option>();
optionsList.Add(option);
Question question = new Question
{
{
Title = $"Random Question {1}",
Answer = new Option { Text = $"Random Option {1}"},
Answer = new Option { Text = $"Random Option {1}" },
Level = LevelEnum.Medium,
};
question.Options = new List<Option>();
Expand All @@ -101,7 +101,7 @@ public void GetAll_Questions_Success()

QuestionRepository repository = new QuestionRepository(context);

repository.GetAll().ForEach(q =>
repository.GetAll().ForEach(q =>
{
Assert.NotNull(q);
Assert.True(q.Id > 0);
Expand All @@ -121,9 +121,9 @@ public void Get_Question_Not_Found_Error()
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}"}
Answer = new Option { Id = i, Text = $"Random Option {i}" }
});
}
context.SaveChanges();
Expand All @@ -141,9 +141,9 @@ public void Get_Question_Success()
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}"},
Answer = new Option { Id = i, Text = $"Random Option {i}" },
Level = LevelEnum.VeryHard
});
}
Expand Down Expand Up @@ -187,7 +187,7 @@ public void Delete_Question_Success()
[Fact]
public void Update_Question_Without_Title_Error()
{
var option = new Option {Text = "Update_Question_Without_Answer_Error Option"};
var option = new Option { Text = "Update_Question_Without_Answer_Error Option" };
var question = new Question
{
Title = "Update_Question_Without_Title_Error",
Expand All @@ -199,15 +199,15 @@ public void Update_Question_Without_Title_Error()

QuestionRepository repository = new QuestionRepository(context);
question.Title = String.Empty;
ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() => repository.Update(question));
ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() => repository.Update(question));
Assert.NotNull(ex);
Assert.Equal(nameof(question.Title), ex.ParamName);
}

[Fact]
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 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",
Expand All @@ -219,15 +219,15 @@ public void Update_Question_With_Title_Greater_Than_Max_Error()

QuestionRepository repository = new QuestionRepository(context);
question.Title = new String('a', 201);
ArgumentOutOfRangeException ex = Assert.Throws<ArgumentOutOfRangeException>(() => repository.Update(question));
ArgumentOutOfRangeException ex = Assert.Throws<ArgumentOutOfRangeException>(() => repository.Update(question));
Assert.NotNull(ex);
Assert.Equal(nameof(question.Title), ex.ParamName);
}

[Fact]
public void Update_Question_Without_Answer_Error()
{
var option = new Option {Text = "Update_Question_Without_Answer_Error Option"};
var option = new Option { Text = "Update_Question_Without_Answer_Error Option" };
var question = new Question
{
Title = "Update_Question_Without_Answer_Error",
Expand All @@ -239,16 +239,16 @@ public void Update_Question_Without_Answer_Error()

QuestionRepository repository = new QuestionRepository(context);
question.Answer = null;
ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() => repository.Update(question));
ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() => repository.Update(question));
Assert.NotNull(ex);
Assert.Equal(nameof(question.Answer), ex.ParamName);
}

[Fact]
public void Update_Question_Success()
{
var option = new Option {Text = "Update_Question_Success Option"};
var option2 = new Option {Text = "Update_Question_Success Option2"};
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",
Expand Down Expand Up @@ -284,12 +284,60 @@ public void Update_Question_Success()
Assert.Equal(expected, actual);
}

[Fact]
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));
}

private IEnumerable<Option> RandomOptions(int nOptions)
{
for (int i = 0; i < nOptions; i++)
{
yield return new Option
{
yield return new Option
{
Text = $"Random Option {i + 1}"
};
}
Expand Down
Loading

0 comments on commit 9ae8e32

Please sign in to comment.