1 /*
2 * Copyright (c) 2021 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_helper.h"
17
18 #include <gtest/gtest.h>
19
20 #include <string>
21
22 #include "common.h"
23 #include "rdb_errno.h"
24 #include "rdb_open_callback.h"
25
26 using namespace testing::ext;
27 using namespace OHOS::NativeRdb;
28
29 class OpenCallback : public RdbOpenCallback {
30 public:
OnCreate(RdbStore & store)31 int OnCreate(RdbStore &store) override
32 {
33 return E_OK;
34 }
OnUpgrade(RdbStore & store,int oldVersion,int newVersion)35 int OnUpgrade(RdbStore &store, int oldVersion, int newVersion) override
36 {
37 return E_OK;
38 }
39 };
40
41 class RdbHelperTest : public testing::Test {
42 public:
43 static void SetUpTestCase(void);
44 static void TearDownTestCase(void);
45 void SetUp();
46 void TearDown();
47 static const std::string rdbStorePath;
48 };
49 const std::string RdbHelperTest::rdbStorePath = RDB_TEST_PATH + std::string("rdbhelper.db");
50
SetUpTestCase(void)51 void RdbHelperTest::SetUpTestCase(void)
52 {
53 }
54
TearDownTestCase(void)55 void RdbHelperTest::TearDownTestCase(void)
56 {
57 RdbHelper::DeleteRdbStore(rdbStorePath);
58 }
59
SetUp(void)60 void RdbHelperTest::SetUp(void)
61 {
62 }
63
TearDown(void)64 void RdbHelperTest::TearDown(void)
65 {
66 }
67
68 class RdbHelperTestOpenCallback : public RdbOpenCallback {
69 public:
70 int OnCreate(RdbStore &store) override;
71 int OnUpgrade(RdbStore &store, int oldVersion, int newVersion) override;
72 static const std::string CREATE_TABLE_TEST;
73 };
74
75 const std::string RdbHelperTestOpenCallback::CREATE_TABLE_TEST = "CREATE TABL IF NOT EXISTS test "
76 "(id INTEGER PRIMARY KEY AUTOINCREMENT, "
77 "name TEXT NOT NULL, age INTEGER, salary REAL, "
78 "blobType BLOB)";
79
OnCreate(RdbStore & store)80 int RdbHelperTestOpenCallback::OnCreate(RdbStore &store)
81 {
82 return store.ExecuteSql(CREATE_TABLE_TEST);
83 }
84
OnUpgrade(RdbStore & store,int oldVersion,int newVersion)85 int RdbHelperTestOpenCallback::OnUpgrade(RdbStore &store, int oldVersion, int newVersion)
86 {
87 return E_OK;
88 }
89
90 /**
91 * @tc.name: DeleteDatabaseCache_001
92 * @tc.desc: delete db cache
93 * @tc.type: FUNC
94 * @tc.require:
95 * @tc.author:
96 */
97 HWTEST_F(RdbHelperTest, DeleteDatabaseCache_001, TestSize.Level1)
98 {
99 int errCode = E_OK;
100 RdbStoreConfig config(RdbHelperTest::rdbStorePath);
101 RdbHelperTestOpenCallback helper;
102 std::shared_ptr<RdbStore> rdbStore = RdbHelper::GetRdbStore(config, 1, helper, errCode);
103 EXPECT_EQ(rdbStore, nullptr);
104 }
105
106 /**
107 * @tc.name: DeleteDatabase_001
108 * @tc.desc: delete db file
109 * @tc.type: FUNC
110 */
111 HWTEST_F(RdbHelperTest, DeleteDatabase_001, TestSize.Level1)
112 {
113 int ret = RdbHelper::DeleteRdbStore("test");
114 EXPECT_EQ(ret, E_OK);
115 }
116
117 /**
118 * @tc.name: getrdbstore_001
119 * @tc.desc: get db file with a invalid path
120 * @tc.type: FUNC
121 */
122 HWTEST_F(RdbHelperTest, GetDatabase_001, TestSize.Level0)
123 {
124 int errCode = E_OK;
125 RdbStoreConfig config("/invalid/invalid/test.db");
126 OpenCallback helper;
127 auto store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
128 EXPECT_EQ(store, nullptr);
129 EXPECT_EQ(errCode, E_INVALID_FILE_PATH);
130 }
131