File: check_gpkgCreateFeaturesTable.c

package info (click to toggle)
spatialite 5.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 74,240 kB
  • sloc: ansic: 587,051; makefile: 8,583; sh: 4,276; yacc: 1,973; xml: 717
file content (534 lines) | stat: -rw-r--r-- 15,414 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*

 check_gkpgCreateFeaturesTable.c - Test case for GeoPackage Extensions

 Author: Brad Hards <bradh@frogmouth.net>

 ------------------------------------------------------------------------------
 
 Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
 The contents of this file are subject to the Mozilla Public License Version
 1.1 (the "License"); you may not use this file except in compliance with
 the License. You may obtain a copy of the License at
 http://www.mozilla.org/MPL/
 
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.

The Original Code is GeoPackage extensions

The Initial Developer of the Original Code is Brad Hards
 
Portions created by the Initial Developer are Copyright (C) 2012-2013
the Initial Developer. All Rights Reserved.

Contributor(s):


Alternatively, the contents of this file may be used under the terms of
either the GNU General Public License Version 2 or later (the "GPL"), or
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
in which case the provisions of the GPL or the LGPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of either the GPL or the LGPL, and not to allow others to
use your version of this file under the terms of the MPL, indicate your
decision by deleting the provisions above and replace them with the notice
and other provisions required by the GPL or the LGPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the MPL, the GPL or the LGPL.
 
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "sqlite3.h"
#include "spatialite.h"

#include "test_helpers.h"

