Use new API for blog

This commit is contained in:
Robert Marshall 2020-04-11 13:37:14 +01:00
parent e389b2404a
commit 25c320bf6b
17 changed files with 356 additions and 314 deletions

View file

@ -0,0 +1,249 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
using Website.Data;
using Website.Models;
using Xunit;
namespace Website.Tests.Data {
public class BlogApiTests {
[Fact]
public async Task GetPostByUrlAsync_WithUrl_ReturnsBlogPost() {
const string json = @"{""id"":1,""title"":""title"",""content"":""content"",""timestamp"":""2020-04-10T13:00:42"",""draft"":""draft"",""url"":""url"",""userId"":0}";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Get)
.WithUrl("/get/test")
.WithResponse(json)
.Build();
var expectation = new BlogPost {
Id = 1,
Title = "title",
Content = "content",
Draft = "draft",
Timestamp = new DateTime(2020, 04, 10, 13, 00, 42),
Url = "url",
UserId = 0
};
var api = new BlogApi(httpClient);
(await api.GetPostByUrlAsync("test")).Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task GetLatestPostsAsync_WithNoParameters_ReturnsBlogPostCollection() {
const string json = @"[{""id"":1,""title"":""title"",""content"":""content"",""timestamp"":""2020-04-10T13:00:42"",""draft"":""draft"",""url"":""url"",""userId"":0}]";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Get)
.WithUrl("/getlatestposts")
.WithResponse(json)
.Build();
var expectation = new[] {
new BlogPost {
Id = 1,
Title = "title",
Content = "content",
Draft = "draft",
Timestamp = new DateTime(2020, 04, 10, 13, 00, 42),
Url = "url",
UserId = 0
}
};
var api = new BlogApi(httpClient);
(await api.GetLatestPostsAsync()).Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task GetLatestPostsAsync_WithCount_ReturnsBlogPostCollection() {
const string json = @"[{""id"":1,""title"":""title"",""content"":""content"",""timestamp"":""2020-04-10T13:00:42"",""draft"":""draft"",""url"":""url"",""userId"":0}]";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Get)
.WithUrl("/getlatestposts?count=1")
.WithResponse(json)
.Build();
var expectation = new[] {
new BlogPost {
Id = 1,
Title = "title",
Content = "content",
Draft = "draft",
Timestamp = new DateTime(2020, 04, 10, 13, 00, 42),
Url = "url",
UserId = 0
}
};
var api = new BlogApi(httpClient);
(await api.GetLatestPostsAsync(1)).Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task GetLatestPostsAsync_WithCountAndOffset_ReturnsBlogPostCollection() {
const string json = @"[{""id"":1,""title"":""title"",""content"":""content"",""timestamp"":""2020-04-10T13:00:42"",""draft"":""draft"",""url"":""url"",""userId"":0}]";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Get)
.WithUrl("/getlatestposts")
.WithQueryString("count", "1")
.WithQueryString("offset", "1")
.WithResponse(json)
.Build();
var expectation = new[] {
new BlogPost {
Id = 1,
Title = "title",
Content = "content",
Draft = "draft",
Timestamp = new DateTime(2020, 04, 10, 13, 00, 42),
Url = "url",
UserId = 0
}
};
var api = new BlogApi(httpClient);
(await api.GetLatestPostsAsync(1, 1)).Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task GetLatestPostAsync_ReturnsBlogPost() {
const string json = @"{""id"":1,""title"":""title"",""content"":""content"",""timestamp"":""2020-04-10T13:00:42"",""draft"":""draft"",""url"":""url"",""userId"":0}";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Get)
.WithUrl("/getlatestpost")
.WithResponse(json)
.Build();
var expectation = new BlogPost {
Id = 1,
Title = "title",
Content = "content",
Draft = "draft",
Timestamp = new DateTime(2020, 04, 10, 13, 00, 42),
Url = "url",
UserId = 0
};
var api = new BlogApi(httpClient);
(await api.GetLatestPostAsync()).Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task GetCountAsync_ReturnsCount() {
const string json = @"23";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Get)
.WithUrl("/getcount")
.WithResponse(json)
.Build();
var api = new BlogApi(httpClient);
(await api.GetCountAsync()).Should().Be(23);
}
[Fact]
public async Task GetPostByIdAsync_WithUrl_ReturnsBlogPost() {
const string json = @"{""id"":1,""title"":""title"",""content"":""content"",""timestamp"":""2020-04-10T13:00:42"",""draft"":""draft"",""url"":""url"",""userId"":0}";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Get)
.WithUrl("/get/1")
.WithResponse(json)
.Build();
var expectation = new BlogPost {
Id = 1,
Title = "title",
Content = "content",
Draft = "draft",
Timestamp = new DateTime(2020, 04, 10, 13, 00, 42),
Url = "url",
UserId = 0
};
var api = new BlogApi(httpClient);
(await api.GetPostByIdAsync(1)).Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task SavePost_WithPost_ReturnsNothing() {
const string requestJson = @"{""Id"":1,""Title"":""title"",""Content"":""content""}";
const string responseJson = @"{""id"":1,""title"":""title"",""content"":""content"",""timestamp"":""2020-04-10T13:00:42"",""draft"":""draft"",""url"":""url"",""userId"":0}";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Post)
.WithUrl("/savepost")
.WithPostBody(requestJson)
.WithResponse(responseJson)
.Build();
var post = new BlogPostSubmission {Id = 1, Title = "title", Content = "content"};
var expectation = new BlogPost {
Id = 1,
Title = "title",
Content = "content",
Draft = "draft",
Timestamp = new DateTime(2020, 04, 10, 13, 00, 42),
Url = "url",
UserId = 0
};
var api = new BlogApi(httpClient);
(await api.SavePost(post)).Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task GetAllPostsAsync_ReturnsBlogPostCollection() {
const string json =
@"[{""id"":1,""title"":""title"",""content"":""content"",""timestamp"":""2020-04-10T13:00:42"",""draft"":""draft"",""url"":""url"",""userId"":0}]";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Get)
.WithUrl("/getallposts")
.WithResponse(json)
.Build();
var expectation = new[] {
new BlogPost {
Id = 1,
Title = "title",
Content = "content",
Draft = "draft",
Timestamp = new DateTime(2020, 04, 10, 13, 00, 42),
Url = "url",
UserId = 0
}
};
var api = new BlogApi(httpClient);
(await api.GetAllPostsAsync()).Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task DeletePost_WithId_ReturnsNothing() {
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Post)
.WithUrl("/deletepost")
.WithPostBody("1")
.Build(out var mockHttpMessageHandler);
var api = new BlogApi(httpClient);
await api.DeletePostAsync(1);
mockHttpMessageHandler.VerifyNoOutstandingExpectation();
}
[Fact]
public async Task PublishPost_WithId_ReturnsNothing() {
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Post)
.WithUrl("/publishpost")
.WithPostBody("1")
.Build(out var mockHttpMessageHandler);
var api = new BlogApi(httpClient);
await api.PublishPostAsync(1);
mockHttpMessageHandler.VerifyNoOutstandingExpectation();
}
}
}

