Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
refactoring
  • Loading branch information
TheTrigger committed Nov 14, 2024
1 parent 36b1fd0 commit fe49e85
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 186 deletions.
37 changes: 37 additions & 0 deletions F2.Repository.Demo/LibraryDbScope.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using F2.Repository.Abstracts;
using F2.Repository.Demo.Models;
using F2.Repository.Demo.Repositories;
using Microsoft.EntityFrameworkCore.ChangeTracking;

namespace F2.Repository.Demo;

/// <summary>
/// Repositories scope
/// </summary>
public class LibraryDbScope : DbContextScope<LibraryContext>
{
public LibraryDbScope(LibraryContext context, BookRepository bookRepository, AuthorRepository authorRepository) : base(context)
{
BookRepository = bookRepository;
AuthorRepository = authorRepository;

context.ChangeTracker.Tracked += ChangeTracker_Tracked;
}

private void ChangeTracker_StateChanged(object sender, EntityStateChangedEventArgs e)
{
var student = e.Entry.Entity;
// e.Entry.State
}

private void ChangeTracker_Tracked(object sender, EntityTrackedEventArgs e)
{
// create dto: state, context, entity ?
var entity = e.Entry.Entity;
//throw new NotImplementedException();
}


public BookRepository BookRepository { get; }
public AuthorRepository AuthorRepository { get; }
}
3 changes: 1 addition & 2 deletions F2.Repository.Demo/Repositories/BookRepository.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using AutoMapper;
using F2.Repository.Abstracts;
using F2.Repository.Abstracts;
using F2.Repository.Demo.Models;

namespace F2.Repository.Demo.Repositories
Expand Down
38 changes: 0 additions & 38 deletions F2.Repository.Demo/Repositories/LibraryDbScope.cs

This file was deleted.

1 change: 0 additions & 1 deletion F2.Repository.Demo/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.Extensions.DependencyInjection;
using F2.Repository.Demo.Mapper;
using F2.Repository.Demo.Models;
using F2.Repository.Demo.Repositories;
using F2.Repository.Extensions;

namespace F2.Repository.Demo;
Expand Down
3 changes: 3 additions & 0 deletions F2.Repository.Demo/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"Demo": "Username=Library;Password=develop;Server=localhost;Port=1440;Database=Library;"
}
}
5 changes: 1 addition & 4 deletions F2.Repository.Demo/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
"Microsoft.EntityFrameworkCore": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"Demo": "Username=Library;Password=develop;Server=localhost;Port=1440;Database=Library;"
}
"AllowedHosts": "*"
}
9 changes: 4 additions & 5 deletions F2.Repository.Tests/DatabaseConnectionTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using F2.Repository.Demo;
using F2.Repository.Demo.Models;
using F2.Repository.Demo.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -21,9 +21,8 @@ public DatabaseConnectionTest(TestContainerApplicationFactory testFixure)
{
_testFixure = testFixure;
_scope = _testFixure.Server.Services.CreateScope();

_libraryScope = _scope.ServiceProvider.GetRequiredService<LibraryDbScope>();
_context = _scope.ServiceProvider.GetRequiredService<LibraryContext>();
_libraryScope = _scope.ServiceProvider.GetRequiredService<LibraryDbScope>();
}

[Fact]
Expand Down
31 changes: 19 additions & 12 deletions F2.Repository.Tests/TestContainerApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,27 @@ public TestContainerApplicationFactory()

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureTestServices(services =>
{
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<LibraryContext>));
services?.Remove(descriptor);
services.AddDbContext<LibraryContext>(options =>
base.ConfigureWebHost(builder);
builder
.ConfigureTestServices(services =>
{
options.UseNpgsql(_applicationDatabase.GetConnectionString(),
npgsqlOptions => npgsqlOptions.MigrationsAssembly(typeof(LibraryContext).Assembly.FullName))
.EnableSensitiveDataLogging(true)
.EnableDetailedErrors()
;
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<LibraryContext>));
services?.Remove(descriptor);
services.AddDbContext<LibraryContext>(options =>
{
// connection string from container
options.UseNpgsql(_applicationDatabase.GetConnectionString(),
npgsqlOptions => npgsqlOptions.MigrationsAssembly(typeof(LibraryContext).Assembly.FullName))
.EnableSensitiveDataLogging(true)
.EnableDetailedErrors()
;

}, ServiceLifetime.Scoped);
});
}, ServiceLifetime.Scoped);
})
.ConfigureAppConfiguration((hostingContext, config) =>
{
})
;
}

