test_table.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <grass/gis.h>
  22. #include <grass/glocale.h>
  23. #include <grass/dbmi.h>
  24. #include "test_dbmi_base_lib.h"
  25. /* prototypes */
  26. static int test_table(void);
  27. static dbColumn * create_column(const char *name, const char *desctiption, int sqltype);
  28. /* ************************************************************************* */
  29. /* Performe the table unit tests ****************************************** */
  30. /* ************************************************************************* */
  31. int unit_test_table(void)
  32. {
  33. int sum = 0;
  34. G_message(_("\n++ Running table unit tests ++"));
  35. sum += test_table();
  36. if (sum > 0)
  37. G_warning(_("\n-- Table unit tests failure --"));
  38. else
  39. G_message(_("\n-- Table unit tests finished successfully --"));
  40. return sum;
  41. }
  42. /* *************************************************************** */
  43. /* Test some functions of the dbTable functionality ************** */
  44. /* *************************************************************** */
  45. int test_table(void)
  46. {
  47. int sum = 0;
  48. dbTable *t1 = db_alloc_table(0);
  49. dbTable *t2;
  50. db_init_table(t1);
  51. /*Append three columns of the different type*/
  52. db_append_table_column(t1, create_column("first", "first column", DB_SQL_TYPE_DOUBLE_PRECISION));
  53. db_append_table_column(t1, create_column("second", "second column", DB_SQL_TYPE_REAL));
  54. db_append_table_column(t1, create_column("third", "third column", DB_SQL_TYPE_DECIMAL));
  55. if(t1->numColumns != 3) {
  56. G_warning("Error appending columns");
  57. sum++;
  58. }
  59. db_set_table_delete_priv_granted(t1);
  60. db_set_table_description(t1, "test table");
  61. db_set_table_insert_priv_granted(t1);
  62. db_set_table_name(t1, "test");
  63. db_set_table_select_priv_granted(t1);
  64. db_set_table_update_priv_granted(t1);
  65. G_message("##### First table:\n");
  66. db_print_table_definition(stdout, t1);
  67. /*Clone the table*/
  68. t2 = db_clone_table(t1);
  69. G_message("##### Second table:\n");
  70. db_print_table_definition(stdout, t2);
  71. /*Compare the tables*/
  72. if(strcmp(t1->description.string, t2->description.string) != 0) {
  73. G_warning("Error copying description");
  74. sum++;
  75. }
  76. if(strcmp(t1->tableName.string, t2->tableName.string) != 0) {
  77. G_warning("Error copying tableName");
  78. sum++;
  79. }
  80. if(t1->numColumns != t2->numColumns) {
  81. G_warning("Error copying table numColumns");
  82. sum++;
  83. }
  84. if(t1->priv_delete != t2->priv_delete) {
  85. G_warning("Error copying privileg delete");
  86. sum++;
  87. }
  88. if(t1->priv_insert != t2->priv_insert) {
  89. G_warning("Error copying privileg insert");
  90. sum++;
  91. }
  92. /*We check only the column names*/
  93. if(strcmp(t1->columns[0].columnName.string, t2->columns[0].columnName.string) != 0) {
  94. G_warning("Error copying first column");
  95. sum++;
  96. }
  97. if(strcmp(t1->columns[1].columnName.string, t2->columns[1].columnName.string) != 0) {
  98. G_warning("Error copying second column");
  99. sum++;
  100. }
  101. if(strcmp(t1->columns[2].columnName.string, t2->columns[2].columnName.string) != 0) {
  102. G_warning("Error copying third column");
  103. sum++;
  104. }
  105. /*Now test the set column and get column by name functions*/
  106. db_set_table_column(t2, 0, create_column("new_first", "new first column", DB_SQL_TYPE_DOUBLE_PRECISION));
  107. db_set_table_column(t2, 1, create_column("new_second", "new second column", DB_SQL_TYPE_REAL));
  108. db_set_table_column(t2, 2, create_column("new_third", "new third column", DB_SQL_TYPE_INTEGER));
  109. G_message("##### Second table new columns:\n");
  110. db_print_table_definition(stdout, t2);
  111. dbColumn *c1 = db_get_table_column_by_name(t2, "new_first");
  112. dbColumn *c2 = db_get_table_column_by_name(t2, "new_second");
  113. dbColumn *c3 = db_get_table_column_by_name(t2, "new_third");
  114. /*We check the column names*/
  115. if(strcmp(c1->columnName.string, "new_first") != 0) {
  116. G_warning("Error set table or get table by name first column");
  117. sum++;
  118. }
  119. if(strcmp(c2->columnName.string, "new_second") != 0) {
  120. G_warning("Error set table or get table by name second column");
  121. sum++;
  122. }
  123. if(strcmp(c3->columnName.string, "new_third") != 0) {
  124. G_warning("Error set table or get table by name third column");
  125. sum++;
  126. }
  127. return sum;
  128. }
  129. /* *************************************************************** */
  130. /* Simple table creation ***************************************** */
  131. /* *************************************************************** */
  132. dbColumn *create_column(const char *name, const char *desctiption, int sqltype)
  133. {
  134. dbColumn *column = (dbColumn*)db_calloc(sizeof(dbColumn), 1);
  135. db_init_column(column);
  136. /*Write some initial values*/
  137. db_set_value_double(&column->defaultValue, 0.5);
  138. db_set_value_double(&column->value, 10.5);
  139. db_set_column_description(column, desctiption);
  140. db_set_column_host_type(column, 1);
  141. db_set_column_length(column, 8);
  142. db_set_column_name(column, name);
  143. db_set_column_null_allowed(column);
  144. db_set_column_precision(column, 20);
  145. db_set_column_scale(column, 1);
  146. db_set_column_select_priv_granted(column);
  147. db_set_column_sqltype(column, sqltype);
  148. db_set_column_select_priv_granted(column);
  149. db_set_column_update_priv_granted(column);
  150. db_set_column_use_default_value(column);
  151. return column;
  152. }