Skip to content
Prev Previous commit
Next Next commit
chore: start integration tests
  • Loading branch information
wisepotato committed Oct 23, 2019
commit 5b10565325162cc2d12e64e036c8a43440fa8ae7
15 changes: 15 additions & 0 deletions JsonApiDotnetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCore", "src\Js
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GettingStarted", "src\Examples\GettingStarted\GettingStarted.csproj", "{067FFD7A-C66B-473D-8471-37F5C95DF61C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntegrationTests", "test\IntegrationTests\IntegrationTests.csproj", "{CEB08B86-6BF1-4227-B20F-45AE9C1CC6D9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -158,6 +160,18 @@ Global
{067FFD7A-C66B-473D-8471-37F5C95DF61C}.Release|x64.Build.0 = Release|Any CPU
{067FFD7A-C66B-473D-8471-37F5C95DF61C}.Release|x86.ActiveCfg = Release|Any CPU
{067FFD7A-C66B-473D-8471-37F5C95DF61C}.Release|x86.Build.0 = Release|Any CPU
{CEB08B86-6BF1-4227-B20F-45AE9C1CC6D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CEB08B86-6BF1-4227-B20F-45AE9C1CC6D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CEB08B86-6BF1-4227-B20F-45AE9C1CC6D9}.Debug|x64.ActiveCfg = Debug|Any CPU
{CEB08B86-6BF1-4227-B20F-45AE9C1CC6D9}.Debug|x64.Build.0 = Debug|Any CPU
{CEB08B86-6BF1-4227-B20F-45AE9C1CC6D9}.Debug|x86.ActiveCfg = Debug|Any CPU
{CEB08B86-6BF1-4227-B20F-45AE9C1CC6D9}.Debug|x86.Build.0 = Debug|Any CPU
{CEB08B86-6BF1-4227-B20F-45AE9C1CC6D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CEB08B86-6BF1-4227-B20F-45AE9C1CC6D9}.Release|Any CPU.Build.0 = Release|Any CPU
{CEB08B86-6BF1-4227-B20F-45AE9C1CC6D9}.Release|x64.ActiveCfg = Release|Any CPU
{CEB08B86-6BF1-4227-B20F-45AE9C1CC6D9}.Release|x64.Build.0 = Release|Any CPU
{CEB08B86-6BF1-4227-B20F-45AE9C1CC6D9}.Release|x86.ActiveCfg = Release|Any CPU
{CEB08B86-6BF1-4227-B20F-45AE9C1CC6D9}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -173,6 +187,7 @@ Global
{789085E1-048F-4996-B600-791B9CA3A663} = {026FBC6C-AF76-4568-9B87-EC73457899FD}
{8BCFF95F-4850-427C-AEDB-B5B4F62B2C7B} = {026FBC6C-AF76-4568-9B87-EC73457899FD}
{21D27239-138D-4604-8E49-DCBE41BCE4C8} = {7A2B7ADD-ECB5-4D00-AA6A-D45BD11C97CF}
{CEB08B86-6BF1-4227-B20F-45AE9C1CC6D9} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A2421882-8F0A-4905-928F-B550B192F9A4}
Expand Down
7 changes: 5 additions & 2 deletions src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,13 @@ public virtual async Task<IEnumerable<TResource>> PageAsync(IQueryable<TResource
}

// since EntityFramework does not support IQueryable.Reverse(), we need to know the number of queried entities
int numberOfEntities = await this.CountAsync(entities);


entities = entities.Reverse();


// may be negative
int virtualFirstIndex = numberOfEntities - pageSize * Math.Abs(pageNumber);
int virtualFirstIndex = pageSize * Math.Abs(pageNumber);
int numberOfElementsInPage = Math.Min(pageSize, virtualFirstIndex + pageSize);

return await ToListAsync(entities
Expand Down
27 changes: 27 additions & 0 deletions test/IntegrationTests/Data/EntityRepositoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace IntegrationTests.Data
{
public class EntityRepositoryTests
{

[Theory]
[InlineData(6, -1, new[] { 4, 5, 6, 7, 8, 9 })]
[InlineData(6, -2, new[] { 1, 2, 3 })]
[InlineData(20, -1, new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 })]
public async Task Paging_PageNumberIsNegative_PageCorrectIds(int pageSize, int pageNumber, int[] expectedIds)
{
// Arrange
var todoItems = DbSetMock.Create(TodoItems(1, 2, 3, 4, 5, 6, 7, 8, 9)).Object;
var repository = GetRepository();

var result = await repository.PageAsync(todoItems, pageSize, pageNumber);

Assert.Equal(TodoItems(expectedIds), result, new IdComparer<TodoItem>());
}
}
}
16 changes: 16 additions & 0 deletions test/IntegrationTests/IntegrationTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions test/IntegrationTests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using Xunit;

namespace IntegrationTests
{
public class UnitTest1
{
[Fact]
public void Test1()
{

}
}
}
12 changes: 0 additions & 12 deletions test/UnitTests/Data/DefaultEntityRepository_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,7 @@ public async Task Page_When_PageNumber_Is_Positive_Returns_PageNumberTh_Page_Of_
Assert.Equal(TodoItems(expectedResult), result, new IdComparer<TodoItem>());
}

[Theory]
[InlineData(6, -1, new[] { 4, 5, 6, 7, 8, 9 })]
[InlineData(6, -2, new[] { 1, 2, 3 })]
[InlineData(20, -1, new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 })]
public async Task Paging_PageNumberIsNegative_PageCorrectIds(int pageSize, int pageNumber, int[] expectedIds)
{
var todoItems = DbSetMock.Create(TodoItems(1, 2, 3, 4, 5, 6, 7, 8, 9)).Object;
var repository = GetRepository();

var result = await repository.PageAsync(todoItems, pageSize, pageNumber);

Assert.Equal(TodoItems(expectedIds), result, new IdComparer<TodoItem>());
}

private static TodoItem[] TodoItems(params int[] ids)
{
Expand Down