1 /*
2  * Copyright (C) 2007 - 2013 Vivien Malerba <malerba@gnome-db.org>
3  * Copyright (C) 2012 Daniel Espinosa <despinosa@src.gnome.org>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #define PROVIDER "PostgreSQL"
23 #include "prov-test-common.h"
24 #include <sql-parser/gda-sql-parser.h>
25 #include "../test-errors.h"
26 
27 #define CHECK_EXTRA_INFO 1
28 
29 extern GdaProviderInfo *pinfo;
30 extern GdaConnection   *cnc;
31 extern gboolean         params_provided;
32 extern gboolean         fork_tests;
33 
34 static int test_timestamp_change_format (void);
35 
36 int
main(int argc,char ** argv)37 main (int argc, char **argv)
38 {
39 	int number_failed = 0;
40 	fork_tests = FALSE;
41 
42 	gda_init ();
43 
44 	pinfo = gda_config_get_provider_info (PROVIDER);
45 	if (!pinfo) {
46 		g_warning ("Could not find provider information for %s", PROVIDER);
47 		return EXIT_SUCCESS;
48 	}
49 	g_print ("============= Provider now testing: %s =============\n", pinfo->id);
50 
51 	number_failed = prov_test_common_setup ();
52 
53 	if (cnc) {
54 		number_failed += prov_test_common_check_timestamp ();
55 		number_failed += prov_test_common_check_date ();
56 		number_failed += test_timestamp_change_format ();
57 		number_failed += prov_test_common_check_meta ();
58 		number_failed += prov_test_common_check_meta_identifiers (TRUE, TRUE);
59 		number_failed += prov_test_common_check_meta_identifiers (TRUE, FALSE);
60 		number_failed += prov_test_common_check_meta_identifiers (FALSE, TRUE);
61 		number_failed += prov_test_common_check_meta_identifiers (FALSE, FALSE);
62 		number_failed += prov_test_common_load_data ();
63 		number_failed += prov_test_common_check_cursor_models ();
64 		number_failed += prov_test_common_check_data_select ();
65 		number_failed += prov_test_common_clean ();
66 	}
67 
68 
69 	g_print ("Test %s\n", (number_failed == 0) ? "Ok" : "failed");
70 	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
71 }
72 
73 static int
test_timestamp_change_format(void)74 test_timestamp_change_format (void)
75 {
76 	GdaSqlParser *parser = NULL;
77 	GdaStatement *stmt = NULL;
78 	GError *error = NULL;
79 	int number_failed = 0;
80 	GdaDataModel *model = NULL;
81 	GValue *value = NULL;
82 
83 #ifdef CHECK_EXTRA_INFO
84 	g_print ("\n============= %s() =============\n", __FUNCTION__);
85 #endif
86 
87 	parser = gda_connection_create_parser (cnc);
88 	if (!parser)
89 		parser = gda_sql_parser_new ();
90 
91 	/* change date style */
92 	stmt = gda_sql_parser_parse_string (parser, "SET datestyle to 'SQL'",
93 					    NULL, &error);
94 	if (!stmt ||
95 	    (gda_connection_statement_execute_non_select (cnc, stmt, NULL, NULL, &error) == -1)) {
96 		number_failed ++;
97 		goto out;
98 	}
99 	g_print ("Changed datestyle to SQL\n");
100 
101 	/* Check that date format has changed */
102 	GdaDataHandler *dh;
103 	gchar *str, *estr;
104 	GDate *date = NULL;
105 	dh = gda_server_provider_get_data_handler_g_type (gda_connection_get_provider (cnc), cnc, G_TYPE_DATE);
106 	date = g_date_new_dmy (28, G_DATE_SEPTEMBER, 2013);
107 	g_value_set_boxed ((value = gda_value_new (G_TYPE_DATE)), date);
108 	g_date_free (date);
109 	str = gda_data_handler_get_str_from_value (dh, value);
110 	estr = "09/28/2013";
111 	if (strcmp (str, estr)) {
112 		g_set_error (&error, TEST_ERROR, TEST_ERROR_GENERIC,
113 			     "GdaDataHandler converted date to STR in an unexpected way: got '%s' and expected '%s'", str, estr);
114 		number_failed ++;
115 		goto out;
116 	}
117 	g_free (str);
118 
119 	str = gda_data_handler_get_sql_from_value (dh, value);
120 	estr = "'09/28/2013'";
121 	if (strcmp (str, estr)) {
122 		g_set_error (&error, TEST_ERROR, TEST_ERROR_GENERIC,
123 			     "GdaDataHandler converted date to SQL in an unexpected way: got '%s' and expected '%s'", str, estr);
124 		number_failed ++;
125 		goto out;
126 	}
127 	g_free (str);
128 
129 	/* change date style */
130 	stmt = gda_sql_parser_parse_string (parser, "SET datestyle to 'ISO'",
131 					    NULL, &error);
132 	if (!stmt ||
133 	    (gda_connection_statement_execute_non_select (cnc, stmt, NULL, NULL, &error) == -1)) {
134 		number_failed ++;
135 		goto out;
136 	}
137 	g_print ("Changed datestyle to ISO\n");
138 
139 	/* Check that date format has changed */
140 	str = gda_data_handler_get_str_from_value (dh, value);
141 	estr = "2013-09-28";
142 	if (strcmp (str, estr)) {
143 		g_set_error (&error, TEST_ERROR, TEST_ERROR_GENERIC,
144 			     "GdaDataHandler converted date in an unexpected way: got '%s' and expected '%s'", str, estr);
145 		number_failed ++;
146 		goto out;
147 	}
148 out:
149 	if (value)
150 		gda_value_free (value);
151 	if (stmt)
152 		g_object_unref (stmt);
153 	g_object_unref (parser);
154 
155 #ifdef CHECK_EXTRA_INFO
156 	g_print ("Date format test resulted in %d error(s)\n", number_failed);
157 	if (number_failed != 0)
158 		g_print ("error: %s\n", error && error->message ? error->message : "No detail");
159 	if (error)
160 		g_error_free (error);
161 #endif
162 
163 	return number_failed;
164 }
165