mirror of
https://github.com/alex289/CleanArchitecture.git
synced 2025-06-29 18:21:08 +00:00
feat: Add redis test
This commit is contained in:
parent
aa4c16922d
commit
5a92022fdc
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using CleanArchitecture.Domain.Entities;
|
||||
using CleanArchitecture.Infrastructure.Database;
|
||||
using CleanArchitecture.IntegrationTests.Fixtures;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace CleanArchitecture.IntegrationTests.ExternalServices;
|
||||
|
||||
public sealed class RedisTestFixture : TestFixtureBase
|
||||
{
|
||||
public Guid CreatedTenantId { get; } = Guid.NewGuid();
|
||||
|
||||
public IDistributedCache DistributedCache { get; }
|
||||
|
||||
public RedisTestFixture()
|
||||
{
|
||||
DistributedCache = Factory.Services.GetRequiredService<IDistributedCache>();
|
||||
}
|
||||
|
||||
public async Task SeedTestData()
|
||||
{
|
||||
await GlobalSetupFixture.RespawnDatabaseAsync();
|
||||
|
||||
using var context = Factory.Services.GetRequiredService<ApplicationDbContext>();
|
||||
|
||||
context.Tenants.Add(new Tenant(
|
||||
CreatedTenantId,
|
||||
"Test Tenant"));
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
using System.Threading.Tasks;
|
||||
using CleanArchitecture.Application.ViewModels.Tenants;
|
||||
using CleanArchitecture.Domain;
|
||||
using CleanArchitecture.Domain.Entities;
|
||||
using CleanArchitecture.IntegrationTests.Extensions;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace CleanArchitecture.IntegrationTests.ExternalServices;
|
||||
|
||||
public sealed class RedisTests
|
||||
{
|
||||
private readonly RedisTestFixture _fixture = new();
|
||||
|
||||
[OneTimeSetUp]
|
||||
public async Task Setup() => await _fixture.SeedTestData();
|
||||
|
||||
[Test, Order(0)]
|
||||
public async Task Should_Get_Tenant_By_Id_And_Ensure_Cache()
|
||||
{
|
||||
var response = await _fixture.ServerClient.GetAsync($"/api/v1/Tenant/{_fixture.CreatedTenantId}");
|
||||
var message = await response.Content.ReadAsJsonAsync<TenantViewModel>();
|
||||
message!.Data!.Id.Should().Be(_fixture.CreatedTenantId);
|
||||
|
||||
var json = await _fixture.DistributedCache.GetStringAsync(CacheKeyGenerator.GetEntityCacheKey<Tenant>(_fixture.CreatedTenantId));
|
||||
json.Should().NotBeNullOrEmpty();
|
||||
|
||||
var tenant = JsonConvert.DeserializeObject<TenantViewModel>(json!)!;
|
||||
|
||||
tenant.Should().NotBeNull();
|
||||
tenant.Id.Should().Be(_fixture.CreatedTenantId);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user