int
main (int argc UNUSED, char *argv[]UNUSED)
{
    sqlite3 *db_handle = NULL;
    int ret;
    char *err_msg = NULL;
    void *cache = spatialite_alloc_connection ();

    ret =
	sqlite3_open_v2 (":memory:", &db_handle,
			 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    /* For debugging / testing if required */
    /*
       ret = sqlite3_open_v2 ("check_gkpgCreateFeaturesTable.sqlite", &db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
     */
    spatialite_init_ex (db_handle, cache, 0);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "cannot open in-memory db: %s\n",
		   sqlite3_errmsg (db_handle));
	  sqlite3_close (db_handle);
	  db_handle = NULL;
	  return -1;
      }

    ret =
	sqlite3_exec (db_handle, "SELECT gpkgCreateBaseTables()", NULL, NULL,
		      &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr,
		   "Unexpected gpkgCreateBaseTables() result: %i, (%s)\n", ret,
		   err_msg);
	  sqlite3_free (err_msg);
	  return -100;
      }

    ret =
	sqlite3_exec (db_handle,
		      "CREATE TABLE \"testfeats1\" (id INTEGER PRIMARY KEY AUTOINCREMENT, text_attribute TEXT, real_attribute REAL, boolean_attribute BOOLEAN, raster_or_photo BLOB)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr,
		   "Unexpected create testfeats1 table result: %i, (%s)\n", ret,
		   err_msg);
	  sqlite3_free (err_msg);
	  return -101;
      }

    ret =
	sqlite3_exec (db_handle,
		      "SELECT gpkgAddGeometryColumn('testfeats1', 'thegeom', 'POINT', 0, 0, 4326)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr,
		   "Unexpected gpkgAddGeometryColumn() result: %i, (%s)\n", ret,
		   err_msg);
	  sqlite3_free (err_msg);
	  return -102;
      }

    ret =
	sqlite3_exec (db_handle,
		      "SELECT gpkgAddGeometryColumn('testfeats1', 'thegeom', 'POINT', 0, 0, 4326)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_ERROR)
      {
	  fprintf (stderr,
		   "Expected error for add geometry column duplicate column name, got %i\n",
		   ret);
	  sqlite3_free (err_msg);
	  return -103;
      }

#if 0
    /* check gpkg_contents table entry is OK */
    ret =
	sqlite3_get_table (db_handle,
			   "SELECT data_type, identifier, description, last_change, min_x, min_y, max_x, max_y, srs_id FROM gpkg_contents WHERE table_name = 'testtiles1'",
			   &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error1: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -104;
      }
    if ((rows != 1) || (columns != 9))
      {
	  sqlite3_free_table (results);
	  fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows,
		   columns);
	  return -105;
      }
    if (strcmp (results[1 * columns + 0], "tiles") != 0)
      {
	  fprintf (stderr,
		   "Unexpected data_type result (got %s, expected tiles)",
		   results[1 * columns + 0]);
	  sqlite3_free_table (results);
	  return -106;
      }
    if (results[1 * columns + 1] != NULL)
      {
	  fprintf (stderr,
		   "Unexpected identifier result (got %s, expected NULL)",
		   results[1 * columns + 1]);
	  sqlite3_free_table (results);
	  return -107;
      }
    if (strcmp (results[1 * columns + 2], "") != 0)
      {
	  fprintf (stderr,
		   "Unexpected description result (got %s, expected an empty string)",
		   results[1 * columns + 2]);
	  sqlite3_free_table (results);
	  return -108;
      }
    /* TODO: this test should be more rigorous */
    if (results[1 * columns + 3] == NULL)
      {
	  fprintf (stderr, "Unexpected last_change result - null)",
		   results[1 * columns + 3]);
	  sqlite3_free_table (results);
	  return -109;
      }
    if ((results[1 * columns + 4] == NULL)
	|| (strcmp (results[1 * columns + 4], "-180.0") != 0))
      {
	  fprintf (stderr, "Unexpected min_x result (got %s, expected -180.0)",
		   results[1 * columns + 4]);
	  sqlite3_free_table (results);
	  return -110;
      }
    if ((results[1 * columns + 5] == NULL)
	|| (strcmp (results[1 * columns + 5], "-90.0") != 0))
      {
	  fprintf (stderr, "Unexpected min_y result (got %s, expected -90.0)",
		   results[1 * columns + 5]);
	  sqlite3_free_table (results);
	  return -111;
      }
    if ((results[1 * columns + 6] == NULL)
	|| (strcmp (results[1 * columns + 6], "180.0") != 0))
      {
	  fprintf (stderr, "Unexpected max_x result (got %s, expected 180.0)",
		   results[1 * columns + 6]);
	  sqlite3_free_table (results);
	  return -112;
      }
    if ((results[1 * columns + 7] == NULL)
	|| (strcmp (results[1 * columns + 7], "90.0") != 0))
      {
	  fprintf (stderr, "Unexpected max_y result (got %s, expected 90.0)",
		   results[1 * columns + 7]);
	  sqlite3_free_table (results);
	  return -113;
      }
    if ((results[1 * columns + 8] == NULL)
	|| (strcmp (results[1 * columns + 8], "4326") != 0))
      {
	  fprintf (stderr, "Unexpected srid result (got %s, expected 4326)",
		   results[1 * columns + 8]);
	  sqlite3_free_table (results);
	  return -114;
      }
    sqlite3_free_table (results);

    /* check gpkg_tile_matrix_set table entry is OK */
    ret =
	sqlite3_get_table (db_handle,
			   "SELECT min_x, min_y, max_x, max_y, srs_id FROM gpkg_tile_matrix_set WHERE table_name = 'testtiles1'",
			   &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error1: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -140;
      }
    if ((rows != 1) || (columns != 5))
      {
	  sqlite3_free_table (results);
	  fprintf (stderr, "Unexpected row / column count: %i x %i\n", rows,
		   columns);
	  return -141;
      }
    if ((results[1 * columns + 0] == NULL)
	|| (strcmp (results[1 * columns + 0], "-180.0") != 0))
      {
	  fprintf (stderr, "Unexpected min_x result (got %s, expected -180.0)",
		   results[1 * columns + 0]);
	  sqlite3_free_table (results);
	  return -142;
      }
    if ((results[1 * columns + 1] == NULL)
	|| (strcmp (results[1 * columns + 1], "-90.0") != 0))
      {
	  fprintf (stderr, "Unexpected min_y result (got %s, expected -90.0)",
		   results[1 * columns + 1]);
	  sqlite3_free_table (results);
	  return -143;
      }
    if ((results[1 * columns + 2] == NULL)
	|| (strcmp (results[1 * columns + 2], "180.0") != 0))
      {
	  fprintf (stderr, "Unexpected max_x result (got %s, expected 180.0)",
		   results[1 * columns + 2]);
	  sqlite3_free_table (results);
	  return -144;
      }
    if ((results[1 * columns + 3] == NULL)
	|| (strcmp (results[1 * columns + 3], "90.0") != 0))
      {
	  fprintf (stderr, "Unexpected max_y result (got %s, expected 90.0)",
		   results[1 * columns + 3]);
	  sqlite3_free_table (results);
	  return -145;
      }
    if ((results[1 * columns + 4] == NULL)
	|| (strcmp (results[1 * columns + 4], "4326") != 0))
      {
	  fprintf (stderr, "Unexpected srid result (got %s, expected 4326)",
		   results[1 * columns + 4]);
	  sqlite3_free_table (results);
	  return -146;
      }
    sqlite3_free_table (results);
