Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eric Kepes Submission #42

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
*.swp
*.*~
project.lock.json
.DS_Store
*.pyc
nupkg/

# Visual Studio Code
.vscode

# Rider
.idea

# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/
[Oo]ut/
msbuild.log
msbuild.err
msbuild.wrn

# Visual Studio 2015
.vs/
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Moq;
using RestaurantReviews.Api.Controllers;
using RestaurantReviews.Api.DataAccess;
using RestaurantReviews.Api.Models;
using Xunit;

namespace RestaurantReviews.Api.UnitTests.ControllerTests
{
public class RestaurantControllerTests
{
[Fact]
public async Task GetListWithNoParamsReturnsList()
{
var mockRestaurantQuery = new Mock<IRestaurantQuery>();
mockRestaurantQuery.Setup(q => q.GetRestaurants(null, null))
.Returns(Task.FromResult(new List<Restaurant> {new Restaurant()}));
var controller = new RestaurantController(null, mockRestaurantQuery.Object, null);

var result = await controller.GetListAsync();

Assert.IsType<OkObjectResult>(result.Result);
Assert.IsType<List<Restaurant>>(((OkObjectResult)result.Result).Value);
var resultList = (List<Restaurant>)((OkObjectResult) result.Result).Value;
Assert.Single(resultList);
}

[Fact]
public async Task GetListWithNoCityReturnsBadRequest()
{
var controller = new RestaurantController(null, null, null);

var result = await controller.GetListAsync(null, "PA");

Assert.IsType<BadRequestObjectResult>(result.Result);
}

[Fact]
public async Task GetListWithNoStateReturnsBadRequest()
{
var controller = new RestaurantController(null, null, null);

var result = await controller.GetListAsync("Pittsburgh");

Assert.IsType<BadRequestObjectResult>(result.Result);
}

[Fact]
public async Task GetListWithParamsReturnsList()
{
var mockRestaurantQuery = new Mock<IRestaurantQuery>();
mockRestaurantQuery.Setup(q =>
q.GetRestaurants(It.IsAny<string>(), It.IsAny<string>()))
.Returns(Task.FromResult(new List<Restaurant> {TestData.McDonalds }));
var controller = new RestaurantController(null, mockRestaurantQuery.Object, null);

var result = await controller.GetListAsync("Pittsburgh", "PA");

Assert.IsType<OkObjectResult>(result.Result);
Assert.IsType<List<Restaurant>>(((OkObjectResult)result.Result).Value);
var resultList = (List<Restaurant>)((OkObjectResult) result.Result).Value;
Assert.Single(resultList);
}

[Fact]
public async Task GetSingleWithValidIdReturnsRestaurant()
{
var mockRestaurantQuery = new Mock<IRestaurantQuery>();
mockRestaurantQuery.Setup(q => q.GetRestaurant(TestData.McDonalds.Id))
.Returns(Task.FromResult(TestData.McDonalds));
var controller = new RestaurantController(null, mockRestaurantQuery.Object, null);

var result = await controller.GetAsync(TestData.McDonalds.Id);

Assert.IsType<OkObjectResult>(result.Result);
Assert.IsType<Restaurant>(((OkObjectResult)result.Result).Value);
var restaurant = (Restaurant)((OkObjectResult) result.Result).Value;
Assert.Equal(TestData.McDonalds.Name, restaurant.Name);
}

[Fact]
public async Task GetSingleWithNonexistentIdReturnsNotFound()
{
var mockRestaurantQuery = new Mock<IRestaurantQuery>();
mockRestaurantQuery.Setup(q => q.GetRestaurant(TestData.McDonalds.Id))
.Returns(Task.FromResult(TestData.McDonalds));
var controller = new RestaurantController(null, mockRestaurantQuery.Object, null);

var result = await controller.GetAsync(TestData.Wendys.Id);

Assert.IsType<NotFoundResult>(result.Result);
}

[Fact]
public async Task GetSingleWithInvalidIdReturnsBadRequest()
{
var controller = new RestaurantController(null, null, null);

var result = await controller.GetAsync(0);

Assert.IsType<BadRequestObjectResult>(result.Result);
}

[Fact]
public async Task PostNewRestaurantThatDoesNotExistReturnsCreated()
{
var mockRestaurantValidator = new Mock<IRestaurantValidator>();
mockRestaurantValidator.Setup(v =>
v.IsRestaurantValid(It.IsAny<NewRestaurant>())).Returns(true);
var mockRestaurantQuery = new Mock<IRestaurantQuery>();
mockRestaurantQuery.Setup(q => q.GetRestaurant(TestData.McDonalds.Name, TestData.McDonalds.City, TestData.McDonalds.State))
.Returns(Task.FromResult(TestData.McDonalds));
var mockInsertRestaurant = new Mock<IInsertRestaurant>();
var controller = new RestaurantController(mockRestaurantValidator.Object,
mockRestaurantQuery.Object,
mockInsertRestaurant.Object);

var result = await controller.PostAsync(TestData.Wendys);

Assert.IsType<CreatedResult>(result.Result);
mockInsertRestaurant.Verify(i => i.Insert(It.IsAny<NewRestaurant>()),
Times.Once);
}

[Fact]
public async Task PostNewRestaurantThatIsInvalidReturnsBadRequest()
{
var mockRestaurantValidator = new Mock<IRestaurantValidator>();
mockRestaurantValidator.Setup(v =>
v.IsRestaurantValid(It.IsAny<NewRestaurant>())).Returns(false);
var controller = new RestaurantController(mockRestaurantValidator.Object,
null, null);

var result = await controller.PostAsync(TestData.Wendys);

Assert.IsType<BadRequestObjectResult>(result.Result);
}

[Fact]
public async Task PostNewRestaurantThatExistsReturnsConflict()
{
var mockRestaurantValidator = new Mock<IRestaurantValidator>();
mockRestaurantValidator.Setup(v =>
v.IsRestaurantValid(It.IsAny<NewRestaurant>())).Returns(true);
var mockRestaurantQuery = new Mock<IRestaurantQuery>();
mockRestaurantQuery.Setup(q => q.GetRestaurant(TestData.McDonalds.Name, TestData.McDonalds.City, TestData.McDonalds.State))
.Returns(Task.FromResult(TestData.McDonalds));
var mockInsertRestaurant = new Mock<IInsertRestaurant>();
var controller = new RestaurantController(mockRestaurantValidator.Object,
mockRestaurantQuery.Object,
mockInsertRestaurant.Object);

var result = await controller.PostAsync(TestData.McDonalds);

Assert.IsType<ConflictObjectResult>(result.Result);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Moq;
using RestaurantReviews.Api.Controllers;
using RestaurantReviews.Api.DataAccess;
using RestaurantReviews.Api.Models;
using Xunit;

namespace RestaurantReviews.Api.UnitTests.ControllerTests
{
public class ReviewControllerTests
{
[Fact]
public async Task GetWithValidIdReturnsReview()
{
var mockReviewQuery = new Mock<IReviewQuery>();
mockReviewQuery.Setup(q => q.GetReview(TestData.McDonaldsReview.Id))
.Returns(Task.FromResult(TestData.McDonaldsReview));
var controller = new ReviewController(null, mockReviewQuery.Object, null, null);

var result = await controller.GetAsync(TestData.McDonaldsReview.Id);

Assert.IsType<OkObjectResult>(result.Result);
Assert.IsType<Review>(((OkObjectResult)result.Result).Value);
var review = (Review)((OkObjectResult) result.Result).Value;
Assert.Equal(TestData.McDonaldsReview.RatingStars, review.RatingStars);
}

[Fact]
public async Task GetWithNonexistentIdReturnsNotFound()
{
var mockReviewQuery = new Mock<IReviewQuery>();
mockReviewQuery.Setup(q => q.GetReview(TestData.McDonaldsReview.Id))
.Returns(Task.FromResult(TestData.McDonaldsReview));
var controller = new ReviewController(null, mockReviewQuery.Object, null, null);

var result = await controller.GetAsync(TestData.WendysReview.Id);

Assert.IsType<NotFoundResult>(result.Result);
}

[Fact]
public async Task GetWithInvalidIdReturnsBadRequest()
{
var controller = new ReviewController(null, null, null, null);

var result = await controller.GetAsync(0);

Assert.IsType<BadRequestObjectResult>(result.Result);
}

[Fact]
public async Task GetListWithValidReviewerEmailReturnsReviews()
{
var mockReviewQuery = new Mock<IReviewQuery>();
mockReviewQuery.Setup(q => q.GetReviews(TestData.McDonaldsReview.ReviewerEmail))
.Returns(Task.FromResult(new List<Review> { TestData.McDonaldsReview }));
var controller = new ReviewController(null, mockReviewQuery.Object, null, null);

var result = await controller.GetListAsync(TestData.McDonaldsReview.ReviewerEmail);

Assert.IsType<OkObjectResult>(result.Result);
Assert.IsType<List<Review>>(((OkObjectResult)result.Result).Value);
var resultList = (List<Review>)((OkObjectResult) result.Result).Value;
Assert.Single(resultList);
}

[Fact]
public async Task GetListWithReviewerEmailNoReviewsReturnsEmptyList()
{
var mockReviewQuery = new Mock<IReviewQuery>();
mockReviewQuery.Setup(q => q.GetReviews(TestData.McDonaldsReview.ReviewerEmail))
.Returns(Task.FromResult(new List<Review> { TestData.McDonaldsReview }));
var controller = new ReviewController(null, mockReviewQuery.Object, null, null);

var result = await controller.GetListAsync(TestData.WendysReview.ReviewerEmail);

Assert.IsType<OkObjectResult>(result.Result);
Assert.Null(((OkObjectResult)result.Result).Value);
}

[Fact]
public async Task GetListWithInvalidReviewerEmailReturnsBadRequest()
{
var controller = new ReviewController(null, null, null, null);

var result = await controller.GetListAsync(string.Empty);

Assert.IsType<BadRequestObjectResult>(result.Result);
}

[Fact]
public async Task PostValidNewReviewReturnsCreated()
{
var mockValidator = new Mock<IReviewValidator>();
mockValidator.Setup(v => v.IsReviewValid(It.IsAny<NewReview>())).Returns(true);
var mockInsertReview = new Mock<IInsertReview>();
var controller = new ReviewController(mockValidator.Object, null, mockInsertReview.Object, null);

var result = await controller.PostAsync(TestData.WendysReview);

Assert.IsType<CreatedResult>(result.Result);
mockInsertReview.Verify(i => i.Insert(It.IsAny<NewReview>()),
Times.Once);
}

[Fact]
public async Task PostInvalidNewReviewReturnsCreated()
{
var mockValidator = new Mock<IReviewValidator>();
mockValidator.Setup(v => v.IsReviewValid(It.IsAny<NewReview>())).Returns(false);
var controller = new ReviewController(mockValidator.Object, null, null, null);

var result = await controller.PostAsync(TestData.WendysReview);

Assert.IsType<BadRequestObjectResult>(result.Result);
}

[Fact]
public async Task DeleteExistingReviewReturnsOk()
{
var mockDeleteReview = new Mock<IDeleteReview>();
mockDeleteReview.Setup(d => d.Delete(123)).Returns(Task.FromResult(1));
var controller = new ReviewController(null, null, null, mockDeleteReview.Object);

var result = await controller.DeleteAsync(123);

Assert.IsType<OkObjectResult>(result.Result);
}

[Fact]
public async Task DeleteMissingReviewReturnsNotFound()
{
var mockDeleteReview = new Mock<IDeleteReview>();
mockDeleteReview.Setup(d => d.Delete(123)).Returns(Task.FromResult(0));
var controller = new ReviewController(null, null, null, mockDeleteReview.Object);

var result = await controller.DeleteAsync(123);

Assert.IsType<NotFoundObjectResult>(result.Result);
}

[Fact]
public async Task DeleteInvalidIdReturnsBadRequest()
{
var controller = new ReviewController(null, null, null, null);

var result = await controller.DeleteAsync(-1);

Assert.IsType<BadRequestObjectResult>(result.Result);
}
}
}
Loading