0
|
1 using System;
|
|
2
|
|
3 using NUnit.Framework;
|
|
4
|
|
5 using BLToolkit.Data;
|
|
6 using BLToolkit.Data.DataProvider;
|
|
7 using BLToolkit.DataAccess;
|
|
8 using BLToolkit.Mapping;
|
|
9 using System.Linq;
|
|
10
|
|
11 namespace DataAccess
|
|
12 {
|
|
13 [TestFixture]
|
|
14 public class SqlTest
|
|
15 {
|
|
16 public class Name
|
|
17 {
|
|
18 public string LastName;
|
|
19 public string FirstName;
|
|
20 public string MiddleName;
|
|
21 }
|
|
22
|
|
23 public class Person
|
|
24 {
|
|
25 [MapField("PersonID"), PrimaryKey]
|
|
26 public int ID;
|
|
27 [MapField(Format="{0}")]
|
|
28 public Name Name = new Name();
|
|
29
|
|
30 [SqlIgnore]
|
|
31 public string Gender;
|
|
32 }
|
|
33
|
|
34 [Test]
|
|
35 public void Test()
|
|
36 {
|
|
37 var da = new SqlQuery();
|
|
38 var p = (Person)da.SelectByKey(typeof(Person), 1);
|
|
39
|
|
40 Assert.AreEqual("Pupkin", p.Name.LastName);
|
|
41 }
|
|
42
|
|
43 [Test]
|
|
44 public void GetFieldListTest()
|
|
45 {
|
|
46 var da = new SqlQuery();
|
|
47
|
|
48 using (var db = new DbManager())
|
|
49 {
|
|
50 var info = da.GetSqlQueryInfo(db, typeof (Person), "SelectAll");
|
|
51
|
|
52 Console.WriteLine(info.QueryText);
|
|
53 Assert.That(info.QueryText.Contains("\t" + db.DataProvider.Convert("PersonID", ConvertType.NameToQueryField)));
|
|
54 Assert.That(info.QueryText.Contains("\t" + db.DataProvider.Convert("LastName", ConvertType.NameToQueryField)));
|
|
55 Assert.That(info.QueryText, Is.Not.Contains("\t" + db.DataProvider.Convert("Name", ConvertType.NameToQueryField)));
|
|
56 }
|
|
57 }
|
|
58
|
|
59 [MapField("InnerId", "InnerObject.Id")]
|
|
60 public class TestObject
|
|
61 {
|
|
62 public int Id;
|
|
63 public TestObject InnerObject;
|
|
64 }
|
|
65
|
|
66 [Test]
|
|
67 public void RecursiveTest()
|
|
68 {
|
|
69 var query = new SqlQuery<TestObject>();
|
|
70 var info = query.GetSqlQueryInfo(new DbManager(), "SelectAll");
|
|
71
|
|
72 Console.WriteLine(info.QueryText);
|
|
73 Assert.That(info.QueryText.Contains("InnerId"));
|
|
74 Assert.That(info.QueryText, Is.Not.Contains("InnerObject"));
|
|
75 }
|
|
76
|
|
77 [Test]
|
|
78 public void SqlIgnoreAttributeTest()
|
|
79 {
|
|
80 var da = new SqlQuery();
|
|
81 var p = (Person)da.SelectByKey(typeof(Person), 1);
|
|
82
|
|
83 Assert.IsNull(p.Gender);
|
|
84 }
|
|
85
|
|
86 public class TestCategory
|
|
87 {
|
|
88 [PrimaryKey, NonUpdatable]
|
|
89 public int Id;
|
|
90 public string Name;
|
|
91 }
|
|
92
|
|
93 [MapField("CategoryId", "Category.Id")]
|
|
94 public class TestObject2
|
|
95 {
|
|
96 [PrimaryKey, NonUpdatable]
|
|
97 public int Id;
|
|
98 public TestCategory Category;
|
|
99 }
|
|
100
|
|
101 [Test]
|
|
102 public void NonUpdatableTest()
|
|
103 {
|
|
104 var da = new SqlQuery();
|
|
105
|
|
106 using (var db = new DbManager())
|
|
107 {
|
|
108 var update = da.GetSqlQueryInfo<TestCategory>(db, "Update");
|
|
109 var insert = da.GetSqlQueryInfo<TestCategory>(db, "Insert");
|
|
110
|
|
111 Assert.That(update.QueryText, Is.Not.Contains(
|
|
112 "\t" + db.DataProvider.Convert("Id", ConvertType.NameToQueryField) + " = " + db.DataProvider.Convert("Id", db.GetConvertTypeToParameter()) + "\n"),
|
|
113 "Update");
|
|
114 Assert.That(insert.QueryText, Is.Not.Contains("Id"), "Insert");
|
|
115 }
|
|
116 }
|
|
117
|
|
118 [Test]
|
|
119 public void ComplexMapperNonUpdatableTest()
|
|
120 {
|
|
121 var da = new SqlQuery();
|
|
122
|
|
123 using (var db = new DbManager())
|
|
124 {
|
|
125 var update = da.GetSqlQueryInfo<TestObject2>(db, "Update");
|
|
126 var insert = da.GetSqlQueryInfo<TestObject2>(db, "Insert");
|
|
127
|
|
128 Assert.That(update.QueryText.Contains("CategoryId"), "Update");
|
|
129 Assert.That(insert.QueryText.Contains("CategoryId"), "Insert");
|
|
130 }
|
|
131 }
|
|
132
|
|
133 [TableName("DataTypeTest")]
|
|
134 class UpdateTest
|
|
135 {
|
|
136 [PrimaryKey] public Guid Guid_;
|
|
137
|
|
138 public UpdateTest(Guid guid)
|
|
139 {
|
|
140 Guid_ = guid;
|
|
141 }
|
|
142 }
|
|
143
|
|
144 [Test, ExpectedException(typeof(DataAccessException))]
|
|
145 public void UpdateGuid()
|
|
146 {
|
|
147 new SqlQuery<UpdateTest>().Update(new UpdateTest(Guid.NewGuid()));
|
|
148 }
|
|
149
|
|
150 [TableName("Person")]
|
|
151 public class Person1
|
|
152 {
|
|
153 [Identity, PrimaryKey] public int PersonID;
|
|
154 public string FirstName;
|
|
155 public string LastName;
|
|
156 [NonUpdatable(OnInsert=false)] public string MiddleName;
|
|
157 public char Gender;
|
|
158 }
|
|
159
|
|
160 [Test]
|
|
161 public void NonUpdatableOnUpdate()
|
|
162 {
|
|
163 using (var db = new DbManager())
|
|
164 {
|
|
165 db.BeginTransaction();
|
|
166
|
|
167 var person = new Person1
|
|
168 {
|
|
169 FirstName = "TestOnInsert",
|
|
170 LastName = "",
|
|
171 MiddleName = "1",
|
|
172 Gender = 'M'
|
|
173 };
|
|
174
|
|
175 var sqlQuery = new SqlQuery<Person1>();
|
|
176
|
|
177 sqlQuery.Insert(db, person);
|
|
178
|
|
179 var p = db.GetTable<Person1>().Single(_ => _.FirstName == "TestOnInsert");
|
|
180
|
|
181 Assert.AreEqual(person.MiddleName, p.MiddleName);
|
|
182
|
|
183 person.PersonID = p.PersonID;
|
|
184 person.MiddleName = "should not be updated";
|
|
185
|
|
186 sqlQuery.Update(db, person);
|
|
187
|
|
188 p = db.GetTable<Person1>().Single(_ => _.FirstName == "TestOnInsert");
|
|
189
|
|
190 Assert.AreNotEqual(person.MiddleName, p.MiddleName);
|
|
191
|
|
192 db.RollbackTransaction();
|
|
193 }
|
|
194 }
|
|
195
|
|
196 [TableName("Person")]
|
|
197 [Identity(FieldName = "PersonID")]
|
|
198 [NonUpdatable(OnInsert = false, FieldName = "MiddleName")]
|
|
199 public class Person2
|
|
200 {
|
|
201 [PrimaryKey] public int PersonID;
|
|
202 [NonUpdatable(OnUpdate = false)] public string FirstName;
|
|
203 public string MiddleName;
|
|
204 public string Gender;
|
|
205 }
|
|
206
|
|
207 [Test]
|
|
208 public void NonUpdatableOnClass()
|
|
209 {
|
|
210 var da = new SqlQuery();
|
|
211
|
|
212 using (var db = new DbManager())
|
|
213 {
|
|
214 var update = da.GetSqlQueryInfo<Person2>(db, "Update");
|
|
215 var insert = da.GetSqlQueryInfo<Person2>(db, "Insert");
|
|
216
|
|
217 var personID = "\t" + db.DataProvider.Convert("PersonID", ConvertType.NameToQueryField).ToString();
|
|
218 var middleName = "\t" + db.DataProvider.Convert("MiddleName", ConvertType.NameToQueryField).ToString();
|
|
219 var firstName = "\t" + db.DataProvider.Convert("FirstName", ConvertType.NameToQueryField).ToString();
|
|
220
|
|
221 var personID_P = " = " + db.DataProvider.Convert("PersonID_P", ConvertType.NameToQueryParameter).ToString();
|
|
222 var middleName_P = " = " + db.DataProvider.Convert("MiddleName_P", ConvertType.NameToQueryParameter).ToString();
|
|
223 var firstName_P = " = " + db.DataProvider.Convert("FirstName_P", ConvertType.NameToQueryParameter).ToString();
|
|
224
|
|
225 Assert.That(update.QueryText, Is.Not.Contains(personID + personID_P), "personId\n" + update.QueryText);
|
|
226 Assert.That(update.QueryText, Is.Not.Contains(middleName + middleName_P), "middleName\n" + update.QueryText);
|
|
227 Assert.That(update.QueryText.Contains(firstName + firstName_P), "firstName\n" + update.QueryText);
|
|
228
|
|
229 Assert.That(insert.QueryText, Is.Not.Contains(personID), "personId\n" + insert.QueryText);
|
|
230 Assert.That(insert.QueryText, Is.Not.Contains(firstName), "firstName\n" + insert.QueryText);
|
|
231 Assert.That(insert.QueryText.Contains(middleName), "middleName\n" + insert.QueryText);
|
|
232 }
|
|
233 }
|
|
234 }
|
|
235 }
|