Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

Commit

Permalink
[Unit-tests]: add blog test
Browse files Browse the repository at this point in the history
  • Loading branch information
jackinf committed Nov 25, 2017
1 parent 44ae09a commit 6eb9f1e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion backend/Idontknow.Domain/Repository/IBlogRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface IBlogRepository
{
Task<PaginatedListResult<GetBlogsResponseViewModel>> GetBlogs(GetBlogsRequestViewModel viewModel);

Task AddBlog(AddBlogRequestViewModel viewModel);
Task<int> AddBlog(AddBlogRequestViewModel viewModel);

}
}
2 changes: 1 addition & 1 deletion backend/Idontknow.Domain/Service/IBloggingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface IBloggingService
//
// Blogs

Task<ServiceResult<bool>> CreateBlog(AddBlogRequestViewModel viewModel);
Task<ServiceResult<int>> CreateBlog(AddBlogRequestViewModel viewModel);

Task<ServiceResult<List<GetBlogsResponseViewModel>>> GetBlogs(GetBlogsRequestViewModel viewModel);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
using System.Net.Http;
using Microsoft.AspNetCore.TestHost;
using System.Linq;
using Idontknow.Domain.ViewModels.Result;
using Idontknow.Domain.ViewModels.Service.Blog;
using Idontknow.IntegrationTests.Utils;
using Xunit;

namespace Idontknow.IntegrationTests.Feature.Blogging
{
public class when_blog_is_added
{
private TestServer _server;
private HttpClient _client;

private readonly int _newBlogId;
private const string RequestUri = "/api/blogs";
public when_blog_is_added()
{
// TODO: post
_newBlogId = ApiClientFixture.Current.HttpPostJson<ServiceResult<int>>(RequestUri, new AddBlogRequestViewModel
{
Rating = 4,
Url = "www.test.com"
}).Result.Payload;
}

[Fact]
public void then_blog_should_be_in_database()
{
// TODO: get
ApiServerFixture.Current.DoDatabaseOperation(context =>
{
Assert.True(context.Blogs.SingleOrDefault(blog => blog.BlogId == _newBlogId) != null);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public when_user_searches_for_blogs()
public async Task user_should_be_able_to_get_blogs_without_filters()
{
// act
var result = await ApiClientFixture.Current.GetApiResult<ServiceResult<List<GetBlogsResponseViewModel>>>(
var result = await ApiClientFixture.Current.HttpGet<ServiceResult<List<GetBlogsResponseViewModel>>>(
RequestUri);

// assert
Expand All @@ -53,7 +53,7 @@ public async Task user_should_be_able_to_get_blogs_without_filters()
public async Task user_should_be_able_to_find_blogs_by_rating()
{
// act
var result = await ApiClientFixture.Current.GetApiResult<ServiceResult<List<GetBlogsResponseViewModel>>>(
var result = await ApiClientFixture.Current.HttpGet<ServiceResult<List<GetBlogsResponseViewModel>>>(
RequestUri,
new Dictionary<string, string> { { nameof(GetBlogsRequestViewModel.Rating), "3" } });

Expand Down
13 changes: 12 additions & 1 deletion backend/Idontknow.IntegrationTests/Utils/ApiClientFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private ApiClientFixture()
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(responseContent.TokenType, responseContent.AccessToken);
}

public async Task<TResult> GetApiResult<TResult>(string requestUri, Dictionary<string, string> parameters = null)
public async Task<TResult> HttpGet<TResult>(string requestUri, Dictionary<string, string> parameters = null)
{
if (parameters != null)
requestUri = Microsoft.AspNetCore.WebUtilities.QueryHelpers.AddQueryString(requestUri, parameters);
Expand All @@ -61,6 +61,17 @@ public async Task<TResult> GetApiResult<TResult>(string requestUri, Dictionary<s
return result;
}

public async Task<TResult> HttpPostJson<TResult>(string requestUri, object value)
{
var contentOne = new StringContent(JsonConvert.SerializeObject(value), Encoding.UTF8, "application/json");
var httpResult = await Client.PostAsync(requestUri, contentOne);
httpResult.EnsureSuccessStatusCode();

var serializedResult = await httpResult.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<TResult>(serializedResult);
return result;
}

~ApiClientFixture()
{
Dispose();
Expand Down
7 changes: 4 additions & 3 deletions backend/Idontknow.Service/BloggingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Idontknow.Service
{
// ReSharper disable once ClassNeverInstantiated.Global
public class BloggingService : IBloggingService
{
private readonly IBloggingUnitOfWork _unitOfWork;
Expand All @@ -27,13 +28,13 @@ public async Task<ServiceResult<List<GetBlogsResponseViewModel>>> GetBlogs(GetBl
return ServiceResultFactory.SuccessWithPaginator(paginatedListResult);
}

public async Task<ServiceResult<bool>> CreateBlog(AddBlogRequestViewModel viewModel)
public async Task<ServiceResult<int>> CreateBlog(AddBlogRequestViewModel viewModel)
{
await _unitOfWork.BeginTransaction();
await _unitOfWork.BlogRepository.AddBlog(viewModel);
var newBlogId = await _unitOfWork.BlogRepository.AddBlog(viewModel);
await _unitOfWork.SaveChangesAsync();
_unitOfWork.CommitTransaction();
return ServiceResultFactory.Success(true);
return ServiceResultFactory.Success(newBlogId);
}

//
Expand Down

0 comments on commit 6eb9f1e

Please sign in to comment.