-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MT-00 Added 3 Unit Tests and CI workflow
- Loading branch information
Showing
22 changed files
with
446 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
pull_request: | ||
branches: [ main ] | ||
env: | ||
DOTNET_VERSION: '8.0.x' | ||
jobs: | ||
ci: | ||
name: ci-${{matrix.os}} | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest, windows-latest] | ||
steps: | ||
- name: Check out the repo | ||
uses: actions/checkout@v4 | ||
- name: Setup .NET | ||
uses: actions/setup-dotnet@v4 | ||
with: | ||
dotnet-version: ${{ env.DOTNET_VERSION }} | ||
- name: Install dependencies | ||
run: dotnet restore | ||
- name: Build | ||
run: dotnet build --configuration Release --no-restore | ||
- name: Test | ||
run: dotnet test --no-restore --verbosity normal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: Docker Image CI | ||
name: Docker Image CD | ||
|
||
on: | ||
push: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/Data/MeTube.Data/Repository/Channels/IChannelRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using MeTube.Data.Models.Channels; | ||
|
||
namespace MeTube.Data.Repository.Channels; | ||
|
||
public interface IChannelRepository : IBaseRepository<Channel> | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace MeTube.Data.Repository; | ||
|
||
public interface IBaseRepository<TEntity> | ||
{ | ||
Task<TEntity> CreateAsync(TEntity entity); | ||
|
||
IQueryable<TEntity> GetAll(); | ||
|
||
IQueryable<TEntity> GetAllAsNoTracking(); | ||
|
||
Task<TEntity> EditAsync(TEntity entity); | ||
|
||
Task<TEntity> DeleteAsync(TEntity entity); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Data/MeTube.Data/Repository/Reactions/IReactionTypeRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using MeTube.Data.Models.Reactions; | ||
|
||
namespace MeTube.Data.Repository.Reactions; | ||
|
||
public interface IReactionTypeRepository : IBaseRepository<ReactionType> | ||
{ | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Data/MeTube.Data/Repository/Reactions/IVideoReactionRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using MeTube.Data.Models.Reactions; | ||
|
||
namespace MeTube.Data.Repository.Reactions; | ||
|
||
public interface IVideoReactionRepository : IBaseRepository<VideoReaction> | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using MeTube.Data.Models.Videos; | ||
|
||
namespace MeTube.Data.Repository.Videos; | ||
|
||
public interface IVideoRepository : IBaseRepository<Video> | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Service/MeTube.Service.Tests/MeTube.Service.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="Moq" Version="4.20.70" /> | ||
<PackageReference Include="NUnit" Version="3.14.0" /> | ||
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Data\MeTube.Data.Models\MeTube.Data.Models.csproj" /> | ||
<ProjectReference Include="..\..\Data\MeTube.Data\MeTube.Data.csproj" /> | ||
<ProjectReference Include="..\MeTube.Service.Mappings\MeTube.Service.Mappings.csproj" /> | ||
<ProjectReference Include="..\MeTube.Service.Models\MeTube.Service.Models.csproj" /> | ||
<ProjectReference Include="..\MeTube.Service\MeTube.Service.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="NUnit.Framework" /> | ||
</ItemGroup> | ||
|
||
</Project> |
29 changes: 29 additions & 0 deletions
29
src/Service/MeTube.Service.Tests/Mocks/TaskAsyncEnumerable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Linq.Expressions; | ||
|
||
namespace MeTube.Service.Tests.Mocks; | ||
|
||
public class TestAsyncEnumerable<T> : EnumerableQuery<T>, IAsyncEnumerable<T>, IQueryable<T> | ||
{ | ||
public TestAsyncEnumerable(IEnumerable<T> enumerable) | ||
: base(enumerable) | ||
{ } | ||
|
||
public TestAsyncEnumerable(Expression expression) | ||
: base(expression) | ||
{ } | ||
|
||
public IAsyncEnumerator<T> GetEnumerator() | ||
{ | ||
return new TestAsyncEnumerator<T>(this.AsEnumerable().GetEnumerator()); | ||
} | ||
|
||
public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default) | ||
{ | ||
return new TestAsyncEnumerator<T>(this.AsEnumerable().GetEnumerator()); | ||
} | ||
|
||
IQueryProvider IQueryable.Provider | ||
{ | ||
get { return new TestAsyncQueryProvider<T>(this); } | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/Service/MeTube.Service.Tests/Mocks/TestAsyncEnumerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
namespace MeTube.Service.Tests.Mocks; | ||
|
||
public class TestAsyncEnumerator<T> : IAsyncEnumerator<T> | ||
{ | ||
private readonly IEnumerator<T> _inner; | ||
|
||
public TestAsyncEnumerator(IEnumerator<T> inner) | ||
{ | ||
_inner = inner; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_inner.Dispose(); | ||
} | ||
|
||
public T Current | ||
{ | ||
get | ||
{ | ||
return _inner.Current; | ||
} | ||
} | ||
|
||
public Task<bool> MoveNext(CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult(_inner.MoveNext()); | ||
} | ||
|
||
public ValueTask<bool> MoveNextAsync() | ||
{ | ||
return ValueTask.FromResult(_inner.MoveNext()); | ||
} | ||
|
||
public ValueTask DisposeAsync() | ||
{ | ||
_inner.Dispose(); | ||
return ValueTask.CompletedTask; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/Service/MeTube.Service.Tests/Mocks/TestAsyncQueryProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using System.Linq.Expressions; | ||
|
||
namespace MeTube.Service.Tests.Mocks; | ||
|
||
public class TestAsyncQueryProvider<TEntity> : IAsyncQueryProvider | ||
{ | ||
private readonly IQueryProvider _inner; | ||
|
||
internal TestAsyncQueryProvider(IQueryProvider inner) | ||
{ | ||
_inner = inner; | ||
} | ||
|
||
public IQueryable CreateQuery(Expression expression) | ||
{ | ||
return new TestAsyncEnumerable<TEntity>(expression); | ||
} | ||
|
||
public IQueryable<TElement> CreateQuery<TElement>(Expression expression) | ||
{ | ||
return new TestAsyncEnumerable<TElement>(expression); | ||
} | ||
|
||
public object Execute(Expression expression) | ||
{ | ||
return _inner.Execute(expression); | ||
} | ||
|
||
public TResult Execute<TResult>(Expression expression) | ||
{ | ||
return _inner.Execute<TResult>(expression); | ||
} | ||
|
||
public IAsyncEnumerable<TResult> ExecuteAsync<TResult>(Expression expression) | ||
{ | ||
return new TestAsyncEnumerable<TResult>(expression); | ||
} | ||
|
||
public Task<TResult> ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult(Execute<TResult>(expression)); | ||
} | ||
|
||
TResult IAsyncQueryProvider.ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken) | ||
{ | ||
return (TResult)(object)Task.FromResult(Execute<TEntity>(expression)); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/Service/MeTube.Service.Tests/Utilities/MoqExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using MeTube.Service.Tests.Mocks; | ||
using Moq; | ||
|
||
namespace MeTube.Service.Tests.Utilities; | ||
|
||
public static class MoqExtensions | ||
{ | ||
public static Mock<IQueryable<TEntity>> MockQueryable<TEntity>(IQueryable<TEntity> mockCollection) | ||
{ | ||
var mockQueryable = new Mock<IQueryable<TEntity>>(); | ||
|
||
mockQueryable.As<IAsyncEnumerable<TEntity>>() | ||
.Setup(m => m.GetAsyncEnumerator(It.IsAny<CancellationToken>())) | ||
.Returns(new TestAsyncEnumerator<TEntity>(mockCollection.GetEnumerator())); | ||
|
||
mockQueryable.As<IQueryable<TEntity>>() | ||
.Setup(m => m.Provider) | ||
.Returns(new TestAsyncQueryProvider<TEntity>(mockCollection.Provider)); | ||
|
||
mockQueryable.As<IQueryable<TEntity>>().Setup(m => m.Expression).Returns(mockCollection.Expression); | ||
mockQueryable.As<IQueryable<TEntity>>().Setup(m => m.ElementType).Returns(mockCollection.ElementType); | ||
mockQueryable.As<IQueryable<TEntity>>().Setup(m => m.GetEnumerator()).Returns(() => mockCollection.GetEnumerator()); | ||
|
||
return mockQueryable; | ||
} | ||
|
||
public static bool CompareCollections<TEntity>( | ||
IEnumerable<TEntity> firstCollection, | ||
IEnumerable<TEntity> secondCollection, | ||
Func<TEntity, TEntity, bool> comparer) | ||
{ | ||
if (firstCollection.Count() != secondCollection.Count()) return false; | ||
|
||
foreach (var expectedItem in firstCollection) | ||
{ | ||
bool isPresent = false; | ||
|
||
foreach (var actualItem in secondCollection) | ||
{ | ||
if(comparer.Invoke(actualItem, expectedItem)) | ||
{ | ||
isPresent = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!isPresent) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.