Skip to content

Commit

Permalink
#8 fixes for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian-Webster committed Feb 7, 2024
1 parent df17af7 commit 37ec6ae
Show file tree
Hide file tree
Showing 20 changed files with 1,036 additions and 50 deletions.
2 changes: 1 addition & 1 deletion DataAccess.Repository.Tests/Tests/Add.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public async Task Should_Add_Expected_Data(Book data)
{
// arrange
var context = GetContext();
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);

// act
var result = await repo.Add(data, Token);
Expand Down
4 changes: 2 additions & 2 deletions DataAccess.Repository.Tests/Tests/Exists.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task Should_Return_True_When_BookExists(string bookIdString)
var bookId = Guid.Parse(bookIdString);

var context = GetContext();
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);

await InsertData(BookTestData.GetBookData(), context);

Expand All @@ -36,7 +36,7 @@ public async Task Should_Return_False_When_BookDoesNotExist(string bookIdString)
var bookId = Guid.Parse(bookIdString);

var context = GetContext();
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);

await InsertData(BookTestData.GetBookData(), context);

Expand Down
6 changes: 3 additions & 3 deletions DataAccess.Repository.Tests/Tests/FirstOrDefaultProjected.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class FirstOrDefaultProjected: RepositoryTestBase<Book>
public async Task Should_Return_Null_When_NoDataExists()
{
// arrange
var repo = GetRepository(Logger, GetContext());
var repo = GetRepository(LoggerFactory, GetContext());

// act
var result = await repo.FirstOrDefaultProjected(p
Expand All @@ -29,7 +29,7 @@ public async Task Should_Return_Null_When_DataNotFound()
{
// arrange
var context = GetContext();
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);

await InsertData(BookTestData.GetBookData(), context);

Expand All @@ -48,7 +48,7 @@ public async Task Should_Get_Expected_Item_When_MatchingItemFound(Guid bookId)
{
// arrange
var context = GetContext();
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);

await InsertData(BookTestData.GetBookData(), context);

Expand Down
8 changes: 4 additions & 4 deletions DataAccess.Repository.Tests/Tests/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class List: RepositoryTestBase<Book>
public async Task Should_Return_EmptyList_When_NoDataExists()
{
// arrange
var repo = GetRepository(Logger, GetContext());
var repo = GetRepository(LoggerFactory, GetContext());

// act
var result = await repo.List(p => p.Name.Contains("Book"), Token);
Expand All @@ -27,7 +27,7 @@ public async Task Should_Return_EmptyList_When_NoMatchingDataFound()
{
// arrange
var context = GetContext();
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);

await InsertData(BookTestData.GetBookData(), context);

Expand All @@ -45,7 +45,7 @@ public async Task Should_Return_Expected_Data_WhenMatchingDataFound(string searc
{
// arrange
var context = GetContext();
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);

await InsertData(BookTestData.GetBookData(), context);

Expand All @@ -71,7 +71,7 @@ public async Task Should_Return_LimitedData_When_TakeParameterIsUsed(int take)
{
// arrange
var context = GetContext();
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);

var testData = BookTestData.GetBookData();
await InsertData(testData, context);
Expand Down
8 changes: 4 additions & 4 deletions DataAccess.Repository.Tests/Tests/ListProjected.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ListProjected : RepositoryTestBase<Book>
public async Task Should_Return_EmptyList_When_NoDataExists()
{
// arrange
var repo = GetRepository(Logger, GetContext());
var repo = GetRepository(LoggerFactory, GetContext());

// act
var result = await repo.ListProjected(p =>
Expand All @@ -33,7 +33,7 @@ public async Task Should_Return_EmptyList_When_NoMatchingDataFound()
{
// arrange
var context = GetContext();
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);

await InsertData(BookTestData.GetBookData(), context);

Expand All @@ -54,7 +54,7 @@ public async Task Should_Return_Expected_Data_WhenMatchingDataFound(string searc
{
// arrange
var context = GetContext();
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);

await InsertData(BookTestData.GetBookData(), context);

Expand Down Expand Up @@ -84,7 +84,7 @@ public async Task Should_Return_LimitedData_When_TakeParameterIsUsed(int take)
{
// arrange
var context = GetContext();
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);

var testData = BookTestData.GetBookData();
await InsertData(testData, context);
Expand Down
6 changes: 3 additions & 3 deletions DataAccess.Repository.Tests/Tests/PageProjected.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class PageProjected: RepositoryTestBase<Book>
public async Task Should_Return_Null_When_NoDataExists()
{
// arrange
var repo = GetRepository(Logger, GetContext());
var repo = GetRepository(LoggerFactory, GetContext());
var pagingRequest = new PagingRequest { PageIndex = 0, PageSize = 10 };

// act
Expand All @@ -35,7 +35,7 @@ public async Task Should_Return_Null_When_NoMatchingDataFound()
{
// arrange
var context = GetContext();
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);
var pagingRequest = new PagingRequest { PageIndex = 0, PageSize = 10 };

await InsertData(BookTestData.GetBookData(), context);
Expand All @@ -57,7 +57,7 @@ public async Task Should_Return_Expected_Data_WhenMatchingDataFound(string searc
{
// arrange
var context = GetContext();
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);


await InsertData(BookTestData.GetPagedBookData(), context);
Expand Down
6 changes: 3 additions & 3 deletions DataAccess.Repository.Tests/Tests/Paged.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Paged : RepositoryTestBase<Book>
public async Task Should_Return_Null_When_NoDataExists()
{
// arrange
var repo = GetRepository(Logger, GetContext());
var repo = GetRepository(LoggerFactory, GetContext());
var pagingRequest = new PagingRequest { PageIndex = 0, PageSize = 10 };

// act
Expand All @@ -33,7 +33,7 @@ public async Task Should_Return_Null_When_NoMatchingDataFound()
{
// arrange
var context = GetContext();
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);
var pagingRequest = new PagingRequest { PageIndex = 0, PageSize = 10 };

await InsertData(BookTestData.GetBookData(), context);
Expand All @@ -54,7 +54,7 @@ public async Task Should_Return_Expected_Data_WhenMatchingDataFound(string searc
{
// arrange
var context = GetContext();
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);


await InsertData(BookTestData.GetPagedBookData(), context);
Expand Down
4 changes: 2 additions & 2 deletions DataAccess.Repository.Tests/Tests/Remove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public async Task Should_Remove_Expected_AttachedItem(Guid bookIdToRemove)
// arrange
var context = GetContext();
var dbSet = GetDbSet(context);
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);

await InsertData(BookTestData.GetBookData(), context);

Expand All @@ -38,7 +38,7 @@ public async Task Should_Return_False_When_TryingToRemove_UnattachedItem(Book bo
// arrange
var context = GetContext();
var dbSet = GetDbSet(context);
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);

await InsertData(BookTestData.GetBookData(), context);

Expand Down
11 changes: 8 additions & 3 deletions DataAccess.Repository.Tests/Tests/RepositoryTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Collections;
using System.Linq.Expressions;
using DataAccess.Repository.Tests.Shared.DatabaseContexts;
using DataAccess.Repository.Tests.Shared.DummyData;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using NSubstitute;
using NUnit.Framework;

namespace DataAccess.Repository.Tests.Tests;
Expand All @@ -12,11 +12,16 @@ public class RepositoryTestBase<TEntity> where TEntity : class
{
protected readonly CancellationToken Token;
protected ILogger Logger;
#pragma warning disable NUnit1032 // An IDisposable field/property should be Disposed in a TearDown method
protected ILoggerFactory LoggerFactory;
#pragma warning restore NUnit1032 // An IDisposable field/property should be Disposed in a TearDown method

[SetUp]
protected void Setup()
{
Logger = NSubstitute.Substitute.For<ILogger>();
LoggerFactory = Substitute.For<ILoggerFactory>();
Logger = Substitute.For<ILogger>();
LoggerFactory.CreateLogger(Arg.Any<string>()).Returns(Logger);
}

public RepositoryTestBase()
Expand All @@ -35,7 +40,7 @@ protected DbSet<TEntity> GetDbSet(DbContext context)
return context.Set<TEntity>();
}

protected IRepository<TEntity> GetRepository(ILogger mockLogger, LibraryDatabaseContext? context = null)
protected IRepository<TEntity> GetRepository(ILoggerFactory mockLogger, LibraryDatabaseContext? context = null)
{
return new UnitOfWork<LibraryDatabaseContext>(context ?? GetContext(), mockLogger).Repository<TEntity>();
}
Expand Down
4 changes: 2 additions & 2 deletions DataAccess.Repository.Tests/Tests/Update.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public async Task Should_Update_ExpectedBook_When_BookAttached(Book bookToUpdate
// arrange
var context = GetContext();
var dbSet = GetDbSet(context);
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);

await InsertData(BookTestData.GetBookData(), context);

Expand Down Expand Up @@ -42,7 +42,7 @@ public async Task Should_NotUpdate_Books_When_BookNotAttached(Book bookToUpdate)
// arrange
var context = GetContext();
var dbSet = GetDbSet(context);
var repo = GetRepository(Logger, context);
var repo = GetRepository(LoggerFactory, context);

await InsertData(BookTestData.GetBookData(), context);

Expand Down
4 changes: 2 additions & 2 deletions DataAccess.Repository/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public class UnitOfWork<TContext> : IDisposable where TContext : DbContext
private bool _disposed;
private Dictionary<Type, object> _repositories;

public UnitOfWork(TContext context, ILogger logger)
public UnitOfWork(TContext context, ILoggerFactory loggerFactory)

Check warning on line 13 in DataAccess.Repository/UnitOfWork.cs

View workflow job for this annotation

GitHub Actions / test_DataAccess

Non-nullable field '_repositories' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 13 in DataAccess.Repository/UnitOfWork.cs

View workflow job for this annotation

GitHub Actions / test_DataAccess

Non-nullable field '_repositories' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 13 in DataAccess.Repository/UnitOfWork.cs

View workflow job for this annotation

GitHub Actions / test_DataAccess

Non-nullable field '_repositories' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 13 in DataAccess.Repository/UnitOfWork.cs

View workflow job for this annotation

GitHub Actions / build_DataAccess

Non-nullable field '_repositories' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 13 in DataAccess.Repository/UnitOfWork.cs

View workflow job for this annotation

GitHub Actions / build_DataAccess

Non-nullable field '_repositories' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
_context = context;
_logger = logger;
_logger = loggerFactory.CreateLogger<UnitOfWork<TContext>>();
}

public IRepository<T> Repository<T>() where T : class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions Example/DataAccess.Example.Data/Models/BookNameOnly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace DataAccess.Example.Data.Models;

public class BookNameOnly
{
public string BookName { get; set; }
}
29 changes: 27 additions & 2 deletions Example/DataAccess.Example.Data/Repositories/BookRepository.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using DataAccess.Example.Data.DatabaseContexts;
using DataAccess.Example.Data.Entities;
using DataAccess.Example.Data.Models;
using DataAccess.Repository;
using DataAccess.Repository.HotChocolate;
using DataAccess.Repository.Models;
using HotChocolate.Resolvers;
using HotChocolate.Types.Pagination;

Expand All @@ -21,9 +23,32 @@ public BookRepository(UnitOfWork<LibraryDatabaseContext> unitOfWork)
return await _bookRepo.FirstOrDefault(b => b.BookId == bookId, token);
}

public async Task<IEnumerable<Book>?> GetAllBooks(CancellationToken token)
public async Task<IEnumerable<Book>?> GetAllBooks(CancellationToken token, int? take = null)
{
return await _bookRepo.List(p => true, token);
return await _bookRepo.List(p => true, token, take);
}

public async Task<PagedResult<Book>> GetPageBooks(PagingRequest request, CancellationToken token)
{
return await _bookRepo.Paged(b => true, request, token);
}

public async Task<BookNameOnly?> GetBookNameOnlyById(Guid bookId, CancellationToken token)
{
return await _bookRepo.FirstOrDefaultProjected(b => b.BookId == bookId,
b => new BookNameOnly { BookName = b.Name }, token);
}

public async Task<IEnumerable<BookNameOnly>?> GetAllBookNamesOnly(CancellationToken token, int? take = null)
{
return await _bookRepo.ListProjected(b => true,
b => new BookNameOnly { BookName = b.Name }, token, take);
}

public async Task<PagedResult<BookNameOnly>> GetPagedBookNamesOnly(PagingRequest request, CancellationToken token)
{
return await _bookRepo.PagedProjected(b => true,
b => new BookNameOnly { BookName = b.Name }, request, token);
}

public async Task<bool> AddBook(Book bookToAdd, CancellationToken token)
Expand Down
12 changes: 11 additions & 1 deletion Example/DataAccess.Example.Data/Repositories/IBookRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using DataAccess.Example.Data.Entities;
using DataAccess.Example.Data.Models;
using DataAccess.Repository.Models;
using HotChocolate.Resolvers;
using HotChocolate.Types.Pagination;

Expand All @@ -8,7 +10,15 @@ public interface IBookRepository
{
Task<Book?> GetBookById(Guid bookId, CancellationToken token);

Task<IEnumerable<Book>?> GetAllBooks(CancellationToken token);
Task<IEnumerable<Book>?> GetAllBooks(CancellationToken token, int? take = null);

Task<PagedResult<Book>> GetPageBooks(PagingRequest request, CancellationToken token);

Task<BookNameOnly?> GetBookNameOnlyById(Guid bookId, CancellationToken token);

Task<IEnumerable<BookNameOnly>?> GetAllBookNamesOnly(CancellationToken token, int? take = null);

Task<PagedResult<BookNameOnly>> GetPagedBookNamesOnly(PagingRequest request, CancellationToken token);

Task<bool> AddBook(Book bookToAdd, CancellationToken token);

Expand Down
8 changes: 8 additions & 0 deletions Example/DataAccess.Example.Web/Controllers/BaseController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Microsoft.AspNetCore.Mvc;

namespace DataAccess.Example.Web.Controllers;

public class BaseController: ControllerBase
{
protected CancellationToken Token => HttpContext.RequestAborted;
}
Loading

0 comments on commit 37ec6ae

Please sign in to comment.