• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 RdbHelperTest : public testing::Test {
30 public:
31     static void SetUpTestCase(void);
32     static void TearDownTestCase(void);
33     void SetUp();
34     void TearDown();
35     static const std::string rdbStorePath;
36 };
37 const std::string RdbHelperTest::rdbStorePath = RDB_TEST_PATH + std::string("rdbhelper.db");
38 
SetUpTestCase(void)39 void RdbHelperTest::SetUpTestCase(void)
40 {
41 }
42 
TearDownTestCase(void)43 void RdbHelperTest::TearDownTestCase(void)
44 {
45 }
46 
SetUp(void)47 void RdbHelperTest::SetUp(void)
48 {
49 }
50 
TearDown(void)51 void RdbHelperTest::TearDown(void)
52 {
53 }
54 
55 class RdbHelperTestOpenCallback : public RdbOpenCallback {
56 public:
57     int OnCreate(RdbStore &rdbStore) override;
58     int OnUpgrade(RdbStore &rdbStore, int oldVersion, int newVersion) override;
59     static const std::string CREATE_TABLE_TEST;
60 };
61 
62 const std::string RdbHelperTestOpenCallback::CREATE_TABLE_TEST = std::string("CREATE TABL IF NOT EXISTS test ") +
63                                                                  std::string("(id INTEGER PRIMARY KEY "
64                                                                              "AUTOINCREMENT, "
65                                                                              "name TEXT NOT NULL, age INTEGER, "
66                                                                              "salary "
67                                                                              "REAL, blobType BLOB)");
68 
OnCreate(RdbStore & store)69 int RdbHelperTestOpenCallback::OnCreate(RdbStore &store)
70 {
71     return store.ExecuteSql(CREATE_TABLE_TEST);
72 }
73 
OnUpgrade(RdbStore & store,int oldVersion,int newVersion)74 int RdbHelperTestOpenCallback::OnUpgrade(RdbStore &store, int oldVersion, int newVersion)
75 {
76     return E_OK;
77 }
78 /**
79  * @tc.name: DeleteDatabase_001
80  * @tc.desc: delete db file
81  * @tc.type: FUNC
82  * @tc.require: SR000CU2BL
83  * @tc.author: chenxi
84  */
85 HWTEST_F(RdbHelperTest, DeleteDatabase_001, TestSize.Level1)
86 {
87     int ret = RdbHelper::DeleteRdbStore("test");
88     EXPECT_EQ(ret, E_OK);
89 }
90 
91 /**
92  * @tc.name: DeleteDatabaseCache_001
93  * @tc.desc: delete db cache
94  * @tc.type: FUNC
95  */
96 HWTEST_F(RdbHelperTest, DeleteDatabaseCache_001, TestSize.Level1)
97 {
98     int errCode = E_OK;
99     RdbStoreConfig config(RdbHelperTest::rdbStorePath);
100     RdbHelperTestOpenCallback helper;
101     std::shared_ptr<RdbStore> rdbStore = RdbHelper::GetRdbStore(config, 1, helper, errCode);
102     EXPECT_EQ(rdbStore, nullptr);
103 }