-
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.
Merge pull request #175 from DFE-Digital/academies-db-connection/trus…
…t-provider Set `TrustProvider` to use academies db instead of academies api
- Loading branch information
Showing
20 changed files
with
290 additions
and
344 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
DfE.FindInformationAcademiesTrusts.Data.AcademiesDb/TrustProvider.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,37 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace DfE.FindInformationAcademiesTrusts.Data.AcademiesDb; | ||
|
||
public class TrustProvider : ITrustProvider | ||
{ | ||
private readonly IAcademiesDbContext _academiesDbContext; | ||
|
||
[ExcludeFromCodeCoverage] | ||
public TrustProvider(AcademiesDbContext academiesDbContext) : this((IAcademiesDbContext)academiesDbContext) | ||
{ | ||
} | ||
|
||
public TrustProvider(IAcademiesDbContext academiesDbContext) | ||
{ | ||
_academiesDbContext = academiesDbContext; | ||
} | ||
|
||
public async Task<Trust?> GetTrustByUidAsync(string uid) | ||
{ | ||
Trust? trust = null; | ||
|
||
var group = await _academiesDbContext.Groups.SingleOrDefaultAsync(g => g.GroupUid == uid); | ||
if (group is not null) | ||
{ | ||
trust = new Trust( | ||
group.GroupUid!, | ||
group.GroupName ?? string.Empty, | ||
group.Ukprn, | ||
group.GroupType ?? string.Empty | ||
); | ||
} | ||
|
||
return trust; | ||
} | ||
} |
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,6 @@ | ||
namespace DfE.FindInformationAcademiesTrusts.Data; | ||
|
||
public interface ITrustProvider | ||
{ | ||
public Task<Trust?> GetTrustByUidAsync(string uid); | ||
} |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
namespace DfE.FindInformationAcademiesTrusts.Data; | ||
|
||
public record Trust(string Name, string? Ukprn, string Type); | ||
public record Trust(string Uid, string Name, string? Ukprn, string Type); |
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
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 was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
...FindInformationAcademiesTrusts.Data.AcademiesDb.UnitTests/Mocks/MockAcademiesDbContext.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,23 @@ | ||
using DfE.FindInformationAcademiesTrusts.Data.AcademiesDb.Models; | ||
|
||
namespace DfE.FindInformationAcademiesTrusts.Data.AcademiesDb.UnitTests.Mocks; | ||
|
||
public class MockAcademiesDbContext : Mock<IAcademiesDbContext> | ||
{ | ||
public List<Group> SetupMockDbContextGroups(int numMatches) | ||
{ | ||
var groups = new List<Group>(); | ||
for (var i = 0; i < numMatches; i++) | ||
{ | ||
groups.Add(new Group | ||
{ | ||
GroupName = $"trust {i}", GroupUid = $"{i}", GroupId = $"TR0{i}", GroupType = "Multi-academy trust" | ||
}); | ||
} | ||
|
||
Setup(academiesDbContext => academiesDbContext.Groups) | ||
.Returns(new MockDbSet<Group>(groups).Object); | ||
|
||
return groups; | ||
} | ||
} |
30 changes: 0 additions & 30 deletions
30
tests/DfE.FindInformationAcademiesTrusts.Data.AcademiesDb.UnitTests/Mocks/MockDbContext.cs
This file was deleted.
Oops, something went wrong.
114 changes: 114 additions & 0 deletions
114
tests/DfE.FindInformationAcademiesTrusts.Data.AcademiesDb.UnitTests/Mocks/MockDbSet.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,114 @@ | ||
using System.Linq.Expressions; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
|
||
namespace DfE.FindInformationAcademiesTrusts.Data.AcademiesDb.UnitTests.Mocks; | ||
|
||
/// <summary> | ||
/// Use this to create a mock DbSet from a collection of items. The mock DbSet points to the collection so changes to | ||
/// the original collection will be reflected in the mock DbSet and vice versa | ||
/// Adapted from https://jason-ge.medium.com/mock-async-data-repository-in-asp-net-core-3-1-634cb19a3013 | ||
/// </summary> | ||
/// <typeparam name="TEntity"></typeparam> | ||
public class MockDbSet<TEntity> : Mock<DbSet<TEntity>> where TEntity : class | ||
{ | ||
public MockDbSet(IEnumerable<TEntity> items) | ||
{ | ||
var itemsAsQueryable = items.AsQueryable(); | ||
|
||
As<IAsyncEnumerable<TEntity>>() | ||
.Setup(x => x.GetAsyncEnumerator(default)) | ||
.Returns(new TestAsyncEnumerator<TEntity>(itemsAsQueryable.GetEnumerator())); | ||
As<IQueryable<TEntity>>() | ||
.Setup(m => m.Provider) | ||
.Returns(new TestAsyncQueryProvider<TEntity>(itemsAsQueryable.Provider)); | ||
As<IQueryable<TEntity>>() | ||
.Setup(m => m.Expression).Returns(itemsAsQueryable.Expression); | ||
As<IQueryable<TEntity>>() | ||
.Setup(m => m.ElementType).Returns(itemsAsQueryable.ElementType); | ||
As<IQueryable<TEntity>>() | ||
.Setup(m => m.GetEnumerator()).Returns(itemsAsQueryable.GetEnumerator()); | ||
} | ||
|
||
public sealed override Mock<TInterface> As<TInterface>() | ||
{ | ||
return base.As<TInterface>(); | ||
} | ||
|
||
private class TestAsyncEnumerator<T> : IAsyncEnumerator<T> | ||
{ | ||
private readonly IEnumerator<T> _enumerator; | ||
|
||
public TestAsyncEnumerator(IEnumerator<T> enumerator) | ||
{ | ||
_enumerator = enumerator; | ||
} | ||
|
||
public T Current => _enumerator.Current; | ||
|
||
public ValueTask DisposeAsync() | ||
{ | ||
return new ValueTask(Task.Run(() => _enumerator.Dispose())); | ||
} | ||
|
||
public ValueTask<bool> MoveNextAsync() | ||
{ | ||
return new ValueTask<bool>(_enumerator.MoveNext()); | ||
} | ||
} | ||
|
||
private class TestAsyncEnumerable<T> : EnumerableQuery<T>, IAsyncEnumerable<T>, IQueryable<T> | ||
{ | ||
public TestAsyncEnumerable(Expression expression) | ||
: base(expression) | ||
{ | ||
} | ||
|
||
IQueryProvider IQueryable.Provider => new TestAsyncQueryProvider<T>(this); | ||
|
||
public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken token) | ||
{ | ||
return new TestAsyncEnumerator<T>(this.AsEnumerable().GetEnumerator()); | ||
} | ||
} | ||
|
||
private class TestAsyncQueryProvider<T> : IAsyncQueryProvider | ||
{ | ||
private readonly IQueryProvider _innerQueryProvider; | ||
|
||
internal TestAsyncQueryProvider(IQueryProvider innerQueryProvider) | ||
{ | ||
_innerQueryProvider = innerQueryProvider; | ||
} | ||
|
||
public IQueryable CreateQuery(Expression expression) | ||
{ | ||
return new TestAsyncEnumerable<T>(expression); | ||
} | ||
|
||
public IQueryable<TElement> CreateQuery<TElement>(Expression expression) | ||
{ | ||
return new TestAsyncEnumerable<TElement>(expression); | ||
} | ||
|
||
public object? Execute(Expression expression) | ||
{ | ||
return _innerQueryProvider.Execute(expression); | ||
} | ||
|
||
public TResult Execute<TResult>(Expression expression) | ||
{ | ||
return _innerQueryProvider.Execute<TResult>(expression); | ||
} | ||
|
||
public TResult ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken = new()) | ||
{ | ||
var expectedResultType = typeof(TResult).GetGenericArguments()[0]; | ||
var executionResult = Execute(expression); | ||
|
||
return (TResult)(typeof(Task).GetMethod(nameof(Task.FromResult))! | ||
.MakeGenericMethod(expectedResultType) | ||
.Invoke(null, new[] { executionResult }) ?? throw new Exception()); | ||
} | ||
} | ||
} |
Oops, something went wrong.