sl@0: // Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: static RFs TheFs; sl@0: RTest TheTest(_L("t_sqlscalarfullselect test")); sl@0: sl@0: _LIT(KTestDir, "c:\\test\\"); sl@0: _LIT(KTestDatabase1, "c:\\test\\t_sqlscalarfullselect.db"); sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: //Deletes all created test files. sl@0: void DeleteTestFiles() sl@0: { sl@0: (void)RSqlDatabase::Delete(KTestDatabase1); sl@0: } sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////////////// sl@0: /////////////////////////////////////////////////////////////////////////////////////// sl@0: //Test macros and functions sl@0: void Check1(TInt64 aValue, TInt aLine) sl@0: { sl@0: if(!aValue) sl@0: { sl@0: DeleteTestFiles(); sl@0: RDebug::Print(_L("*** Line %d\r\n"), aLine); sl@0: TheTest(EFalse, aLine); sl@0: } sl@0: } sl@0: void Check2(TInt64 aValue, TInt64 aExpected, TInt aLine) sl@0: { sl@0: if(aValue != aExpected) sl@0: { sl@0: DeleteTestFiles(); sl@0: RDebug::Print(_L("*** Line %d, Expected error: %d, got: %d\r\n"), aLine, aExpected, aValue); sl@0: TheTest(EFalse, aLine); sl@0: } sl@0: } sl@0: #define TEST(arg) ::Check1((arg), __LINE__) sl@0: #define TEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__) sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: //Creates file session instance and the test directory sl@0: void CreateTestEnv() sl@0: { sl@0: TInt err = TheFs.Connect(); sl@0: TEST2(err, KErrNone); sl@0: sl@0: err = TheFs.MkDir(KTestDir); sl@0: TEST(err == KErrNone || err == KErrAlreadyExists); sl@0: } sl@0: sl@0: void CreateTestDbLC(RSqlDatabase& aDb) sl@0: { sl@0: CleanupClosePushL(aDb); sl@0: TInt rc = aDb.Create(KTestDatabase1); sl@0: TEST2(rc, KErrNone); sl@0: rc = aDb.Exec(_L("CREATE TABLE A(F1 INTEGER, F2 INTEGER, F3 FLOAT, F4 TEXT, F5 BLOB)")); sl@0: TEST(rc >= 0); sl@0: rc = aDb.Exec(_L("INSERT INTO A(F1, F2, F3, F4, F5) VALUES(1, 10000000000, 2.54, 'NAME1234567890', NULL)")); sl@0: TEST2(rc, 1); sl@0: rc = aDb.Exec(_L("INSERT INTO A(F1, F2, F3, F4) VALUES(2, 200, -1.11, 'ADDRESS')")); sl@0: TEST2(rc, 1); sl@0: RSqlStatement stmt; sl@0: CleanupClosePushL(stmt); sl@0: rc = stmt.Prepare(aDb, _L("UPDATE A SET F5=:P WHERE F1 = 2")); sl@0: TEST2(rc, KErrNone); sl@0: RSqlParamWriteStream strm; sl@0: CleanupClosePushL(strm); sl@0: rc = strm.BindBinary(stmt, 0); sl@0: TEST2(rc, KErrNone); sl@0: for(TInt i=0;i<100;++i) sl@0: { sl@0: strm << static_cast (i); sl@0: } sl@0: strm.CommitL(); sl@0: rc = stmt.Exec(); sl@0: TEST2(rc, 1); sl@0: CleanupStack::PopAndDestroy(&strm); sl@0: CleanupStack::PopAndDestroy(&stmt); sl@0: } sl@0: sl@0: void DestroyTestDb(RSqlDatabase& aDb) sl@0: { sl@0: CleanupStack::PopAndDestroy(&aDb); sl@0: TInt err = RSqlDatabase::Delete(KTestDatabase1); sl@0: TEST2(err, KErrNone); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-SQL-CT-1809 sl@0: @SYMTestCaseDesc A test database is created, test table created in the database with integer, 64-bit sl@0: integer, float, text and blob fields. sl@0: Inserted two records. sl@0: The test calls TSqlScalarFullSelectQuery functions in all possible sl@0: "requested value type:real column type" combinations and checks the returned value. sl@0: @SYMTestPriority High sl@0: @SYMTestActions SQL, Scalar fullselect test. sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMREQ REQ5792 sl@0: REQ5793 sl@0: */ sl@0: template void ScalarFullSelectTestL() sl@0: { sl@0: //Create test database. sl@0: RSqlDatabase db; sl@0: CreateTestDbLC(db); sl@0: TSqlScalarFullSelectQuery fullSelectQuery(db); sl@0: sl@0: BUF sql; sl@0: sl@0: /////////////////// tests with F1 column (integer column) /////////////////////////// sl@0: _LIT(KSql1, "SELECT F1 FROM A WHERE F2 = 10000000000"); sl@0: sql.Copy(KSql1); sl@0: //Read F1 as integer. Expected value: 1. sl@0: TInt valInt = fullSelectQuery.SelectIntL(sql); sl@0: TEST2(valInt, 1); sl@0: //Read F1 as 64-bit integer. Expected value: 1. sl@0: TInt64 valInt64 = fullSelectQuery.SelectInt64L(sql); sl@0: TEST2(valInt64, 1); sl@0: //Read F1 as real. Expected value: 1.0. sl@0: TReal valReal = fullSelectQuery.SelectRealL(sql); sl@0: TEST(Abs(valReal - 1.0) < 0.0001); sl@0: TBuf<10> valText; sl@0: //Read F1 as text. Expected value: zero-length 16-bit descriptor. sl@0: TInt err = fullSelectQuery.SelectTextL(sql, valText); sl@0: TEST2(err, KErrNone); sl@0: TEST2(valText.Length(), 0); sl@0: //Read F1 as binary. Expected value: zero-length 8-bit descriptor. sl@0: TBuf8<10> valBinary; sl@0: err = fullSelectQuery.SelectBinaryL(sql, valBinary); sl@0: TEST2(err, KErrNone); sl@0: TEST2(valBinary.Length(), 0); sl@0: /////////////////// tests with F2 column (64-bit integer column) /////////////////////////// sl@0: _LIT(KSql2, "SELECT F2 FROM A WHERE F1 = 1"); sl@0: sql.Copy(KSql2); sl@0: //Read F2 as integer. Expected value: KMaxTInt. sl@0: valInt = fullSelectQuery.SelectIntL(sql); sl@0: TEST2(valInt, KMaxTInt); sl@0: //Read F2 as 64-bit integer. Expected value: 10000000000 sl@0: valInt64 = fullSelectQuery.SelectInt64L(sql); sl@0: TEST2(valInt64, 10000000000LL); sl@0: //Read F2 as real. Expected value: 10000000000.0 sl@0: valReal = fullSelectQuery.SelectRealL(sql); sl@0: TEST(Abs(valReal - 10000000000.0) < 0.0001); sl@0: //Read F2 as text. Expected value: zero-length 16-bit descriptor. sl@0: err = fullSelectQuery.SelectTextL(sql, valText); sl@0: TEST2(err, KErrNone); sl@0: TEST2(valText.Length(), 0); sl@0: //Read F2 as binary. Expected value: zero-length 8-bit descriptor. sl@0: err = fullSelectQuery.SelectBinaryL(sql, valBinary); sl@0: TEST2(err, KErrNone); sl@0: TEST2(valBinary.Length(), 0); sl@0: /////////////////// tests with F3 column (real column) /////////////////////////// sl@0: _LIT(KSql3, "SELECT F3 FROM A WHERE F1 = 1"); sl@0: sql.Copy(KSql3); sl@0: //Read F3 as integer. Expected value: 3. sl@0: valInt = fullSelectQuery.SelectIntL(sql); sl@0: TEST2(valInt, 3); sl@0: //Read F3 as 64-bit integer. Expected value: 3. sl@0: valInt64 = fullSelectQuery.SelectInt64L(sql); sl@0: TEST2(valInt64, 3); sl@0: //Read F3 as real. Expected value: 2.54. sl@0: valReal = fullSelectQuery.SelectRealL(sql); sl@0: TEST(Abs(valReal - 2.54) < 0.0001); sl@0: //Read F3 as text. Expected value: zero-length 16-bit descriptor. sl@0: err = fullSelectQuery.SelectTextL(sql, valText); sl@0: TEST2(err, KErrNone); sl@0: TEST2(valText.Length(), 0); sl@0: //Read F3 as binary. Expected value: zero-length 8-bit descriptor. sl@0: err = fullSelectQuery.SelectBinaryL(sql, valBinary); sl@0: TEST2(err, KErrNone); sl@0: TEST2(valBinary.Length(), 0); sl@0: /////////////////// tests with F4 column (text column) /////////////////////////// sl@0: _LIT(KSql4, "SELECT F4 FROM A WHERE F1 = 1"); sl@0: sql.Copy(KSql4); sl@0: //Read F4 as integer. Expected value: 0. sl@0: valInt = fullSelectQuery.SelectIntL(sql); sl@0: TEST2(valInt, 0); sl@0: //Read F4 as 64-bit integer. Expected value: 0. sl@0: valInt64 = fullSelectQuery.SelectInt64L(sql); sl@0: TEST2(valInt64, 0); sl@0: //Read F4 as real. Expected value: 0.0. sl@0: valReal = fullSelectQuery.SelectRealL(sql); sl@0: TEST(Abs(valReal - 0.0) < 0.0001); sl@0: //Read F4 as text. Small buffer. Expected return code: positive value, which is the column length in characters. sl@0: err = fullSelectQuery.SelectTextL(sql, valText); sl@0: TEST(err > 0); sl@0: TEST2(valText.Length(), 10); sl@0: _LIT(KColText, "NAME123456"); sl@0: TEST(valText == KColText()); sl@0: //Read F4 as text. Big enough buffer. Expected error: KErrNone. sl@0: TBuf<32> valText2; sl@0: err = fullSelectQuery.SelectTextL(sql, valText2); sl@0: TEST2(err, KErrNone); sl@0: TEST2(valText2.Length(), 14); sl@0: _LIT(KColText2, "NAME1234567890"); sl@0: TEST(valText2 == KColText2()); sl@0: //Read F4 as binary. Expected error: KErrNone. Zero-length 8-bit descriptor. sl@0: err = fullSelectQuery.SelectBinaryL(sql, valBinary); sl@0: TEST2(err, KErrNone); sl@0: TEST2(valBinary.Length(), 0); sl@0: /////////////////// tests with F5 column (binary column) /////////////////////////// sl@0: _LIT(KSql5, "SELECT F5 FROM A WHERE F1 = 2"); sl@0: sql.Copy(KSql5); sl@0: //Read F5 as integer. Expected value: 0. sl@0: valInt = fullSelectQuery.SelectIntL(sql); sl@0: TEST2(valInt, 0); sl@0: //Read F5 as 64-bit integer. Expected value: 0. sl@0: valInt64 = fullSelectQuery.SelectInt64L(sql); sl@0: TEST2(valInt64, 0); sl@0: //Read F5 as real. Expected value: 0.0. sl@0: valReal = fullSelectQuery.SelectRealL(sql); sl@0: TEST(Abs(valReal - 0.0) < 0.0001); sl@0: //Read F5 as text. Expected error: KErrNone. Zero-length 16-bit descriptor. sl@0: err = fullSelectQuery.SelectTextL(sql, valText); sl@0: TEST2(err, KErrNone); sl@0: TEST2(valText.Length(), 0); sl@0: //Read F5 as binary. Small buffer. Expected return code: positive value, which is the column length in bytes. sl@0: err = fullSelectQuery.SelectBinaryL(sql, valBinary); sl@0: TEST(err > 0); sl@0: TEST2(valBinary.Length(), 10); sl@0: TInt i; sl@0: for(i=0;i<10;++i) sl@0: { sl@0: TEST(valBinary[i] == i); sl@0: } sl@0: //Read F5 as binary. Big enough buffer. Expected error: KErrNone. sl@0: TBuf8<100> valBinary2; sl@0: err = fullSelectQuery.SelectBinaryL(sql, valBinary2); sl@0: TEST2(err, KErrNone); sl@0: TEST2(valBinary2.Length(), 100); sl@0: for(i=0;i<100;++i) sl@0: { sl@0: TEST(valBinary2[i] == i); sl@0: } sl@0: /////////////////// Text column value, small buffer, reallocation /////////////////// sl@0: HBufC* hbuf = HBufC::NewLC(10); sl@0: TPtr name = hbuf->Des(); sl@0: sql.Copy(KSql4); sl@0: err = fullSelectQuery.SelectTextL(sql, name); sl@0: TEST(err >= 0); //the function may return only non-negative values sl@0: if(err > 0) sl@0: { sl@0: hbuf = hbuf->ReAllocL(err); sl@0: CleanupStack::Pop(); sl@0: CleanupStack::PushL(hbuf); sl@0: name.Set(hbuf->Des()); sl@0: err = fullSelectQuery.SelectTextL(sql, name); sl@0: TEST2(err, KErrNone); sl@0: TEST(name == KColText2()); sl@0: } sl@0: CleanupStack::PopAndDestroy();//hbuf, can't be put as parameter, because may be reallocated sl@0: //Close database, delete database file. sl@0: DestroyTestDb(db); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-SQL-CT-1810 sl@0: @SYMTestCaseDesc A test database is created, test table created in the database with integer, 64-bit sl@0: integer, float, text and blob fields. sl@0: Inserted two records. sl@0: The test calls TSqlScalarFullSelectQuery functions using inappropriate SQL statements: sl@0: SELECT sql statement with parameters, INSERT sql statement, SELECT sql statement, sl@0: which does not return records. sl@0: @SYMTestPriority High sl@0: @SYMTestActions SQL, Scalar fullselect test. sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMREQ REQ5792 sl@0: REQ5793 sl@0: */ sl@0: template void ScalarFullSelectNegativeTestL() sl@0: { sl@0: //Create test database. sl@0: RSqlDatabase db; sl@0: CreateTestDbLC(db); sl@0: TSqlScalarFullSelectQuery fullSelectQuery(db); sl@0: sl@0: BUF sql; sl@0: sl@0: //An attempt to use inappropriate SQL - 1. sl@0: _LIT(KSql1, "SELECT F1 FROM A WHERE F2 = :P"); sl@0: sql.Copy(KSql1); sl@0: TRAPD(err, fullSelectQuery.SelectIntL(sql)); sl@0: TEST2(err, KErrArgument); sl@0: sl@0: //An attempt to use inappropriate SQL - 2. sl@0: _LIT(KSql2, "INSERT INTO A(F1) VALUES(2)"); sl@0: sql.Copy(KSql2); sl@0: TRAP(err, fullSelectQuery.SelectIntL(sql)); sl@0: TEST2(err, KErrArgument); sl@0: sl@0: //The SQL statement does not return any rows sl@0: _LIT(KSql3, "SELECT F1 FROM A WHERE F2 = 456231"); sl@0: sql.Copy(KSql3); sl@0: TRAP(err, fullSelectQuery.SelectIntL(sql)); sl@0: TEST2(err, KErrNotFound); sl@0: sl@0: //Close database, delete database file. sl@0: DestroyTestDb(db); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID PDS-SQL-CT-4204 sl@0: @SYMTestCaseDesc TSqlScalarFullSelectQuery - border test. sl@0: @SYMTestPriority High sl@0: @SYMTestActions The test checks some border test cases such as: sl@0: - retrieving NULL column as integer; sl@0: - retrieving NULL column as 64-bit integer; sl@0: - retrieving NULL column as TReal; sl@0: - retrieving column value smaller than KMinTInt, as integer; sl@0: - retrieving column value bigger than KMaxTInt, as integer; sl@0: @SYMTestExpectedResults Test must not fail sl@0: */ sl@0: void ScalarFullSelectBorderTest() sl@0: { sl@0: (void)RSqlDatabase::Delete(KTestDatabase1); sl@0: RSqlDatabase db; sl@0: TInt rc = db.Create(KTestDatabase1); sl@0: TEST2(rc, KErrNone); sl@0: rc = db.Exec(_L("CREATE TABLE A(F1 INTEGER NULL, F2 INTEGER NULL, F3 FLOAT NULL, F4 TEXT NULL, F5 BLOB NULL)")); sl@0: TEST(rc >= 0); sl@0: sl@0: TSqlScalarFullSelectQuery q(db); sl@0: sl@0: //Insert one record. Bigger than KMaxTInt F1 column value. Smaller than KMinTInt F2 column value. sl@0: rc = db.Exec(_L("INSERT INTO A(F1,F2,F4) VALUES(5000000000,-5000000000,'aljhsfdlgefberveurfgvefkjgs;kjfgs;kjfsd')")); sl@0: TEST2(rc, 1); sl@0: //Select NULL column value as int. sl@0: TInt res = -1; sl@0: TRAP(rc, res = q.SelectIntL(_L("SELECT F5 FROM A"))); sl@0: TEST2(rc, KErrNone); sl@0: TEST2(res, 0); sl@0: //Select NULL column value as int64. sl@0: res = -1; sl@0: TRAP(rc, res = q.SelectInt64L(_L("SELECT F5 FROM A"))); sl@0: TEST2(rc, KErrNone); sl@0: TEST2(res, 0); sl@0: //Select NULL column value as TReal. sl@0: TReal res2 = -1.0; sl@0: TRAP(rc, res2 = q.SelectRealL(_L("SELECT F5 FROM A"))); sl@0: TEST2(rc, KErrNone); sl@0: TEST(Abs(res2) < 0.000001); sl@0: //Select NULL column value as text. sl@0: TBuf<10> text; sl@0: TRAP(rc, res = q.SelectTextL(_L("SELECT F5 FROM A"), text)); sl@0: TEST2(rc, KErrNone); sl@0: TEST2(res, 0); sl@0: TEST2(text.Length(), 0); sl@0: //Select NULL column value as binary. sl@0: TBuf8<10> data; sl@0: TRAP(rc, res = q.SelectBinaryL(_L("SELECT F5 FROM A"), data)); sl@0: TEST2(rc, KErrNone); sl@0: TEST2(res, 0); sl@0: TEST2(data.Length(), 0); sl@0: //Select column value bigger than KMaxTInt, as int. sl@0: res = -1; sl@0: TRAP(rc, res = q.SelectIntL(_L("SELECT F1 FROM A"))); sl@0: TEST2(rc, KErrNone); sl@0: TEST2(res, KMaxTInt); sl@0: //Select column value smaller than KMinTInt, as int. sl@0: res = -1; sl@0: TRAP(rc, res = q.SelectIntL(_L("SELECT F2 FROM A"))); sl@0: TEST2(rc, KErrNone); sl@0: TEST2(res, KMinTInt); sl@0: sl@0: db.Close(); sl@0: (void)RSqlDatabase::Delete(KTestDatabase1); sl@0: } sl@0: sl@0: sl@0: void DoTestsL() sl@0: { sl@0: TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1809 Scalar fullselect test. 16-bit SQL ")); sl@0: ScalarFullSelectTestL< TBuf16<100> >(); sl@0: sl@0: TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1809 Scalar fullselect test. 8-bit SQL ")); sl@0: ScalarFullSelectTestL< TBuf8<100> >(); sl@0: sl@0: TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1810 Scalar fullselect - negative test. 16-bit SQL ")); sl@0: ScalarFullSelectNegativeTestL< TBuf16<100> >(); sl@0: sl@0: TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1810 Scalar fullselect - negative test. 8-bit SQL ")); sl@0: ScalarFullSelectNegativeTestL< TBuf8<100> >(); sl@0: sl@0: TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-CT-4204 Scalar fullselect - border cases ")); sl@0: ScalarFullSelectBorderTest(); sl@0: } sl@0: sl@0: TInt E32Main() sl@0: { sl@0: TheTest.Title(); sl@0: sl@0: CTrapCleanup* tc = CTrapCleanup::New(); sl@0: sl@0: __UHEAP_MARK; sl@0: sl@0: CreateTestEnv(); sl@0: DeleteTestFiles(); sl@0: TRAPD(err, DoTestsL()); sl@0: DeleteTestFiles(); sl@0: TheFs.Close(); sl@0: TEST2(err, KErrNone); sl@0: sl@0: __UHEAP_MARKEND; sl@0: sl@0: TheTest.End(); sl@0: TheTest.Close(); sl@0: sl@0: delete tc; sl@0: sl@0: User::Heap().Check(); sl@0: return KErrNone; sl@0: }