Skip to content

Commit 49dc69a

Browse files
committed
Merge branch 'develop'
2 parents bfec1a5 + 0729831 commit 49dc69a

23 files changed

Lines changed: 1516 additions & 154 deletions

QueryKit.IntegrationTests/QueryKit.IntegrationTests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.5" />
1818
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.5" />
1919
<PackageReference Include="Moq" Version="4.18.4" />
20-
<PackageReference Include="Testcontainers" Version="2.4.0" />
20+
<PackageReference Include="Testcontainers" Version="3.3.0" />
21+
<PackageReference Include="Testcontainers.PostgreSql" Version="3.3.0" />
2122
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
2223
<PackageReference Include="xunit" Version="2.4.2" />
2324
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">

QueryKit.IntegrationTests/TestFixture.cs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace QueryKit.IntegrationTests;
1111
using Microsoft.Extensions.DependencyInjection;
1212
using Microsoft.Extensions.DependencyInjection.Extensions;
1313
using Moq;
14+
using Testcontainers.PostgreSql;
1415
using WebApiTestProject;
1516
using WebApiTestProject.Database;
1617
using Xunit;
@@ -21,7 +22,7 @@ public class TestFixtureCollection : ICollectionFixture<TestFixture> {}
2122
public class TestFixture : IAsyncLifetime
2223
{
2324
public static IServiceScopeFactory BaseScopeFactory;
24-
private readonly TestcontainerDatabase _dbContainer = DbSetup();
25+
private PostgreSqlContainer _dbContainer;
2526

2627
public async Task InitializeAsync()
2728
{
@@ -30,9 +31,10 @@ public async Task InitializeAsync()
3031
EnvironmentName = Consts.Testing.IntegrationTestingEnvName
3132
});
3233

34+
_dbContainer = new PostgreSqlBuilder().Build();
3335
await _dbContainer.StartAsync();
34-
builder.Configuration.GetSection(ConnectionStringOptions.SectionName)[ConnectionStringOptions.RecipeManagementKey] = _dbContainer.ConnectionString;
35-
await RunMigration(_dbContainer.ConnectionString);
36+
builder.Configuration.GetSection(ConnectionStringOptions.SectionName)[ConnectionStringOptions.RecipeManagementKey] = _dbContainer.GetConnectionString();
37+
await RunMigration(_dbContainer.GetConnectionString());
3638

3739
builder.ConfigureServices();
3840
var services = builder.Services;
@@ -53,21 +55,7 @@ private static async Task RunMigration(string connectionString)
5355
var context = new TestingDbContext(options);
5456
await context?.Database?.MigrateAsync();
5557
}
56-
57-
private static TestcontainerDatabase DbSetup()
58-
{
59-
return new TestcontainersBuilder<PostgreSqlTestcontainer>()
60-
.WithDatabase(new PostgreSqlTestcontainerConfiguration
61-
{
62-
Database = "db",
63-
Username = "postgres",
64-
Password = "postgres"
65-
})
66-
.WithName($"IntegrationTesting_QueryKit_{Guid.NewGuid()}")
67-
.WithImage("postgres:latest")
68-
.Build();
69-
}
70-
58+
7159
public async Task DisposeAsync()
7260
{
7361
await _dbContainer.DisposeAsync();

QueryKit.IntegrationTests/Tests/DatabaseFilteringTests.cs

Lines changed: 180 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ namespace QueryKit.IntegrationTests.Tests;
77
using Microsoft.EntityFrameworkCore;
88
using SharedTestingHelper.Fakes;
99
using SharedTestingHelper.Fakes.Author;
10+
using SharedTestingHelper.Fakes.Ingredients;
1011
using SharedTestingHelper.Fakes.Recipes;
12+
using WebApiTestProject.Database;
1113
using WebApiTestProject.Entities;
1214
using WebApiTestProject.Entities.Recipes;
1315
using WebApiTestProject.Features;
@@ -27,7 +29,7 @@ public async Task can_filter_by_string()
2729
await testingServiceScope.InsertAsync(fakePersonOne, fakePersonTwo);
2830

2931
var input = $"""{nameof(TestingPerson.Title)} == "{fakePersonOne.Title}" """;
30-
32+
3133
// Act
3234
var queryablePeople = testingServiceScope.DbContext().People;
3335
var appliedQueryable = queryablePeople.ApplyQueryKitFilter(input);
@@ -38,6 +40,183 @@ public async Task can_filter_by_string()
3840
people[0].Id.Should().Be(fakePersonOne.Id);
3941
}
4042

