Skip to content

Commit 0073322

Browse files
committed
Add ComparisonOperator tests (unit, SQL generation, SQLite integration)
- ComparisonOperatorTests: Like() and ToExpression() unit tests - ComparisonOperatorSqlTests: SQL generation via Query.CreateBuildCondition - ComparisonOperatorIntegrationTests: SQLite integration tests for all operators with int, string, decimal, null, multiple filters, update, and delete
1 parent 583bdf3 commit 0073322

File tree

3 files changed

+828
-0
lines changed

3 files changed

+828
-0
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
namespace Ecng.Tests.ComponentModel;
2+
3+
using Ecng.ComponentModel;
4+
5+
[TestClass]
6+
public class ComparisonOperatorTests : BaseTestClass
7+
{
8+
// ─── Like(value, pattern, operator) ───
9+
10+
[TestMethod]
11+
public void Like_Null_Contains()
12+
{
13+
"hello world".Like("world", null).AssertTrue();
14+
"hello world".Like("xyz", null).AssertFalse();
15+
}
16+
17+
[TestMethod]
18+
public void Like_In_Contains()
19+
{
20+
"hello world".Like("world", ComparisonOperator.In).AssertTrue();
21+
"hello world".Like("xyz", ComparisonOperator.In).AssertFalse();
22+
}
23+
24+
[TestMethod]
25+
public void Like_Greater_StartsWith()
26+
{
27+
"hello world".Like("hello", ComparisonOperator.Greater).AssertTrue();
28+
"hello world".Like("world", ComparisonOperator.Greater).AssertFalse();
29+
}
30+
31+
[TestMethod]
32+
public void Like_GreaterOrEqual_StartsWith()
33+
{
34+
"hello world".Like("hello", ComparisonOperator.GreaterOrEqual).AssertTrue();
35+
"hello world".Like("world", ComparisonOperator.GreaterOrEqual).AssertFalse();
36+
}
37+
38+
[TestMethod]
39+
public void Like_Less_EndsWith()
40+
{
41+
"hello world".Like("world", ComparisonOperator.Less).AssertTrue();
42+
"hello world".Like("hello", ComparisonOperator.Less).AssertFalse();
43+
}
44+
45+
[TestMethod]
46+
public void Like_LessOrEqual_EndsWith()
47+
{
48+
"hello world".Like("world", ComparisonOperator.LessOrEqual).AssertTrue();
49+
"hello world".Like("hello", ComparisonOperator.LessOrEqual).AssertFalse();
50+
}
51+
52+
[TestMethod]
53+
public void Like_Equal_Exact()
54+
{
55+
"hello".Like("hello", ComparisonOperator.Equal).AssertTrue();
56+
"hello".Like("HELLO", ComparisonOperator.Equal).AssertTrue();
57+
"hello".Like("hell", ComparisonOperator.Equal).AssertFalse();
58+
}
59+
60+
[TestMethod]
61+
public void Like_NotEqual_NotContains()
62+
{
63+
"hello world".Like("xyz", ComparisonOperator.NotEqual).AssertTrue();
64+
"hello world".Like("world", ComparisonOperator.NotEqual).AssertFalse();
65+
}
66+
67+
[TestMethod]
68+
public void Like_Like_ShouldContain()
69+
{
70+
// Like operator should work like Contains (same as In)
71+
"hello world".Like("world", ComparisonOperator.Like).AssertTrue();
72+
"hello world".Like("xyz", ComparisonOperator.Like).AssertFalse();
73+
}
74+
75+
[TestMethod]
76+
public void Like_Any_AlwaysTrue()
77+
{
78+
// Any operator means any value matches
79+
"hello".Like("xyz", ComparisonOperator.Any).AssertTrue();
80+
"".Like("xyz", ComparisonOperator.Any).AssertTrue();
81+
}
82+
83+
[TestMethod]
84+
public void Like_CaseInsensitive()
85+
{
86+
"Hello World".Like("hello", ComparisonOperator.In).AssertTrue();
87+
"Hello World".Like("HELLO", ComparisonOperator.Greater).AssertTrue();
88+
"Hello World".Like("WORLD", ComparisonOperator.Less).AssertTrue();
89+
}
90+
91+
[TestMethod]
92+
public void Like_EmptyPattern_ReturnsTrue()
93+
{
94+
"anything".Like("", null).AssertTrue();
95+
"anything".Like(null, null).AssertTrue();
96+
}
97+
98+
[TestMethod]
99+
public void Like_NullValue_Throws()
100+
{
101+
Throws<ArgumentNullException>(() => ((string)null).Like("test", null));
102+
}
103+
104+
// ─── ToExpression(pattern, operator) ───
105+
106+
[TestMethod]
107+
public void ToExpression_Null_Contains()
108+
{
109+
"test".ToExpression(null).AssertEqual("%test%");
110+
}
111+
112+
[TestMethod]
113+
public void ToExpression_In_Contains()
114+
{
115+
"test".ToExpression(ComparisonOperator.In).AssertEqual("%test%");
116+
}
117+
118+
[TestMethod]
119+
public void ToExpression_Like_Contains()
120+
{
121+
// Like operator wraps with % (same as In - contains)
122+
"test".ToExpression(ComparisonOperator.Like).AssertEqual("%test%");
123+
}
124+
125+
[TestMethod]
126+
public void ToExpression_Greater_StartsWith()
127+
{
128+
"test".ToExpression(ComparisonOperator.Greater).AssertEqual("test%");
129+
}
130+
131+
[TestMethod]
132+
public void ToExpression_GreaterOrEqual_StartsWith()
133+
{
134+
"test".ToExpression(ComparisonOperator.GreaterOrEqual).AssertEqual("test%");
135+
}
136+
137+
[TestMethod]
138+
public void ToExpression_Less_EndsWith()
139+
{
140+
"test".ToExpression(ComparisonOperator.Less).AssertEqual("%test");
141+
}
142+
143+
[TestMethod]
144+
public void ToExpression_LessOrEqual_EndsWith()
145+
{
146+
"test".ToExpression(ComparisonOperator.LessOrEqual).AssertEqual("%test");
147+
}
148+
149+
[TestMethod]
150+
public void ToExpression_Equal_Exact()
151+
{
152+
"test".ToExpression(ComparisonOperator.Equal).AssertEqual("test");
153+
}
154+
155+
[TestMethod]
156+
public void ToExpression_NotEqual_ThrowsNotSupported()
157+
{
158+
Throws<NotSupportedException>(() => "test".ToExpression(ComparisonOperator.NotEqual));
159+
}
160+
161+
[TestMethod]
162+
public void ToExpression_Any_MatchAll()
163+
{
164+
// Any means match everything - should produce '%' wildcard
165+
"test".ToExpression(ComparisonOperator.Any).AssertEqual("%");
166+
}
167+
168+
[TestMethod]
169+
public void ToExpression_Empty_Throws()
170+
{
171+
Throws<ArgumentNullException>(() => "".ToExpression(null));
172+
}
173+
174+
[TestMethod]
175+
public void ToExpression_Default_SameAsNull()
176+
{
177+
"test".ToExpression(default).AssertEqual("test".ToExpression(null));
178+
}
179+
}

0 commit comments

Comments
 (0)