|
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include <e32test.h> |
|
17 #include <bautils.h> |
|
18 #include <s32strm.h> |
|
19 #include <e32math.h> |
|
20 #include <sqldb.h> |
|
21 |
|
22 /////////////////////////////////////////////////////////////////////////////////////// |
|
23 |
|
24 static RFs TheFs; |
|
25 RTest TheTest(_L("t_sqlscalarfullselect test")); |
|
26 |
|
27 _LIT(KTestDir, "c:\\test\\"); |
|
28 _LIT(KTestDatabase1, "c:\\test\\t_sqlscalarfullselect.db"); |
|
29 |
|
30 /////////////////////////////////////////////////////////////////////////////////////// |
|
31 |
|
32 //Deletes all created test files. |
|
33 void DeleteTestFiles() |
|
34 { |
|
35 (void)RSqlDatabase::Delete(KTestDatabase1); |
|
36 } |
|
37 |
|
38 /////////////////////////////////////////////////////////////////////////////////////// |
|
39 /////////////////////////////////////////////////////////////////////////////////////// |
|
40 //Test macros and functions |
|
41 void Check1(TInt64 aValue, TInt aLine) |
|
42 { |
|
43 if(!aValue) |
|
44 { |
|
45 DeleteTestFiles(); |
|
46 RDebug::Print(_L("*** Line %d\r\n"), aLine); |
|
47 TheTest(EFalse, aLine); |
|
48 } |
|
49 } |
|
50 void Check2(TInt64 aValue, TInt64 aExpected, TInt aLine) |
|
51 { |
|
52 if(aValue != aExpected) |
|
53 { |
|
54 DeleteTestFiles(); |
|
55 RDebug::Print(_L("*** Line %d, Expected error: %d, got: %d\r\n"), aLine, aExpected, aValue); |
|
56 TheTest(EFalse, aLine); |
|
57 } |
|
58 } |
|
59 #define TEST(arg) ::Check1((arg), __LINE__) |
|
60 #define TEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__) |
|
61 |
|
62 /////////////////////////////////////////////////////////////////////////////////////// |
|
63 |
|
64 //Creates file session instance and the test directory |
|
65 void CreateTestEnv() |
|
66 { |
|
67 TInt err = TheFs.Connect(); |
|
68 TEST2(err, KErrNone); |
|
69 |
|
70 err = TheFs.MkDir(KTestDir); |
|
71 TEST(err == KErrNone || err == KErrAlreadyExists); |
|
72 } |
|
73 |
|
74 void CreateTestDbLC(RSqlDatabase& aDb) |
|
75 { |
|
76 CleanupClosePushL(aDb); |
|
77 TInt rc = aDb.Create(KTestDatabase1); |
|
78 TEST2(rc, KErrNone); |
|
79 rc = aDb.Exec(_L("CREATE TABLE A(F1 INTEGER, F2 INTEGER, F3 FLOAT, F4 TEXT, F5 BLOB)")); |
|
80 TEST(rc >= 0); |
|
81 rc = aDb.Exec(_L("INSERT INTO A(F1, F2, F3, F4, F5) VALUES(1, 10000000000, 2.54, 'NAME1234567890', NULL)")); |
|
82 TEST2(rc, 1); |
|
83 rc = aDb.Exec(_L("INSERT INTO A(F1, F2, F3, F4) VALUES(2, 200, -1.11, 'ADDRESS')")); |
|
84 TEST2(rc, 1); |
|
85 RSqlStatement stmt; |
|
86 CleanupClosePushL(stmt); |
|
87 rc = stmt.Prepare(aDb, _L("UPDATE A SET F5=:P WHERE F1 = 2")); |
|
88 TEST2(rc, KErrNone); |
|
89 RSqlParamWriteStream strm; |
|
90 CleanupClosePushL(strm); |
|
91 rc = strm.BindBinary(stmt, 0); |
|
92 TEST2(rc, KErrNone); |
|
93 for(TInt i=0;i<100;++i) |
|
94 { |
|
95 strm << static_cast <TUint8> (i); |
|
96 } |
|
97 strm.CommitL(); |
|
98 rc = stmt.Exec(); |
|
99 TEST2(rc, 1); |
|
100 CleanupStack::PopAndDestroy(&strm); |
|
101 CleanupStack::PopAndDestroy(&stmt); |
|
102 } |
|
103 |
|
104 void DestroyTestDb(RSqlDatabase& aDb) |
|
105 { |
|
106 CleanupStack::PopAndDestroy(&aDb); |
|
107 TInt err = RSqlDatabase::Delete(KTestDatabase1); |
|
108 TEST2(err, KErrNone); |
|
109 } |
|
110 |
|
111 /** |
|
112 @SYMTestCaseID SYSLIB-SQL-CT-1809 |
|
113 @SYMTestCaseDesc A test database is created, test table created in the database with integer, 64-bit |
|
114 integer, float, text and blob fields. |
|
115 Inserted two records. |
|
116 The test calls TSqlScalarFullSelectQuery functions in all possible |
|
117 "requested value type:real column type" combinations and checks the returned value. |
|
118 @SYMTestPriority High |
|
119 @SYMTestActions SQL, Scalar fullselect test. |
|
120 @SYMTestExpectedResults Test must not fail |
|
121 @SYMREQ REQ5792 |
|
122 REQ5793 |
|
123 */ |
|
124 template <class BUF> void ScalarFullSelectTestL() |
|
125 { |
|
126 //Create test database. |
|
127 RSqlDatabase db; |
|
128 CreateTestDbLC(db); |
|
129 TSqlScalarFullSelectQuery fullSelectQuery(db); |
|
130 |
|
131 BUF sql; |
|
132 |
|
133 /////////////////// tests with F1 column (integer column) /////////////////////////// |
|
134 _LIT(KSql1, "SELECT F1 FROM A WHERE F2 = 10000000000"); |
|
135 sql.Copy(KSql1); |
|
136 //Read F1 as integer. Expected value: 1. |
|
137 TInt valInt = fullSelectQuery.SelectIntL(sql); |
|
138 TEST2(valInt, 1); |
|
139 //Read F1 as 64-bit integer. Expected value: 1. |
|
140 TInt64 valInt64 = fullSelectQuery.SelectInt64L(sql); |
|
141 TEST2(valInt64, 1); |
|
142 //Read F1 as real. Expected value: 1.0. |
|
143 TReal valReal = fullSelectQuery.SelectRealL(sql); |
|
144 TEST(Abs(valReal - 1.0) < 0.0001); |
|
145 TBuf<10> valText; |
|
146 //Read F1 as text. Expected value: zero-length 16-bit descriptor. |
|
147 TInt err = fullSelectQuery.SelectTextL(sql, valText); |
|
148 TEST2(err, KErrNone); |
|
149 TEST2(valText.Length(), 0); |
|
150 //Read F1 as binary. Expected value: zero-length 8-bit descriptor. |
|
151 TBuf8<10> valBinary; |
|
152 err = fullSelectQuery.SelectBinaryL(sql, valBinary); |
|
153 TEST2(err, KErrNone); |
|
154 TEST2(valBinary.Length(), 0); |
|
155 /////////////////// tests with F2 column (64-bit integer column) /////////////////////////// |
|
156 _LIT(KSql2, "SELECT F2 FROM A WHERE F1 = 1"); |
|
157 sql.Copy(KSql2); |
|
158 //Read F2 as integer. Expected value: KMaxTInt. |
|
159 valInt = fullSelectQuery.SelectIntL(sql); |
|
160 TEST2(valInt, KMaxTInt); |
|
161 //Read F2 as 64-bit integer. Expected value: 10000000000 |
|
162 valInt64 = fullSelectQuery.SelectInt64L(sql); |
|
163 TEST2(valInt64, 10000000000LL); |
|
164 //Read F2 as real. Expected value: 10000000000.0 |
|
165 valReal = fullSelectQuery.SelectRealL(sql); |
|
166 TEST(Abs(valReal - 10000000000.0) < 0.0001); |
|
167 //Read F2 as text. Expected value: zero-length 16-bit descriptor. |
|
168 err = fullSelectQuery.SelectTextL(sql, valText); |
|
169 TEST2(err, KErrNone); |
|
170 TEST2(valText.Length(), 0); |
|
171 //Read F2 as binary. Expected value: zero-length 8-bit descriptor. |
|
172 err = fullSelectQuery.SelectBinaryL(sql, valBinary); |
|
173 TEST2(err, KErrNone); |
|
174 TEST2(valBinary.Length(), 0); |
|
175 /////////////////// tests with F3 column (real column) /////////////////////////// |
|
176 _LIT(KSql3, "SELECT F3 FROM A WHERE F1 = 1"); |
|
177 sql.Copy(KSql3); |
|
178 //Read F3 as integer. Expected value: 3. |
|
179 valInt = fullSelectQuery.SelectIntL(sql); |
|
180 TEST2(valInt, 3); |
|
181 //Read F3 as 64-bit integer. Expected value: 3. |
|
182 valInt64 = fullSelectQuery.SelectInt64L(sql); |
|
183 TEST2(valInt64, 3); |
|
184 //Read F3 as real. Expected value: 2.54. |
|
185 valReal = fullSelectQuery.SelectRealL(sql); |
|
186 TEST(Abs(valReal - 2.54) < 0.0001); |
|
187 //Read F3 as text. Expected value: zero-length 16-bit descriptor. |
|
188 err = fullSelectQuery.SelectTextL(sql, valText); |
|
189 TEST2(err, KErrNone); |
|
190 TEST2(valText.Length(), 0); |
|
191 //Read F3 as binary. Expected value: zero-length 8-bit descriptor. |
|
192 err = fullSelectQuery.SelectBinaryL(sql, valBinary); |
|
193 TEST2(err, KErrNone); |
|
194 TEST2(valBinary.Length(), 0); |
|
195 /////////////////// tests with F4 column (text column) /////////////////////////// |
|
196 _LIT(KSql4, "SELECT F4 FROM A WHERE F1 = 1"); |
|
197 sql.Copy(KSql4); |
|
198 //Read F4 as integer. Expected value: 0. |
|
199 valInt = fullSelectQuery.SelectIntL(sql); |
|
200 TEST2(valInt, 0); |
|
201 //Read F4 as 64-bit integer. Expected value: 0. |
|
202 valInt64 = fullSelectQuery.SelectInt64L(sql); |
|
203 TEST2(valInt64, 0); |
|
204 //Read F4 as real. Expected value: 0.0. |
|
205 valReal = fullSelectQuery.SelectRealL(sql); |
|
206 TEST(Abs(valReal - 0.0) < 0.0001); |
|
207 //Read F4 as text. Small buffer. Expected return code: positive value, which is the column length in characters. |
|
208 err = fullSelectQuery.SelectTextL(sql, valText); |
|
209 TEST(err > 0); |
|
210 TEST2(valText.Length(), 10); |
|
211 _LIT(KColText, "NAME123456"); |
|
212 TEST(valText == KColText()); |
|
213 //Read F4 as text. Big enough buffer. Expected error: KErrNone. |
|
214 TBuf<32> valText2; |
|
215 err = fullSelectQuery.SelectTextL(sql, valText2); |
|
216 TEST2(err, KErrNone); |
|
217 TEST2(valText2.Length(), 14); |
|
218 _LIT(KColText2, "NAME1234567890"); |
|
219 TEST(valText2 == KColText2()); |
|
220 //Read F4 as binary. Expected error: KErrNone. Zero-length 8-bit descriptor. |
|
221 err = fullSelectQuery.SelectBinaryL(sql, valBinary); |
|
222 TEST2(err, KErrNone); |
|
223 TEST2(valBinary.Length(), 0); |
|
224 /////////////////// tests with F5 column (binary column) /////////////////////////// |
|
225 _LIT(KSql5, "SELECT F5 FROM A WHERE F1 = 2"); |
|
226 sql.Copy(KSql5); |
|
227 //Read F5 as integer. Expected value: 0. |
|
228 valInt = fullSelectQuery.SelectIntL(sql); |
|
229 TEST2(valInt, 0); |
|
230 //Read F5 as 64-bit integer. Expected value: 0. |
|
231 valInt64 = fullSelectQuery.SelectInt64L(sql); |
|
232 TEST2(valInt64, 0); |
|
233 //Read F5 as real. Expected value: 0.0. |
|
234 valReal = fullSelectQuery.SelectRealL(sql); |
|
235 TEST(Abs(valReal - 0.0) < 0.0001); |
|
236 //Read F5 as text. Expected error: KErrNone. Zero-length 16-bit descriptor. |
|
237 err = fullSelectQuery.SelectTextL(sql, valText); |
|
238 TEST2(err, KErrNone); |
|
239 TEST2(valText.Length(), 0); |
|
240 //Read F5 as binary. Small buffer. Expected return code: positive value, which is the column length in bytes. |
|
241 err = fullSelectQuery.SelectBinaryL(sql, valBinary); |
|
242 TEST(err > 0); |
|
243 TEST2(valBinary.Length(), 10); |
|
244 TInt i; |
|
245 for(i=0;i<10;++i) |
|
246 { |
|
247 TEST(valBinary[i] == i); |
|
248 } |
|
249 //Read F5 as binary. Big enough buffer. Expected error: KErrNone. |
|
250 TBuf8<100> valBinary2; |
|
251 err = fullSelectQuery.SelectBinaryL(sql, valBinary2); |
|
252 TEST2(err, KErrNone); |
|
253 TEST2(valBinary2.Length(), 100); |
|
254 for(i=0;i<100;++i) |
|
255 { |
|
256 TEST(valBinary2[i] == i); |
|
257 } |
|
258 /////////////////// Text column value, small buffer, reallocation /////////////////// |
|
259 HBufC* hbuf = HBufC::NewLC(10); |
|
260 TPtr name = hbuf->Des(); |
|
261 sql.Copy(KSql4); |
|
262 err = fullSelectQuery.SelectTextL(sql, name); |
|
263 TEST(err >= 0); //the function may return only non-negative values |
|
264 if(err > 0) |
|
265 { |
|
266 hbuf = hbuf->ReAllocL(err); |
|
267 CleanupStack::Pop(); |
|
268 CleanupStack::PushL(hbuf); |
|
269 name.Set(hbuf->Des()); |
|
270 err = fullSelectQuery.SelectTextL(sql, name); |
|
271 TEST2(err, KErrNone); |
|
272 TEST(name == KColText2()); |
|
273 } |
|
274 CleanupStack::PopAndDestroy();//hbuf, can't be put as parameter, because may be reallocated |
|
275 //Close database, delete database file. |
|
276 DestroyTestDb(db); |
|
277 } |
|
278 |
|
279 /** |
|
280 @SYMTestCaseID SYSLIB-SQL-CT-1810 |
|
281 @SYMTestCaseDesc A test database is created, test table created in the database with integer, 64-bit |
|
282 integer, float, text and blob fields. |
|
283 Inserted two records. |
|
284 The test calls TSqlScalarFullSelectQuery functions using inappropriate SQL statements: |
|
285 SELECT sql statement with parameters, INSERT sql statement, SELECT sql statement, |
|
286 which does not return records. |
|
287 @SYMTestPriority High |
|
288 @SYMTestActions SQL, Scalar fullselect test. |
|
289 @SYMTestExpectedResults Test must not fail |
|
290 @SYMREQ REQ5792 |
|
291 REQ5793 |
|
292 */ |
|
293 template <class BUF> void ScalarFullSelectNegativeTestL() |
|
294 { |
|
295 //Create test database. |
|
296 RSqlDatabase db; |
|
297 CreateTestDbLC(db); |
|
298 TSqlScalarFullSelectQuery fullSelectQuery(db); |
|
299 |
|
300 BUF sql; |
|
301 |
|
302 //An attempt to use inappropriate SQL - 1. |
|
303 _LIT(KSql1, "SELECT F1 FROM A WHERE F2 = :P"); |
|
304 sql.Copy(KSql1); |
|
305 TRAPD(err, fullSelectQuery.SelectIntL(sql)); |
|
306 TEST2(err, KErrArgument); |
|
307 |
|
308 //An attempt to use inappropriate SQL - 2. |
|
309 _LIT(KSql2, "INSERT INTO A(F1) VALUES(2)"); |
|
310 sql.Copy(KSql2); |
|
311 TRAP(err, fullSelectQuery.SelectIntL(sql)); |
|
312 TEST2(err, KErrArgument); |
|
313 |
|
314 //The SQL statement does not return any rows |
|
315 _LIT(KSql3, "SELECT F1 FROM A WHERE F2 = 456231"); |
|
316 sql.Copy(KSql3); |
|
317 TRAP(err, fullSelectQuery.SelectIntL(sql)); |
|
318 TEST2(err, KErrNotFound); |
|
319 |
|
320 //Close database, delete database file. |
|
321 DestroyTestDb(db); |
|
322 } |
|
323 |
|
324 void DoTestsL() |
|
325 { |
|
326 TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1809 Scalar fullselect test. 16-bit SQL ")); |
|
327 ScalarFullSelectTestL< TBuf16<100> >(); |
|
328 |
|
329 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1809 Scalar fullselect test. 8-bit SQL ")); |
|
330 ScalarFullSelectTestL< TBuf8<100> >(); |
|
331 |
|
332 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1810 Scalar fullselect - negative test. 16-bit SQL ")); |
|
333 ScalarFullSelectNegativeTestL< TBuf16<100> >(); |
|
334 |
|
335 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1810 Scalar fullselect - negative test. 8-bit SQL ")); |
|
336 ScalarFullSelectNegativeTestL< TBuf8<100> >(); |
|
337 } |
|
338 |
|
339 TInt E32Main() |
|
340 { |
|
341 TheTest.Title(); |
|
342 |
|
343 CTrapCleanup* tc = CTrapCleanup::New(); |
|
344 |
|
345 __UHEAP_MARK; |
|
346 |
|
347 CreateTestEnv(); |
|
348 DeleteTestFiles(); |
|
349 TRAPD(err, DoTestsL()); |
|
350 DeleteTestFiles(); |
|
351 TheFs.Close(); |
|
352 TEST2(err, KErrNone); |
|
353 |
|
354 __UHEAP_MARKEND; |
|
355 |
|
356 TheTest.End(); |
|
357 TheTest.Close(); |
|
358 |
|
359 delete tc; |
|
360 |
|
361 User::Heap().Check(); |
|
362 return KErrNone; |
|
363 } |