#endif
    sqlite3_free (err_msg);

    ret =
	sqlite3_exec (db_handle,
		      "CREATE TABLE \"ctd\" (id INTEGER PRIMARY KEY AUTOINCREMENT, text_attribute TEXT, real_attribute REAL, boolean_attribute BOOLEAN, raster_or_photo BLOB)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Unexpected create ctd table result: %i, (%s)\n",
		   ret, err_msg);
	  sqlite3_free (err_msg);
	  return -200;
      }
    ret =
	sqlite3_exec (db_handle,
		      "SELECT gpkgAddGeometryColumn('ctd', 2.4, 'POINT', 0, 0, 4326)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_ERROR)
      {
	  fprintf (stderr,
		   "Expected error for add geometry column bad column type, got %i\n",
		   ret);
	  sqlite3_free (err_msg);
	  return -201;
      }
    if (strcmp
	(err_msg,
	 "gpkgAddGeometryColumn() error: argument 2 [geometry_column_name] is not of the string type")
	!= 0)
      {
	  fprintf (stderr,
		   "Unexpected error message for gpkgAddGeometryColumn arg 2: %s\n",
		   err_msg);
	  sqlite3_free (err_msg);
	  return -202;
      }
    sqlite3_free (err_msg);


    ret =
	sqlite3_exec (db_handle,
		      "SELECT gpkgAddGeometryColumn('ctd', 'the_geom', 4, 0, 0, 4326)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_ERROR)
      {
	  fprintf (stderr,
		   "Expected error for add geometry column bad geom type, got %i\n",
		   ret);
	  sqlite3_free (err_msg);
	  return -203;
      }
    if (strcmp
	(err_msg,
	 "gpkgAddGeometryColumn() error: argument 3 [geometry_type] is not of the string type")
	!= 0)
      {
	  fprintf (stderr,
		   "Unexpected error message for gpkgAddGeometryColumn arg 3: %s\n",
		   err_msg);
	  sqlite3_free (err_msg);
	  return -204;
      }
    sqlite3_free (err_msg);

    ret =
	sqlite3_exec (db_handle,
		      "SELECT gpkgAddGeometryColumn(8.3, 'the_geom', 'POINT', 0, 0, 4326)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_ERROR)
      {
	  fprintf (stderr,
		   "Expected error for add geometry column bad table type, got %i\n",
		   ret);
	  sqlite3_free (err_msg);
	  return -205;
      }
    if (strcmp
	(err_msg,
	 "gpkgAddGeometryColumn() error: argument 1 [table] is not of the string type")
	!= 0)
      {
	  fprintf (stderr,
		   "Unexpected error message for gpkgAddGeometryColumn arg 1: %s\n",
		   err_msg);
	  sqlite3_free (err_msg);
	  return -206;
      }
    sqlite3_free (err_msg);

    ret =
	sqlite3_exec (db_handle,
		      "SELECT gpkgAddGeometryColumn('ctd', 'the_geom', 'POINT', 'z', 0, 4326)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_ERROR)
      {
	  fprintf (stderr,
		   "Expected error for add geometry column bad z type, got %i\n",
		   ret);
	  sqlite3_free (err_msg);
	  return -207;
      }
    if (strcmp
	(err_msg,
	 "gpkgAddGeometryColumn() error: argument 4 [with_z] is not of the integer type")
	!= 0)
      {
	  fprintf (stderr,
		   "Unexpected error message for gpkgAddGeometryColumn arg 4: %s\n",
		   err_msg);
	  sqlite3_free (err_msg);
	  return -208;
      }
    sqlite3_free (err_msg);

    ret =
	sqlite3_exec (db_handle,
		      "SELECT gpkgAddGeometryColumn('ctd', 'the_geom', 'POINT', 0, 'm', 4326)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_ERROR)
      {
	  fprintf (stderr,
		   "Expected error for add geometry column bad m type, got %i\n",
		   ret);
	  sqlite3_free (err_msg);
	  return -209;
      }
    if (strcmp
	(err_msg,
	 "gpkgAddGeometryColumn() error: argument 5 [with_m] is not of the integer type")
	!= 0)
      {
	  fprintf (stderr,
		   "Unexpected error message for gpkgAddGeometryColumn arg 5: %s\n",
		   err_msg);
	  sqlite3_free (err_msg);
	  return -210;
      }
    sqlite3_free (err_msg);

    ret =
	sqlite3_exec (db_handle,
		      "SELECT gpkgAddGeometryColumn('ctd', 'the_geom', 'POINT', 3, 0, 4326)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_ERROR)
      {
	  fprintf (stderr,
		   "Expected error for add geometry column bad z value, got %i\n",
		   ret);
	  sqlite3_free (err_msg);
	  return -211;
      }
    if (strcmp
	(err_msg,
	 "gpkgAddGeometryColumn() error: argument 4 [with_z] is not a known value (expected 0, 1 or 2)")
	!= 0)
      {
	  fprintf (stderr,
		   "Unexpected error message for gpkgAddGeometryColumn arg 4: %s\n",
		   err_msg);
	  sqlite3_free (err_msg);
	  return -212;
      }
    sqlite3_free (err_msg);

    ret =
	sqlite3_exec (db_handle,
		      "SELECT gpkgAddGeometryColumn('ctd', 'the_geom', 'POINT', 0, 3, 4326)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_ERROR)
      {
	  fprintf (stderr,
		   "Expected error for add geometry column bad m value, got %i\n",
		   ret);
	  sqlite3_free (err_msg);
	  return -213;
      }
    if (strcmp
	(err_msg,
	 "gpkgAddGeometryColumn() error: argument 5 [with_m] is not a known value (expected 0, 1 or 2)")
	!= 0)
      {
	  fprintf (stderr,
		   "Unexpected error message for gpkgAddGeometryColumn arg 5: %s\n",
		   err_msg);
	  sqlite3_free (err_msg);
	  return -214;
      }
    sqlite3_free (err_msg);

    ret =
	sqlite3_exec (db_handle,
		      "SELECT gpkgAddGeometryColumn('ctd', 'the_geom', 'POINT', 0, 2, 'srid')",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_ERROR)
      {
	  fprintf (stderr,
		   "Expected error for add geometry column bad srid type, got %i\n",
		   ret);
	  sqlite3_free (err_msg);
	  return -215;
      }
    if (strcmp
	(err_msg,
	 "gpkgAddGeometryColumn() error: argument 6 [srid] is not of the integer type")
	!= 0)
      {
	  fprintf (stderr,
		   "Unexpected error message for gpkgAddGeometryColumn arg 6: %s\n",
		   err_msg);
	  sqlite3_free (err_msg);
	  return -216;
      }
    sqlite3_free (err_msg);

    ret =
	sqlite3_exec (db_handle,
		      "SELECT gpkgAddGeometryColumn('ctd', 'the_geom', 'blah', 0, 1, 4326)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_ERROR)
      {
	  fprintf (stderr,
		   "Expected error for add geometry column bad geom type value, got %i\n",
		   ret);
	  sqlite3_free (err_msg);
	  return -217;
      }
    if (strcmp
	(err_msg,
	 "gpkgAddGeometryColumn() error: argument 3 [geometry_type] not a recognised geometry type")
	!= 0)
      {
	  fprintf (stderr,
		   "Unexpected error message for gpkgAddGeometryColumn arg 3: %s\n",
		   err_msg);
	  sqlite3_free (err_msg);
	  return -218;
      }
    sqlite3_free (err_msg);

    ret = sqlite3_close (db_handle);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "sqlite3_close() error: %s\n",
		   sqlite3_errmsg (db_handle));
	  return -300;
      }

    spatialite_cleanup_ex (cache);
    spatialite_shutdown ();

    return 0;
}