Skip to content

Commit 3ca55b2

Browse files
committed
tests
1 parent 5824282 commit 3ca55b2

File tree

6 files changed

+98
-25
lines changed

6 files changed

+98
-25
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Testcontainers.PostgreSql;
3+
using Xunit;
4+
5+
public class DatabaseTestFixture : IAsyncLifetime
6+
{
7+
private PostgreSqlContainer? _postgresContainer;
8+
private TestDbContext? _dbContext;
9+
10+
public TestDbContext DbContext => _dbContext ?? throw new InvalidOperationException("Database not initialized");
11+
12+
public async Task InitializeAsync()
13+
{
14+
// Create and start PostgreSQL container
15+
_postgresContainer = new PostgreSqlBuilder()
16+
.WithImage("postgres:16-alpine")
17+
.WithDatabase("testdb")
18+
.WithUsername("test")
19+
.WithPassword("test")
20+
.Build();
21+
22+
await _postgresContainer.StartAsync();
23+
24+
// Create DbContext with connection to container
25+
var connectionString = _postgresContainer.GetConnectionString();
26+
var optionsBuilder = new DbContextOptionsBuilder<TestDbContext>();
27+
optionsBuilder.UseNpgsql(connectionString);
28+
29+
_dbContext = new TestDbContext(optionsBuilder.Options);
30+
31+
// Create database schema
32+
await _dbContext.Database.EnsureCreatedAsync();
33+
34+
// Seed test data
35+
await SeedTestData();
36+
}
37+
38+
private async Task SeedTestData()
39+
{
40+
var users = TestData.Users.Values.ToList();
41+
await _dbContext.Users.AddRangeAsync(users);
42+
await _dbContext.SaveChangesAsync();
43+
}
44+
45+
public async Task DisposeAsync()
46+
{
47+
if (_dbContext != null)
48+
{
49+
await _dbContext.DisposeAsync();
50+
}
51+
52+
if (_postgresContainer != null)
53+
{
54+
await _postgresContainer.StopAsync();
55+
await _postgresContainer.DisposeAsync();
56+
}
57+
}
58+
}

src/GoatQuery/tests/Filter/FilterTest.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
using Xunit;
22

