-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
483eff1
commit f1dda9b
Showing
37 changed files
with
595 additions
and
216 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using YDotNet.Server.EntityFramework; | ||
|
||
namespace Demo.Db; | ||
|
||
public class AppDbContext(DbContextOptions options) : DbContext(options) | ||
{ | ||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.UseYDotNet(); | ||
base.OnModelCreating(modelBuilder); | ||
} | ||
} |
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,22 @@ | ||
|
||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
|
||
namespace Demo.Db; | ||
|
||
public class DbInitializer(IDbContextFactory<AppDbContext> dbContextFactory) : IHostedService | ||
{ | ||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
await using var context = await dbContextFactory.CreateDbContextAsync(cancellationToken); | ||
|
||
var creator = (RelationalDatabaseCreator)context.Database.GetService<IDatabaseCreator>(); | ||
await creator.EnsureCreatedAsync(cancellationToken); | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} |
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 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,60 @@ | ||
namespace YDotNet.Tests.Server.Unit; | ||
|
||
using NUnit.Framework; | ||
using YDotNet.Server.Storage; | ||
|
||
public abstract class DocumentStorageTests | ||
{ | ||
protected abstract IDocumentStorage CreateSut(); | ||
|
||
[Test] | ||
public async Task GetDocData() | ||
{ | ||
// Arrange | ||
var sut = CreateSut(); | ||
|
||
// Act | ||
var received = await sut.GetDocAsync(Guid.NewGuid().ToString()); | ||
|
||
// Assert | ||
Assert.That(received, Is.Null); | ||
} | ||
|
||
[Test] | ||
public async Task InsertDocument() | ||
{ | ||
// Arrange | ||
var sut = CreateSut(); | ||
|
||
var docName = Guid.NewGuid().ToString(); | ||
var docData = new byte[] { 1, 2, 3, 4 }; | ||
|
||
// Act | ||
await sut.StoreDocAsync(docName, docData); | ||
|
||
// Assert | ||
var received = await sut.GetDocAsync(docName); | ||
|
||
Assert.That(received, Is.EqualTo(docData)); | ||
} | ||
|
||
[Test] | ||
public async Task UpdateDocument() | ||
{ | ||
// Arrange | ||
var sut = CreateSut(); | ||
|
||
var docName = Guid.NewGuid().ToString(); | ||
var docData1 = new byte[] { 1, 2, 3, 4 }; | ||
var docData2 = new byte[] { 5, 6, 7, 8 }; | ||
|
||
// Act | ||
await sut.StoreDocAsync(docName, docData1); | ||
await sut.StoreDocAsync(docName, docData2); | ||
|
||
// Assert | ||
var received = await sut.GetDocAsync(docName); | ||
|
||
Assert.That(received, Is.EqualTo(docData2)); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
Tests/YDotNet.Tests.Server.Unit/EFDocumentStorageFixture.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,80 @@ | ||
namespace YDotNet.Tests.Server.Unit; | ||
|
||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using NUnit.Framework; | ||
using Testcontainers.PostgreSql; | ||
using YDotNet.Server.EntityFramework; | ||
using YDotNet.Server.Storage; | ||
|
||
[TestFixture] | ||
[Category("Docker")] | ||
public class EFDocumentStorageFixture : DocumentStorageTests | ||
{ | ||
private readonly PostgreSqlContainer postgres = new PostgreSqlBuilder().Build(); | ||
private IServiceProvider services; | ||
|
||
public class AppDbContext(DbContextOptions options) : DbContext(options) | ||
{ | ||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.UseYDotNet(); | ||
base.OnModelCreating(modelBuilder); | ||
} | ||
} | ||
|
||
[OneTimeSetUp] | ||
public async Task OneTimeSetup() | ||
{ | ||
await postgres.StartAsync(); | ||
} | ||
|
||
[OneTimeTearDown] | ||
public async Task OneTimeTeardown() | ||
{ | ||
await postgres.StopAsync(); | ||
} | ||
|
||
[SetUp] | ||
public async Task Setup() | ||
{ | ||
services = new ServiceCollection() | ||
.AddLogging() | ||
.AddDbContext<AppDbContext>(options => | ||
{ | ||
options.UseNpgsql(postgres.GetConnectionString()); | ||
}) | ||
.AddYDotNet() | ||
.AddEntityFrameworkStorage<AppDbContext>() | ||
.Services | ||
.BuildServiceProvider(); | ||
|
||
foreach (var service in services.GetRequiredService<IEnumerable<IHostedService>>()) | ||
{ | ||
await service.StartAsync(default); | ||
} | ||
|
||
var factory = services.GetRequiredService<IDbContextFactory<AppDbContext>>(); | ||
var context = await factory.CreateDbContextAsync(); | ||
var creator = (RelationalDatabaseCreator)context.Database.GetService<IDatabaseCreator>(); | ||
|
||
await creator.EnsureCreatedAsync(); | ||
} | ||
|
||
[TearDown] | ||
public async Task Teardown() | ||
{ | ||
foreach (var service in services.GetRequiredService<IEnumerable<IHostedService>>()) | ||
{ | ||
await service.StopAsync(default); | ||
} | ||
} | ||
|
||
protected override IDocumentStorage CreateSut() | ||
{ | ||
return services.GetRequiredService<IDocumentStorage>(); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
Tests/YDotNet.Tests.Server.Unit/MongoDocumentStorageFixture.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,60 @@ | ||
namespace YDotNet.Tests.Server.Unit; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using MongoDB.Driver; | ||
using NUnit.Framework; | ||
using Testcontainers.MongoDb; | ||
using YDotNet.Server.MongoDB; | ||
using YDotNet.Server.Storage; | ||
|
||
[TestFixture] | ||
[Category("Docker")] | ||
public class MongoDocumentStorageFixture : DocumentStorageTests | ||
{ | ||
private readonly MongoDbContainer mongo = new MongoDbBuilder().Build(); | ||
private IServiceProvider services; | ||
|
||
[OneTimeSetUp] | ||
public async Task OneTimeSetup() | ||
{ | ||
await mongo.StartAsync(); | ||
} | ||
|
||
[OneTimeTearDown] | ||
public async Task OneTimeTeardown() | ||
{ | ||
await mongo.StopAsync(); | ||
} | ||
|
||
[SetUp] | ||
public async Task Setup() | ||
{ | ||
services = new ServiceCollection() | ||
.AddSingleton<IMongoClient>(_ => new MongoClient(mongo.GetConnectionString())) | ||
.AddLogging() | ||
.AddYDotNet() | ||
.AddMongoStorage() | ||
.Services | ||
.BuildServiceProvider(); | ||
|
||
foreach (var service in services.GetRequiredService<IEnumerable<IHostedService>>()) | ||
{ | ||
await service.StartAsync(default); | ||
} | ||
} | ||
|
||
[TearDown] | ||
public async Task Teardown() | ||
{ | ||
foreach (var service in services.GetRequiredService<IEnumerable<IHostedService>>()) | ||
{ | ||
await service.StopAsync(default); | ||
} | ||
} | ||
|
||
protected override IDocumentStorage CreateSut() | ||
{ | ||
return services.GetRequiredService<IDocumentStorage>(); | ||
} | ||
} |
Oops, something went wrong.