1 /*
2  * Copyright (C) 2008 - 2011 Vivien Malerba <malerba@gnome-db.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18 #include <stdio.h>
19 #include <glib.h>
20 #include <glib-object.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <gmodule.h>
25 #include <libgda/libgda.h>
26 #include <libgda/gda-util.h>
27 #include <sql-parser/gda-sql-parser.h>
28 
29 #include <libxml/parser.h>
30 #include <libxml/tree.h>
31 
32 GdaConnection *cnc;
33 static gint do_test (const xmlChar *id, const xmlChar *sql, gboolean valid_expected);
34 
35 int
main(int argc,char ** argv)36 main (int argc, char** argv)
37 {
38 	xmlDocPtr doc;
39         xmlNodePtr root, node;
40 	gint failures = 0;
41 	gint ntests = 0;
42 	gchar *fname;
43 
44 	gda_init ();
45 
46 	/* open connection */
47 	gchar *cnc_string;
48 	fname = g_build_filename (ROOT_DIR, "data", NULL);
49 	cnc_string = g_strdup_printf ("DB_DIR=%s;DB_NAME=sales_test", fname);
50 	g_free (fname);
51 	cnc = gda_connection_open_from_string ("SQLite", cnc_string, NULL,
52 					       GDA_CONNECTION_OPTIONS_READ_ONLY, NULL);
53 	if (!cnc) {
54 		g_print ("Failed to open connection, cnc_string = %s\n", cnc_string);
55 		exit (1);
56 	}
57 	if (!gda_connection_update_meta_store (cnc, NULL, NULL)) {
58 		g_print ("Failed to update meta store, cnc_string = %s\n", cnc_string);
59 		exit (1);
60 	}
61 	g_free (cnc_string);
62 
63 	/* load file */
64 	fname = g_build_filename (ROOT_DIR, "tests", "parser", "testvalid.xml", NULL);
65 	if (! g_file_test (fname, G_FILE_TEST_EXISTS)) {
66                 g_print ("File '%s' does not exist\n", fname);
67                 exit (1);
68         }
69 
70 	/* use test data */
71 	doc = xmlParseFile (fname);
72 	g_free (fname);
73 	g_assert (doc);
74 	root = xmlDocGetRootElement (doc);
75 	g_assert (!strcmp ((gchar*) root->name, "testdata"));
76 	for (node = root->children; node; node = node->next) {
77 		if (strcmp ((gchar*) node->name, "test"))
78 			continue;
79 		xmlNodePtr snode;
80 		xmlChar *sql = NULL;
81 		xmlChar *id;
82 		gboolean valid = FALSE;
83 
84 		id = xmlGetProp (node, BAD_CAST "id");
85 		for (snode = node->children; snode; snode = snode->next) {
86 			if (!strcmp ((gchar*) snode->name, "sql")) {
87 				sql = xmlNodeGetContent (snode);
88 				xmlChar *prop;
89 				prop = xmlGetProp (snode, "valid");
90 				if (prop) {
91 					if ((*prop == 't') || (*prop == 'T') || (*prop == '1'))
92 						valid = TRUE;
93 					xmlFree (prop);
94 				}
95 			}
96 		}
97 		if (sql) {
98 			if (!do_test (id, sql, valid))
99 				failures++;
100 			ntests++;
101 		}
102 
103 		/* mem free */
104 		if (sql) xmlFree (sql);
105 		if (id)	xmlFree (id);
106 	}
107 	xmlFreeDoc (doc);
108 
109 	g_print ("TESTS COUNT: %d\n", ntests);
110 	g_print ("FAILURES: %d\n", failures);
111 
112 	return failures != 0 ? 1 : 0;
113 }
114 
115 /*
116  * Returns: the number of failures
117  */
118 static gint
do_test(const xmlChar * id,const xmlChar * sql,gboolean valid_expected)119 do_test (const xmlChar *id, const xmlChar *sql, gboolean valid_expected)
120 {
121 	static GdaSqlParser *parser = NULL;
122 	GdaStatement *stmt;
123 	gboolean is_valid;
124 	GError *error = NULL;
125 
126 	if (!parser) {
127 		parser = gda_connection_create_parser (cnc);
128 		if (!parser)
129 			parser = gda_sql_parser_new ();
130 	}
131 
132 #ifdef GDA_DEBUG
133 	g_print ("===== TEST %s SQL: @%s@\n", id, sql);
134 #endif
135 
136 	stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
137 	if (!stmt) {
138 		g_print ("ERROR for test '%s': could not parse statement\n", id);
139 		return FALSE;
140 	}
141 	is_valid = gda_statement_check_validity (stmt, cnc, &error);
142 	if (is_valid && !valid_expected) {
143 		g_print ("ERROR for test '%s': statement is valid but test expected it invalid\n", id);
144 		g_object_unref (stmt);
145 		return FALSE;
146 	}
147 	if (!is_valid && valid_expected) {
148 		g_print ("ERROR for test '%s': statement is invalid but test expected it valid: %s\n", id,
149 			 error && error->message ? error->message : "No detail");
150 		g_object_unref (stmt);
151 		return FALSE;
152 	}
153 	/*g_print ("EXP %d, got %d\n", valid_expected, is_valid);*/
154 	/*g_print ("PARSED: %s\n", gda_statement_serialize (stmt));*/
155 
156 	g_object_unref (stmt);
157 	return TRUE;
158 }
159