Skip to content

Commit c401dab

Browse files
committed
feat: add Ecommerce.Tests.Shared project and integrate with existing tests
1 parent 3e6fc83 commit c401dab

File tree

6 files changed

+48
-14
lines changed

6 files changed

+48
-14
lines changed

Ecommerce_Platform.NET.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ecommerce.UnitTests", "Test
2121
EndProject
2222
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ecommerce.IntegrationTests", "Tests\IntegrationTests\Ecommerce.IntegrationTests.csproj", "{FF8A5144-9BB7-4FDF-9288-96A4736627A6}"
2323
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ecommerce.Tests.Shared", "Tests\Shared\Ecommerce.Tests.Shared.csproj", "{A0ECA74D-5773-4278-BF1B-5AF564437D6F}"
25+
EndProject
2426
Global
2527
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2628
Debug|Any CPU = Debug|Any CPU
@@ -58,6 +60,10 @@ Global
5860
{FF8A5144-9BB7-4FDF-9288-96A4736627A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
5961
{FF8A5144-9BB7-4FDF-9288-96A4736627A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
6062
{FF8A5144-9BB7-4FDF-9288-96A4736627A6}.Release|Any CPU.Build.0 = Release|Any CPU
63+
{A0ECA74D-5773-4278-BF1B-5AF564437D6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
64+
{A0ECA74D-5773-4278-BF1B-5AF564437D6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
65+
{A0ECA74D-5773-4278-BF1B-5AF564437D6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
66+
{A0ECA74D-5773-4278-BF1B-5AF564437D6F}.Release|Any CPU.Build.0 = Release|Any CPU
6167
EndGlobalSection
6268
GlobalSection(NestedProjects) = preSolution
6369
{9837F584-B985-4C56-86B2-2F6967FEABE2} = {822DD145-7FF0-4C65-A5BE-1EBAC64933DE}
@@ -67,5 +73,6 @@ Global
6773
{67F6F1B5-51EF-4519-87DB-16697D544D02} = {822DD145-7FF0-4C65-A5BE-1EBAC64933DE}
6874
{991BEE77-9523-44D4-9CCD-A56180292637} = {15592CAE-0D6B-4FFE-A9DA-D72C0EB42BD7}
6975
{FF8A5144-9BB7-4FDF-9288-96A4736627A6} = {15592CAE-0D6B-4FFE-A9DA-D72C0EB42BD7}
76+
{A0ECA74D-5773-4278-BF1B-5AF564437D6F} = {15592CAE-0D6B-4FFE-A9DA-D72C0EB42BD7}
7077
EndGlobalSection
7178
EndGlobal

Source/Ecommerce.Infrastructure/Ecommerce.Infrastructure.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<ItemGroup>
1818
<PackageReference Include="CloudinaryDotNet" Version="1.27.3" />
1919
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
20+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.0" />
2021
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
2122
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0">
2223
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

Tests/IntegrationTests/Ecommerce.IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<ProjectReference Include="..\..\Source\Ecommerce.Application\Ecommerce.Application.csproj" />
2727
<ProjectReference Include="..\..\Source\Ecommerce.Domain\Ecommerce.Domain.csproj" />
2828
<ProjectReference Include="..\..\Source\Ecommerce.Infrastructure\Ecommerce.Infrastructure.csproj" />
29+
<ProjectReference Include="..\Shared\Ecommerce.Tests.Shared.csproj" />
2930
</ItemGroup>
3031

3132

Tests/IntegrationTests/Utils/Fixtures/EcommerceWebApplicationFactoryFixture.cs renamed to Tests/IntegrationTests/EcommerceWebApplicationFactoryFixture.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
using System.Data;
12
using Ecommerce.Infrastructure.Persistence.EfCore;
23
using Microsoft.AspNetCore.Hosting;
34
using Microsoft.AspNetCore.Mvc.Testing;
45
using Microsoft.EntityFrameworkCore;
6+
using Microsoft.EntityFrameworkCore.Storage;
57
using Microsoft.Extensions.DependencyInjection;
68

7-
namespace Ecommerce.IntegrationTests.Utils.Fixtures;
9+
namespace Ecommerce.IntegrationTests;
810

911
public class EcommerceWebApplicationFactoryFixture : WebApplicationFactory<Ecommerce.Api.Program>
1012
{
@@ -41,11 +43,14 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
4143
services.AddDbContext<EfCoreContext>(options =>
4244
{
4345
// options.UseNpgsql(
44-
// "Host=localhost;Database=frontierfinds_test;Username=postgres;Password=123;Port=5432"
46+
// "Host=localhost;Database=FrontierFinds_TestDb;Username=postgres;Password=123;Port=5432"
4547
// );
48+
4649
options.UseSqlServer(
47-
"Server=EVOO-EG-LP7\\SQLEXPRESS;Database=ecommerce;Trusted_Connection=True;TrustServerCertificate=True;"
50+
"Server=EVOO-EG-LP7\\SQLEXPRESS;Database=FrontierFinds_TestDb;Trusted_Connection=True;TrustServerCertificate=True;"
4851
);
52+
53+
// options.UseInMemoryDatabase("FrontierFinds_TestDb");
4954
});
5055

5156
var sp = services.BuildServiceProvider();
@@ -55,6 +60,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
5560
try // Add try-catch block
5661
{
5762
var dbContext = scope.ServiceProvider.GetRequiredService<EfCoreContext>();
63+
dbContext.Database.EnsureDeleted();
5864
dbContext.Database.EnsureCreated();
5965
Console.WriteLine("Database EnsureCreated() successful."); // Log success
6066
}
@@ -78,7 +84,7 @@ public override async ValueTask DisposeAsync()
7884
var scopedServices = scope.ServiceProvider;
7985
var context = scopedServices.GetRequiredService<EfCoreContext>();
8086
await context.Database.EnsureDeletedAsync(); // Delete database
81-
Console.WriteLine("Test database deleted.");
87+
Console.WriteLine("\nTest database deleted.\n");
8288
}
8389
}
8490
catch (Exception ex)
@@ -88,4 +94,13 @@ public override async ValueTask DisposeAsync()
8894

8995
await base.DisposeAsync();
9096
}
97+
98+
public async Task InitializeAsync()
99+
{
100+
using var scope = Services.CreateScope();
101+
var dbContext = scope.ServiceProvider.GetRequiredService<EfCoreContext>();
102+
await dbContext.Database.EnsureDeletedAsync();
103+
await dbContext.Database.EnsureCreatedAsync();
104+
Console.WriteLine("\nTest database Created.\n");
105+
}
91106
}

Tests/IntegrationTests/UnitTest1.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

Tests/Shared/Utils/Utils.User.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Ecommerce.Application.UseCases.Products.Common;
22
using Ecommerce.Application.UseCases.Users.Commands.RegisterUser;
33
using Ecommerce.Application.UseCases.Users.Queries.LoginUser;
4+
using Ecommerce.Contracts.Authentication;
5+
using Microsoft.CodeAnalysis.CSharp.Syntax;
46

57
namespace Ecommerce.Tests.Shared;
68

@@ -48,5 +50,23 @@ public static LoginUserQuery CreateLoginUserQuery()
4850
{
4951
return new LoginUserQuery(Utils.User.Email, Utils.User.Password);
5052
}
53+
54+
public static RegisterRequest CreateRegisterRequest()
55+
{
56+
return new RegisterRequest(
57+
FirstName: FirstName,
58+
LastName: LastName,
59+
Email: Email,
60+
Password: Password,
61+
ConfirmPassword: Password,
62+
PhoneNumber: PhoneNumber,
63+
CountryCode: int.Parse(CountryCode)
64+
);
65+
}
66+
67+
public static LoginRequest CreateLoginRequest()
68+
{
69+
return new LoginRequest(Email: Email, Password: Password);
70+
}
5171
}
5272
}

0 commit comments

Comments
 (0)