3-
public sealed class FilterTest
3+
public sealed class FilterTest : IClassFixture<DatabaseTestFixture>
44
{
5+
private readonly DatabaseTestFixture _fixture;
6+
7+
public FilterTest(DatabaseTestFixture fixture)
8+
{
9+
_fixture = fixture;
10+
}
11+
512
public static IEnumerable<object[]> Parameters()
613
{
714
yield return new object[] {

src/GoatQuery/tests/TestData.cs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ public static class TestData
66
{
77
Age = 2,
88
Firstname = "John",
9-
UserId = Guid.Parse("58cdeca3-645b-457c-87aa-7d5f87734255"),
10-
DateOfBirth = DateTime.Parse("2004-01-31 23:59:59"),
9+
DateOfBirth = DateTime.Parse("2004-01-31 23:59:59").ToUniversalTime(),
1110
BalanceDecimal = 1.50m,
1211
IsEmailVerified = true,
1312
Addresses = new[]
@@ -27,8 +26,7 @@ public static class TestData
2726
{
2827
Age = 16,
2928
Firstname = "Manager 01",
30-
UserId = Guid.Parse("671e6bac-b6de-4cc7-b3e9-1a6ac4546b43"),
31-
DateOfBirth = DateTime.Parse("2000-01-01 00:00:00"),
29+
DateOfBirth = DateTime.Parse("2000-01-01 00:00:00").ToUniversalTime(),
3230
BalanceDecimal = 2.00m,
3331
IsEmailVerified = false
3432
}
@@ -37,8 +35,7 @@ public static class TestData
3735
{
3836
Age = 9,
3937
Firstname = "Jane",
40-
UserId = Guid.Parse("58cdeca3-645b-457c-87aa-7d5f87734255"),
41-
DateOfBirth = DateTime.Parse("2020-05-09 15:30:00"),
38+
DateOfBirth = DateTime.Parse("2020-05-09 15:30:00").ToUniversalTime(),
4239
BalanceDecimal = 0,
4340
IsEmailVerified = false,
4441
Addresses = new[]
@@ -59,8 +56,7 @@ public static class TestData
5956
{
6057
Age = 1,
6158
Firstname = "Apple",
62-
UserId = Guid.Parse("58cdeca3-645b-457c-87aa-7d5f87734255"),
63-
DateOfBirth = DateTime.Parse("1980-12-31 00:00:01"),
59+
DateOfBirth = DateTime.Parse("1980-12-31 00:00:01").ToUniversalTime(),
6460
BalanceFloat = 1204050.98f,
6561
IsEmailVerified = true,
6662
Addresses = new[]
@@ -80,8 +76,7 @@ public static class TestData
8076
{
8177
Age = 16,
8278
Firstname = "Manager 01",
83-
UserId = Guid.Parse("671e6bac-b6de-4cc7-b3e9-1a6ac4546b43"),
84-
DateOfBirth = DateTime.Parse("2000-01-01 00:00:00"),
79+
DateOfBirth = DateTime.Parse("2000-01-01 00:00:00").ToUniversalTime(),
8580
BalanceDecimal = 2.00m,
8681
IsEmailVerified = true
8782
},
@@ -91,8 +86,7 @@ public static class TestData
9186
{
9287
Age = 1,
9388
Firstname = "Harry",
94-
UserId = Guid.Parse("e4c7772b-8947-4e46-98ed-644b417d2a08"),
95-
DateOfBirth = DateTime.Parse("2002-08-01"),
89+
DateOfBirth = DateTime.Parse("2002-08-01").ToUniversalTime(),
9690
BalanceDecimal = 0.5372958205929493m,
9791
IsEmailVerified = false,
9892
Addresses = Array.Empty<Address>()
@@ -101,8 +95,7 @@ public static class TestData
10195
{
10296
Age = 1,
10397
Firstname = "Doe",
104-
UserId = Guid.Parse("58cdeca3-645b-457c-87aa-7d5f87734255"),
105-
DateOfBirth = DateTime.Parse("2023-07-26 12:00:30"),
98+
DateOfBirth = DateTime.Parse("2023-07-26 12:00:30").ToUniversalTime(),
10699
BalanceDecimal = null,
107100
IsEmailVerified = true,
108101
Addresses = new[]
@@ -118,8 +111,7 @@ public static class TestData
118111
{
119112
Age = 33,
120113
Firstname = "Egg",
121-
UserId = Guid.Parse("58cdeca3-645b-457c-87aa-7d5f87734255"),
122-
DateOfBirth = DateTime.Parse("2000-01-01 00:00:00"),
114+
DateOfBirth = DateTime.Parse("2000-01-01 00:00:00").ToUniversalTime(),
123115
BalanceDouble = 1334534453453433.33435443343231235652d,
124116
IsEmailVerified = false,
125117
Addresses = new[]
@@ -139,24 +131,21 @@ public static class TestData
139131
{
140132
Age = 18,
141133
Firstname = "Manager 02",
142-
UserId = Guid.Parse("2bde56ac-4829-41fb-abbc-2b8454962e2a"),
143-
DateOfBirth = DateTime.Parse("1999-04-21 00:00:00"),
134+
DateOfBirth = DateTime.Parse("1999-04-21 00:00:00").ToUniversalTime(),
144135
BalanceDecimal = 19.00m,
145136
IsEmailVerified = true,
146137
Manager = new User
147138
{
148139
Age = 30,
149140
Firstname = "Manager 03",
150-
UserId = Guid.Parse("8ef23728-c429-42f9-98ee-425419092664"),
151-
DateOfBirth = DateTime.Parse("1993-04-21 00:00:00"),
141+
DateOfBirth = DateTime.Parse("1993-04-21 00:00:00").ToUniversalTime(),
152142
BalanceDecimal = 29.00m,
153143
IsEmailVerified = true,
154144
Manager = new User
155145
{
156146
Age = 40,
157147
Firstname = "Manager 04",
158-
UserId = Guid.Parse("4cde56ac-4829-41fb-abbc-2b8454962e2a"),
159-
DateOfBirth = DateTime.Parse("1983-04-21 00:00:00"),
148+
DateOfBirth = DateTime.Parse("1983-04-21 00:00:00").ToUniversalTime(),
160149
BalanceDecimal = 39.00m,
161150
IsEmailVerified = true
162151
},
@@ -173,7 +162,6 @@ public static class TestData
173162
{
174163
Age = 4,
175164
Firstname = "NullUser",
176-
UserId = Guid.Parse("11111111-1111-1111-1111-111111111111"),
177165
DateOfBirth = null,
178166
BalanceDecimal = null,
179167
BalanceDouble = null,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
public class TestDbContext : DbContext
4+
{
5+
public TestDbContext(DbContextOptions<TestDbContext> options) : base(options) { }
6+
7+
public DbSet<User> Users => Set<User>();
8+
9+
protected override void OnModelCreating(ModelBuilder modelBuilder)
10+
{
11+
12+
}
13+
}

src/GoatQuery/tests/User.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
public record User
44
{
5+
public Guid Id { get; set; }
56
public int Age { get; set; }
6-
public Guid UserId { get; set; }
77
public string Firstname { get; set; } = string.Empty;
88
public decimal? BalanceDecimal { get; set; }
99
public double? BalanceDouble { get; set; }
@@ -24,18 +24,21 @@ public sealed record CustomJsonPropertyUser : User
2424

2525
public record Address
2626
{
27+
public Guid Id { get; set; }
2728
public City City { get; set; } = new City();
2829
public string AddressLine1 { get; set; } = string.Empty;
2930
}
3031

3132
public record City
3233
{
34+
public Guid Id { get; set; }
3335
public string Name { get; set; } = string.Empty;
3436
public string Country { get; set; } = string.Empty;
3537
}
3638

3739
public record Company
3840
{
41+
public Guid Id { get; set; }
3942
public string Name { get; set; } = string.Empty;
4043
public string Department { get; set; } = string.Empty;
4144
}

src/GoatQuery/tests/tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1919
<PrivateAssets>all</PrivateAssets>
2020
</PackageReference>
21+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />
22+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
23+
<PackageReference Include="Testcontainers" Version="4.5.0" />
24+
<PackageReference Include="Testcontainers.PostgreSql" Version="4.5.0" />
2125
</ItemGroup>
2226

2327
<ItemGroup>

0 commit comments

Comments
 (0)