File: mg-test-sqlquery.c

package info (click to toggle)
mergeant 0.52-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,848 kB
  • ctags: 6,584
  • sloc: ansic: 63,372; xml: 23,218; sh: 8,316; makefile: 613; sql: 237
file content (180 lines) | stat: -rw-r--r-- 4,490 bytes parent folder | download | duplicates (2)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdio.h>
#include <string.h>
#include "../libmergeant/libmergeant.h"

#define MAKE_DUMPS 1

#define STEP_SEPARATOR "------------------"
#define xmlfile "SQL_tests.xml"

void make_query_test (MgConf *conf, FILE *stream, const gchar *sql, gboolean expected_result);

gint main (int argc, char **argv) {
	MgConf *conf = NULL;
	GError *error = NULL;
	xmlDocPtr doc;
	xmlNodePtr node, subnode;
	FILE *out = stdout;
	gboolean in_file = FALSE;

	gtk_init (&argc, &argv);

	g_print ("# This test creates\n");
	g_print ("# Queries FROM SQL statements\n\n");


	if (! g_file_test (xmlfile, G_FILE_TEST_EXISTS)) {
		g_print ("cant' find file '%s'", xmlfile);
		exit (1);
	}

	doc = xmlParseFile (xmlfile);
	if (!doc) {
		g_print ("Cant' load XML file '%s'", xmlfile);
		exit (1);
	}

	node = xmlDocGetRootElement (doc);
	if (strcmp (node->name, "test_scenario")) {
		g_print ("XML file top node is not <test_scenario>\n");
		exit (1);
	}

	subnode = node->children;
	while (subnode) {
		/* new dictionnary */
		if (!strcmp (subnode->name, "dictionnary")) {
			gchar *filename = xmlGetProp (subnode, "name");
			if (conf) {
				g_object_unref (G_OBJECT (conf));
				conf = NULL;
			}

			if (filename) {
				conf = MG_CONF (mg_conf_new ());
				g_print ("Loading dictionnary %s\n", filename);
				if (!mg_conf_load_xml_file (conf, filename, &error)) {
					g_print ("Error occurred:\n\t%s\n", error->message);
					g_error_free (error);
					exit (1);
				}
				g_free (filename);
			}
		}
		
		/* new output file */
		if (!strcmp (subnode->name, "output_file")) {
			gchar *filename = xmlGetProp (subnode, "name");
			if (in_file) {
				/* HTML end */
				g_fprintf (out, "</table>\n");
				g_fprintf (out, "</body>\n");
				g_fprintf (out, "</html>\n");
				fclose (out);
			}
			out = stdout;

			if (filename) {
				g_print ("Writing to %s\n", filename);
				out = fopen (filename, "w");
				if (!out) {
					g_print ("Can't open '%s' to write\n", filename);
					exit (1);
				}
				g_free (filename);
			}

			/* HTML init */
			g_fprintf (out, "<html>\n");
			g_fprintf (out, "<body>\n");
			g_fprintf (out, "<table cellspacing=\"3\" cellpadding=\"3\" border=\"1\" width=\"100%\">\n");

			in_file = TRUE;
		}
		
		/* new test group */
		if (!strcmp (subnode->name, "test_group")) {
			xmlNodePtr test = subnode->children;
			gchar *descr = xmlGetProp (subnode, "descr");

			g_fprintf (out, "<tr><th colspan=\"3\" bgcolor=\"green4\">%s</th></tr>\n", descr);
			g_fprintf (out, "<tr><th>SQL to parse</th><th>Result SQL</th><th>Error</th></tr>\n", descr);
			g_free (descr);

			while (test) {
				if (!strcmp (test->name, "test")) {
					gchar *sql = xmlGetProp (test, "sql");
					gchar *expected = xmlGetProp (test, "expect");

					if (sql) {
						make_query_test (conf, out, sql, 
								 expected && (*expected=='N') ? FALSE : TRUE);
						g_free (sql);
					}
					if (expected)
						g_free (expected);
				}
				test = test->next;
			}
		}
		subnode = subnode->next;
	}
	xmlFreeDoc (doc);

	if (in_file) {
		/* HTML end */
		g_fprintf (out, "</table>\n");
		g_fprintf (out, "</body>\n");
		g_fprintf (out, "</html>\n");
		fclose (out);
	}

	if (conf)
		g_object_unref (G_OBJECT (conf));
	return 0;
}

void
make_query_test (MgConf *conf, FILE *stream, const gchar *sql, gboolean expected_result)
{
	MgQuery *query;
	GError *error = NULL;

	g_fprintf (stream, "\t<tr><td>%s</td>", sql);
	query = (MgQuery *) mg_query_new_from_sql (conf, sql, &error);
	if (mg_query_get_query_type (query) != MG_QUERY_TYPE_NON_PARSED_SQL) {
		gchar *sql2, *extra = "";
		
		if (!expected_result)
			extra = "bgcolor=\"red\"";

		sql2 = mg_renderer_render_as_sql (MG_RENDERER (query), NULL, MG_RENDERER_EXTRA_VAL_ATTRS, &error);
		if (sql2)
			g_fprintf (stream, "<td>%s</td><td></td>", sql2);
		else {
			if (error && error->message)
				g_fprintf (stream, "<td>---</td><td %s>PARSING gen. error: %s</td>", 
					   extra, error->message);
			else
				g_fprintf (stream, "<td>---</td><td %s>PARSING gen. error: NO ERROR MSG</td>", 
					   extra);
			if (error)
				g_error_free (error);
		}
	}
	else {
		gchar *extra = "";		
		if (expected_result)
			extra = "bgcolor=\"red\"";

		if (error && error->message)
			g_fprintf (stream, "<td>---</td><td %s>PARSING gen. error: %s</td>", 
				   extra, error->message);
		else
			g_fprintf (stream, "<td>---</td><td %s>PARSING gen. error: NO ERROR MSG</td>", 
				   extra);
		if (error)
			g_error_free (error);
	}
	g_fprintf (stream, "</tr>\n");
}