Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
huysentruitw committed Dec 13, 2024
1 parent 3ff4716 commit b80c8ba
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -760,55 +760,33 @@ public async Task Project_Key_To_Collection_Expression_Integration()
}

[Fact]
public async Task Brand_Without_Country_Should_Allow_Nullable_Country()
public async Task Product_With_Nullable_Reference_Property_Set_To_Null()
{
// Arrange
var queries = new List<string>();
var connectionString = CreateConnectionString();

var services = new ServiceCollection()
.AddScoped(_ => queries)
.AddTransient(_ => new CatalogContext(connectionString))
.AddDataLoader(
sp => new ProductByBrandIdDataLoader2(
sp,
sp.GetRequiredService<List<string>>(),
sp.GetRequiredService<IBatchScheduler>(),
sp.GetRequiredService<DataLoaderOptions>()))
.BuildServiceProvider();

await using var catalogContext = services.GetRequiredService<CatalogContext>();
await catalogContext.Database.EnsureCreatedAsync();
catalogContext.Brands.Add(new Brand
{
Name = "Some brand",
Details = new BrandDetails { Country = null },
});
await catalogContext.SaveChangesAsync();
await CatalogContext.SeedAsync(connectionString);

// Act
var result = await new ServiceCollection()
.AddScoped(_ => queries)
.AddTransient(_ => new CatalogContext(connectionString))
.AddGraphQLServer()
.AddQueryType<BrandsQuery>()
.AddTypeExtension(typeof(BrandListExtensions))
.AddQueryType<ProductsWithNullBrandQuery>()
.ExecuteRequestAsync(
"""
{
brandById(id: 1) {
details {
country {
name
}
productById(id: 1) {
name
type {
name
}
}
}
""");

// Assert
Snapshot.Create()
.AddSql(queries)
.Add(result, "Result")
.MatchMarkdownSnapshot();
}
Expand Down Expand Up @@ -890,6 +868,30 @@ public async Task<IEnumerable<Brand>> GetBrandByIdAsync(
.ToListAsync(cancellationToken);
}

public class ProductsWithNullBrandQuery
{
public async Task<ProductProjection?> GetProductByIdAsync(
int id,
ISelection selection,
CatalogContext context,
CancellationToken cancellationToken)
=> await context.Products
.Where(p => p.Id == id)
.Select(p => new ProductProjection
{
Name = p.Name,
})
.Select(selection.AsSelector<ProductProjection>())
.SingleOrDefaultAsync(cancellationToken);

public class ProductProjection
{
public string Name { get; set; } = default!;

public ProductType? Type { get; set; }
}
}

[ExtendObjectType<Brand>]
public class BrandExtensions
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Product_With_Nullable_Reference_Property_Set_To_Null

```json
{
"data": {
"productById": {
"name": "Product 0-0",
"type": null
}
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Brand

public class BrandDetails
{
public Country? Country { get; set; }
public Country Country { get; set; } = default!;
}

public class Country
Expand Down

0 comments on commit b80c8ba

Please sign in to comment.