forked from koenbeuk/EntityFrameworkCore.Projectables
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Алексей Пыжов
committed
Apr 3, 2024
1 parent
f0c2359
commit 22e3ec0
Showing
7 changed files
with
159 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 1 addition & 6 deletions
7
...ntityFrameworkCore.Projectables.FunctionalTests/ExtensionsMethods/ExtensionMethodTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
tests/EntityFrameworkCore.Projectables.FunctionalTests/Helpers/SampleBodyParamDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System.Collections.Generic; | ||
using EntityFrameworkCore.Projectables.Infrastructure; | ||
using Microsoft.EntityFrameworkCore; | ||
using Order = EntityFrameworkCore.Projectables.FunctionalTests.MemberBodyParameterValueTests.Order; | ||
using OrderItem = EntityFrameworkCore.Projectables.FunctionalTests.MemberBodyParameterValueTests.OrderItem; | ||
|
||
namespace EntityFrameworkCore.Projectables.FunctionalTests.Helpers | ||
{ | ||
public class SampleBodyParamDbContext : DbContext | ||
{ | ||
readonly CompatibilityMode _compatibilityMode; | ||
|
||
public SampleBodyParamDbContext(CompatibilityMode compatibilityMode = CompatibilityMode.Full) | ||
{ | ||
_compatibilityMode = compatibilityMode; | ||
|
||
var _orders = new List<Order>() { | ||
new() { | ||
Id = 1, | ||
|
||
}, | ||
new() { | ||
Id = 2, | ||
|
||
} | ||
}; | ||
|
||
var _orders_items = new List<OrderItem>() { | ||
new() { | ||
Id = 1, | ||
OrderId = 1, | ||
Name = "Order_1" | ||
}, | ||
new() { | ||
Id = 2, | ||
OrderId = 1, | ||
Name = "Order_2" | ||
}, | ||
new() { | ||
Id = 3, | ||
OrderId = 2, | ||
Name = "Order_3" | ||
}, | ||
new() { | ||
Id = 4, | ||
OrderId = 2, | ||
Name = "Order_4" | ||
}, | ||
}; | ||
|
||
Order!.AddRange(_orders); | ||
OrderItem!.AddRange(_orders_items); | ||
SaveChanges(); | ||
} | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
optionsBuilder.UseInMemoryDatabase("TestDb"); | ||
optionsBuilder.UseProjectables(options => { | ||
options.CompatibilityMode(_compatibilityMode); // Needed by our ComplexModelTests | ||
}); | ||
} | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<Order>() | ||
.HasKey(x => x.Id); | ||
modelBuilder.Entity<OrderItem>().HasKey(x => x.Id); | ||
|
||
} | ||
|
||
public DbSet<Order>? Order { get; set; } | ||
public DbSet<OrderItem>? OrderItem { get; set; } | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
tests/EntityFrameworkCore.Projectables.FunctionalTests/MemberBodyParameterValueTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers; | ||
using Microsoft.EntityFrameworkCore; | ||
using Xunit; | ||
|
||
namespace EntityFrameworkCore.Projectables.FunctionalTests | ||
{ | ||
public class MemberBodyParameterValueTests | ||
{ | ||
public class Order | ||
{ | ||
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)] | ||
public int Id { get; set; } | ||
public List<OrderItem> OrderItem { get; set; } = new List<OrderItem>(); | ||
|
||
[Projectable(nameof(GetOrderItemNameExpression), 1)] | ||
public string FirstOrderPropName => GetOrderItemName(1); | ||
|
||
|
||
[Projectable(UseMemberBody = nameof(GetOrderItemNameInnerExpression))] | ||
public string GetOrderItemName(int id) | ||
=> OrderItem.Where(av => av.Id == id) | ||
.Select(av => av.Name) | ||
.FirstOrDefault() ?? throw new ArgumentException("Argument matching identifier not found"); | ||
|
||
private static Expression<Func<Order, int, string>> GetOrderItemNameInnerExpression() | ||
=> (@this, id) => @this.OrderItem | ||
.Where(av => av.Id == id) | ||
.Select(av => av.Name) | ||
.FirstOrDefault() ?? string.Empty; | ||
|
||
public static Expression<Func<Order, int, string>> GetOrderItemNameExpression | ||
=> (order, id) => order.GetOrderItemName(id); | ||
} | ||
|
||
public class OrderItem | ||
{ | ||
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)] | ||
public int Id { get; set; } | ||
public int OrderId { get; set; } | ||
public string Name { get; set; } = string.Empty; | ||
} | ||
|
||
[Fact] | ||
public void UseBodyParameterValue() | ||
{ | ||
//Arrange | ||
using var dbContext = new SampleBodyParamDbContext(); | ||
|
||
// Act | ||
var query = dbContext | ||
.Set<Order>() | ||
.Include(a => a.OrderItem) | ||
.FirstOrDefault(d => d.FirstOrderPropName == "Order_1"); | ||
|
||
// Assert | ||
Assert.NotNull(query); | ||
Assert.True(query!.FirstOrderPropName == "Order_1"); | ||
} | ||
} | ||
} |