Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into add-sonarcloud-scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
cshnimble authored Nov 10, 2022
2 parents 27100c3 + 32dde0f commit 4c21daf
Show file tree
Hide file tree
Showing 17 changed files with 571 additions and 362 deletions.
10 changes: 0 additions & 10 deletions .github/PULL_REQUEST_TEMPLATE.md

This file was deleted.

3 changes: 2 additions & 1 deletion Data.Mock/MockProjectRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public MockProjectRepository(ILogger<MockProjectRepository> logger)
_projects = new List<Project> {PopulatedProject(), EmptyProject()};
}

public Task<RepositoryResult<List<ProjectSearchResult>>> GetProjects(int page = 1, string title = default)
public Task<RepositoryResult<List<ProjectSearchResult>>> GetProjects(int page = 1, string title = default,
int pageSize = 10)
{
var result = new RepositoryResult<List<ProjectSearchResult>>
{
Expand Down
4 changes: 4 additions & 0 deletions Data.TRAMS.Tests/Data.TRAMS.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<Folder Include="Extensions\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Collections.Generic;
using Data.TRAMS.ExtensionMethods;
using FluentAssertions;
using Xunit;

namespace Data.TRAMS.Tests.Extensions
{
public class DictionaryQuerystringExtensionsTests
{
private static readonly IDictionary<string, string> Parameters = new Dictionary<string, string>
{
{ "first", "v1" },
{ "second", "v2" }
};

[Fact]
public void Should_convert_string_pairs_to_a_querystring()
{
Parameters.ToQueryString().Should().Be("?first=v1&second=v2");
}

[Fact]
public void Should_optionally_prefix_with_a_question_mark()
{
Parameters.ToQueryString(prefix: false).StartsWith('?').Should().BeFalse();
}

[Fact]
public void Should_produce_an_empty_string_if_no_parameters_are_provided()
{
new Dictionary<string, string>().ToQueryString().Should().BeEmpty();
}

[Fact]
public void Should_cope_with_null_values()
{
var withNullValue = new Dictionary<string, string>
{
{ "key", null }
}.ToQueryString();

withNullValue.Should().Be("?key=");
}

[Fact]
public void Should_optionally_skip_parameters_with_null_values()
{
var skippedEmpty = new Dictionary<string, string>(Parameters)
{
{ "key", null }
}.ToQueryString(keepEmpty: false);

skippedEmpty.Should().NotContain("key=");
skippedEmpty.Should().Be("?first=v1&second=v2");
}
}
}
1 change: 0 additions & 1 deletion Data.TRAMS.Tests/TramsAcademiesRepositoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Net;
using System.Net.Http;
using System.Threading;
using Data.Models;
using Data.TRAMS.Models;
using Data.TRAMS.Tests.TestFixtures;
Expand Down
12 changes: 6 additions & 6 deletions Data.TRAMS.Tests/TramsProjectsRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public GetProjectsTests()
[Fact]
public async void GivenSingleProjectSummaryReturned_MapsCorrectly()
{
_httpClient.Setup(c => c.GetAsync("academyTransferProject?page=1&title=")).ReturnsAsync(new HttpResponseMessage
_httpClient.Setup(c => c.GetAsync("academyTransferProjects?page=1&count=10&title=")).ReturnsAsync(new HttpResponseMessage
{
Content = new StringContent(JsonConvert.SerializeObject(new PagedResult<TramsProjectSummary>(_foundSummaries, 12)))
});
Expand Down Expand Up @@ -324,7 +324,7 @@ public async void GivenMultipleProjectSummariesReturned_MapsCorrectly()
}
);

_httpClient.Setup(c => c.GetAsync("academyTransferProject?page=1&title=")).ReturnsAsync(new HttpResponseMessage
_httpClient.Setup(c => c.GetAsync("academyTransferProjects?page=1&count=10&title=")).ReturnsAsync(new HttpResponseMessage
{
Content = new StringContent(JsonConvert.SerializeObject(new PagedResult<TramsProjectSummary>(_foundSummaries)))
});
Expand All @@ -340,7 +340,7 @@ public async void GivenMultipleProjectSummariesReturned_MapsCorrectly()
[Fact]
public async void GivenMultipleProjectSummaries_ReturnsMappedSummariesCorrectly()
{
_httpClient.Setup(c => c.GetAsync("academyTransferProject?page=1&title=")).ReturnsAsync(new HttpResponseMessage
_httpClient.Setup(c => c.GetAsync("academyTransferProjects?page=1&count=10&title=")).ReturnsAsync(new HttpResponseMessage
{
Content = new StringContent(JsonConvert.SerializeObject(new PagedResult<TramsProjectSummary>(_foundSummaries,12)))
});
Expand All @@ -360,7 +360,7 @@ public async void GivenMultipleProjectSummaries_ReturnsMappedSummariesCorrectly(
[InlineData(HttpStatusCode.InternalServerError)]
public async void GivenApiReturnsError_ThrowsApiError(HttpStatusCode httpStatusCode)
{
_httpClient.Setup(c => c.GetAsync("academyTransferProject?page=1&title=")).ReturnsAsync(new HttpResponseMessage
_httpClient.Setup(c => c.GetAsync("academyTransferProjects?page=1&count=10&title=")).ReturnsAsync(new HttpResponseMessage
{
StatusCode = httpStatusCode
});
Expand All @@ -374,14 +374,14 @@ public async void GivenApiReturnsError_ThrowsApiError(HttpStatusCode httpStatusC
[InlineData(3)]
public async void GivenPage_GetsProjectForPage(int page)
{
_httpClient.Setup(c => c.GetAsync($"academyTransferProject?page={page}&title=")).ReturnsAsync(new HttpResponseMessage
_httpClient.Setup(c => c.GetAsync($"academyTransferProjects?page={page}&count=10&title=")).ReturnsAsync(new HttpResponseMessage
{
Content = new StringContent(JsonConvert.SerializeObject(new PagedResult<TramsProjectSummary>(_foundSummaries, 12)))
});

await _subject.GetProjects(page);

_httpClient.Verify(c => c.GetAsync($"academyTransferProject?page={page}&title="), Times.Once());
_httpClient.Verify(c => c.GetAsync($"academyTransferProjects?page={page}&count=10&title="), Times.Once());
}
}

Expand Down
40 changes: 40 additions & 0 deletions Data.TRAMS/ExtensionMethods/DictionaryQuerystringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Encodings.Web;

namespace Data.TRAMS.ExtensionMethods
{
public static class DictionaryQuerystringExtensions
{
/// <summary>
/// Converts a <see cref="IDictionary{string, string}" /> to an encoded querystring.
/// </summary>
/// <param name="parameters">
/// An <see cref="IDictionary{string, string}" /> containing parameter names (of
/// <see cref="string" />) and values (of <see cref="string" />)
/// </param>
/// <param name="prefix">A <see cref="bool" /> defining whether the querystring should have a '?' prefix (default: true)</param>
/// <param name="keepEmpty">
/// A <see cref="bool" /> defining whether keys with null/empty values should be kept (default:
/// true)
/// </param>
/// <returns>A string representing the parameters combined, UrlEncoded and (optionally) prefixed ready to be used in a URI</returns>
public static string ToQueryString(this IDictionary<string, string> parameters, bool prefix = true,
bool keepEmpty = true)
{
IList<string> parameterPairs = parameters
.Where(x => keepEmpty || string.IsNullOrWhiteSpace(x.Value) is false)
.Select(x => $"{Encode(x.Key)}={Encode(x.Value)}")
.ToList();

return parameterPairs.Count > 0
? $"{(prefix ? "?" : string.Empty)}{string.Join("&", parameterPairs)}"
: string.Empty;

string Encode(string x)
{
return string.IsNullOrWhiteSpace(x) ? string.Empty : UrlEncoder.Default.Encode(x);
}
}
}
}
Loading

0 comments on commit 4c21daf

Please sign in to comment.