From d5f0c8353a61906632c76694dda1955fe12e08fd Mon Sep 17 00:00:00 2001 From: Gabriel Sales Date: Sat, 21 Dec 2024 00:26:51 -0300 Subject: [PATCH 1/2] Create integrations tests --- SSync.LiteDB.sln | 9 +- .../Data/TestDbContext.cs | 57 +++++ .../Model/Finance.cs | 10 + .../Model/User.cs | 9 + src/SSync.Server.LiteDB.PlayGround/Program.cs | 216 ++---------------- .../SSync.Server.LiteDB.PlayGround.csproj | 12 +- .../Sync/Dto/FinanceSync.cs | 12 + .../Sync/Dto/UserSync.cs | 12 + .../Pull/PullFinanceRequestHandler.cs | 29 +++ .../Handlers/Pull/PullUserRequestHandler.cs | 29 +++ .../Handlers/Push/PushUserRequestHandler.cs | 61 +++++ .../Sync/PlayParamenter.cs | 8 + .../MockServerApp/IntegrationFixture.cs | 106 +++++++++ .../MockServerApp/Sync/Store_Test.cs | 73 ++++++ ...nc.Server.LitebDB.Integration.Tests.csproj | 41 ++++ 15 files changed, 480 insertions(+), 204 deletions(-) create mode 100644 src/SSync.Server.LiteDB.PlayGround/Data/TestDbContext.cs create mode 100644 src/SSync.Server.LiteDB.PlayGround/Model/Finance.cs create mode 100644 src/SSync.Server.LiteDB.PlayGround/Model/User.cs create mode 100644 src/SSync.Server.LiteDB.PlayGround/Sync/Dto/FinanceSync.cs create mode 100644 src/SSync.Server.LiteDB.PlayGround/Sync/Dto/UserSync.cs create mode 100644 src/SSync.Server.LiteDB.PlayGround/Sync/Handlers/Pull/PullFinanceRequestHandler.cs create mode 100644 src/SSync.Server.LiteDB.PlayGround/Sync/Handlers/Pull/PullUserRequestHandler.cs create mode 100644 src/SSync.Server.LiteDB.PlayGround/Sync/Handlers/Push/PushUserRequestHandler.cs create mode 100644 src/SSync.Server.LiteDB.PlayGround/Sync/PlayParamenter.cs create mode 100644 test/SSync.Server.LitebDB.Integration.Tests/MockServerApp/IntegrationFixture.cs create mode 100644 test/SSync.Server.LitebDB.Integration.Tests/MockServerApp/Sync/Store_Test.cs create mode 100644 test/SSync.Server.LitebDB.Integration.Tests/SSync.Server.LitebDB.Integration.Tests.csproj diff --git a/SSync.LiteDB.sln b/SSync.LiteDB.sln index 4bf41f9..dadd13c 100644 --- a/SSync.LiteDB.sln +++ b/SSync.LiteDB.sln @@ -21,12 +21,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SSync.Server.LitebDB.Tests" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{36F96630-08DF-4386-AADB-C8E121988D5B}" ProjectSection(SolutionItems) = preProject - build_and_test.yaml = build_and_test.yaml .github\workflows\publish.yaml = .github\workflows\publish.yaml readme-ptBR.md = readme-ptBR.md readme.md = readme.md + .github\workflows\build_and_test.yaml = .github\workflows\build_and_test.yaml EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SSync.Server.LitebDB.Integration.Tests", "test\SSync.Server.LitebDB.Integration.Tests\SSync.Server.LitebDB.Integration.Tests.csproj", "{1DF82900-C83A-4813-969C-EFB4E50E4476}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -57,6 +59,10 @@ Global {41717DBF-139C-4A74-8BA4-E9B2B289F529}.Debug|Any CPU.Build.0 = Debug|Any CPU {41717DBF-139C-4A74-8BA4-E9B2B289F529}.Release|Any CPU.ActiveCfg = Release|Any CPU {41717DBF-139C-4A74-8BA4-E9B2B289F529}.Release|Any CPU.Build.0 = Release|Any CPU + {1DF82900-C83A-4813-969C-EFB4E50E4476}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1DF82900-C83A-4813-969C-EFB4E50E4476}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1DF82900-C83A-4813-969C-EFB4E50E4476}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1DF82900-C83A-4813-969C-EFB4E50E4476}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -68,6 +74,7 @@ Global {3B922C05-29AC-46F4-AB13-7D68418DA126} = {EFFD198B-C248-4AE0-9085-2EDFAEA57294} {47F5CD68-81DD-453D-A5A3-F42FDB844775} = {5F8FC591-4435-4387-838B-D8EC7768C42D} {41717DBF-139C-4A74-8BA4-E9B2B289F529} = {5F8FC591-4435-4387-838B-D8EC7768C42D} + {1DF82900-C83A-4813-969C-EFB4E50E4476} = {5F8FC591-4435-4387-838B-D8EC7768C42D} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {F1AE824B-4065-4342-A7CD-35122F2B99B4} diff --git a/src/SSync.Server.LiteDB.PlayGround/Data/TestDbContext.cs b/src/SSync.Server.LiteDB.PlayGround/Data/TestDbContext.cs new file mode 100644 index 0000000..33f13ac --- /dev/null +++ b/src/SSync.Server.LiteDB.PlayGround/Data/TestDbContext.cs @@ -0,0 +1,57 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Storage; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SSync.Server.LitebDB.Abstractions; +using SSync.Server.LiteDB.PlayGround.Model; + +namespace SSync.Server.LiteDB.PlayGround.Data; + +public class TestDbContext : DbContext, ISSyncDbContextTransaction +{ + private IDbContextTransaction? transaction; + private readonly IConfiguration _configuration; + + + public TestDbContext(DbContextOptions options, IConfiguration configuration) + : base(options) + { + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); + + _configuration = configuration; + } + + public DbSet User => Set(); + public DbSet Finances => Set(); + + public async Task BeginTransactionSyncAsync() + => transaction = await Database.BeginTransactionAsync(); + + public async Task CommitSyncAsync() + => await Database.CommitTransactionAsync(); + + public Task CommitTransactionSyncAsync() + { + ArgumentNullException.ThrowIfNull(transaction); + + return transaction.CommitAsync(); + } + + public Task RollbackTransactionSyncAsync() + { + ArgumentNullException.ThrowIfNull(transaction); + return transaction.RollbackAsync(); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .Property(u => u.Id).HasConversion(new GuidToStringConverter()); + + modelBuilder.Entity() + .Property(u => u.Id).HasConversion(new GuidToStringConverter()); + + base.OnModelCreating(modelBuilder); + } + +} \ No newline at end of file diff --git a/src/SSync.Server.LiteDB.PlayGround/Model/Finance.cs b/src/SSync.Server.LiteDB.PlayGround/Model/Finance.cs new file mode 100644 index 0000000..c6eb71a --- /dev/null +++ b/src/SSync.Server.LiteDB.PlayGround/Model/Finance.cs @@ -0,0 +1,10 @@ +using SSync.Server.LitebDB.Abstractions; + +namespace SSync.Server.LiteDB.PlayGround.Model; + +public class Finance : ISSyncEntityRoot +{ + public double Price { get; set; } + + +} \ No newline at end of file diff --git a/src/SSync.Server.LiteDB.PlayGround/Model/User.cs b/src/SSync.Server.LiteDB.PlayGround/Model/User.cs new file mode 100644 index 0000000..c3ae45d --- /dev/null +++ b/src/SSync.Server.LiteDB.PlayGround/Model/User.cs @@ -0,0 +1,9 @@ +using SSync.Server.LitebDB.Abstractions; + +namespace SSync.Server.LiteDB.PlayGround.Model; + +public class User : ISSyncEntityRoot +{ + public string? Name { get; set; } + +} \ No newline at end of file diff --git a/src/SSync.Server.LiteDB.PlayGround/Program.cs b/src/SSync.Server.LiteDB.PlayGround/Program.cs index 01bedd6..d148bbd 100644 --- a/src/SSync.Server.LiteDB.PlayGround/Program.cs +++ b/src/SSync.Server.LiteDB.PlayGround/Program.cs @@ -1,13 +1,13 @@ +using System.Text.Json.Nodes; using Microsoft.AspNetCore.Mvc; -using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Storage; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using SSync.Server.LitebDB.Abstractions; -using SSync.Server.LitebDB.Abstractions.Sync; using SSync.Server.LitebDB.Engine; using SSync.Server.LitebDB.Enums; -using System.Text.Json.Nodes; +using SSync.Server.LiteDB.PlayGround.Data; +using SSync.Server.LiteDB.PlayGround.Model; +using SSync.Server.LiteDB.PlayGround.Sync; +using SSync.Server.LiteDB.PlayGround.Sync.Dto; var builder = WebApplication.CreateBuilder(args); @@ -25,16 +25,12 @@ .ThenBy("Finance"); }); -var connectionString = builder.Configuration.GetConnectionString("SqliteConnectionString") - ?? "Data Source=Examples\\MyRemote.db"; -builder.Services.AddSqlite(connectionString); - -builder.Services.AddScoped(_ => new SqliteConnection(connectionString)); +builder.Services.AddDbContext(optionsBuilder => optionsBuilder.UseNpgsql("Host=localhost;Port=5432;Pooling=true;Database=poc_ssync;User Id=postgres;Password=postgres;")); var app = builder.Build(); -await CreateDBIfNotExistAsync(app.Services, app.Logger); +await CreateDbIfNotExistAsync(app.Services, app.Logger); app.UseHttpsRedirection(); @@ -116,7 +112,7 @@ { var now = DateTime.UtcNow; - var user = cxt.User.SingleOrDefault(u => u.Id == userId); + var user = cxt.User.Find(userId); if (user is null) { @@ -125,7 +121,7 @@ user.Name = $"Update {new Random().Next()}"; - user.UpdatedAt = DateTime.UtcNow; + user.SetUpdatedAt(DateTime.UtcNow); cxt.User.Update(user); @@ -138,7 +134,7 @@ { var now = DateTime.UtcNow; - var finance = cxt.Finances.SingleOrDefault(u => u.Id == id); + var finance = cxt.Finances.Find(id); if (finance is null) { @@ -147,7 +143,7 @@ finance.Price = new Random().Next(); - finance.UpdatedAt = DateTime.UtcNow; + finance.SetUpdatedAt(DateTime.UtcNow); cxt.Finances.Update(finance); @@ -160,14 +156,14 @@ { var now = DateTime.UtcNow; - var finance = cxt.Finances.SingleOrDefault(f => f.Id == id); + var finance = cxt.Finances.Find(id); if (finance is null) { return Results.NotFound(); } - finance.DeletedAt = DateTime.UtcNow; + finance.SetDeletedAt(DateTime.UtcNow); cxt.Finances.Update(finance); @@ -180,14 +176,14 @@ { var now = DateTime.UtcNow; - var user = cxt.User.SingleOrDefault(u => u.Id == id); + var user = cxt.User.Find(id); if (user is null) { return Results.NotFound(); } - user.DeletedAt = DateTime.UtcNow; + user.SetDeletedAt(DateTime.UtcNow); cxt.User.Update(user); @@ -198,188 +194,12 @@ app.Run(); -async Task CreateDBIfNotExistAsync(IServiceProvider services, ILogger logger) +async Task CreateDbIfNotExistAsync(IServiceProvider services, ILogger logger) { - using var db = services.CreateScope().ServiceProvider.GetRequiredService(); + await using var db = services.CreateScope().ServiceProvider.GetRequiredService(); await db.Database.EnsureCreatedAsync(); await db.Database.MigrateAsync(); } -internal class TestDbContext : DbContext, ISSyncDbContextTransaction -{ - private IDbContextTransaction? transaction; - - public TestDbContext(DbContextOptions options) - : base(options) { } - - public DbSet User => Set(); - public DbSet Finances => Set(); - - public async Task BeginTransactionSyncAsync() - => transaction = await Database.BeginTransactionAsync(); - - public async Task CommitSyncAsync() - => await Database.CommitTransactionAsync(); - - public Task CommitTransactionSyncAsync() - { - ArgumentNullException.ThrowIfNull(transaction); - - return transaction.CommitAsync(); - } - - public Task RollbackTransactionSyncAsync() - { - ArgumentNullException.ThrowIfNull(transaction); - return transaction.RollbackAsync(); - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder.Entity() - .Property(u => u.Id).HasConversion(new GuidToStringConverter()); - - modelBuilder.Entity() - .Property(u => u.Id).HasConversion(new GuidToStringConverter()); - - base.OnModelCreating(modelBuilder); - } -} - -internal class User : ISSyncEntityRoot -{ - public string? Name { get; set; } - -} - -internal class Finance : ISSyncEntityRoot -{ - public double Price { get; set; } - - -} - -internal class PlayParamenter : SSyncParameter -{ - public int Time { get; set; } = new Random().Next(100); -} - -internal class UserSync : ISchema -{ - public UserSync(Guid id) : base(id) - { - } - - public string? Name { get; set; } -} - -internal class FinanceSync : ISchema -{ - public FinanceSync(Guid id) : base(id) - { - } - - public double Price { get; set; } -} - -internal class PullUserRequestHandler : ISSyncPullRequest -{ - private readonly TestDbContext _ctx; - - public PullUserRequestHandler(TestDbContext ctx) - { - _ctx = ctx; - } - - public async Task> QueryAsync(PlayParamenter parameter) - { - var users = await _ctx.User.Select(u => new UserSync(u.Id) - { - Name = u.Name, - CreatedAt = u.CreatedAt, - DeletedAt = u.DeletedAt, - UpdatedAt = u.UpdatedAt - }).ToListAsync(); - - return users; - } -} - -internal class PullFinanceRequestHandler : ISSyncPullRequest -{ - private readonly TestDbContext _ctx; - - public PullFinanceRequestHandler(TestDbContext ctx) - { - _ctx = ctx; - } - - public async Task> QueryAsync(PlayParamenter parameter) - { - var finances = await _ctx.Finances.Select(u => new FinanceSync(u.Id) - { - Price = new Random().Next(100), - CreatedAt = u.CreatedAt, - DeletedAt = u.DeletedAt, - UpdatedAt = u.UpdatedAt - }).ToListAsync(); - - return finances; - } -} - -internal class PushUserRequestHandler : ISSyncPushRequest -{ - private readonly TestDbContext _db; - - public PushUserRequestHandler(TestDbContext db) => _db = db; - - public async Task FindByIdAsync(Guid id) - { - return await _db.User.Where(u => u.Id == id) - .Select(us => new UserSync(id) - { - Name = us.Name, - CreatedAt = us.CreatedAt, - DeletedAt = us.DeletedAt, - UpdatedAt = us.UpdatedAt - }) - .FirstOrDefaultAsync(); - } - - public async Task CreateAsync(UserSync schema) - { - var us = new User() - { - Id = schema.Id, - Name = schema.Name, - CreatedAt = schema.CreatedAt, - DeletedAt = schema.DeletedAt, - UpdatedAt = schema.UpdatedAt - }; - - await _db.User.AddAsync(us); - - return await _db.SaveChangesAsync() > 0; - } - - public async Task UpdateAsync(UserSync schema) - { - var us = await _db.User.FindAsync(schema.Id); - - us!.UpdatedAt = DateTime.Now; - - us.Name = schema.Name; - - return await _db.SaveChangesAsync() > 0; - } - - public async Task DeleteAsync(UserSync schema) - { - var us = await _db.User.FindAsync(schema.Id); - us!.DeletedAt = DateTime.Now; - - return await _db.SaveChangesAsync() > 0; - } -} \ No newline at end of file +public partial class Program{} \ No newline at end of file diff --git a/src/SSync.Server.LiteDB.PlayGround/SSync.Server.LiteDB.PlayGround.csproj b/src/SSync.Server.LiteDB.PlayGround/SSync.Server.LiteDB.PlayGround.csproj index c55e5b7..2a775e3 100644 --- a/src/SSync.Server.LiteDB.PlayGround/SSync.Server.LiteDB.PlayGround.csproj +++ b/src/SSync.Server.LiteDB.PlayGround/SSync.Server.LiteDB.PlayGround.csproj @@ -9,15 +9,17 @@ - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + - - - - diff --git a/src/SSync.Server.LiteDB.PlayGround/Sync/Dto/FinanceSync.cs b/src/SSync.Server.LiteDB.PlayGround/Sync/Dto/FinanceSync.cs new file mode 100644 index 0000000..1f0cf4d --- /dev/null +++ b/src/SSync.Server.LiteDB.PlayGround/Sync/Dto/FinanceSync.cs @@ -0,0 +1,12 @@ +using SSync.Server.LitebDB.Abstractions; + +namespace SSync.Server.LiteDB.PlayGround.Sync.Dto; + +public class FinanceSync : ISchema +{ + public FinanceSync(Guid id) : base(id) + { + } + + public double Price { get; set; } +} \ No newline at end of file diff --git a/src/SSync.Server.LiteDB.PlayGround/Sync/Dto/UserSync.cs b/src/SSync.Server.LiteDB.PlayGround/Sync/Dto/UserSync.cs new file mode 100644 index 0000000..9ba35d1 --- /dev/null +++ b/src/SSync.Server.LiteDB.PlayGround/Sync/Dto/UserSync.cs @@ -0,0 +1,12 @@ +using SSync.Server.LitebDB.Abstractions; + +namespace SSync.Server.LiteDB.PlayGround.Sync.Dto; + +public class UserSync : ISchema +{ + public UserSync(Guid id) : base(id) + { + } + + public string? Name { get; set; } +} \ No newline at end of file diff --git a/src/SSync.Server.LiteDB.PlayGround/Sync/Handlers/Pull/PullFinanceRequestHandler.cs b/src/SSync.Server.LiteDB.PlayGround/Sync/Handlers/Pull/PullFinanceRequestHandler.cs new file mode 100644 index 0000000..c63241a --- /dev/null +++ b/src/SSync.Server.LiteDB.PlayGround/Sync/Handlers/Pull/PullFinanceRequestHandler.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore; +using SSync.Server.LitebDB.Abstractions.Sync; +using SSync.Server.LiteDB.PlayGround.Data; +using SSync.Server.LiteDB.PlayGround.Sync.Dto; + +namespace SSync.Server.LiteDB.PlayGround.Sync.Handlers.Pull; + +public class PullFinanceRequestHandler : ISSyncPullRequest +{ + private readonly TestDbContext _ctx; + + public PullFinanceRequestHandler(TestDbContext ctx) + { + _ctx = ctx; + } + + public async Task> QueryAsync(PlayParamenter parameter) + { + var finances = await _ctx.Finances.Select(u => new FinanceSync(u.Id) + { + Price = new Random().Next(100), + CreatedAt = u.CreatedAt, + DeletedAt = u.DeletedAt, + UpdatedAt = u.UpdatedAt + }).ToListAsync(); + + return finances; + } +} \ No newline at end of file diff --git a/src/SSync.Server.LiteDB.PlayGround/Sync/Handlers/Pull/PullUserRequestHandler.cs b/src/SSync.Server.LiteDB.PlayGround/Sync/Handlers/Pull/PullUserRequestHandler.cs new file mode 100644 index 0000000..1c8b961 --- /dev/null +++ b/src/SSync.Server.LiteDB.PlayGround/Sync/Handlers/Pull/PullUserRequestHandler.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore; +using SSync.Server.LitebDB.Abstractions.Sync; +using SSync.Server.LiteDB.PlayGround.Data; +using SSync.Server.LiteDB.PlayGround.Sync.Dto; + +namespace SSync.Server.LiteDB.PlayGround.Sync.Handlers.Pull; + +public class PullUserRequestHandler : ISSyncPullRequest +{ + private readonly TestDbContext _ctx; + + public PullUserRequestHandler(TestDbContext ctx) + { + _ctx = ctx; + } + + public async Task> QueryAsync(PlayParamenter parameter) + { + var users = await _ctx.User.Select(u => new UserSync(u.Id) + { + Name = u.Name, + CreatedAt = u.CreatedAt, + DeletedAt = u.DeletedAt, + UpdatedAt = u.UpdatedAt + }).ToListAsync(); + + return users; + } +} \ No newline at end of file diff --git a/src/SSync.Server.LiteDB.PlayGround/Sync/Handlers/Push/PushUserRequestHandler.cs b/src/SSync.Server.LiteDB.PlayGround/Sync/Handlers/Push/PushUserRequestHandler.cs new file mode 100644 index 0000000..8a732a0 --- /dev/null +++ b/src/SSync.Server.LiteDB.PlayGround/Sync/Handlers/Push/PushUserRequestHandler.cs @@ -0,0 +1,61 @@ +using Microsoft.EntityFrameworkCore; +using SSync.Server.LitebDB.Abstractions.Sync; +using SSync.Server.LiteDB.PlayGround.Data; +using SSync.Server.LiteDB.PlayGround.Model; +using SSync.Server.LiteDB.PlayGround.Sync.Dto; + +public class PushUserRequestHandler : ISSyncPushRequest +{ + private readonly TestDbContext _db; + + public PushUserRequestHandler(TestDbContext db) => _db = db; + + public async Task FindByIdAsync(Guid id) + { + return await _db.User.Where(u => u.Id == id) + .Select(us => new UserSync(id) + { + Name = us.Name, + CreatedAt = us.CreatedAt, + DeletedAt = us.DeletedAt, + UpdatedAt = us.UpdatedAt + }) + .FirstOrDefaultAsync(); + } + + public async Task CreateAsync(UserSync schema) + { + var us = new User() + { + Id = schema.Id, + Name = schema.Name, + CreatedAt = schema.CreatedAt, + DeletedAt = schema.DeletedAt, + UpdatedAt = schema.UpdatedAt + }; + + await _db.User.AddAsync(us); + + return await _db.SaveChangesAsync() > 0; + } + + public async Task UpdateAsync(UserSync schema) + { + var us = await _db.User.FindAsync(schema.Id); + + us!.UpdatedAt = DateTime.Now; + + us.Name = schema.Name; + + return await _db.SaveChangesAsync() > 0; + } + + public async Task DeleteAsync(UserSync schema) + { + var us = await _db.User.FindAsync(schema.Id); + + us!.DeletedAt = DateTime.Now; + + return await _db.SaveChangesAsync() > 0; + } +} \ No newline at end of file diff --git a/src/SSync.Server.LiteDB.PlayGround/Sync/PlayParamenter.cs b/src/SSync.Server.LiteDB.PlayGround/Sync/PlayParamenter.cs new file mode 100644 index 0000000..5ff21e8 --- /dev/null +++ b/src/SSync.Server.LiteDB.PlayGround/Sync/PlayParamenter.cs @@ -0,0 +1,8 @@ +using SSync.Server.LitebDB.Engine; + +namespace SSync.Server.LiteDB.PlayGround.Sync; + +public class PlayParamenter : SSyncParameter +{ + public int Time { get; set; } = new Random().Next(100); +} \ No newline at end of file diff --git a/test/SSync.Server.LitebDB.Integration.Tests/MockServerApp/IntegrationFixture.cs b/test/SSync.Server.LitebDB.Integration.Tests/MockServerApp/IntegrationFixture.cs new file mode 100644 index 0000000..27e8baf --- /dev/null +++ b/test/SSync.Server.LitebDB.Integration.Tests/MockServerApp/IntegrationFixture.cs @@ -0,0 +1,106 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.TestHost; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using SSync.Server.LiteDB.PlayGround.Data; +using Testcontainers.PostgreSql; + +namespace SSync.Server.LitebDB.Integration.Tests.MockServerApp; + +public class IntegrationFixture : IAsyncLifetime +{ + private readonly PostgreSqlContainer _postgreSqlContainer; + + public IntegrationFixture() + { + _postgreSqlContainer = new PostgreSqlBuilder() + .WithDatabase("ssync-litedb-db-test") + .WithPortBinding(49111, 5432) + .WithUsername("postgres") + .WithPassword("postgres") + .WithName("ct_postgres_ssync_litedb_db_test") + .WithImage("postgres:16") + .Build(); + + } + + public async Task InitializeAsync() + { + await _postgreSqlContainer.StartAsync(); + App = new MockServerApp(_postgreSqlContainer.GetConnectionString()); + Client = App.CreateClient(); + } + + public MockServerApp App { get; set; } + public HttpClient Client { get; set; } + + public async Task DisposeAsync() + { + await _postgreSqlContainer.StopAsync(); + } + + public class MockServerApp : WebApplicationFactory + { + private readonly string _postgresqlConnectionString; + + public MockServerApp(string postgresqlConnectionString) + { + _postgresqlConnectionString = postgresqlConnectionString; + } + protected override void ConfigureWebHost(IWebHostBuilder builder) + { + builder.ConfigureTestServices(services => + { + + var descriptorType = + typeof(DbContextOptions); + + var descriptor = services + .SingleOrDefault(s => s.ServiceType == descriptorType); + + if (descriptor is not null) + { + services.Remove(descriptor); + } + services.AddDbContext(options => + options.UseNpgsql(_postgresqlConnectionString)); + }); + } + } +} + +[CollectionDefinition(nameof(IntegrationFixtureCollection))] +public class IntegrationFixtureCollection : ICollectionFixture{ } + +[Collection(nameof(IntegrationFixtureCollection))] +public class IntegrationTest : IAsyncLifetime +{ + public IntegrationTest(IntegrationFixture integrationFixture ) + { + IntegrationFixture = integrationFixture; + } + + public IntegrationFixture IntegrationFixture { get; } + public HttpClient Client => IntegrationFixture.Client; + public IServiceScope Scope { get; set; } +// public IServiceProvider Services => Scope.ServiceProvider; case teste service with interfaces + public TestDbContext DbContext { get; set; } + + + public Task InitializeAsync() + { + Scope = IntegrationFixture.App.Services.CreateScope(); + DbContext = Scope.ServiceProvider.GetRequiredService(); + return Task.CompletedTask; + } + + + public Task DisposeAsync() + { + Scope.Dispose(); + DbContext.Dispose(); + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/test/SSync.Server.LitebDB.Integration.Tests/MockServerApp/Sync/Store_Test.cs b/test/SSync.Server.LitebDB.Integration.Tests/MockServerApp/Sync/Store_Test.cs new file mode 100644 index 0000000..e26ad35 --- /dev/null +++ b/test/SSync.Server.LitebDB.Integration.Tests/MockServerApp/Sync/Store_Test.cs @@ -0,0 +1,73 @@ +namespace SSync.Server.LitebDB.Integration.Tests.MockServerApp.Sync; + +/// +/// Test to operation crud +/// +public class Store_Test : IntegrationTest +{ + public Store_Test(IntegrationFixture integrationFixture) : base(integrationFixture) + { + } + + [Fact] + public async Task Create_User_And_Finance_Should_Return_Success() + { + var createResult = await Client.GetAsync("/create"); + + Assert.True(createResult.IsSuccessStatusCode); + + var resultListObjUserAndFinance = await Client.GetAsync("/list"); + + Assert.True(resultListObjUserAndFinance.IsSuccessStatusCode); + } + + + [Fact] + public async Task Set_Update_Should_Changes_UpdatedAt() + { + var createResult = await Client.GetAsync("/create"); + + Assert.True(createResult.IsSuccessStatusCode); + + var fistUser = DbContext.User.FirstOrDefault(); + var fistFinance = DbContext.Finances.FirstOrDefault(); + + var updateUserResult = await Client.GetAsync($"/user-update/{fistUser!.Id}"); + var updateFinanceResult = await Client.GetAsync($"/finance-update/{fistFinance!.Id}"); + Assert.True(updateUserResult.IsSuccessStatusCode); + Assert.True(updateFinanceResult.IsSuccessStatusCode); + + DbContext.ChangeTracker.Clear(); + + var stateUser = DbContext.User.FirstOrDefault(u => u.Id == fistUser.Id); + var stateFinance = DbContext.Finances.FirstOrDefault(u => u.Id == fistFinance.Id); + + + Assert.True(stateUser!.CreatedAt != stateUser!.UpdatedAt); + Assert.True(stateFinance!.CreatedAt != stateFinance!.UpdatedAt); + } + + [Fact] + public async Task Set_Delete_Should_Changes_Set_DeleteAt() + { + var createResult = await Client.GetAsync("/create"); + + Assert.True(createResult.IsSuccessStatusCode); + + var fistUser = DbContext.User.FirstOrDefault(); + var fistFinance = DbContext.Finances.FirstOrDefault(); + + var updateUserResult = await Client.GetAsync($"/user-delete/{fistUser!.Id}"); + var updateFinanceResult = await Client.GetAsync($"/finance-delete/{fistFinance!.Id}"); + Assert.True(updateUserResult.IsSuccessStatusCode); + Assert.True(updateFinanceResult.IsSuccessStatusCode); + + DbContext.ChangeTracker.Clear(); + + var stateUser = DbContext.User.FirstOrDefault(u => u.Id == fistUser.Id); + var stateFinance = DbContext.Finances.FirstOrDefault(u => u.Id == fistFinance.Id); + + Assert.True(stateUser!.DeletedAt.HasValue); + Assert.True(stateFinance!.DeletedAt.HasValue); + } +} \ No newline at end of file diff --git a/test/SSync.Server.LitebDB.Integration.Tests/SSync.Server.LitebDB.Integration.Tests.csproj b/test/SSync.Server.LitebDB.Integration.Tests/SSync.Server.LitebDB.Integration.Tests.csproj new file mode 100644 index 0000000..1702bf3 --- /dev/null +++ b/test/SSync.Server.LitebDB.Integration.Tests/SSync.Server.LitebDB.Integration.Tests.csproj @@ -0,0 +1,41 @@ + + + + net8.0 + enable + enable + + false + true + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + + + + + From 53a596fd74233ce15594bc7b3d33c836142b430e Mon Sep 17 00:00:00 2001 From: Gabriel Sales Date: Sat, 21 Dec 2024 00:27:24 -0300 Subject: [PATCH 2/2] Change workflow pipeline with integrations tests --- .../workflows/build_and_test.yaml | 15 +++++++++------ .github/workflows/publish.yaml | 2 -- 2 files changed, 9 insertions(+), 8 deletions(-) rename build_and_test.yaml => .github/workflows/build_and_test.yaml (58%) diff --git a/build_and_test.yaml b/.github/workflows/build_and_test.yaml similarity index 58% rename from build_and_test.yaml rename to .github/workflows/build_and_test.yaml index 5733032..a3b34be 100644 --- a/build_and_test.yaml +++ b/.github/workflows/build_and_test.yaml @@ -3,8 +3,8 @@ name: Build and Tests on: pull_request jobs: - build_teste_nuget_package: - name: Build and Teste SSync Client and Server + build_tests_nuget_package: + name: Build and Tests SSync Client and Server runs-on: ubuntu-latest steps: @@ -24,8 +24,8 @@ jobs: run: dotnet build --no-restore ./src/SSync.Client.LitebDB/SSync.Client.LitebDB.csproj - - name: Test Client - run: dotnet test --no-build --verbosity normal ./src/SSync.Client.LitebDB/SSync.Client.LitebDB.csproj + - name: Run Unit Tests Client + run: dotnet test --no-build --verbosity normal ./test/SSync.Client.LiteDB.Tests/SSync.Client.LiteDB.Tests.csproj # end project ssync client @@ -40,7 +40,10 @@ jobs: - name: Build Server run: dotnet build --no-restore ./src/SSync.Server.LitebDB/SSync.Server.LitebDB.csproj - - name: Test Server - run: dotnet test --no-build --verbosity normal ./src/SSync.Server.LitebDB/SSync.Server.LitebDB.csproj + - name: Run Unit Tests Server + run: dotnet test --no-build --verbosity normal ./test/SSync.Server.LitebDB.Tests/SSync.Server.LitebDB.Tests.csproj + + - name: Run Integration Tests Server + run: dotnet test --no-build --verbosity normal ./test/SSync.Server.LitebDB.Integration.Tests/SSync.Server.LitebDB.Integration.Tests.csproj # end project ssync server \ No newline at end of file diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 2f83177..bad119a 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -28,7 +28,6 @@ jobs: - name: Build Client run: dotnet build --no-restore ./src/SSync.Client.LitebDB/SSync.Client.LitebDB.csproj - - name: Test Client run: dotnet test --no-build --verbosity normal ./src/SSync.Client.LitebDB/SSync.Client.LitebDB.csproj @@ -64,7 +63,6 @@ jobs: run: | dotnet pack ./src/SSync.Server.LitebDB/SSync.Server.LitebDB.csproj -c Release -o output-server - - name: Publish Server to Nuget.Org run: | dotnet nuget push output-server/*.nupkg -k ${{ secrets.NUGET_API_KEY_SERVER }} -s https://api.nuget.org/v3/index.json --skip-duplicate