From 219e23b6f6ba246cb0ee245a107361e80febf23d Mon Sep 17 00:00:00 2001 From: Tomas Mlcoch Date: Tue, 26 Jun 2012 13:15:20 +0200 Subject: [PATCH] Unittests for SQLite module --- tests/CMakeLists.txt | 4 + tests/test_data/packages/empty-0-0.src.rpm | Bin 0 -> 1488 bytes tests/test_data/packages/empty-0-0.x86_64.rpm | Bin 0 -> 1401 bytes tests/test_data/specs/fake-empty.spec | 19 ++ tests/testsqlite.c | 380 ++++++++++++++++++++++++++ 5 files changed, 403 insertions(+) create mode 100644 tests/test_data/packages/empty-0-0.src.rpm create mode 100644 tests/test_data/packages/empty-0-0.x86_64.rpm create mode 100644 tests/test_data/specs/fake-empty.spec create mode 100644 tests/testsqlite.c diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 38ed81d..4f03f99 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,3 +9,7 @@ ADD_DEPENDENCIES(tests testload_metadata) ADD_EXECUTABLE(testmisc testmisc.c) TARGET_LINK_LIBRARIES(testmisc libcreaterepo_c ${GLIB2_LIBRARIES}) ADD_DEPENDENCIES(tests testmisc) + +ADD_EXECUTABLE(testsqlite testsqlite.c) +TARGET_LINK_LIBRARIES(testsqlite libcreaterepo_c ${GLIB2_LIBRARIES}) +ADD_DEPENDENCIES(tests testsqlite) diff --git a/tests/test_data/packages/empty-0-0.src.rpm b/tests/test_data/packages/empty-0-0.src.rpm new file mode 100644 index 0000000000000000000000000000000000000000..fae39b17e725c1b3e8fa16ed11833e47ef2b5728 GIT binary patch literal 1488 zcmbW0TWkzr6vw~muC^B0)+H_xq^-M7W_K<->wyYErF0Y5NRio@SsnH=v5U6ifkZ+? zQ?~~RQcZ&>f_gwC>KaWWZXpsO;(>Oflpt8O&e=Ug#FH;M``_RE&wO*vnQyvV-ad6O z#xc%}*ooRwp;Tc1y*SqA?|bV{b#inKy+shyAo3t)K@>3N=md2_;DTJo8&LG|8u;7* z&|HYY5a_=HMXxN&&I@^aQ`_6losHfrAL3a5;bx^*Hv~mhWKmNzQC0S;v4ZNwvpdx$51LJv@CwqEQL_T%s#tbZ;s&EHuL3?7Gd zppSiEKAk(~3i7cI^j8uWXL-{NJcGgCvpZd)<7y3VqhpZ#+_ zV?3fHD1Jp&an9L~IGyfofd2)fcj8%q)8yC{nBQ+}py18rD@D6bpKWTcoxDB2IQO-5mM{I54#g4*5F88FdcUxza_0-HgF!I#%uH=@=txw!L?3Rc7_TOpi zoZ6NQ#0$TEJW*TzU`5W!;yqgX&Uw9oh4+#tK2$8J$bYmg8Ev^y)ewF?zGL9P+MF-# eYev2LHm>zjD%HC2qdb3YYn|ip@|W@BF!-O;0T0{& literal 0 HcmV?d00001 diff --git a/tests/test_data/packages/empty-0-0.x86_64.rpm b/tests/test_data/packages/empty-0-0.x86_64.rpm new file mode 100644 index 0000000000000000000000000000000000000000..cb324ae423fa59e805e7db8faa3f2542e779498a GIT binary patch literal 1401 zcmbVMT}TvB6uzTd{!{cr_RCffO5S1DogLj$%d{;?Tr(vV!e(dg>d@@WG&3vf=2Hmu zUU*qZ zUD2+#^xPe|b$Z~%Y{VhcY5qaSVH7b<|) z0yY8A{{lDol+4^7&Zu3Bs+tOG(O9@HsVb3JjLDM5V)b?PN~Bhm zSzT0nz~< z1J@XX{*PRja*chK0Lr<>yr|c4y#UPbV5RTX_wbebYk2&3VDyK${sD~nqFiGS=x^rw z7uUy@bpY=d{T*Cy0mjm)GlZve*q8rM@?8(cr;ItPsNc>xUb**F1=`QLIs&KvX1ah>A& z6Y3V?zyEM~A+l!=A&Cz|YYj_Pj3LYQME{p&r4`+TU|j9TI@?K)>Do-yQ##X#p_`d3 z$=1sQvb3BFza`OiRMD~1JiC8J6$DXPs>Y^DR|DbrR`DLuR5Zn4s~!(5m75gkgJ{k65t?afsaNtDG94FlsdB8Ef>I4aiqwwM?e zBjlfL2g4C5B*)}veJ!?+b1%mK3It8m2I*js+Pckx^t56a@U&}a>p(ow)!i2F8#tBd z>1sLJ2w^FfRhjMSmg$0UJw-i7ZO8JM>RC>ZQd@D9bffFqG?_`Im_uDX#~P(zkfvn` z9$v;|jf2c&j;<2b)-8VJ)M46?;!%GLbkNL@qKT!;PafPir`t9cX1+a5VA-TNnPlp{|bU}{m;5wZ$Av<%~JV|0 +#include +#include +#include +#include +#include +#include "createrepo/misc.h" +#include "createrepo/package.h" +#include "createrepo/sqlite.h" +#include "createrepo/parsepkg.h" +#include "createrepo/constants.h" + +#define TMP_DIR_PATTERN "/tmp/createrepo_test_XXXXXX" +#define TMP_PRIMARY_NAME "primary.sqlite" +#define TMP_FILELISTS_NAME "filelists.sqlite" +#define TMP_OTHER_NAME "other.sqlite" + +#define EMPTY_PKG "test_data/packages/empty-0-0.x86_64.rpm" +#define EMPTY_PKG_SRC "test_data/packages/empty-0-0.src.rpm" + + +typedef struct { + gchar *tmp_dir; +} TestData; + + +Package * +get_package() +{ + Package *p; + Dependency *dep; + PackageFile *file; + + p = package_new(); + p->pkgId = "123456"; + p->name = "foo"; + p->arch = "x86_64"; + p->version = "1.2.3"; + p->epoch = "1"; + p->release = "2"; + p->summary = "foo package"; + p->description = "super cool package"; + p->url = "http://package.com"; + p->time_file = 123456; + p->time_build = 234567; + p->rpm_license = "GPL"; + p->rpm_vendor = NULL; + p->rpm_group = NULL; + p->rpm_buildhost = NULL; + p->rpm_sourcerpm = "foo.src.rpm"; + p->rpm_header_start = 20; + p->rpm_header_end = 120; + p->rpm_packager = NULL; + p->size_package = 123; + p->size_installed = 20; + p->size_archive = 30; + p->location_href = "foo.rpm"; + p->location_base = NULL; + p->checksum_type = "sha256"; + + dep = dependency_new(); + dep->name = "foobar_dep"; + dep->flags = NULL; + dep->pre = FALSE; + p->requires = (g_slist_prepend(p->requires, dep)); + + dep = dependency_new(); + dep->name = "foobar_pre_dep"; + dep->flags = "LE"; + dep->pre = TRUE; + p->requires = g_slist_prepend(p->requires, dep); + + file = package_file_new(); + file->type = ""; + file->path = "/bin/"; + file->name = "foo"; + p->files = g_slist_prepend(p->files, file); + + file = package_file_new(); + file->type = "dir"; + file->path = "/var/foo/"; + file->name = NULL; + p->files = g_slist_prepend(p->files, file); + + return p; +} + + + +Package * +get_empty_package() +{ + Package *p; + Dependency *dep; + PackageFile *file; + + p = package_new(); + p->name = "foo"; + + dep = dependency_new(); + dep->name = NULL; + dep->flags = NULL; + dep->pre = FALSE; + p->requires = (g_slist_prepend(p->requires, dep)); + + dep = dependency_new(); + dep->name = NULL; + dep->flags = NULL; + dep->pre = TRUE; + p->requires = g_slist_prepend(p->requires, dep); + + file = package_file_new(); + file->type = NULL; + file->path = NULL; + file->name = NULL; + p->files = g_slist_prepend(p->files, file); + + return p; +} + + +static void +testdata_setup(TestData *testdata, gconstpointer test_data) +{ + UNUSED(test_data); + testdata->tmp_dir = g_strdup(TMP_DIR_PATTERN); + mkdtemp(testdata->tmp_dir); +} + + +static void +testdata_teardown(TestData *testdata, gconstpointer test_data) +{ + UNUSED(test_data); + remove_dir(testdata->tmp_dir); + g_free(testdata->tmp_dir); +} + + +static void +test_open_db(TestData *testdata, gconstpointer test_data) +{ + UNUSED(test_data); + + GError *err = NULL; + gchar *path = NULL; + sqlite3 *db; + + // Create new db + + path = g_strconcat(testdata->tmp_dir, "/", TMP_PRIMARY_NAME, NULL); + db = open_primary_db(path, &err); + g_assert(db); + g_assert(!err); + g_assert(g_file_test(path, G_FILE_TEST_EXISTS)); + g_free(path); + close_primary_db(db, &err); + g_assert(!err); + + path = g_strconcat(testdata->tmp_dir, "/", TMP_FILELISTS_NAME, NULL); + db = open_filelists_db(path, &err); + g_assert(db); + g_assert(!err); + g_assert(g_file_test(path, G_FILE_TEST_EXISTS)); + g_free(path); + close_filelists_db(db, &err); + g_assert(!err); + + path = g_strconcat(testdata->tmp_dir, "/", TMP_OTHER_NAME, NULL); + db = open_other_db(path, &err); + g_assert(db); + g_assert(!err); + g_assert(g_file_test(path, G_FILE_TEST_EXISTS)); + g_free(path); + close_other_db(db, &err); + g_assert(!err); +} + + +static void +test_add_primary_pkg_db(TestData *testdata, gconstpointer test_data) +{ + UNUSED(test_data); + + GError *err = NULL; + gchar *path; + sqlite3 *db; + Package *pkg; + + GTimer *timer = g_timer_new(); + gdouble topen, tprepare, tadd, tclean, tmp; + + // Create new db + + path = g_strconcat(testdata->tmp_dir, "/", TMP_PRIMARY_NAME, NULL); + g_timer_start(timer); + db = open_primary_db(path, &err); + topen = g_timer_elapsed(timer, NULL); + g_assert(db); + g_assert(!err); + g_assert(g_file_test(path, G_FILE_TEST_EXISTS)); + + // Load package + + pkg = get_package(); + + // Add package + + DbPrimaryStatements pri_stmts; + + tmp = g_timer_elapsed(timer, NULL); + pri_stmts = prepare_primary_db_statements(db, &err); + tprepare = g_timer_elapsed(timer, NULL) - tmp; + g_assert(!err); + + tmp = g_timer_elapsed(timer, NULL); + add_primary_pkg_db(pri_stmts, pkg); + tadd = g_timer_elapsed(timer, NULL) - tmp; + + tmp = g_timer_elapsed(timer, NULL); + destroy_primary_db_statements(pri_stmts); + close_primary_db(db, &err); + tclean = g_timer_elapsed(timer, NULL) - tmp; + + printf("Stats:\nOpen: %f\nPrepare: %f\nAdd: %f\nCleanup: %f\nSum: %f\n", + topen, tprepare, tadd, tclean, (topen + tprepare + tadd + tclean)); + + // Cleanup + + g_timer_stop(timer); + g_timer_destroy(timer); + g_free(path); + g_assert(!err); +} + + +static void +test_dbinfo_update(TestData *testdata, gconstpointer test_data) +{ + UNUSED(test_data); + + GError *err = NULL; + gchar *path; + sqlite3 *db; + Package *pkg; + + // Create new db + + path = g_strconcat(testdata->tmp_dir, "/", TMP_PRIMARY_NAME, NULL); + db = open_primary_db(path, &err); + g_assert(db); + g_assert(!err); + g_assert(g_file_test(path, G_FILE_TEST_EXISTS)); + + // Try dbinfo_update + + dbinfo_update(db, "foochecksum", &err); + g_assert(!err); + + // Load package + + pkg = get_package(); + + // Add package + + DbPrimaryStatements pri_stmts; + + pri_stmts = prepare_primary_db_statements(db, &err); + g_assert(!err); + + add_primary_pkg_db(pri_stmts, pkg); + + destroy_primary_db_statements(pri_stmts); + + // Try dbinfo_update again + + dbinfo_update(db, "foochecksum", &err); + g_assert(!err); + + // Cleanup + + package_free(pkg); + g_free(path); + close_primary_db(db, &err); + g_assert(!err); +} + + + +static void +test_all(TestData *testdata, gconstpointer test_data) +{ + UNUSED(test_data); + + GError *err = NULL; + gchar *path; + sqlite3 *db = NULL; + Package *pkg, *pkg2 = NULL; + + // Create new db + + path = g_strconcat(testdata->tmp_dir, "/", TMP_PRIMARY_NAME, NULL); + db = open_primary_db(path, &err); + g_assert(db); + g_assert(!err); + g_assert(g_file_test(path, G_FILE_TEST_EXISTS)); + + // Try dbinfo_update + + dbinfo_update(db, "foochecksum", &err); + g_assert(!err); + + // Load package + + init_package_parser(); + pkg = package_from_file(EMPTY_PKG, PKG_CHECKSUM_SHA256, EMPTY_PKG, NULL, 5, NULL); + g_assert(pkg); + free_package_parser(); + + pkg2 = get_empty_package(); + + // Add package + + DbPrimaryStatements pri_stmts; + + pri_stmts = prepare_primary_db_statements(db, &err); + g_assert(!err); + + add_primary_pkg_db(pri_stmts, pkg); + add_primary_pkg_db(pri_stmts, pkg2); + + destroy_primary_db_statements(pri_stmts); + + // Try dbinfo_update again + + dbinfo_update(db, "foochecksum", &err); + g_assert(!err); + + + // Cleanup + + package_free(pkg); + package_free(pkg2); + close_primary_db(db, &err); + g_assert(!err); + g_free(path); +} + + + +int +main(int argc, char *argv[]) +{ + g_test_init(&argc, &argv, NULL); + + g_test_add("/sqlite/test_open_db", TestData, NULL, testdata_setup, test_open_db, testdata_teardown); + g_test_add("/sqlite/test_add_primary_pkg_db", TestData, NULL, testdata_setup, test_add_primary_pkg_db, testdata_teardown); + g_test_add("/sqlite/test_dbinfo_update", TestData, NULL, testdata_setup, test_dbinfo_update, testdata_teardown); + g_test_add("/sqlite/test_all", TestData, NULL, testdata_setup, test_all, testdata_teardown); + + return g_test_run(); +} -- 2.7.4