-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathDictionary.cs
128 lines (110 loc) · 5.08 KB
/
Dictionary.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using FluentAssertions;
using NFluent;
using Xunit;
namespace System.Linq.Dynamic.Core.Tests.MikArea
{
public class Dictionary
{
public class Customer
{
public string City { get; set; }
public Dictionary<string, Order> Orders { get; set; }
public IReadOnlyDictionary<string, Order> ReadOnlyOrders { get; set; }
public string CompanyName { get; set; }
public string Phone { get; set; }
}
public class Order
{
}
[Fact]
public void ReadOnlyDictionary_ContainsKey_1()
{
var orders = new ReadOnlyDictionary<string, Order>(new Dictionary<string, Order>
{
{ "TEST", new Order() }
});
var customers = new List<Customer>
{
new Customer { City = "abc", CompanyName = "ZZZ", ReadOnlyOrders = orders }
};
var data = customers.AsQueryable()
.Where("ReadOnlyOrders.ContainsKey(\"TEST\")")
.ToList();
data.Should().HaveCount(1);
}
[Fact]
public void Dictionary_ContainsKey_1()
{
var customers = new List<Customer>
{
new Customer { City = "ZZZ1", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer { City = "ZZZ2", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer { City = "ZZZ3", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() }
};
customers.ForEach(x => x.Orders.Add(x.City + "TEST", new Order()));
var data = customers.AsQueryable()
.Where("Orders.ContainsKey(\"ZZZ2TEST\")")
.OrderBy("CompanyName")
.Select("new(City as City, Phone)").ToDynamicList();
Check.That("ZZZ2").IsEqualTo((string)data.First().City);
}
[Fact]
public void Dictionary_ContainsKey_2()
{
var customers = new List<Customer>
{
new Customer { City = "ZZZ1", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer { City = "ZZZ2", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer { City = "ZZZ3", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() }
};
customers.ForEach(x => x.Orders.Add(x.City + "TEST", new Order()));
var data = customers.AsQueryable()
.Where("Orders.ContainsKey(it.City + \"TEST\")")
.OrderBy("City")
.Select("new(City as City, Phone)").ToDynamicList();
Check.That("ZZZ1").IsEqualTo((string)data.First().City);
Check.That(3).IsEqualTo(data.Count);
}
[Fact]
public void Dictionary_ContainsKey_3()
{
var customers = new List<Customer>
{
new Customer { City = "ZZZ1", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer { City = "ZZZ2", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer { City = "ZZZ3", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() }
};
customers.ForEach(x => x.Orders.Add(x.City + "TEST1", new Order()));
customers.ForEach(x => x.Orders.Add(x.City + "TEST2", new Order()));
var noDynamicList = customers
.Where(x => x.Orders.Skip(1).First().Key == (x.City + "TEST2"))
.OrderBy(x => x.City)
.ToList();
var data = customers.AsQueryable()
.Where("Orders.Skip(1).First().Key == (it.City + \"TEST2\")")
.OrderBy("City")
.Select("new(City as City, Phone)").ToDynamicList();
Check.That(3).IsEqualTo(noDynamicList.Count);
Check.That("ZZZ1").IsEqualTo((string)data.First().City);
Check.That(3).IsEqualTo(data.Count);
}
[Fact(Skip = "Fails sometimes in GitHub CI build")]
public void DynamicIndexCall() // https://github.com/zzzprojects/System.Linq.Dynamic.Core/issues/397
{
object CreateDicParameter(string name) => new Dictionary<string, object>
{
{ "Name", new Dictionary<string, object> { { "FirstName", name }, { "LastName", name + "Test" } } }
};
var parType = new Dictionary<string, object>().GetType();
var lambda = DynamicExpressionParser.ParseLambda(new[] { Expression.Parameter(parType, "item") },
typeof(object), "item.Name.FirstName + \"7\" + item.Name.LastName ").Compile();
var x1 = lambda.DynamicInvoke(CreateDicParameter("Julio"));
var x2 = lambda.DynamicInvoke(CreateDicParameter("John"));
Check.That(x1).IsEqualTo("Julio7JulioTest");
Check.That(x2).IsEqualTo("John7JohnTest");
}
}
}