1 /****************************************************************************
2 *
3 * MODULE: test.dbmi_base.lib
4 *
5 * AUTHOR(S): Original author
6 * Soeren Gebbert soerengebbert <at> googlemail <dot> com
7 * 2010 Braunschweig, Germany
8 *
9 * PURPOSE: Unit and integration tests for the dbmi_base library
10 *
11 * COPYRIGHT: (C) 2007 by the GRASS Development Team
12 *
13 * This program is free software under the GNU General Public
14 * License (>=v2). Read the file COPYING that comes with GRASS
15 * for details.
16 *
17 *****************************************************************************/
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <grass/gis.h>
23 #include <grass/glocale.h>
24 #include <grass/dbmi.h>
25 #include "test_dbmi_base_lib.h"
26
27 /* prototypes */
28 static int test_copy_column(void);
29
30 /* ************************************************************************* */
31 /* Performe the column unit tests ****************************************** */
32 /* ************************************************************************* */
unit_test_column(void)33 int unit_test_column(void)
34 {
35 int sum = 0;
36
37 G_message(_("\n++ Running column unit tests ++"));
38
39 sum += test_copy_column();
40
41 if (sum > 0)
42 G_warning(_("\n-- column unit tests failure --"));
43 else
44 G_message(_("\n-- column unit tests finished successfully --"));
45
46 return sum;
47 }
48
49 /* *************************************************************** */
50 /* Test copy dbColumn functionality ****************************** */
51 /* *************************************************************** */
test_copy_column(void)52 int test_copy_column(void)
53 {
54 int sum = 0;
55 dbColumn *column, *c2;
56
57 G_message(_("\n++ Run test copy column ++"));
58
59 /*Test the set and copy function*/
60 column = (dbColumn*)db_calloc(sizeof(dbColumn), 2);
61 db_init_column(&column[0]);
62 db_init_column(&column[1]);
63
64 /*Write some initial values*/
65 db_set_value_double(&column[0].defaultValue, 0.5);
66 db_set_value_double(&column[0].value, 10.5);
67
68 db_set_column_description(&column[0], "Test column");
69 db_set_column_host_type(&column[0], 1);
70 db_set_column_length(&column[0], 8);
71 db_set_column_name(&column[0], "test");
72 db_set_column_null_allowed(&column[0]);
73 db_set_column_precision(&column[0], 20);
74 db_set_column_scale(&column[0], 1);
75 db_set_column_select_priv_granted(&column[0]);
76 db_set_column_sqltype(&column[0], DB_SQL_TYPE_DOUBLE_PRECISION);
77 db_set_column_select_priv_granted(&column[0]);
78 db_set_column_update_priv_granted(&column[0]);
79 db_set_column_use_default_value(&column[0]);
80
81 /*Copy the column*/
82 db_copy_column(&column[1], &column[0]);
83 c2 = db_copy_column(NULL, &column[1]);
84
85 fprintf(stdout, "##### First column:\n");
86 db_print_column_definition(stdout, &column[0]);
87 fprintf(stdout, "##### Second column:\n");
88 db_print_column_definition(stdout, &column[1]);
89 fprintf(stdout, "##### Third column:\n");
90 db_print_column_definition(stdout, c2);
91
92 /*Compare the columns*/
93 if(strcmp(column[0].columnName.string, c2->columnName.string) != 0) {
94 G_warning("Error copying column name");
95 sum++;
96 }
97 if(strcmp(column[0].description.string, c2->description.string) != 0) {
98 G_warning("Error copying column description");
99 sum++;
100 }
101 if(column[0].dataLen != c2->dataLen) {
102 G_warning("Error copying dataLen");
103 sum++;
104 }
105 if(column[0].defaultValue.d != c2->defaultValue.d) {
106 G_warning("Error copying default value");
107 sum++;
108 }
109 if(column[0].hasDefaultValue != c2->hasDefaultValue) {
110 G_warning("Error copying hasDefaultValue");
111 sum++;
112 }
113 if(column[0].hostDataType != column[1].hostDataType) {
114 G_warning("Error copying hostDataType");
115 sum++;
116 }
117 if(column[0].nullAllowed != c2->nullAllowed) {
118 G_warning("Error copying nullAllowed");
119 sum++;
120 }
121 if(column[0].precision != c2->precision) {
122 G_warning("Error copying precision");
123 sum++;
124 }
125 if(column[0].scale != c2->scale) {
126 G_warning("Error copying scale");
127 sum++;
128 }
129 if(column[0].select != c2->select) {
130 G_warning("Error copying select");
131 sum++;
132 }
133 if(column[0].sqlDataType != c2->sqlDataType) {
134 G_warning("Error copying sqlDataType");
135 sum++;
136 }
137 if(column[0].update != c2->update) {
138 G_warning("Error copying update");
139 sum++;
140 }
141 if(column[0].useDefaultValue != c2->useDefaultValue) {
142 G_warning("Error copying useDefaultValue");
143 sum++;
144 }
145 if(column[0].value.d != c2->value.d) {
146 G_warning("Error copying value");
147 sum++;
148 }
149
150 G_message(_("\n++ Test copy column finished ++"));
151
152 return sum;
153 }
154
155