diff options
Diffstat (limited to 'gentoobrowse-api/unittests/testMaintenance.cpp')
-rw-r--r-- | gentoobrowse-api/unittests/testMaintenance.cpp | 181 |
1 files changed, 132 insertions, 49 deletions
diff --git a/gentoobrowse-api/unittests/testMaintenance.cpp b/gentoobrowse-api/unittests/testMaintenance.cpp index e05d9ad..21b77db 100644 --- a/gentoobrowse-api/unittests/testMaintenance.cpp +++ b/gentoobrowse-api/unittests/testMaintenance.cpp @@ -7,56 +7,27 @@ #include <modifycommand.h> #include <git2.h> #include <fstream> +#include <maintenanceimpl.h> +#include <sql/maintenance/gitListCreateRaw.sql.h> +#include <sql/maintenance/gitListCreate.sql.h> +#include <sql/maintenance/gitListCreateIdx.sql.h> class MaintenanceClientCombined : public Maintenance, public TestClient { }; BOOST_FIXTURE_TEST_SUITE(maintenance, MaintenanceClientCombined) -const boost::filesystem::path treeDir(binDir / "tree" / "gentoobrowse"); +const boost::filesystem::path treeDir(binDir / "tree"); const boost::filesystem::path fixturesDir(rootDir / "fixtures"); -class SampleData { - public: - SampleData() - { - clean(); - } - - ~SampleData() - { - clean(); - } - - void clean() - { - BOOST_TEST_CHECKPOINT("Cleaning sample data dir " << treeDir); - boost::filesystem::remove_all(treeDir); - } - - void extract(const std::string & archive, const std::string & repo) - { - boost::filesystem::create_directories(treeDir); - boost::filesystem::create_symlink(fixturesDir / archive, treeDir / repo); - } -}; - void dumpDb(DB::ConnectionPtr db); void -doRefreshPackageTree(SampleData & sd, DB::ConnectionPtr db, const std::string & archive, const std::string & dir, - Gentoo::MaintenancePrx m, int64_t files, int64_t cats, int64_t devvcs, int64_t pkgs, int64_t ebs, int64_t ebus, +doRefreshPackageTree(DB::ConnectionPtr db, int64_t cats, int64_t devvcs, int64_t pkgs, int64_t ebs, int64_t ebus, int64_t ebas, int64_t pus, int64_t ug, int64_t ul, int64_t ugs, int64_t ugds, int64_t deps, int64_t rdeps, int64_t news, int64_t masks) { - BOOST_TEST_INFO("archive: " << archive); - if (!archive.empty()) { - sd.extract(archive, dir); - } - m->refreshPackageTree(); - sd.clean(); dumpDb(db); - SQL_REQUIRE_EQUAL("SELECT COUNT(*) FROM gentoobrowse.files", int64_t, files); SQL_REQUIRE_EQUAL("SELECT COUNT(*) FROM gentoobrowse.categories", int64_t, cats); SQL_REQUIRE_EQUAL("SELECT COUNT(*) FROM gentoobrowse.categories WHERE name = 'dev-vcs'", int64_t, devvcs); SQL_REQUIRE_EQUAL("SELECT COUNT(*) FROM gentoobrowse.packages", int64_t, pkgs); @@ -97,23 +68,130 @@ dumpDb(DB::ConnectionPtr db) #endif } +class M2 : public Gentoo::Service::Maintenance { + public: + typedef std::map<boost::filesystem::path, size_t> FileMap; + + M2(IceTray::DatabasePoolPtr d) : + Gentoo::Service::Maintenance(d) + { + auto dbc = db->get(); + DB::TransactionScope tx(dbc.get()); + updateFileTypes(dbc.get()); + } + + void applyDiffOfFolders(int64_t repoId, const boost::filesystem::path & from, const boost::filesystem::path & to) const + { + fprintf(stderr, "%s\n", __PRETTY_FUNCTION__); + auto dbc = db->get(); + auto fromFiles = fileSet(from); + auto toFiles = fileSet(to); + dbc->execute("SET search_path = gentoobrowse, pg_catalog"); + Gentoo::Service::sql::maintenance::gitListCreateRaw.modify(dbc.get())->execute(); + auto ins = dbc->modify("INSERT INTO filelistraw(repoid, status, filename) VALUES(?, ?, ?)"); + ins->bindParamI(0, repoId); + newFiles(ins, fromFiles, toFiles); + removedFiles(ins, fromFiles, toFiles); + changedFiles(ins, fromFiles, toFiles); + Gentoo::Service::sql::maintenance::gitListCreate.modify(dbc.get())->execute(); + Gentoo::Service::sql::maintenance::gitListCreateIdx.modify(dbc.get())->execute(); + boost::filesystem::remove(treeDir); + boost::filesystem::create_symlink(to, treeDir); + applyFileChanges(dbc.get(), "/"); + boost::filesystem::remove(treeDir); + dbc->execute("DROP TABLE filelist"); + dbc->execute("DROP TABLE filelistraw"); + dbc->execute("SET search_path = public, pg_catalog"); + } + + static FileMap fileSet(const boost::filesystem::path & p) + { + FileMap found; + for (boost::filesystem::recursive_directory_iterator r(p); r != boost::filesystem::recursive_directory_iterator(); r++) { + if (boost::filesystem::is_regular(r->status())) { + found[boost::filesystem::relative(*r, p)] = boost::filesystem::file_size(*r); + } + } + return found; + } + static void newFiles(DB::ModifyCommandPtr ins, const FileMap & from, const FileMap & to) + { + ins->bindParamS(1, "A"); + for(const auto & f : to) { + if (from.find(f.first) == from.end()) { + fprintf(stderr, "Added %s\n", f.first.c_str()); + ins->bindParamS(2, f.first.string()); + ins->execute(); + } + } + } + static void removedFiles(DB::ModifyCommandPtr ins, const FileMap & from, const FileMap & to) + { + ins->bindParamS(1, "D"); + for(const auto & f : from) { + if (to.find(f.first) == to.end()) { + fprintf(stderr, "Removed %s\n", f.first.c_str()); + ins->bindParamS(2, f.first.string()); + ins->execute(); + } + } + } + static void changedFiles(DB::ModifyCommandPtr ins, const FileMap & from, const FileMap & to) + { + ins->bindParamS(1, "M"); + for(const auto & f : to) { + auto i = from.find(f.first); + if (i != from.end() && i->second != f.second) { + fprintf(stderr, "Changed %s\n", f.first.c_str()); + ins->bindParamS(2, f.first.string()); + ins->execute(); + } + } + } +}; + +class MockPool : public IceTray::DatabasePool { + public: + MockPool() : IceTray::DatabasePool(10, 2) {} + DB::Connection * createResource() const override + { + return DB::MockDatabase::openConnectionTo("GentooBrowseAPI"); + } +}; + BOOST_AUTO_TEST_CASE( refreshPackageTree ) { - SampleData sd; auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("GentooBrowseAPI")); - auto insRepo = db->modify("INSERT INTO gentoobrowse.repos(name, path) VALUES(?, ?)"); + auto insRepo = db->modify("INSERT INTO gentoobrowse.repos(name, path, upstream, branch, lastcommit) VALUES(?, ?, ?, ?, ?)"); insRepo->bindParamS(0, "gentoo"); - insRepo->bindParamS(1, (treeDir / "gentoo").string()); + insRepo->bindParamS(1, treeDir.string()); + insRepo->bindParamS(2, "origin"); + insRepo->bindParamS(3, "master"); + insRepo->bindParamS(4, "8292397bf6a8c91215b03a558e8bc843aff25b64"); insRepo->execute(); - doRefreshPackageTree(sd, db, "4156eb45cf3b0ce1d7125b84efd8688c2d6e831d", "gentoo", - m, 2080, 5, 1, 482, 981, 3626, 4593, 501, 393, 238, 50, 1573, 2008, 1543, 81, 152); + boost::filesystem::remove_all(binDir / "empty"); + boost::filesystem::create_directory(binDir / "empty"); + auto p = IceTray::DatabasePoolPtr(new MockPool()); + M2 m2(p); + + BOOST_TEST_CONTEXT("4156eb45cf3b0ce1d7125b84efd8688c2d6e831d") { + m2.applyDiffOfFolders(1, binDir / "empty", rootDir / "fixtures" / "4156eb45cf3b0ce1d7125b84efd8688c2d6e831d"); + doRefreshPackageTree(db, + 5, 1, 482, 981, 3626, 4593, 501, 393, 238, 50, 1573, 2008, 1543, 81, 152); + } - doRefreshPackageTree(sd, db, "756569aa764177340726dd3d40b41d89b11b20c7", "gentoo", - m, 2082, 5, 1, 483, 982, 3638, 4599, 502, 393, 238, 50, 1573, 2009, 1546, 79, 152); + BOOST_TEST_CONTEXT("756569aa764177340726dd3d40b41d89b11b20c7") { + m2.applyDiffOfFolders(1, rootDir / "fixtures" / "4156eb45cf3b0ce1d7125b84efd8688c2d6e831d", rootDir / "fixtures" / "756569aa764177340726dd3d40b41d89b11b20c7"); + doRefreshPackageTree(db, + 5, 1, 483, 982, 3638, 4599, 502, 393, 238, 50, 1573, 2009, 1546, 79, 152); + } - doRefreshPackageTree(sd, db, "", "gentoo", - m, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); + BOOST_TEST_CONTEXT("empty") { + m2.applyDiffOfFolders(1, rootDir / "fixtures" / "756569aa764177340726dd3d40b41d89b11b20c7", binDir / "empty"); + doRefreshPackageTree(db, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); + } } static @@ -150,15 +228,18 @@ BOOST_AUTO_TEST_CASE( testUpdateGitRepository ) git_remote * origin; BOOST_REQUIRE_EQUAL(0, git_remote_create(&origin, repo, "origin", "http://git.randomdan.homeip.net/git/portage")); git_remote_free(origin); - create_initial_commit(repo); + auto commitOid = create_initial_commit(repo); + char commit[GIT_OID_HEXSZ + 1]; + git_oid_tostr(commit, GIT_OID_HEXSZ, &commitOid); git_repository_free(repo); git_libgit2_shutdown(); // Reference it - auto insRepo = db->modify("INSERT INTO gentoobrowse.repos(name, path, upstream, branch) VALUES(?, ?, ?, ?)"); + auto insRepo = db->modify("INSERT INTO gentoobrowse.repos(name, path, upstream, branch, lastcommit) VALUES(?, ?, ?, ?, ?)"); insRepo->bindParamS(0, "testrepo"); insRepo->bindParamS(1, testRepo.string()); insRepo->bindParamS(2, "origin"); insRepo->bindParamS(3, "master"); + insRepo->bindParamS(4, commit); insRepo->execute(); // Update @@ -175,10 +256,12 @@ BOOST_AUTO_TEST_CASE( testUpdateGitRepository ) BOOST_AUTO_TEST_CASE( testRefreshGitRepository ) { auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("GentooBrowseAPI")); - auto insRepo = db->modify("INSERT INTO gentoobrowse.repos(name, path, lastcommit) VALUES(?, ?, ?)"); + auto insRepo = db->modify("INSERT INTO gentoobrowse.repos(name, path, upstream, branch, lastcommit) VALUES(?, ?, ?, ?, ?)"); insRepo->bindParamS(0, "gentoo"); insRepo->bindParamS(1, "/usr/portage"); - insRepo->bindParamS(2, "8292397bf6a8c91215b03a558e8bc843aff25b64"); + insRepo->bindParamS(2, "origin"); + insRepo->bindParamS(3, "master"); + insRepo->bindParamS(4, "8292397bf6a8c91215b03a558e8bc843aff25b64"); insRepo->execute(); std::ifstream data((rootDir / "gitdata.sql").string()); db->executeScript(data, rootDir); @@ -193,7 +276,7 @@ BOOST_AUTO_TEST_CASE( testRefreshGitRepository ) } // Import it - m->refreshPackageTreeGit(); + m->refreshPackageTree(); SQL_REQUIRE_EQUAL(R"SQL(SELECT lastCommit FROM gentoobrowse.repos)SQL", std::string, "40539afe6705aee26a55bb861f5e892ae7240057"); BOOST_TEST_CONTEXT("libkgapi was moved correctly") { |