43+
[Fact]
44+
public async Task can_filter_by_string_for_collection()
45+
{
46+
// Arrange
47+
var testingServiceScope = new TestingServiceScope();
48+
var faker = new Faker();
49+
var fakeIngredientOne = new FakeIngredientBuilder()
50+
.WithName(faker.Lorem.Sentence())
51+
.Build();
52+
var fakeRecipeOne = new FakeRecipeBuilder().Build();
53+
fakeRecipeOne.AddIngredient(fakeIngredientOne);
54+
55+
var fakeIngredientTwo = new FakeIngredientBuilder()
56+
.WithName(faker.Lorem.Sentence())
57+
.Build();
58+
var fakeRecipeTwo = new FakeRecipeBuilder().Build();
59+
fakeRecipeTwo.AddIngredient(fakeIngredientTwo);
60+
await testingServiceScope.InsertAsync(fakeRecipeOne, fakeRecipeTwo);
61+
62+
var input = $"""Ingredients.Name == "{fakeIngredientOne.Name}" """;
63+
64+
// Act
65+
var queryableRecipes = testingServiceScope.DbContext().Recipes;
66+
var appliedQueryable = queryableRecipes.ApplyQueryKitFilter(input);
67+
var recipes = await appliedQueryable.ToListAsync();
68+
69+
// Assert
70+
recipes.Count.Should().Be(1);
71+
recipes[0].Id.Should().Be(fakeRecipeOne.Id);
72+
}
73+
74+
[Fact]
75+
public async Task can_filter_by_string_for_collection_with_count()
76+
{
77+
// Arrange
78+
var testingServiceScope = new TestingServiceScope();
79+
var faker = new Faker();
80+
var fakeIngredientOne = new FakeIngredientBuilder()
81+
.WithName(faker.Lorem.Sentence())
82+
.Build();
83+
var fakeRecipeOne = new FakeRecipeBuilder().Build();
84+
fakeRecipeOne.AddIngredient(fakeIngredientOne);
85+
86+
var fakeIngredientTwo = new FakeIngredientBuilder()
87+
.WithName(faker.Lorem.Sentence())
88+
.Build();
89+
var fakeRecipeTwo = new FakeRecipeBuilder().Build();
90+
fakeRecipeTwo.AddIngredient(fakeIngredientTwo);
91+
await testingServiceScope.InsertAsync(fakeRecipeOne, fakeRecipeTwo);
92+
93+
var input = $"""Title == "{fakeRecipeOne.Title}" && Ingredients #>= 1""";
94+
95+
// Act
96+
var queryableRecipes = testingServiceScope.DbContext().Recipes;
97+
var appliedQueryable = queryableRecipes.ApplyQueryKitFilter(input);
98+
var recipes = await appliedQueryable.ToListAsync();
99+
100+
// Assert
101+
recipes.Count.Should().Be(1);
102+
recipes[0].Id.Should().Be(fakeRecipeOne.Id);
103+
}
104+
105+
[Fact]
106+
public async Task can_filter_by_string_for_collection_contains()
107+
{
108+
// Arrange
109+
var testingServiceScope = new TestingServiceScope();
110+
var faker = new Faker();
111+
var fakeIngredientOne = new FakeIngredientBuilder()
112+
.WithName($"{faker.Lorem.Sentence()}partial")
113+
.Build();
114+
var fakeRecipeOne = new FakeRecipeBuilder().Build();
115+
fakeRecipeOne.AddIngredient(fakeIngredientOne);
116+
117+
var fakeIngredientTwo = new FakeIngredientBuilder()
118+
.WithName(faker.Lorem.Sentence())
119+
.Build();
120+
var fakeRecipeTwo = new FakeRecipeBuilder().Build();
121+
fakeRecipeTwo.AddIngredient(fakeIngredientTwo);
122+
await testingServiceScope.InsertAsync(fakeRecipeOne, fakeRecipeTwo);
123+
124+
var input = $"""Ingredients.Name @= "partial" """;
125+
126+
// Act
127+
var queryableRecipes = testingServiceScope.DbContext().Recipes;
128+
var appliedQueryable = queryableRecipes.ApplyQueryKitFilter(input);
129+
var recipes = await appliedQueryable.ToListAsync();
130+
131+
// Assert
132+
recipes.Count.Should().Be(1);
133+
recipes[0].Id.Should().Be(fakeRecipeOne.Id);
134+
}
135+
136+
[Fact]
137+
public async Task can_filter_by_string_for_collection_does_not_contain()
138+
{
139+
// Arrange
140+
var testingServiceScope = new TestingServiceScope();
141+
var faker = new Faker();
142+
var fakeIngredientOne = new FakeIngredientBuilder()
143+
.WithName($"{faker.Lorem.Sentence()}partial")
144+
.Build();
145+
var fakeRecipeOne = new FakeRecipeBuilder().Build();
146+
fakeRecipeOne.AddIngredient(fakeIngredientOne);
147+
148+
var fakeIngredientTwo = new FakeIngredientBuilder()
149+
.WithName(faker.Lorem.Sentence())
150+
.Build();
151+
var fakeRecipeTwo = new FakeRecipeBuilder().Build();
152+
fakeRecipeTwo.AddIngredient(fakeIngredientTwo);
153+
await testingServiceScope.InsertAsync(fakeRecipeOne, fakeRecipeTwo);
154+
155+
var input = $"""Ingredients.Name !@= "partial" """;
156+
157+
// Act
158+
var queryableRecipes = testingServiceScope.DbContext().Recipes;
159+
var appliedQueryable = queryableRecipes.ApplyQueryKitFilter(input);
160+
var recipes = await appliedQueryable.ToListAsync();
161+
162+
// Assert
163+
recipes.FirstOrDefault(x => x.Id == fakeRecipeOne.Id).Should().BeNull();
164+
recipes.FirstOrDefault(x => x.Id == fakeRecipeTwo.Id).Should().NotBeNull();
165+
}
166+
167+
[Fact]
168+
public async Task can_use_soundex_equals()
169+
{
170+
// Arrange
171+
var testingServiceScope = new TestingServiceScope();
172+
173+
var fakePersonOne = new FakeTestingPersonBuilder()
174+
.WithTitle("DeVito")
175+
.Build();
176+
var fakePersonTwo = new FakeTestingPersonBuilder().Build();
177+
await testingServiceScope.InsertAsync(fakePersonOne, fakePersonTwo);
178+
179+
var input = $"""{nameof(TestingPerson.Title)} ~~ "davito" """;
180+
181+
// Act
182+
var queryablePeople = testingServiceScope.DbContext().People;
183+
var appliedQueryable = queryablePeople.ApplyQueryKitFilter(input, new QueryKitConfiguration(o =>
184+
{
185+
o.DbContextType = typeof(TestingDbContext);
186+
}));
187+
var people = await appliedQueryable.ToListAsync();
188+
189+
// Assert
190+
people.Count.Should().Be(1);
191+
people[0].Id.Should().Be(fakePersonOne.Id);
192+
}
193+
194+
[Fact]
195+
public async Task can_use_soundex_not_equals()
196+
{
197+
// Arrange
198+
var testingServiceScope = new TestingServiceScope();
199+
200+
var fakePersonOne = new FakeTestingPersonBuilder()
201+
.WithTitle("Jaime")
202+
.Build();
203+
var fakePersonTwo = new FakeTestingPersonBuilder().Build();
204+
await testingServiceScope.InsertAsync(fakePersonOne, fakePersonTwo);
205+
206+
var input = $"""{nameof(TestingPerson.Title)} !~ "jaymee" """;
207+
208+
// Act
209+
var queryablePeople = testingServiceScope.DbContext().People;
210+
var appliedQueryable = queryablePeople.ApplyQueryKitFilter(input, new QueryKitConfiguration(o =>
211+
{
212+
o.DbContextType = typeof(TestingDbContext);
213+
}));
214+
var people = await appliedQueryable.ToListAsync();
215+
216+
// Assert
217+
people.Count(x => x.Id == fakePersonOne.Id).Should().Be(0);
218+
}
219+
41220
[Fact]
42221
public async Task can_filter_by_datetime_with_milliseconds()
43222
{

0 commit comments

Comments
 (0)