Remove the memory database for the cache
[platform/core/appfw/pkgmgr-info.git] / test / unit_tests / test_parser_db_handlers.cc
index b9a8979..9310113 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 "depinfo_db_handler.hh"
 #include "db_type.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"
+#define DB_VERSION_FILE "./pkg_db_version.txt"
+
+namespace psd = pkgmgr_server::database;
+namespace pc = pkgmgr_common;
+
+class CreateParserDBHandlerMock : public psd::CreateDBHandler {
+ public:
+  CreateParserDBHandlerMock(uid_t uid, int pid)
+      : psd::CreateDBHandler(uid, pid) {}
+
+  MOCK_METHOD0(Connect, bool());
+  MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
+      uid_t>>&());
+};
 
-class CreateDBHandlerMock : public pkgmgr_server::database::CreateDBHandler {
+class PkgSetDBHandlerMock : public psd::PkgSetDBHandler {
  public:
-  CreateDBHandlerMock(uid_t uid, int pid)
-      : pkgmgr_server::database::CreateDBHandler(uid, pid) {}
+  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 PkgSetDBHandlerMock : public pkgmgr_server::database::PkgSetDBHandler {
+class PkgGetDBHandlerMock : public psd::PkgGetDBHandler {
  public:
-  PkgSetDBHandlerMock(uid_t uid, int pid) : pkgmgr_server::database::PkgSetDBHandler(uid, pid) {}
+  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 PkgGetDBHandlerMock : public pkgmgr_server::database::PkgGetDBHandler {
+class AppInfoDBHandlerMock : public psd::AppInfoDBHandler {
  public:
-  PkgGetDBHandlerMock(uid_t uid, int pid) : pkgmgr_server::database::PkgGetDBHandler(uid, pid) {}
+  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 AppInfoDBHandlerMock : public pkgmgr_server::database::AppInfoDBHandler {
+class DepInfoGetDBHandlerMock : public psd::DepInfoGetDBHandler {
  public:
-  AppInfoDBHandlerMock(uid_t uid, int pid) : pkgmgr_server::database::AppInfoDBHandler(uid, pid) {}
+  DepInfoGetDBHandlerMock(uid_t uid, int pid) :
+      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> {};
 
-class DBHandlerTest : public TestFixture {
+class ParserDBHandlerTest : public TestFixture {
  public:
-  DBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
-  virtual ~DBHandlerTest() {}
+  ParserDBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
+  virtual ~ParserDBHandlerTest() {}
 
   virtual void SetUp() {
-    sqlite3 *db;
-    ASSERT_EQ(sqlite3_open_v2(TEST_PARSER_DB, &db,
-        SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
-
-    SetDBHandles(
-        std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
-    CreateDBHandlerMock create_db_handler(0, 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_;
+    ASSERT_EQ(remove(DB_VERSION_FILE), 0);
   }
 
  private:
-  void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
-    db_handles_ = db_handles;
+  void MakeVersionFile() {
+    std::ofstream ofs(DB_VERSION_FILE);
+    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(DBHandlerTest, PkgSetDBHandlerTest) {
+TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Install) {
   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
@@ -129,11 +142,60 @@ TEST_F(DBHandlerTest, PkgSetDBHandlerTest) {
 
   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);
+}
+
+TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Update) {
+  PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
+  pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Update);
+  std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
+      GetUpdatedTestPackage("pkgA"), pkgmgrinfo_basic_free_package);
+  pkg_set_db_handler.SetPkgInfo(ptr.get());
+
+  EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
+  EXPECT_CALL(pkg_set_db_handler, GetConnection())
+      .WillOnce(testing::ReturnRef(db_handles_));
   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
 }
 
-TEST_F(DBHandlerTest, PkgGetDBHandlerTest) {
+TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Delete) {
+  std::string pkgid = "pkgA";
+  PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
+
+  pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Delete);
+  std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
+      GetTestPackage(pkgid), pkgmgrinfo_basic_free_package);
+  pkg_set_db_handler.SetPkgInfo(ptr.get());
+
+  EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
+  EXPECT_CALL(pkg_set_db_handler, GetConnection())
+      .WillOnce(testing::ReturnRef(db_handles_));
+  ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
+
+  pkgmgrinfo_pkginfo_filter_h filter;
+
+  ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
+  std::unique_ptr<pkgmgrinfo_filter_x, decltype(
+      pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
+          reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
+              pkgmgrinfo_pkginfo_filter_destroy);
+  ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
+      PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
+  ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
+      PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
+
+  PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
+  pkg_get_db_handler.SetFilter(filter_ptr.get());
+  pkg_get_db_handler.SetLocale("test_lang");
+
+  EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
+  EXPECT_CALL(pkg_get_db_handler, GetConnection())
+      .WillOnce(testing::ReturnRef(db_handles_));
+  ASSERT_EQ(pkg_get_db_handler.Execute(), PMINFO_R_ENOENT);
+}
+
+TEST_F(ParserDBHandlerTest, PkgGetDBHandlerTest) {
   std::string pkgid = "pkgA";
   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
@@ -143,7 +205,7 @@ TEST_F(DBHandlerTest, 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;
@@ -151,7 +213,7 @@ TEST_F(DBHandlerTest, PkgGetDBHandlerTest) {
   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
   std::unique_ptr<pkgmgrinfo_filter_x, decltype(
       pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
-          reinterpret_cast<pkgmgrinfo_filter_x *>(filter),
+          reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
               pkgmgrinfo_pkginfo_filter_destroy);
   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
       PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
@@ -164,20 +226,20 @@ TEST_F(DBHandlerTest, 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();
   ASSERT_EQ(lpkginfo_list.size(), 1);
 
   auto test_pkginfo = GetTestPackage(pkgid);
-  std::vector<package_x*> rpkginfo_list;
-  rpkginfo_list.emplace_back(test_pkginfo);
+  std::vector<std::shared_ptr<package_x>> rpkginfo_list;
+  rpkginfo_list.emplace_back(test_pkginfo, pkgmgrinfo_basic_free_package);
 
   ASSERT_EQ(IsEqualPackagesInfo(lpkginfo_list, rpkginfo_list), true);
 }
 
-TEST_F(DBHandlerTest, AppInfoDBHandlerTest) {
+TEST_F(ParserDBHandlerTest, AppInfoDBHandlerTest) {
   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
@@ -186,7 +248,7 @@ TEST_F(DBHandlerTest, 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;
@@ -195,7 +257,7 @@ TEST_F(DBHandlerTest, AppInfoDBHandlerTest) {
   ASSERT_EQ(pkgmgrinfo_appinfo_filter_create(&filter), PMINFO_R_OK);
   std::unique_ptr<pkgmgrinfo_filter_x, decltype(
       pkgmgrinfo_appinfo_filter_destroy)*> filter_ptr(
-          reinterpret_cast<pkgmgrinfo_filter_x *>(filter),
+          reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
               pkgmgrinfo_appinfo_filter_destroy);
   ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_string(filter,
       PMINFO_APPINFO_PROP_APP_ID, appid.c_str()), PMINFO_R_OK);
@@ -207,16 +269,45 @@ TEST_F(DBHandlerTest, 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);
 
-  auto lappinfo_list = appinfo_db_handler.GetAppHandle();
+  auto lappinfo_list = appinfo_db_handler.DetachAppHandle();
   ASSERT_EQ(lappinfo_list.size(), 1);
 
   auto test_appinfo = GetTestApplication(appid);
-  std::vector<application_x*> rappinfo_list;
-  rappinfo_list.emplace_back(test_appinfo);
+  std::vector<std::shared_ptr<application_x>> rappinfo_list;
+  rappinfo_list.emplace_back(test_appinfo, std::free);
 
   ASSERT_EQ(IsEqualApplicationsInfo(lappinfo_list, rappinfo_list), true);
 }
+
+TEST_F(ParserDBHandlerTest, DepInfoDBHandlerTest) {
+  PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
+  pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
+  std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
+      GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
+  pkg_set_db_handler.SetPkgInfo(ptr.get());
+
+  EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
+  EXPECT_CALL(pkg_set_db_handler, GetConnection())
+      .WillOnce(testing::ReturnRef(db_handles_));
+  ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
+
+  DepInfoGetDBHandlerMock depinfo_get_db_handler(0, 0);
+  depinfo_get_db_handler.SetPkgID("depends_on_pkgid");
+  EXPECT_CALL(depinfo_get_db_handler,
+      Connect()).WillOnce(testing::Return(true));
+  EXPECT_CALL(depinfo_get_db_handler, GetConnection())
+      .WillOnce(testing::ReturnRef(db_handles_));
+  ASSERT_EQ(depinfo_get_db_handler.Execute(), 0);
+
+  auto depinfo_from_db = depinfo_get_db_handler.GetDependencyList();
+  ASSERT_EQ(depinfo_from_db.size(), 1);
+
+  std::vector<dependency_x*> depinfo_list;
+  depinfo_list.emplace_back(GetTestDepInfo("test_package"));
+
+  EXPECT_TRUE(IsEqualDepInfo(depinfo_list, depinfo_from_db));
+}