[libgda/wip/xml-ddl-tool: 3/5] Test for GdaDdlBase was written. Bug Fix.



commit f2bfee7be22797005cd08ac3cee7549d21b779d7
Author: Pavlo Solntsev <p sun fun gmail com>
Date:   Wed Apr 18 16:29:34 2018 -0500

    Test for GdaDdlBase was written. Bug Fix.
    
    The test was written and some logical bugs in GdaDdlBase implementation were
    identified and fixed.

 libgda/gda-ddl-base.c      |  65 +++++++++++-
 tests/ddl/Makefile.am      |  17 ++++
 tests/ddl/check-ddl-base.c | 245 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 322 insertions(+), 5 deletions(-)
---
diff --git a/libgda/gda-ddl-base.c b/libgda/gda-ddl-base.c
index 9a915cc78..112ff6705 100644
--- a/libgda/gda-ddl-base.c
+++ b/libgda/gda-ddl-base.c
@@ -150,9 +150,9 @@ gda_ddl_base_init (GdaDdlBase *self)
  * */
 gboolean
 gda_ddl_base_set_names (GdaDdlBase *self,
-                       const char* catalog,
-                       const char* schema,
-                       const char* name,
+                       const gchar* catalog,
+                       const gchar* schema,
+                       const gchar* name,
                        GError **error)
 {
        g_return_val_if_fail (self,FALSE);
@@ -165,7 +165,7 @@ gda_ddl_base_set_names (GdaDdlBase *self,
        }
 
        /* Check if catalog NULL but schema is not NULL */
-       if (!catalog && schema) {
+       if (catalog && !schema) {
                g_set_error (error,GDA_DDL_BASE_ERROR,
                             GDA_DDL_BASE_NAME_MISSMATCH,
                             _("Schema name is not set while catalog name is set\n"));
@@ -227,13 +227,34 @@ gda_ddl_base_get_full_name (GdaDdlBase *self,
 
        GdaDdlBasePrivate *priv = gda_ddl_base_get_instance_private (self);
 
-       if (!priv->m_fullname) {
+       /* if (!priv->m_fullname) { */
+       /*      g_set_error (error,GDA_DDL_BASE_ERROR, */
+       /*                   GDA_DDL_BASE_NAME_IS_NULL, */
+       /*                   _("Name is NULL. It should be set before use.\n")); */
+       /*      return NULL; */
+       /* } */
+
+       GString *fullnamestr = NULL;
+
+       fullnamestr = g_string_new (NULL);
+
+       if (priv->m_catalog && priv->m_schema && priv->m_name)
+               g_string_printf (fullnamestr,"%s.%s.%s",priv->m_catalog,priv->m_schema,priv->m_name);
+       else if (priv->m_schema && priv->m_name)
+               g_string_printf (fullnamestr,"%s.%s",priv->m_schema,priv->m_name);
+       else if (priv->m_name)
+               g_string_printf (fullnamestr,"%s",priv->m_name);
+       else {
                g_set_error (error,GDA_DDL_BASE_ERROR,
                             GDA_DDL_BASE_NAME_IS_NULL,
                             _("Name is NULL. It should be set before use.\n"));
                return NULL;
        }
 
+       /* In this block  catalog is NULL */
+       priv->m_fullname = g_strdup (fullnamestr->str);
+       g_string_free (fullnamestr, TRUE);
+
        return priv->m_fullname;
 }
 
@@ -331,5 +352,39 @@ gda_ddl_base_free (GdaDdlBase *self)
        g_clear_object (&self);
 }
 
+void
+gda_ddl_base_set_catalog (GdaDdlBase  *self,
+                         const gchar *catalog)
+{
+       g_return_if_fail (self);
+
+       GdaDdlBasePrivate *priv = gda_ddl_base_get_instance_private (self);
+
+       g_free (priv->m_catalog);
+       priv->m_catalog = g_strdup (catalog);
+}
 
+void
+gda_ddl_base_set_schema (GdaDdlBase  *self,
+                        const gchar *schema)
+{
+       g_return_if_fail (self);
+
+       GdaDdlBasePrivate *priv = gda_ddl_base_get_instance_private (self);
+
+       g_free (priv->m_schema);
+       priv->m_schema = g_strdup (schema);
+}
+
+void
+gda_ddl_base_set_name (GdaDdlBase  *self,
+                      const gchar *name)
+{
+       g_return_if_fail (self);
+
+       GdaDdlBasePrivate *priv = gda_ddl_base_get_instance_private (self);
+
+       g_free (priv->m_name);
+       priv->m_name = g_strdup (name);
+}
 
diff --git a/tests/ddl/Makefile.am b/tests/ddl/Makefile.am
new file mode 100644
index 000000000..d80b69b9d
--- /dev/null
+++ b/tests/ddl/Makefile.am
@@ -0,0 +1,17 @@
+include $(top_srcdir)/glib-tap.mk
+
+AM_CPPFLAGS = \
+       -I$(top_srcdir) \
+       -I$(top_srcdir)/libgda \
+       -I$(top_builddir) \
+       $(COREDEPS_CFLAGS) \
+       $(COREDEPS_WFLAGS) \
+       -DROOT_DIR=\""$(top_srcdir)"\"
+
+test_programs = \
+    check_ddl_base
+
+check_ddl_base_SOURCES = check-ddl-base.c
+check_ddl_base_LDADD = \
+       $(top_builddir)/libgda/libgda-6.0.la \
+       $(COREDEPS_LIBS)
diff --git a/tests/ddl/check-ddl-base.c b/tests/ddl/check-ddl-base.c
new file mode 100644
index 000000000..f114421f5
--- /dev/null
+++ b/tests/ddl/check-ddl-base.c
@@ -0,0 +1,245 @@
+/* check-ddl-creator.c
+ *
+ * Copyright 2018 Pavlo Solntsev <p sun fun gmail com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <locale.h>
+#include <libgda/libgda.h>
+#include <libgda/gda-ddl-base.h>
+
+#define GDA_BOOL_TO_STRING(x) x ? "TRUE" : "FALSE"
+
+typedef struct {
+       GdaDdlBase *obj;
+}BaseFixture;
+
+static void
+test_ddl_base_start (BaseFixture *self,
+                    gconstpointer user_data)
+{
+       self->obj = gda_ddl_base_new();
+
+}
+
+static void
+test_ddl_base_finish (BaseFixture *self,
+                     gconstpointer user_data)
+{
+       gda_ddl_base_free (self->obj);
+}
+
+static void
+test_ddl_base_run1 (BaseFixture *self,
+                  gconstpointer user_data)
+{
+       const gchar *catalog = "First";
+       const gchar *schema = "Second";
+       const gchar *name = "Third";
+
+       gboolean res = gda_ddl_base_set_names (self->obj,
+                                              catalog,
+                                              schema,
+                                              name,
+                                              NULL);
+
+       g_assert_true (res);
+
+
+       const gchar *ret_catalog = gda_ddl_base_get_catalog (self->obj,NULL);
+
+       g_assert_cmpstr (ret_catalog, ==, catalog);
+
+       const gchar *ret_schema = gda_ddl_base_get_schema (self->obj,NULL);
+
+       g_assert_cmpstr (ret_schema, ==, schema);
+
+       const gchar *ret_name = gda_ddl_base_get_name (self->obj,NULL);
+
+       g_assert_cmpstr (ret_name, ==, name);
+
+       const gchar *full_name = gda_ddl_base_get_full_name (self->obj,NULL);
+
+       g_assert_cmpstr (full_name, ==, "First.Second.Third");
+}
+
+static void
+test_ddl_base_run2 (BaseFixture *self,
+                  gconstpointer user_data)
+{
+       const gchar *catalog = "First";
+       const gchar *schema = "Second";
+       const gchar *name = "Third";
+
+       gda_ddl_base_set_catalog (self->obj,catalog);
+
+       const gchar *ret_catalog = gda_ddl_base_get_catalog (self->obj,NULL);
+
+       g_assert_cmpstr (ret_catalog, ==, catalog);
+
+       gda_ddl_base_set_schema (self->obj,schema);
+
+       const gchar *ret_schema = gda_ddl_base_get_schema (self->obj,NULL);
+
+       g_assert_cmpstr (ret_schema, ==, schema);
+
+       gda_ddl_base_set_name (self->obj,name);
+
+       const gchar *ret_name = gda_ddl_base_get_name (self->obj,NULL);
+
+       g_assert_cmpstr (ret_name, ==, name);
+
+       const gchar *full_name = gda_ddl_base_get_full_name (self->obj,NULL);
+
+       g_assert_cmpstr (full_name, ==, "First.Second.Third");
+}
+
+static void
+test_ddl_base_run3 (BaseFixture *self,
+                  gconstpointer user_data)
+{
+       const gchar *catalog = "First";
+       const gchar *schema = "Second";
+       const gchar *name = "Third";
+
+       gboolean res = gda_ddl_base_set_names (self->obj,
+                                              NULL,
+                                              schema,
+                                              name,
+                                              NULL);
+
+       g_assert_true (res);
+
+
+       const gchar *ret_catalog = gda_ddl_base_get_catalog (self->obj,NULL);
+
+       g_assert_cmpstr (ret_catalog, ==, NULL);
+
+       const gchar *ret_schema = gda_ddl_base_get_schema (self->obj,NULL);
+
+       g_assert_cmpstr (ret_schema, ==, schema);
+
+       const gchar *ret_name = gda_ddl_base_get_name (self->obj,NULL);
+
+       g_assert_cmpstr (ret_name, ==, name);
+
+       const gchar *full_name = gda_ddl_base_get_full_name (self->obj,NULL);
+
+       g_assert_cmpstr (full_name, ==, "Second.Third");
+
+}
+
+static void
+test_ddl_base_run4 (BaseFixture *self,
+                  gconstpointer user_data)
+{
+       const gchar *catalog = "First";
+       const gchar *schema = "Second";
+       const gchar *name = "Third";
+
+       gboolean res = gda_ddl_base_set_names (self->obj,
+                                              catalog,
+                                              NULL,
+                                              name,
+                                              NULL);
+
+       g_assert_false (res);
+}
+
+static void
+test_ddl_base_run5 (BaseFixture *self,
+                  gconstpointer user_data)
+{
+       const gchar *catalog = "First";
+       const gchar *schema = "Second";
+       const gchar *name = "Third";
+
+       gboolean res = gda_ddl_base_set_names (self->obj,
+                                              NULL,
+                                              NULL,
+                                              name,
+                                              NULL);
+
+       g_assert_true (res);
+
+
+       const gchar *ret_catalog = gda_ddl_base_get_catalog (self->obj,NULL);
+
+       g_assert_cmpstr (ret_catalog, ==, NULL);
+
+       const gchar *ret_schema = gda_ddl_base_get_schema (self->obj,NULL);
+
+       g_assert_cmpstr (ret_schema, ==, NULL);
+
+       const gchar *ret_name = gda_ddl_base_get_name (self->obj,NULL);
+
+       g_assert_cmpstr (ret_name, ==, name);
+
+       const gchar *full_name = gda_ddl_base_get_full_name (self->obj,NULL);
+
+       g_assert_cmpstr (full_name, ==, "Third");
+}
+
+
+gint
+main (gint   argc,
+      gchar *argv[])
+{
+       setlocale (LC_ALL,"");
+
+       g_test_init (&argc,&argv,NULL);
+
+
+       g_test_add ("/test-ddl/base-all",
+                   BaseFixture,
+                   NULL,
+                   test_ddl_base_start,
+                   test_ddl_base_run1,
+                   test_ddl_base_finish);
+
+       g_test_add ("/test-ddl/base-separate",
+                   BaseFixture,
+                   NULL,
+                   test_ddl_base_start,
+                   test_ddl_base_run2,
+                   test_ddl_base_finish);
+
+       g_test_add ("/test-ddl/base-schema-name",
+                   BaseFixture,
+                   NULL,
+                   test_ddl_base_start,
+                   test_ddl_base_run3,
+                   test_ddl_base_finish);
+
+       g_test_add ("/test-ddl/base-catalog-name",
+                   BaseFixture,
+                   NULL,
+                   test_ddl_base_start,
+                   test_ddl_base_run4,
+                   test_ddl_base_finish);
+
+       g_test_add ("/test-ddl/base-name",
+                   BaseFixture,
+                   NULL,
+                   test_ddl_base_start,
+                   test_ddl_base_run5,
+                   test_ddl_base_finish);
+
+       return g_test_run();
+}


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