View file

@ -1,55 +0,0 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Dapper;
using FluentAssertions;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using Website.Data;
using Website.Data.States;
using Website.Models;
using Xunit;
namespace Website.Tests.Data
{
public class BlogRepositoryTests
{
// [Fact]
// public void GetPostAsync_WithValidId_ReturnsBlogPost() {
// var blogPostState = new BlogPostState
// {
// Post_Id = 1,
// Post_Title = "title",
// Post_Content = "content",
// Post_Timestamp = new DateTime(),
// Post_Deleted = false,
// Post_Draft = "draft",
// Post_Url = "url",
// User_Id = 1
// };
// var connection = Substitute.For<IDbConnection>();
// connection.QueryAsync(null, null, (Func<object[], BlogPostState>)null, null, null, false, null, null, null).ReturnsForAnyArgs(new[]{ blogPostState });
// var provider = Substitute.For<IDatabaseProvider>();
// provider.NewConnection().Returns(connection);
// var repo = new BlogRepository(provider);
// var post = repo.GetPostAsync(1);
// var expected = new BlogPost
// {
// Id = 1,
// Title = "title",
// Content = "content",
// Timestamp = new DateTime(),
// Draft = "draft",
// Url = "url",
// UserId = 1
// }; ;
// post.Should().BeEquivalentTo(expected);
// }
//public async Task GetPostAsync_WithInvalidId_ReturnsNull()
//public async Task GetPostAsync_WithDeletedPost_ReturnsNull()
}
}