Refactor pkgmgr-info
[platform/core/appfw/pkgmgr-info.git] / test / unit_tests / test_parser_db_handlers.cc
index f1fb3ef..30545bd 100644 (file)
  */
 
 #include <gmock/gmock.h>
-
 #include <gtest/gtest.h>
 
 #include <cstdio>
+#include <fstream>
 
 #include "appinfo_db_handler.hh"
 #include "create_db_handler.hh"
 #include "parcel_utils.hh"
 #include "pkg_get_db_handler.hh"
 #include "pkg_set_db_handler.hh"
-
-#include "mock/file_mock.h"
 #include "mock/test_fixture.h"
 #include "mock/system_info_mock.h"
-
 #include "pkgmgr-info.h"
 #include "pkgmgr_query_index.h"
 #include "pkgmgrinfo_basic.h"
+#include "server/pkgmgrinfo_internal.h"
 
 #define TEST_PARSER_DB "test.pkgmgr_parser.db"
 
@@ -47,7 +45,8 @@ class CreateParserDBHandlerMock : public psd::CreateDBHandler {
       : psd::CreateDBHandler(uid, pid) {}
 
   MOCK_METHOD0(Connect, bool());
-  MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
+  MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
+      uid_t>>&());
 };
 
 class PkgSetDBHandlerMock : public psd::PkgSetDBHandler {
@@ -55,7 +54,8 @@ class PkgSetDBHandlerMock : public psd::PkgSetDBHandler {
   PkgSetDBHandlerMock(uid_t uid, int pid) : psd::PkgSetDBHandler(uid, pid) {}
 
   MOCK_METHOD0(Connect, bool());
-  MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
+  MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
+      uid_t>>&());
 };
 
 class PkgGetDBHandlerMock : public psd::PkgGetDBHandler {
@@ -63,7 +63,8 @@ class PkgGetDBHandlerMock : public psd::PkgGetDBHandler {
   PkgGetDBHandlerMock(uid_t uid, int pid) : psd::PkgGetDBHandler(uid, pid) {}
 
   MOCK_METHOD0(Connect, bool());
-  MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
+  MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
+      uid_t>>&());
 };
 
 class AppInfoDBHandlerMock : public psd::AppInfoDBHandler {
@@ -71,7 +72,8 @@ class AppInfoDBHandlerMock : public psd::AppInfoDBHandler {
   AppInfoDBHandlerMock(uid_t uid, int pid) : psd::AppInfoDBHandler(uid, pid) {}
 
   MOCK_METHOD0(Connect, bool());
-  MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
+  MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
+      uid_t>>&());
 };
 
 class DepInfoGetDBHandlerMock : public psd::DepInfoGetDBHandler {
@@ -80,7 +82,8 @@ class DepInfoGetDBHandlerMock : public psd::DepInfoGetDBHandler {
       psd::DepInfoGetDBHandler(uid, pid) {}
 
   MOCK_METHOD0(Connect, bool());
-  MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
+  MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
+      uid_t>>&());
 };
 
 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
@@ -91,52 +94,42 @@ class ParserDBHandlerTest : public TestFixture {
   virtual ~ParserDBHandlerTest() {}
 
   virtual void SetUp() {
-    sqlite3* db;
-
-    int ret = sqlite3_open_v2(TEST_PARSER_DB, &db,
-        SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
-    if (ret != SQLITE_OK)
-      sqlite3_close_v2(db);
-
-    ASSERT_EQ(ret, SQLITE_OK);
-
-    SetDBHandles(
-        std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
+    tizen_base::Database db(TEST_PARSER_DB, SQLITE_OPEN_READWRITE |
+        SQLITE_OPEN_CREATE);
+    db_handles_.emplace_back(std::move(db), 0);
     CreateParserDBHandlerMock create_db_handler(0, 0);
 
     EXPECT_CALL(create_db_handler, Connect())
         .Times(2).WillRepeatedly(testing::Return(true));
     EXPECT_CALL(create_db_handler, GetConnection())
-        .Times(2).WillRepeatedly(testing::Return(GetDBHandles()));
+        .Times(2).WillRepeatedly(testing::ReturnRef(db_handles_));
     EXPECT_CALL(GetMock<SystemInfoMock>(),
       system_info_get_platform_int(testing::_, testing::_))
           .WillRepeatedly(testing::DoAll(
                   testing::SetArgPointee<1>(120), testing::Return(0)));
-    fopen_mock_setup(true);
+    MakeVersionFile();
+    pkgmgr_server::internal::SetEnableUnitTest(true);
     ASSERT_EQ(create_db_handler.Execute(), 0);
-    fopen_mock_setup(false);
+    pkgmgr_server::internal::SetEnableUnitTest(false);
   }
 
   virtual void TearDown() {
-    for (auto& handle : db_handles_)
-      sqlite3_close_v2(handle.first);
-
+    db_handles_.clear();
     ASSERT_EQ(remove(TEST_PARSER_DB), 0);
     std::string journal_path(TEST_PARSER_DB);
     journal_path += "-journal";
     ASSERT_EQ(remove(journal_path.c_str()), 0);
   }
 
-  const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
-    return db_handles_;
-  }
-
  private:
-  void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
-    db_handles_ = db_handles;
+  void MakeVersionFile() {
+    std::remove("./pkg_db_version.txt");
+    std::ofstream ofs("./pkg_db_version.txt");
+    ofs << "30005";
   }
 
-  std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
+ protected:
+  std::vector<std::pair<tizen_base::Database, uid_t>> db_handles_;
 };
 
 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Install) {
@@ -148,7 +141,7 @@ TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Install) {
 
   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
   EXPECT_CALL(pkg_set_db_handler, GetConnection())
-      .WillOnce(testing::Return(GetDBHandles()));
+      .WillOnce(testing::ReturnRef(db_handles_));
   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
 }
 
