File: check_model_import.c

package info (click to toggle)
libgda5 5.2.10-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 76,168 kB
  • sloc: ansic: 495,319; xml: 10,486; yacc: 5,165; sh: 4,451; makefile: 4,095; php: 1,416; java: 1,300; javascript: 1,298; python: 896; sql: 879; perl: 116
file content (158 lines) | stat: -rw-r--r-- 4,462 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (C) 2007 - 2011 Vivien Malerba <malerba@gnome-db.org>
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <libgda/libgda.h>

#define fail(x) g_warning (x)
#define fail_if(x,y) if (x) g_warning (y)
#define fail_unless(x,y) if (!(x)) g_warning (y)

#define CHECK_EXTRA_INFO
static gboolean do_test_load_file (const gchar *filename);

int
main (int argc, char **argv)
{
	int number_failed = 0;
	GDir *dir;
	GError *err = NULL;
	const gchar *name;

	gda_init ();

	if (argc == 2) {
		if (g_str_has_suffix (argv[1], ".xml")) {
			g_print ("Tested: %s\n", argv[1]);
			if (!do_test_load_file (argv[1]))
				number_failed ++;
		}
	}
	else {
		/* data models in tests/providers */
		gchar *dirname;
		dirname = g_build_filename (CHECK_FILES, "tests", "providers", NULL);
		if (!(dir = g_dir_open (dirname, 0, &err))) {
#ifdef CHECK_EXTRA_INFO
			g_warning ("Could not open directory '%s': %s", dirname,
				   err && err->message ? err->message : "No detail");
#endif
			return EXIT_FAILURE;
		}

		while ((name = g_dir_read_name (dir))) {
			if (g_str_has_suffix (name, ".xml")) {
				gchar *full_path = g_build_filename (dirname, name, NULL);
				g_print ("Tested: %s\n", full_path);
				if (!do_test_load_file (full_path))
					number_failed ++;
				g_free (full_path);
			}
		}
		g_free (dirname);
		g_dir_close (dir);

		/* data models in the current dir */
		dirname = g_build_filename (CHECK_FILES, "tests", "data-models", NULL);
		if (!(dir = g_dir_open (dirname, 0, &err))) {
#ifdef CHECK_EXTRA_INFO
			g_warning ("Could not open directory '%s': %s", dirname,
				   err && err->message ? err->message : "No detail");
#endif
			return EXIT_FAILURE;
		}

		while ((name = g_dir_read_name (dir))) {
			if (g_str_has_suffix (name, ".xml")) {
				gchar *full_path = g_build_filename (dirname, name, NULL);
				g_print ("Tested: %s\n", full_path);
				if (!do_test_load_file (full_path))
					number_failed ++;
				g_free (full_path);
			}
		}
		g_free (dirname);
		g_dir_close (dir);
	}

	if (number_failed == 0)
		g_print ("Ok.\n");
	else
		g_print ("%d failed\n", number_failed);

	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}

static gboolean
do_test_load_file (const gchar *filename)
{
	GdaDataModel *import;
	GSList *errors;
	gboolean retval = TRUE;
	gchar *export, *contents;

	/* make sure we only test data model dumps */
	xmlDocPtr doc;
	xmlNodePtr root;
	doc = xmlParseFile (filename);
	if (!doc) 
		/* abort the test */
		return TRUE;
	root = xmlDocGetRootElement (doc);
	if (strcmp (root->name, "gda_array")) {
		/* abort the test */
		xmlFreeDoc (doc);
		return TRUE;
	}
	xmlFreeDoc (doc);

	import = gda_data_model_import_new_file (filename, TRUE, NULL);

	if ((errors = gda_data_model_import_get_errors (GDA_DATA_MODEL_IMPORT (import)))) {
#ifdef CHECK_EXTRA_INFO
		g_warning ("Could not load file '%s': ", filename);
		for (; errors; errors = errors->next) 
			g_print ("\t%s\n", 
				 ((GError *)(errors->data))->message ? ((GError *)(errors->data))->message : "No detail");
#endif
		retval = FALSE;
		goto out;
	}

	export = gda_data_model_export_to_string (import, GDA_DATA_MODEL_IO_DATA_ARRAY_XML, NULL, 0, NULL, 0, NULL);
	g_assert (g_file_get_contents (filename, &contents, NULL, NULL));
	if (strcmp (export, contents)) {
#ifdef CHECK_EXTRA_INFO
		g_print ("========== Imported data model ==========\n");
		gda_data_model_dump (import, stdout);

		g_warning ("Model exported as string differs from loaded data model:");
		g_print ("========== Export as string ==========\n%s\n", export);
		g_file_set_contents ("err.xml", export, -1, NULL);
#endif
		retval = FALSE;
		goto out;
	}
	g_free (export);
	g_free (contents);

 out:
	g_object_unref (import);
	return retval;
}