Skip to content

Commit

Permalink
feat: Add redis test
Browse files Browse the repository at this point in the history
  • Loading branch information
alex289 committed Nov 22, 2024
1 parent aa4c169 commit 5a92022
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
34 changes: 34 additions & 0 deletions CleanArchitecture.IntegrationTests/ExternalServices/RedisTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 5a92022

Please sign in to comment.