Task IAsyncLifetime.InitializeAsync()
Expand Down
150 changes: 76 additions & 74 deletions F2.Repository.Tests/TestControllerApi.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using F2.Repository.Demo;
using F2.Repository.Demo.Models;
using F2.Repository.Demo.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;
using Xunit;
Expand All @@ -9,77 +10,78 @@ namespace F2.Repository.Tests
{
public class TestControllerApi : IClassFixture<TestContainerApplicationFactory>
{
private readonly TestContainerApplicationFactory _serverFixture;
private readonly LibraryDbScope _libraryScope;

public TestControllerApi(TestContainerApplicationFactory serverFixture)
{
_serverFixture = serverFixture;
_libraryScope = _serverFixture.GetService<LibraryDbScope>();
}

[Fact]
public async Task CreateExampleDataIfNotExists()
{
var a1 = new Author { Name = "William Shakespeare" };
var a2 = new Author { Name = "Oibi.dev" };

_libraryScope.AuthorRepository.Create(a1);
_libraryScope.AuthorRepository.Create(a2);

var affectedRows = await _libraryScope.SaveChangesAsync();
Assert.NotEqual(Guid.Empty, a1.Id);
Assert.NotEqual(default, affectedRows);

var b1 = new Book { Title = "Hamlet", Isbn = "1234567890123" };
var b2 = new Book { Title = "King Lear", Isbn = "0987654321045" };
var b3 = new Book { Id = Guid.NewGuid(), Title = "Random Othello w/ no authors" };

_libraryScope.BookRepository.Create(b1);
_libraryScope.BookRepository.Create(b2);
_libraryScope.BookRepository.Create(b3);

a1.Books = [b1, b2];
a2.Books = [b2, b3];

affectedRows = await _libraryScope.SaveChangesAsync();
Assert.NotEqual(default, affectedRows);

var results = await _libraryScope.AuthorRepository
.Include(i => i.Books)
.ToListAsync();

Assert.NotEmpty(results);
foreach (var author in results)
{
Assert.NotEmpty(author.Name);
Assert.NotEmpty(author.Books);
}
}

[Fact]
public async Task ToListAsync()
{
var b1 = new Book { Title = "Hamlet", Isbn = "1234567890123" };
_libraryScope.BookRepository.Create(b1);
var affectedRows = await _libraryScope.SaveChangesAsync();
Assert.Equal(1, affectedRows);

var results = await _libraryScope.BookRepository.ToListAsync();
Assert.NotNull(results);
}

[Fact]
public async Task CanCreateAndDelete()
{
var a1 = new Author { Name = "William Shakespeare" };
_libraryScope.AuthorRepository.Create(a1);
await _libraryScope.SaveChangesAsync(default);

_libraryScope.AuthorRepository.RemoveRange(a1);
await _libraryScope.SaveChangesAsync(default);

Assert.NotEqual(Guid.Empty, a1.Id);
}
}
private readonly TestContainerApplicationFactory _serverFixture;
private readonly IServiceScope _scope;
private readonly LibraryDbScope _libraryScope;
public TestControllerApi(TestContainerApplicationFactory serverFixture)
{
_serverFixture = serverFixture;
_scope = _serverFixture.Server.Services.CreateScope();
_libraryScope = _scope.ServiceProvider.GetRequiredService<LibraryDbScope>();
}

[Fact]
public async Task CreateExampleDataIfNotExists()
{
var a1 = new Author { Name = "William Shakespeare" };
var a2 = new Author { Name = "Oibi.dev" };

_libraryScope.AuthorRepository.Create(a1);
_libraryScope.AuthorRepository.Create(a2);

var affectedRows = await _libraryScope.SaveChangesAsync();
Assert.NotEqual(Guid.Empty, a1.Id);
Assert.NotEqual(default, affectedRows);

var b1 = new Book { Title = "Hamlet", Isbn = "1234567890123" };
var b2 = new Book { Title = "King Lear", Isbn = "0987654321045" };
var b3 = new Book { Id = Guid.NewGuid(), Title = "Random Othello w/ no authors" };

_libraryScope.BookRepository.Create(b1);
_libraryScope.BookRepository.Create(b2);
_libraryScope.BookRepository.Create(b3);

a1.Books = [b1, b2];
a2.Books = [b2, b3];

affectedRows = await _libraryScope.SaveChangesAsync();
Assert.NotEqual(default, affectedRows);

var results = await _libraryScope.AuthorRepository
.Include(i => i.Books)
.ToListAsync();

Assert.NotEmpty(results);
foreach (var author in results)
{
Assert.NotEmpty(author.Name);
Assert.NotEmpty(author.Books);
}
}

[Fact]
public async Task ToListAsync()
{
var b1 = new Book { Title = "Hamlet", Isbn = "1234567890123" };
_libraryScope.BookRepository.Create(b1);
var affectedRows = await _libraryScope.SaveChangesAsync();
Assert.Equal(1, affectedRows);

var results = await _libraryScope.BookRepository.ToListAsync();
Assert.NotNull(results);
}

[Fact]
public async Task CanCreateAndDelete()
{
var a1 = new Author { Name = "William Shakespeare" };
_libraryScope.AuthorRepository.Create(a1);
await _libraryScope.SaveChangesAsync(default);

_libraryScope.AuthorRepository.RemoveRange(a1);
await _libraryScope.SaveChangesAsync(default);

Assert.NotEqual(Guid.Empty, a1.Id);
}
}
}
5 changes: 1 addition & 4 deletions F2.Repository.Tests/appsettings.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
"Microsoft.EntityFrameworkCore": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"Demo": "Username=Library;Password=develop;Server=localhost;Port=1440;Database=Library;"
}
"AllowedHosts": "*"
}
Loading

0 comments on commit fe49e85

Please sign in to comment.