@@ -161,7 +154,7 @@ TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Update) {
 
   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
   EXPECT_CALL(pkg_set_db_handler, GetConnection())
-      .WillOnce(testing::Return(GetDBHandles()));
+      .WillOnce(testing::ReturnRef(db_handles_));
   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
 }
 
@@ -176,7 +169,7 @@ TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Delete) {
 
   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
   EXPECT_CALL(pkg_set_db_handler, GetConnection())
-      .WillOnce(testing::Return(GetDBHandles()));
+      .WillOnce(testing::ReturnRef(db_handles_));
   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
 
   pkgmgrinfo_pkginfo_filter_h filter;
@@ -197,7 +190,7 @@ TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Delete) {
 
   EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
   EXPECT_CALL(pkg_get_db_handler, GetConnection())
-      .WillOnce(testing::Return(GetDBHandles()));
+      .WillOnce(testing::ReturnRef(db_handles_));
   ASSERT_EQ(pkg_get_db_handler.Execute(), PMINFO_R_ENOENT);
 }
 
@@ -211,7 +204,7 @@ TEST_F(ParserDBHandlerTest, PkgGetDBHandlerTest) {
 
   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
   EXPECT_CALL(pkg_set_db_handler, GetConnection())
-      .WillOnce(testing::Return(GetDBHandles()));
+      .WillOnce(testing::ReturnRef(db_handles_));
   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
 
   pkgmgrinfo_pkginfo_filter_h filter;
@@ -232,7 +225,7 @@ TEST_F(ParserDBHandlerTest, PkgGetDBHandlerTest) {
 
   EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
   EXPECT_CALL(pkg_get_db_handler, GetConnection())
-      .WillOnce(testing::Return(GetDBHandles()));
+      .WillOnce(testing::ReturnRef(db_handles_));
   ASSERT_EQ(pkg_get_db_handler.Execute(), 0);
 
   auto lpkginfo_list = pkg_get_db_handler.GetPkgHandle();
@@ -240,7 +233,7 @@ TEST_F(ParserDBHandlerTest, PkgGetDBHandlerTest) {
 
   auto test_pkginfo = GetTestPackage(pkgid);
   std::vector<std::shared_ptr<package_x>> rpkginfo_list;
-  rpkginfo_list.emplace_back(test_pkginfo);
+  rpkginfo_list.emplace_back(test_pkginfo, pkgmgrinfo_basic_free_package);
 
   ASSERT_EQ(IsEqualPackagesInfo(lpkginfo_list, rpkginfo_list), true);
 }
@@ -254,7 +247,7 @@ TEST_F(ParserDBHandlerTest, AppInfoDBHandlerTest) {
 
   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
   EXPECT_CALL(pkg_set_db_handler, GetConnection())
-      .WillOnce(testing::Return(GetDBHandles()));
+      .WillOnce(testing::ReturnRef(db_handles_));
   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
 
   pkgmgrinfo_appinfo_filter_h filter;
@@ -275,7 +268,7 @@ TEST_F(ParserDBHandlerTest, AppInfoDBHandlerTest) {
 
   EXPECT_CALL(appinfo_db_handler, Connect()).WillOnce(testing::Return(true));
   EXPECT_CALL(appinfo_db_handler, GetConnection())
-      .WillOnce(testing::Return(GetDBHandles()));
+      .WillOnce(testing::ReturnRef(db_handles_));
   appinfo_db_handler.SetLocale("test_lang");
   ASSERT_EQ(appinfo_db_handler.Execute(), 0);
 
@@ -284,7 +277,7 @@ TEST_F(ParserDBHandlerTest, AppInfoDBHandlerTest) {
 
   auto test_appinfo = GetTestApplication(appid);
   std::vector<std::shared_ptr<application_x>> rappinfo_list;
-  rappinfo_list.emplace_back(test_appinfo);
+  rappinfo_list.emplace_back(test_appinfo, std::free);
 
   ASSERT_EQ(IsEqualApplicationsInfo(lappinfo_list, rappinfo_list), true);
 }
@@ -298,7 +291,7 @@ TEST_F(ParserDBHandlerTest, DepInfoDBHandlerTest) {
 
   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
   EXPECT_CALL(pkg_set_db_handler, GetConnection())
-      .WillOnce(testing::Return(GetDBHandles()));
+      .WillOnce(testing::ReturnRef(db_handles_));
   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
 
   DepInfoGetDBHandlerMock depinfo_get_db_handler(0, 0);
@@ -306,7 +299,7 @@ TEST_F(ParserDBHandlerTest, DepInfoDBHandlerTest) {
   EXPECT_CALL(depinfo_get_db_handler,
       Connect()).WillOnce(testing::Return(true));
   EXPECT_CALL(depinfo_get_db_handler, GetConnection())
-      .WillOnce(testing::Return(GetDBHandles()));
+      .WillOnce(testing::ReturnRef(db_handles_));
   ASSERT_EQ(depinfo_get_db_handler.Execute(), 0);
 
   auto depinfo_from_db = depinfo_get_db_handler.GetDependencyList();