~ubuntu-branches/ubuntu/karmic/mergeant/karmic

« back to all changes in this revision

Viewing changes to testing/mg-test-sqlquery.c

  • Committer: Bazaar Package Importer
  • Author(s): Gustavo R. Montesino
  • Date: 2007-11-29 08:44:48 UTC
  • mfrom: (2.1.4 hardy)
  • Revision ID: james.westby@ubuntu.com-20071129084448-6aon73d22bv6hzfw
Tags: 0.67-3
* Re-enable installation of the mime files in mergeant.install
* mergeant.dirs: create usr/share/mime/packages to make dh_installmime add
  the update-mime-database code snippets

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <stdio.h>
2
 
#include <string.h>
3
 
#include "../libmergeant/libmergeant.h"
4
 
 
5
 
#define MAKE_DUMPS 1
6
 
 
7
 
#define STEP_SEPARATOR "------------------"
8
 
#define xmlfile "SQL_tests.xml"
9
 
 
10
 
void make_query_test (MgConf *conf, FILE *stream, const gchar *sql, gboolean expected_result);
11
 
 
12
 
gint main (int argc, char **argv) {
13
 
        MgConf *conf = NULL;
14
 
        GError *error = NULL;
15
 
        xmlDocPtr doc;
16
 
        xmlNodePtr node, subnode;
17
 
        FILE *out = stdout;
18
 
        gboolean in_file = FALSE;
19
 
 
20
 
        gtk_init (&argc, &argv);
21
 
 
22
 
        g_print ("# This test creates\n");
23
 
        g_print ("# Queries FROM SQL statements\n\n");
24
 
 
25
 
 
26
 
        if (! g_file_test (xmlfile, G_FILE_TEST_EXISTS)) {
27
 
                g_print ("cant' find file '%s'", xmlfile);
28
 
                exit (1);
29
 
        }
30
 
 
31
 
        doc = xmlParseFile (xmlfile);
32
 
        if (!doc) {
33
 
                g_print ("Cant' load XML file '%s'", xmlfile);
34
 
                exit (1);
35
 
        }
36
 
 
37
 
        node = xmlDocGetRootElement (doc);
38
 
        if (strcmp (node->name, "test_scenario")) {
39
 
                g_print ("XML file top node is not <test_scenario>\n");
40
 
                exit (1);
41
 
        }
42
 
 
43
 
        subnode = node->children;
44
 
        while (subnode) {
45
 
                /* new dictionnary */
46
 
                if (!strcmp (subnode->name, "dictionnary")) {
47
 
                        gchar *filename = xmlGetProp (subnode, "name");
48
 
                        if (conf) {
49
 
                                g_object_unref (G_OBJECT (conf));
50
 
                                conf = NULL;
51
 
                        }
52
 
 
53
 
                        if (filename) {
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);
58
 
                                        g_error_free (error);
59
 
                                        exit (1);
60
 
                                }
61
 
                                g_free (filename);
62
 
                        }
63
 
                }
64
 
                
65
 
                /* new output file */
66
 
                if (!strcmp (subnode->name, "output_file")) {
67
 
                        gchar *filename = xmlGetProp (subnode, "name");
68
 
                        if (in_file) {
69
 
                                /* HTML end */
70
 
                                g_fprintf (out, "</table>\n");
71
 
                                g_fprintf (out, "</body>\n");
72
 
                                g_fprintf (out, "</html>\n");
73
 
                                fclose (out);
74
 
                        }
75
 
                        out = stdout;
76
 
 
77
 
                        if (filename) {
78
 
                                g_print ("Writing to %s\n", filename);
79
 
                                out = fopen (filename, "w");
80
 
                                if (!out) {
81
 
                                        g_print ("Can't open '%s' to write\n", filename);
82
 
                                        exit (1);
83
 
                                }
84
 
                                g_free (filename);
85
 
                        }
86
 
 
87
 
                        /* HTML init */
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");
91
 
 
92
 
                        in_file = TRUE;
93
 
                }
94
 
                
95
 
                /* new test group */
96
 
                if (!strcmp (subnode->name, "test_group")) {
97
 
                        xmlNodePtr test = subnode->children;
98
 
                        gchar *descr = xmlGetProp (subnode, "descr");
99
 
 
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);
102
 
                        g_free (descr);
103
 
 
104
 
                        while (test) {
105
 
                                if (!strcmp (test->name, "test")) {
106
 
                                        gchar *sql = xmlGetProp (test, "sql");
107
 
                                        gchar *expected = xmlGetProp (test, "expect");
108
 
 
109
 
                                        if (sql) {
110
 
                                                make_query_test (conf, out, sql, 
111
 
                                                                 expected && (*expected=='N') ? FALSE : TRUE);
112
 
                                                g_free (sql);
113
 
                                        }
114
 
                                        if (expected)
115
 
                                                g_free (expected);
116
 
                                }
117
 
                                test = test->next;
118
 
                        }
119
 
                }
120
 
                subnode = subnode->next;
121
 
        }
122
 
        xmlFreeDoc (doc);
123
 
 
124
 
        if (in_file) {
125
 
                /* HTML end */
126
 
                g_fprintf (out, "</table>\n");
127
 
                g_fprintf (out, "</body>\n");
128
 
                g_fprintf (out, "</html>\n");
129
 
                fclose (out);
130
 
        }
131
 
 
132
 
        if (conf)
133
 
                g_object_unref (G_OBJECT (conf));
134
 
        return 0;
135
 
}
136
 
 
137
 
void
138
 
make_query_test (MgConf *conf, FILE *stream, const gchar *sql, gboolean expected_result)
139
 
{
140
 
        MgQuery *query;
141
 
        GError *error = NULL;
142
 
 
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 = "";
147
 
                
148
 
                if (!expected_result)
149
 
                        extra = "bgcolor=\"red\"";
150
 
 
151
 
                sql2 = mg_renderer_render_as_sql (MG_RENDERER (query), NULL, MG_RENDERER_EXTRA_VAL_ATTRS, &error);
152
 
                if (sql2)
153
 
                        g_fprintf (stream, "<td>%s</td><td></td>", sql2);
154
 
                else {
155
 
                        if (error && error->message)
156
 
                                g_fprintf (stream, "<td>---</td><td %s>PARSING gen. error: %s</td>", 
157
 
                                           extra, error->message);
158
 
                        else
159
 
                                g_fprintf (stream, "<td>---</td><td %s>PARSING gen. error: NO ERROR MSG</td>", 
160
 
                                           extra);
161
 
                        if (error)
162
 
                                g_error_free (error);
163
 
                }
164
 
        }
165
 
        else {
166
 
                gchar *extra = "";              
167
 
                if (expected_result)
168
 
                        extra = "bgcolor=\"red\"";
169
 
 
170
 
                if (error && error->message)
171
 
                        g_fprintf (stream, "<td>---</td><td %s>PARSING gen. error: %s</td>", 
172
 
                                   extra, error->message);
173
 
                else
174
 
                        g_fprintf (stream, "<td>---</td><td %s>PARSING gen. error: NO ERROR MSG</td>", 
175
 
                                   extra);
176
 
                if (error)
177
 
                        g_error_free (error);
178
 
        }
179
 
        g_fprintf (stream, "</tr>\n");
180
 
}