forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryByExampleTest.cs
190 lines (172 loc) · 5 KB
/
QueryByExampleTest.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections;
using NHibernate.Criterion;
using NHibernate.DomainModel;
using NUnit.Framework;
namespace NHibernate.Test.ExpressionTest
{
[TestFixture]
public class QueryByExampleTest : TestCase
{
protected override string[] Mappings
{
get { return new string[] {"Componentizable.hbm.xml"}; }
}
protected override void OnSetUp()
{
InitData();
}
protected override void OnTearDown()
{
DeleteData();
}
[Test]
public void TestSimpleQBE()
{
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
Componentizable master = GetMaster("hibernate", null, "ope%");
ICriteria crit = s.CreateCriteria(typeof(Componentizable));
Example ex = Example.Create(master).EnableLike();
crit.Add(ex);
IList result = crit.List();
Assert.IsNotNull(result);
Assert.AreEqual(1, result.Count);
t.Commit();
}
}
[Test]
public void TestEnableLikeWithMatchmodeStart() {
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction()) {
Componentizable master = GetMaster("hib", null, "open source1");
ICriteria crit = s.CreateCriteria(typeof(Componentizable));
Example ex = Example.Create(master).EnableLike(MatchMode.Start);
crit.Add(ex);
IList result = crit.List();
Assert.IsNotNull(result);
Assert.AreEqual(1, result.Count);
t.Commit();
}
}
[Test]
public void TestEnableLikeWithMatchmodeEnd() {
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction()) {
Componentizable master = GetMaster("nate", null, "ORM tool1");
ICriteria crit = s.CreateCriteria(typeof(Componentizable));
Example ex = Example.Create(master).EnableLike(MatchMode.End);
crit.Add(ex);
IList result = crit.List();
Assert.IsNotNull(result);
Assert.AreEqual(1, result.Count);
t.Commit();
}
}
[Test]
public void TestEnableLikeWithMatchmodeAnywhere() {
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction()) {
Componentizable master = GetMaster("bern", null, null);
ICriteria crit = s.CreateCriteria(typeof(Componentizable));
Example ex = Example.Create(master).EnableLike(MatchMode.Anywhere);
crit.Add(ex);
IList result = crit.List();
Assert.IsNotNull(result);
Assert.AreEqual(3, result.Count);
t.Commit();
}
}
[Test]
public void TestJunctionNotExpressionQBE()
{
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
Componentizable master = GetMaster("hibernate", null, "ope%");
ICriteria crit = s.CreateCriteria(typeof(Componentizable));
Example ex = Example.Create(master).EnableLike();
crit.Add(Expression.Or(Expression.Not(ex), ex));
IList result = crit.List();
Assert.IsNotNull(result);
//if ( !(dialect is HSQLDialect - h2.1 test
Assert.AreEqual(2, result.Count, "expected 2 objects");
t.Commit();
}
}
[Test]
public void TestExcludingQBE()
{
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
Componentizable master = GetMaster("hibernate", null, "ope%");
ICriteria crit = s.CreateCriteria(typeof(Componentizable));
Example ex = Example.Create(master).EnableLike()
.ExcludeProperty("Component.SubComponent");
crit.Add(ex);
IList result = crit.List();
Assert.IsNotNull(result);
Assert.AreEqual(3, result.Count);
master = GetMaster("hibernate", "ORM tool", "fake stuff");
crit = s.CreateCriteria(typeof(Componentizable));
ex = Example.Create(master).EnableLike()
.ExcludeProperty("Component.SubComponent.SubName1");
crit.Add(ex);
result = crit.List();
Assert.IsNotNull(result);
Assert.AreEqual(1, result.Count);
t.Commit();
}
}
private void InitData()
{
using (ISession s = OpenSession())
{
Componentizable master = GetMaster("hibernate", "ORM tool", "ORM tool1");
s.Save(master);
s.Flush();
}
using (ISession s = OpenSession())
{
Componentizable master = GetMaster("hibernate", "open source", "open source1");
s.Save(master);
s.Flush();
}
using (ISession s = OpenSession())
{
Componentizable master = GetMaster("hibernate", null, null);
s.Save(master);
s.Flush();
}
}
private void DeleteData()
{
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
s.Delete("from Componentizable");
t.Commit();
}
}
private Componentizable GetMaster(String name, String subName, String subName1)
{
Componentizable master = new Componentizable();
if (name != null)
{
NHibernate.DomainModel.Component masterComp = new NHibernate.DomainModel.Component();
masterComp.Name = name;
if (subName != null || subName1 != null)
{
SubComponent subComponent = new SubComponent();
subComponent.SubName = subName;
subComponent.SubName1 = subName1;
masterComp.SubComponent = subComponent;
}
master.Component = masterComp;
}
return master;
}
}
}