~ubuntu-branches/ubuntu/wily/spatialite/wily-proposed

« back to all changes in this revision

Viewing changes to test/check_gpkg_base_core_contents_data_table_def.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-07-14 11:57:46 UTC
  • mfrom: (16.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20150714115746-e2iljfmb5sq7o5hh
Tags: 4.3.0-1
Move from experimental to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
 check_gpkg_base_core_contents_data_table_def - Test case for GeoPackage Extensions
 
4
 
 
5
 Author: Brad Hards <bradh@frogmouth.net>
 
6
 
 
7
 ------------------------------------------------------------------------------
 
8
 
 
9
 Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
10
 
 
11
 The contents of this file are subject to the Mozilla Public License Version
 
12
 1.1 (the "License"); you may not use this file except in compliance with
 
13
 the License. You may obtain a copy of the License at
 
14
 http://www.mozilla.org/MPL/
 
15
 
 
16
Software distributed under the License is distributed on an "AS IS" basis,
 
17
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
18
for the specific language governing rights and limitations under the
 
19
License.
 
20
 
 
21
The Original Code is GeoPackage extensions
 
22
 
 
23
The Initial Developer of the Original Code is Brad Hards
 
24
 
 
25
Portions created by the Initial Developer are Copyright (C) 2013
 
26
the Initial Developer. All Rights Reserved.
 
27
 
 
28
Contributor(s):
 
29
 
 
30
 
 
31
Alternatively, the contents of this file may be used under the terms of
 
32
either the GNU General Public License Version 2 or later (the "GPL"), or
 
33
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
34
in which case the provisions of the GPL or the LGPL are applicable instead
 
35
of those above. If you wish to allow use of your version of this file only
 
36
under the terms of either the GPL or the LGPL, and not to allow others to
 
37
use your version of this file under the terms of the MPL, indicate your
 
38
decision by deleting the provisions above and replace them with the notice
 
39
and other provisions required by the GPL or the LGPL. If you do not delete
 
40
the provisions above, a recipient may use your version of this file under
 
41
the terms of any one of the MPL, the GPL or the LGPL.
 
42
 
 
43
*/
 
44
 
 
45
#include <stdbool.h>
 
46
#include <stdlib.h>
 
47
#include <stdio.h>
 
48
#include <ctype.h>
 
49
 
 
50
#define __USE_GNU
 
51
#include <string.h>
 
52
 
 
53
#include "sqlite3.h"
 
54
#include "spatialite.h"
 
55
 
 
56
#include "test_helpers.h"
 
57
 
 
58
struct tableDefinitionElement
 
59
{
 
60
    const char *column_name;
 
61
    const char *column_type;
 
62
    bool mayBeNull;
 
63
    const char *defaultValue;
 
64
    const char *keySignature;
 
65
    bool wasFound;
 
66
};
 
67
 
 
68
struct tableDefinitionElement tableValues[] = {
 
69
    {"table_name", "TEXT", false, NULL, "PRIMARY KEY", false},
 
70
    {"data_type", "TEXT", false, NULL, NULL, false},
 
71
    {"identifier", "TEXT", true, NULL, NULL, false},
 
72
    {"description", "TEXT", true, "''", NULL, false},
 
73
    {"min_x", "DOUBLE", true, NULL, NULL, false},
 
74
    {"min_y", "DOUBLE", true, NULL, NULL, false},
 
75
    {"max_x", "DOUBLE", true, NULL, NULL, false},
 
76
    {"max_y", "DOUBLE", true, NULL, NULL, false},
 
77
    {"srs_id", "INTEGER", true, NULL, NULL, false},
 
78
    {NULL, NULL, false, NULL, NULL, false}
 
79
};
 
80
 
 
81
int
 
82
main (int argc UNUSED, char *argv[]UNUSED)
 
83
{
 
84
    sqlite3 *db_handle = NULL;
 
85
    int ret;
 
86
    char *err_msg = NULL;
 
87
    char *sql_statement;
 
88
    sqlite3_stmt *stmt;
 
89
    void *cache = spatialite_alloc_connection ();
 
90
    int i, j;
 
91
    char *sql;
 
92
    char *str;
 
93
    char *token;
 
94
    char *saveptr;
 
95
    bool valid_last_change = false;
 
96
    bool valid_fk_constraint = false;
 
97
 
 
98
    ret =
 
99
        sqlite3_open_v2 (":memory:", &db_handle,
 
100
                         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
 
101
    /* For debugging / testing if required */
 
102
    /*
 
103
       ret = sqlite3_open_v2 ("check_gpkg_base_core_spatial_ref_sys_data_table_def.sqlite", &db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
 
104
     */
 
105
    spatialite_init_ex (db_handle, cache, 0);
 
106
    if (ret != SQLITE_OK)
 
107
      {
 
108
          fprintf (stderr, "cannot open in-memory db: %s\n",
 
109
                   sqlite3_errmsg (db_handle));
 
110
          sqlite3_close (db_handle);
 
111
          db_handle = NULL;
 
112
          return -1;
 
113
      }
 
114
 
 
115
    ret =
 
116
        sqlite3_exec (db_handle, "SELECT gpkgCreateBaseTables()", NULL, NULL,
 
117
                      &err_msg);
 
118
    if (ret != SQLITE_OK)
 
119
      {
 
120
          fprintf (stderr,
 
121
                   "Unexpected gpkgCreateBaseTables() result: %i, (%s)\n", ret,
 
122
                   err_msg);
 
123
          sqlite3_free (err_msg);
 
124
          return -2;
 
125
      }
 
126
 
 
127
    /*
 
128
       / Test Case ID    /base/core/contents/data/table_def
 
129
       / Test Purpose Verify that the gpkg_contents table exists and has the correct definition.
 
130
       / Test Method
 
131
       /    SELECT sql FROM sqlite_master WHERE type = table AND tbl_name = gpkg_contents
 
132
       /    Fail if returns an empty result set.
 
133
       /    Pass if the column names and column definitions in the returned CREATE TABLE statement,
 
134
       /       including data type, nullability, default values and primary, foreign and unique key
 
135
       /       constraints match all of those in the contents of C.2 Table 18. Column order, check
 
136
       /       constraint and trigger definitions, and other column definitions in the returned sql
 
137
       /       are irrelevant.
 
138
       /    Fail Otherwise
 
139
       / Reference  Clause 1.1.3.1.1 Req 13:
 
140
       / Test Type Basic
 
141
     */
 
142
    sql_statement =
 
143
        "SELECT sql FROM sqlite_master WHERE type = 'table' AND tbl_name = 'gpkg_contents'";
 
144
    ret =
 
145
        sqlite3_prepare_v2 (db_handle, sql_statement, strlen (sql_statement),
 
146
                            &stmt, NULL);
 
147
    if (ret != SQLITE_OK)
 
148
      {
 
149
          fprintf (stderr, "failed to prepare SQL SELECT statement: %i (%s)\n",
 
150
                   ret, sqlite3_errmsg (db_handle));
 
151
          return -10;
 
152
      }
 
153
    ret = sqlite3_step (stmt);
 
154
    if (ret != SQLITE_ROW)
 
155
      {
 
156
          fprintf (stderr, "unexpected return value for first step: %i (%s)\n",
 
157
                   ret, sqlite3_errmsg (db_handle));
 
158
          return -11;
 
159
      }
 
160
    if (sqlite3_column_type (stmt, 0) != SQLITE_TEXT)
 
161
      {
 
162
          fprintf (stderr, "bad type for column 0: %i\n",
 
163
                   sqlite3_column_type (stmt, 0));
 
164
          return -12;
 
165
      }
 
166
    sql =
 
167
        strdup (strstr ((const char *) sqlite3_column_text (stmt, 0), "(") +
 
168
                strlen ("("));
 
169
    if (!sql)
 
170
      {
 
171
          fprintf (stderr, "unrecognised format for SQL\n");
 
172
          return -13;
 
173
      }
 
174
 
 
175
    for (j = 1, str = sql;; j++, str = NULL)
 
176
      {
 
177
          token = strtok_r (str, ",", &saveptr);
 
178
          if (token == NULL)
 
179
            {
 
180
                break;
 
181
            }
 
182
          for (i = 0; tableValues[i].column_name != NULL; ++i)
 
183
            {
 
184
                while (isspace (*token))
 
185
                  {
 
186
                      token++;
 
187
                  }
 
188
                if (strncasecmp (token, "CONSTRAINT", strlen ("CONSTRAINT")) ==
 
189
                    0)
 
190
                  {
 
191
                      if (strcasestr
 
192
                          (token,
 
193
                           "FOREIGN KEY (srs_id) REFERENCES gpkg_spatial_ref_sys(srs_id)"))
 
194
                        {
 
195
                            valid_fk_constraint = true;
 
196
                        }
 
197
                      continue;
 
198
                  }
 
199
                if (strcasestr (token, tableValues[i].column_name))
 
200
                  {
 
201
                      if (!strcasestr (token, tableValues[i].column_type))
 
202
                        {
 
203
                            fprintf (stderr,
 
204
                                     "missing COLUMN TYPE %s for %s: %s\n",
 
205
                                     tableValues[i].column_type,
 
206
                                     tableValues[i].column_name, token);
 
207
                            return -20;
 
208
                        }
 
209
                      if (tableValues[i].mayBeNull
 
210
                          && strcasestr (token, "NOT NULL"))
 
211
                        {
 
212
                            fprintf (stderr,
 
213
                                     "Unexpected NOT NULL constraint for %s: %s\n",
 
214
                                     tableValues[i].column_name, token);
 
215
                            return -21;
 
216
                        }
 
217
                      if ((!tableValues[i].mayBeNull)
 
218
                          && (!strcasestr (token, "NOT NULL")))
 
219
                        {
 
220
                            fprintf (stderr,
 
221
                                     "Missing NOT NULL constraint for %s: %s\n",
 
222
                                     tableValues[i].column_name, token);
 
223
                            return -22;
 
224
                        }
 
225
                      if ((tableValues[i].keySignature != NULL)
 
226
                          && (!strcasestr (token, tableValues[i].keySignature)))
 
227
                        {
 
228
                            fprintf (stderr,
 
229
                                     "Missing %s constraint for %s: %s\n",
 
230
                                     tableValues[i].keySignature,
 
231
                                     tableValues[i].column_name, token);
 
232
                            return -23;
 
233
                        }
 
234
                      if ((tableValues[i].keySignature == NULL)
 
235
                          && (strcasestr (token, "KEY")))
 
236
                        {
 
237
                            fprintf (stderr,
 
238
                                     "Unexpected key constraint for %s: %s\n",
 
239
                                     tableValues[i].column_name, token);
 
240
                            return -24;
 
241
                        }
 
242
                      if ((tableValues[i].defaultValue == NULL)
 
243
                          && (strcasestr (token, "DEFAULT")))
 
244
                        {
 
245
                            fprintf (stderr,
 
246
                                     "Unexpected default value for %s: %s\n",
 
247
                                     tableValues[i].column_name, token);
 
248
                            return -25;
 
249
                        }
 
250
                      if ((tableValues[i].defaultValue != NULL)
 
251
                          && (!strcasestr (token, tableValues[i].defaultValue)))
 
252
                        {
 
253
                            fprintf (stderr,
 
254
                                     "Missing default value for %s: %s\n",
 
255
                                     tableValues[i].column_name, token);
 
256
                            return -26;
 
257
                        }
 
258
                      tableValues[i].wasFound = true;
 
259
                  }
 
260
            }
 
261
          if (strcasestr (token, "last_change "))
 
262
            {
 
263
                /* last_change default has an embedded ',' so we need to grab the next token too. */
 
264
                char *nexttoken = strtok_r (str, ",", &saveptr);
 
265
                char *fulltoken =
 
266
                    malloc (strlen (token) + strlen (",") + strlen (nexttoken) +
 
267
                            1);
 
268
                strcpy (fulltoken, token);
 
269
                strcat (fulltoken, ",");
 
270
                strcat (fulltoken, nexttoken);
 
271
                if (!strcasestr (fulltoken, "DATETIME"))
 
272
                  {
 
273
                      fprintf (stderr,
 
274
                               "missing COLUMN TYPE DATETIME for last_change: %s\n",
 
275
                               fulltoken);
 
276
                      free (fulltoken);
 
277
                      return -40;
 
278
                  }
 
279
                if (!strcasestr (fulltoken, "NOT NULL"))
 
280
                  {
 
281
                      fprintf (stderr,
 
282
                               "Missing NOT NULL constraint for last_change: %s\n",
 
283
                               fulltoken);
 
284
                      free (fulltoken);
 
285
                      return -41;
 
286
                  }
 
287
                if (strcasestr (fulltoken, "KEY"))
 
288
                  {
 
289
                      fprintf (stderr,
 
290
                               "Unexpected KEY constraint for last_change: %s\n",
 
291
                               token);
 
292
                      free (fulltoken);
 
293
                      return -42;
 
294
                  }
 
295
                if (!strcasestr
 
296
                    (fulltoken,
 
297
                     "DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ',CURRENT_TIMESTAMP))"))
 
298
                  {
 
299
                      fprintf (stderr,
 
300
                               "Missing default value for last_change: %s\n",
 
301
                               fulltoken);
 
302
                      free (fulltoken);
 
303
                      return -43;
 
304
                  }
 
305
                free (fulltoken);
 
306
                valid_last_change = true;
 
307
            }
 
308
      }
 
309
    free (sql);
 
310
 
 
311
    for (i = 0; tableValues[i].column_name != NULL; ++i)
 
312
      {
 
313
          if (tableValues[i].wasFound == false)
 
314
            {
 
315
                fprintf (stderr, "did not find expected %s column\n",
 
316
                         tableValues[i].column_name);
 
317
                return -50;
 
318
            }
 
319
      }
 
320
    if (!valid_last_change)
 
321
      {
 
322
          fprintf (stderr,
 
323
                   "did not find expected last_change column, or required properties were not found\n");
 
324
          return -51;
 
325
      }
 
326
    if (!valid_fk_constraint)
 
327
      {
 
328
          fprintf (stderr, "did not find expected FK constraint\n");
 
329
          return -52;
 
330
      }
 
331
 
 
332
 
 
333
    ret = sqlite3_step (stmt);
 
334
    if (ret != SQLITE_DONE)
 
335
      {
 
336
          fprintf (stderr, "unexpected return value for second step: %i\n",
 
337
                   ret);
 
338
          return -199;
 
339
      }
 
340
    ret = sqlite3_finalize (stmt);
 
341
 
 
342
    sqlite3_free (err_msg);
 
343
 
 
344
    ret = sqlite3_close (db_handle);
 
345
    if (ret != SQLITE_OK)
 
346
      {
 
347
          fprintf (stderr, "sqlite3_close() error: %s\n",
 
348
                   sqlite3_errmsg (db_handle));
 
349
          return -200;
 
350
      }
 
351
 
 
352
    spatialite_cleanup_ex (cache);
 
353
    spatialite_shutdown ();
 
354
 
 
355
    return 0;
 
356
}