[libgda: 2/6] Test: Moving random string code to a separate file




commit 8bd4f7bad73764ad2dca31ea6f5202a1137eff3c
Author: Pavlo Solntsev <p sun fun gmail com>
Date:   Mon Aug 17 13:17:00 2020 -0500

    Test: Moving random string code to a separate file
    
    test_random_string() API was added and implemented in the separate file
    to avoid repetition. meson.build file was updated to reflect this change.

 tests/meta-store/common.c          | 11 +++--------
 tests/meta-store/meson.build       |  8 +++++++-
 tests/providers/prov-test-common.c | 10 +++-------
 tests/test-cnc-utils.c             | 13 +++++++++++++
 tests/test-cnc-utils.h             |  1 +
 5 files changed, 27 insertions(+), 16 deletions(-)
---
diff --git a/tests/meta-store/common.c b/tests/meta-store/common.c
index b44139852..db59efb9b 100644
--- a/tests/meta-store/common.c
+++ b/tests/meta-store/common.c
@@ -19,6 +19,7 @@
  */
 #include "common.h"
 #include <sql-parser/gda-sql-parser.h>
+#include "../test-cnc-utils.h"
 
 /* list of changes expected during a modify operation */
 GSList *expected_changes;
@@ -765,15 +766,9 @@ test_setup (const gchar *prov_id, const gchar *dbcreate_str) {
         * dbXXXXX where XXXXX is a string generated from the random int32 numbers
         * that correspond to ASCII codes for characters a-z
         */
-       GString *buffer = g_string_new ("db");
+       dbname = test_random_string ("db", 5);
 
-       for (int i = 0; i < 7; ++i) {
-           gint32 character = g_random_int_range (97, 123);
-           buffer = g_string_append_c (buffer, character);
-       }
-
-       opndb = gda_server_operation_prepare_create_database (prov_id, buffer->str, &error);
-       dbname = g_string_free (buffer, FALSE);
+       opndb = gda_server_operation_prepare_create_database (prov_id, dbname, &error);
 
        g_assert_nonnull (opndb);
 
diff --git a/tests/meta-store/meson.build b/tests/meta-store/meson.build
index e5656075a..6e4cb2f52 100644
--- a/tests/meta-store/meson.build
+++ b/tests/meta-store/meson.build
@@ -1,6 +1,12 @@
 meta_store_common_sources = files([
        'common.c',
-       'common.h'
+       'common.h',
+       '../test-cnc-utils.h',
+       '../test-cnc-utils.c',
+       '../raw-ddl-creator.c',
+       '../raw-ddl-creator.h',
+       '../test-errors.c',
+       '../test-errors.h'
        ])
 
 tchkmst = executable('check_meta_store_memory',
diff --git a/tests/providers/prov-test-common.c b/tests/providers/prov-test-common.c
index 9129e1fbb..cc47e4345 100644
--- a/tests/providers/prov-test-common.c
+++ b/tests/providers/prov-test-common.c
@@ -55,14 +55,10 @@ prov_test_common_setup (void)
         * testXXXXX where XXXXX is a string generated from the random int32 numbers
         * that correspond to ASCII codes for characters a-z
         */
-       GString *buffer = g_string_new ("test");
+       gchar *dbname = test_random_string ("test", 5);
 
-       for (int i = 0; i < 5; ++i) {
-           gint32 character = g_random_int_range (97, 123);
-           buffer = g_string_append_c (buffer, character);
-       }
-       cnc = test_cnc_setup_connection (pinfo->id, buffer->str, &error);
-       g_string_free (buffer, TRUE);
+       cnc = test_cnc_setup_connection (pinfo->id, dbname, &error);
+       g_free (dbname);
        if (!cnc) {
                if (error) {
                        if (error->domain != TEST_ERROR) {
diff --git a/tests/test-cnc-utils.c b/tests/test-cnc-utils.c
index 17bd4b084..6cb99e9a1 100644
--- a/tests/test-cnc-utils.c
+++ b/tests/test-cnc-utils.c
@@ -492,3 +492,16 @@ test_cnc_load_data_from_file (GdaConnection *cnc, const gchar *table, const gcha
 
        return retval;
 }
+
+gchar*
+test_random_string(const gchar *prefix, gint ncharacters)
+{
+       GString *buffer = g_string_new (prefix);
+
+       for (int i = 0; i < ncharacters; ++i) {
+           gint32 character = g_random_int_range (97, 123);
+           buffer = g_string_append_c (buffer, character);
+       }
+
+    return g_string_free (buffer, FALSE);
+}
diff --git a/tests/test-cnc-utils.h b/tests/test-cnc-utils.h
index 6e3def88a..4574484c3 100644
--- a/tests/test-cnc-utils.h
+++ b/tests/test-cnc-utils.h
@@ -31,5 +31,6 @@ gboolean       test_cnc_clean_connection (GdaConnection *cnc, GError **error);
 gboolean       test_cnc_load_data_from_file (GdaConnection *cnc, const gchar *table, const gchar *full_file, 
GError **error);
 
 gchar         *test_prov_name_upcase (const gchar *prov_name);
+gchar         *test_random_string(const gchar *prefix, gint ncharacters);
 
 #endif


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