[libgda: 3/5] DbBase: Simplified implementation of gda_db_base_compare()




commit b5e8398fd4f249cc5f5871595350d74fc204157d
Author: Pavlo Solntsev <p sun fun gmail com>
Date:   Fri Oct 22 22:35:46 2021 -0500

    DbBase: Simplified implementation of gda_db_base_compare()
    
    Implementation of gda_db_base_compare() was simplified and supported
    by test coverage.

 libgda/gda-db-base.c     |  30 ++++++-----
 tests/db/check-db-base.c | 134 +++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 148 insertions(+), 16 deletions(-)
---
diff --git a/libgda/gda-db-base.c b/libgda/gda-db-base.c
index d7059c5e7..f9fa63cbb 100644
--- a/libgda/gda-db-base.c
+++ b/libgda/gda-db-base.c
@@ -349,31 +349,35 @@ gda_db_base_compare (GdaDbBase *a,
   const gchar *a_name = gda_db_base_get_name (a);
   const gchar *b_name = gda_db_base_get_name (b);
 
-  gint res;
+  gint res_name;
+  gint res_schema;
+  gint res_catalog;
 
-  res = g_strcmp0 (a_name, b_name);
-  if (a_name && b_name)
+  res_name = g_strcmp0 (a_name, b_name);
+  res_schema = g_strcmp0 (a_schema, b_schema);
+  res_catalog = g_strcmp0 (a_catalog, b_catalog);
+
+  if (!res_name)
     {
-      if (a_schema && b_schema)
+      if (!res_schema)
         {
-          res = g_strcmp0 (a_schema, b_schema);
-          if (a_catalog && b_catalog)
+          if (!res_catalog)
             {
-              return g_strcmp0 (a_catalog, b_catalog);
+              return res_catalog;
             }
-          else
+          else /* catalogs are different */
             {
-              return res;
+              return res_catalog;
             }
         }
-      else
+      else /* Schemas are different */
         {
-          return res;
+          return res_schema;
         }
     }
-  else
+  else /* names are different */
     {
-      return res;
+      return res_name;
     }
 
   return -1;
diff --git a/tests/db/check-db-base.c b/tests/db/check-db-base.c
index c59260cb1..b2e36e4cb 100644
--- a/tests/db/check-db-base.c
+++ b/tests/db/check-db-base.c
@@ -71,12 +71,113 @@ test_db_base_finish (BaseFixture *self,
 }
 
 static void
-test_db_base_compare_run1 (TestBaseCompare *self,
+test_db_base_compare_same_names (TestBaseCompare *self,
+                  G_GNUC_UNUSED gconstpointer user_data)
+{
+    const gchar *name = "name_a";
+    gda_db_base_set_name (self->obja, name);
+    gda_db_base_set_name (self->objb, name);
+
+    g_assert_cmpint (gda_db_base_compare (self->obja, self->objb), ==, 0);
+
+    gda_db_base_set_name (self->objb, "other");
+
+    g_assert_cmpint (gda_db_base_compare (self->obja, self->objb), !=, 0);
+}
+
+static void
+test_db_base_compare_different_names (TestBaseCompare *self,
                   G_GNUC_UNUSED gconstpointer user_data)
 {
     gda_db_base_set_name (self->obja, "name_a");
     gda_db_base_set_name (self->objb, "name_b");
 
+    g_assert_cmpint (gda_db_base_compare (self->obja, self->objb), !=, 0);
+}
+
+static void
+test_db_base_compare_schema_name (TestBaseCompare *self,
+                  G_GNUC_UNUSED gconstpointer user_data)
+{
+    const gchar *name = "name";
+
+    gda_db_base_set_name (self->obja, name);
+    gda_db_base_set_schema (self->obja, "schema_a");
+
+    gda_db_base_set_name (self->objb, name);
+/* one schema is NULL */
+    g_assert_cmpint (gda_db_base_compare (self->obja, self->objb), !=, 0);
+
+    gda_db_base_set_schema (self->objb, "schema_b");
+    gda_db_base_set_schema (self->obja, NULL);
+
+/* one schema is NULL */
+    g_assert_cmpint (gda_db_base_compare (self->obja, self->objb), !=, 0);
+
+    gda_db_base_set_schema (self->obja, name);
+    gda_db_base_set_schema (self->objb, name);
+
+/* same schema */
+    g_assert_cmpint (gda_db_base_compare (self->obja, self->objb), ==, 0);
+}
+
+static void
+test_db_base_compare_catalog_name (TestBaseCompare *self,
+                  G_GNUC_UNUSED gconstpointer user_data)
+{
+    const gchar *name = "name";
+    const gchar *catalog = "catalog";
+
+    gda_db_base_set_name (self->obja, name);
+
+    g_assert_cmpint (gda_db_base_compare (self->obja, self->objb), !=, 0);
+
+    gda_db_base_set_catalog (self->obja, catalog);
+
+    g_assert_cmpint (gda_db_base_compare (self->obja, self->objb), !=, 0);
+
+    gda_db_base_set_name (self->objb, name);
+
+    g_assert_cmpint (gda_db_base_compare (self->obja, self->objb), !=, 0);
+
+    gda_db_base_set_catalog (self->objb, catalog);
+
+    g_assert_cmpint (gda_db_base_compare (self->obja, self->objb), ==, 0);
+
+    gda_db_base_set_catalog (self->obja, NULL);
+
+    g_assert_cmpint (gda_db_base_compare (self->obja, self->objb), !=, 0);
+
+    gda_db_base_set_catalog (self->obja, "catalog_a");
+
+    g_assert_cmpint (gda_db_base_compare (self->obja, self->objb), !=, 0);
+}
+
+static void
+test_db_base_compare_catalog_schema_name (TestBaseCompare *self,
+                  G_GNUC_UNUSED gconstpointer user_data)
+{
+    const gchar *name = "name";
+
+    gda_db_base_set_catalog (self->obja, name);
+    gda_db_base_set_catalog (self->objb, name);
+
+    g_assert_cmpint (gda_db_base_compare (self->obja, self->objb), ==, 0);
+
+    gda_db_base_set_schema (self->obja, name);
+
+    g_assert_cmpint (gda_db_base_compare (self->obja, self->objb), !=, 0);
+
+    gda_db_base_set_schema (self->objb, name);
+
+    g_assert_cmpint (gda_db_base_compare (self->obja, self->objb), ==, 0);
+
+    gda_db_base_set_name (self->obja, name);
+
+    g_assert_cmpint (gda_db_base_compare (self->obja, self->objb), !=, 0);
+
+    gda_db_base_set_name (self->objb, name);
+
     g_assert_cmpint (gda_db_base_compare (self->obja, self->objb), ==, 0);
 }
 
@@ -259,12 +360,39 @@ main (gint   argc,
                    test_db_base_run5,
                    test_db_base_finish);
 
-       g_test_add ("/test-db/base-compare",
+       g_test_add ("/test-db/base-compare-same-name",
                    TestBaseCompare,
                    NULL,
                    test_db_base_compare_start,
-                   test_db_base_compare_run1,
+                   test_db_base_compare_same_names,
                    test_db_base_compare_finish);
 
+       g_test_add ("/test-db/base-compare-different-name",
+                   TestBaseCompare,
+                   NULL,
+                   test_db_base_compare_start,
+                   test_db_base_compare_different_names,
+                   test_db_base_compare_finish);
+
+       g_test_add ("/test-db/base-compare-schema-name",
+                   TestBaseCompare,
+                   NULL,
+                   test_db_base_compare_start,
+                   test_db_base_compare_schema_name,
+                   test_db_base_compare_finish);
+
+       g_test_add ("/test-db/base-compare-catalog-name",
+                   TestBaseCompare,
+                   NULL,
+                   test_db_base_compare_start,
+                   test_db_base_compare_catalog_name,
+                   test_db_base_compare_finish);
+
+       g_test_add ("/test-db/base-compare-catalog-schema-name",
+                   TestBaseCompare,
+                   NULL,
+                   test_db_base_compare_start,
+                   test_db_base_compare_catalog_schema_name,
+                   test_db_base_compare_finish);
        return g_test_run();
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]