1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "rdb_sql_utils.h"
17
18 #include <gtest/gtest.h>
19
20 #include <climits>
21 #include <string>
22
23 #include "grd_type_export.h"
24 #include "rd_utils.h"
25 #include "sqlite_sql_builder.h"
26 #include "values_buckets.h"
27
28 using namespace testing::ext;
29 using namespace OHOS::NativeRdb;
30
31 class RdbSqlUtilsTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34 static void TearDownTestCase(void);
SetUp(void)35 void SetUp(void) {};
TearDown(void)36 void TearDown(void) {};
37 };
38
SetUpTestCase(void)39 void RdbSqlUtilsTest::SetUpTestCase(void)
40 {
41 }
42
TearDownTestCase(void)43 void RdbSqlUtilsTest::TearDownTestCase(void)
44 {
45 }
46
47 /**
48 * @tc.name: RdbStore_SqliteUtils_001
49 * @tc.desc: Normal testCase of sqlite_utils for IsSpecial, if sqlType is special
50 * @tc.type: FUNC
51 */
52 HWTEST_F(RdbSqlUtilsTest, RdbSqlUtils_Test_001, TestSize.Level1)
53 {
54 auto [dataBasePath, errCode] = RdbSqlUtils::GetDefaultDatabasePath("/data/test", "RdbTest.db");
55 EXPECT_EQ(dataBasePath, "/data/test/rdb/RdbTest.db");
56 EXPECT_EQ(errCode, E_OK);
57
58 auto [dataBasePath1, errCode1] = RdbSqlUtils::GetDefaultDatabasePath("/data/test", "RdbTest.db", "myself");
59 EXPECT_EQ(dataBasePath1, "/data/test/rdb/myself/RdbTest.db");
60 EXPECT_EQ(errCode1, E_OK);
61 }
62
63 /**
64 * @tc.name: RdbStore_UpdateSqlBuilder_001
65 * @tc.desc: test RdbStore UpdateSqlBuilder
66 * @tc.type: FUNC
67 */
68 HWTEST_F(RdbSqlUtilsTest, RdbSqlUtils_UpdateSqlBuilder_001, TestSize.Level1)
69 {
70 ValuesBucket values;
71 values.PutString("name", std::string("zhangsan"));
72 values.PutInt("age", 20);
73 values.PutDouble("salary", 300.5);
74
75 std::vector<ValueObject> bindArgs;
76 std::string updateSql = SqliteSqlBuilder::BuildUpdateString(values, "test", std::vector<std::string>{ "19" }, "",
77 "age = ?", "", "", INT_MIN, INT_MIN, bindArgs, ConflictResolution::ON_CONFLICT_NONE);
78 EXPECT_EQ(updateSql, "UPDATE test SET age=?,name=?,salary=? WHERE age = ?");
79
80 updateSql = SqliteSqlBuilder::BuildUpdateString(values, "test", std::vector<std::string>{}, "", "", "", "",
81 INT_MIN, INT_MIN, bindArgs, ConflictResolution::ON_CONFLICT_NONE);
82 EXPECT_EQ(updateSql, "UPDATE test SET age=?,name=?,salary=?");
83 }
84