Skip to content

Commit

Permalink
EFCore.QueryRepository.Tests Project added.
Browse files Browse the repository at this point in the history
  • Loading branch information
TanvirAgoda authored and Tanvir Ahmad Arjel committed Nov 19, 2023
1 parent 2eef4a5 commit 6d72934
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 55 deletions.
11 changes: 0 additions & 11 deletions tests/EFCore.QueryRepository.Tests/DemoDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,5 @@ public class Employee
{
public int Id { get; set; }

public int DepartmentId { get; set; }

public string Name { get; set; }

public Department Department { get; set; }
}

public class Department
{
public int Id { get; set; }

public string Name { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
Expand Down
55 changes: 12 additions & 43 deletions tests/EFCore.QueryRepository.Tests/QueryRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,16 @@ namespace EFCore.QueryRepository.Tests;

public class QueryRepositoryTests
{
private readonly List<Employee> fakeEmployees = new()
{
new()
{
Id = 1,
DepartmentId = 1,
Name = "Mark",
Department = new Department
{
Id = 1,
Name = "IT"
}
},
new()
{
Id = 2,
DepartmentId = 1,
Name = "Merry",
Department = new Department
{
Id = 2,
Name = "HR"
}
}
};

[Fact]
public async Task GetListAsyncTest()
{
// Arrange
var fakeEmployees = new List<Employee>()
{
new() {Id = 1,Name = "Mark"},
new() {Id = 1, Name = "Merry"}
};

Mock<DemoDbContext> mockDemoContext = new();
mockDemoContext.Setup(context => context.Set<Employee>()).ReturnsDbSet(fakeEmployees);

Expand All @@ -54,6 +34,12 @@ public async Task GetListAsyncTest()
public async Task GetListAsync_WithCondition_Test()
{
// Arrange
var fakeEmployees = new List<Employee>()
{
new() {Id = 1,Name = "Mark"},
new() {Id = 2, Name = "Merry"}
};

int expectedCount = fakeEmployees.Where(e => e.Id == 1).Count();
string expectedName = "Mark";

Expand All @@ -70,21 +56,4 @@ public async Task GetListAsync_WithCondition_Test()
Assert.True(employees.Count == expectedCount);
Assert.Equal(expectedName, employees.First().Name);
}

[Fact]
public async Task GetListAsync_WithInclude_Test()
{
// Arrange
Mock<DemoDbContext> mockDemoContext = new();
mockDemoContext.Setup(context => context.Set<Employee>()).ReturnsDbSet(fakeEmployees);

// Act
QueryRepository<DemoDbContext> queryRepository = new(mockDemoContext.Object);
List<Employee> employees = await queryRepository.GetListAsync<Employee>(q => q.Include(e => e.Department));

// Assert
Assert.NotNull(employees);
Assert.NotEmpty(employees);
Assert.True(employees.Count == fakeEmployees.Count);
}
}
62 changes: 62 additions & 0 deletions tests/EFCore.QueryRepository.Tests/QueryableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Collections;
using System.Linq.Expressions;

namespace EFCore.QueryRepository.Tests;

public static class QueryableExtensions
{
public static IQueryable<T> AsAsyncQueryable<T>(this IEnumerable<T> input)
{
return new NotInDbSet<T>(input);
}
}

public class NotInDbSet<T> : IQueryable<T>, IAsyncEnumerable<T>, IEnumerable<T>, IEnumerable
{
private readonly List<T> _innerCollection;
public NotInDbSet(IEnumerable<T> innerCollection)
{
_innerCollection = innerCollection.ToList();
}


public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = new CancellationToken())
{
return new AsyncEnumerator(GetEnumerator());
}

public IEnumerator<T> GetEnumerator()
{
return _innerCollection.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public class AsyncEnumerator : IAsyncEnumerator<T>
{
private readonly IEnumerator<T> _enumerator;
public AsyncEnumerator(IEnumerator<T> enumerator)
{
_enumerator = enumerator;
}

public ValueTask DisposeAsync()
{
return new ValueTask();
}

public ValueTask<bool> MoveNextAsync()
{
return new ValueTask<bool>(_enumerator.MoveNext());
}

public T Current => _enumerator.Current;
}

public Type ElementType => typeof(T);
public Expression Expression => Expression.Empty();
public IQueryProvider Provider => new EnumerableQuery<T>(Expression);
}

0 comments on commit 6d72934

Please sign in to comment.