3
#include "../libmergeant/libmergeant.h"
7
#define STEP_SEPARATOR "------------------"
8
#define xmlfile "SQL_tests.xml"
10
void make_query_test (MgConf *conf, FILE *stream, const gchar *sql, gboolean expected_result);
12
gint main (int argc, char **argv) {
16
xmlNodePtr node, subnode;
18
gboolean in_file = FALSE;
20
gtk_init (&argc, &argv);
22
g_print ("# This test creates\n");
23
g_print ("# Queries FROM SQL statements\n\n");
26
if (! g_file_test (xmlfile, G_FILE_TEST_EXISTS)) {
27
g_print ("cant' find file '%s'", xmlfile);
31
doc = xmlParseFile (xmlfile);
33
g_print ("Cant' load XML file '%s'", xmlfile);
37
node = xmlDocGetRootElement (doc);
38
if (strcmp (node->name, "test_scenario")) {
39
g_print ("XML file top node is not <test_scenario>\n");
43
subnode = node->children;
46
if (!strcmp (subnode->name, "dictionnary")) {
47
gchar *filename = xmlGetProp (subnode, "name");
49
g_object_unref (G_OBJECT (conf));
54
conf = MG_CONF (mg_conf_new ());
55
g_print ("Loading dictionnary %s\n", filename);
56
if (!mg_conf_load_xml_file (conf, filename, &error)) {
57
g_print ("Error occurred:\n\t%s\n", error->message);
66
if (!strcmp (subnode->name, "output_file")) {
67
gchar *filename = xmlGetProp (subnode, "name");
70
g_fprintf (out, "</table>\n");
71
g_fprintf (out, "</body>\n");
72
g_fprintf (out, "</html>\n");
78
g_print ("Writing to %s\n", filename);
79
out = fopen (filename, "w");
81
g_print ("Can't open '%s' to write\n", filename);
88
g_fprintf (out, "<html>\n");
89
g_fprintf (out, "<body>\n");
90
g_fprintf (out, "<table cellspacing=\"3\" cellpadding=\"3\" border=\"1\" width=\"100%\">\n");
96
if (!strcmp (subnode->name, "test_group")) {
97
xmlNodePtr test = subnode->children;
98
gchar *descr = xmlGetProp (subnode, "descr");
100
g_fprintf (out, "<tr><th colspan=\"3\" bgcolor=\"green4\">%s</th></tr>\n", descr);
101
g_fprintf (out, "<tr><th>SQL to parse</th><th>Result SQL</th><th>Error</th></tr>\n", descr);
105
if (!strcmp (test->name, "test")) {
106
gchar *sql = xmlGetProp (test, "sql");
107
gchar *expected = xmlGetProp (test, "expect");
110
make_query_test (conf, out, sql,
111
expected && (*expected=='N') ? FALSE : TRUE);
120
subnode = subnode->next;
126
g_fprintf (out, "</table>\n");
127
g_fprintf (out, "</body>\n");
128
g_fprintf (out, "</html>\n");
133
g_object_unref (G_OBJECT (conf));
138
make_query_test (MgConf *conf, FILE *stream, const gchar *sql, gboolean expected_result)
141
GError *error = NULL;
143
g_fprintf (stream, "\t<tr><td>%s</td>", sql);
144
query = (MgQuery *) mg_query_new_from_sql (conf, sql, &error);
145
if (mg_query_get_query_type (query) != MG_QUERY_TYPE_NON_PARSED_SQL) {
146
gchar *sql2, *extra = "";
148
if (!expected_result)
149
extra = "bgcolor=\"red\"";
151
sql2 = mg_renderer_render_as_sql (MG_RENDERER (query), NULL, MG_RENDERER_EXTRA_VAL_ATTRS, &error);
153
g_fprintf (stream, "<td>%s</td><td></td>", sql2);
155
if (error && error->message)
156
g_fprintf (stream, "<td>---</td><td %s>PARSING gen. error: %s</td>",
157
extra, error->message);
159
g_fprintf (stream, "<td>---</td><td %s>PARSING gen. error: NO ERROR MSG</td>",
162
g_error_free (error);
168
extra = "bgcolor=\"red\"";
170
if (error && error->message)
171
g_fprintf (stream, "<td>---</td><td %s>PARSING gen. error: %s</td>",
172
extra, error->message);
174
g_fprintf (stream, "<td>---</td><td %s>PARSING gen. error: NO ERROR MSG</td>",
177
g_error_free (error);
179
g_fprintf (stream, "</tr>\n");