Rewrite PSPP output engine.
[pspp-builds.git] / tests / command / get-data-psql.sh
1 #!/bin/sh
2
3 # This program tests the psql import feature.
4
5 TEMPDIR=/tmp/pspp-tst-$$
6 TESTFILE=$TEMPDIR/`basename $0`.sps
7
8 # ensure that top_srcdir and top_builddir  are absolute
9 if [ -z "$top_srcdir" ] ; then top_srcdir=. ; fi
10 if [ -z "$top_builddir" ] ; then top_builddir=. ; fi
11 top_srcdir=`cd $top_srcdir; pwd`
12 top_builddir=`cd $top_builddir; pwd`
13
14 PSPP=$top_builddir/src/ui/terminal/pspp
15
16 STAT_CONFIG_PATH=$top_srcdir/config
17 export STAT_CONFIG_PATH
18
19 LANG=C
20 export LANG
21
22 port=6543
23 dbase=pspptest
24 PG_CONFIG=${PG_CONFIG:=pg_config}
25 pgpath=`$PG_CONFIG --bindir`
26
27 cleanup()
28 {
29     if [ x"$PSPP_TEST_NO_CLEANUP" != x ] ; then 
30         echo "NOT cleaning $TEMPDIR"
31         return ; 
32     fi
33     PGHOST=$TEMPDIR $pgpath/pg_ctl -D $TEMPDIR/cluster  stop -W -o "-k $TEMPDIR -h ''"   > /dev/null 2>&1
34     cd /
35     rm -rf $TEMPDIR
36 }
37
38
39 fail()
40 {
41     echo $activity
42     echo FAILED
43     cleanup;
44     exit 1;
45 }
46
47
48 no_result()
49 {
50     echo $activity
51     echo NO RESULT;
52     cleanup;
53     exit 2;
54 }
55
56 pass()
57 {
58     cleanup;
59     exit 0;
60 }
61
62 if [ ! -x $pgpath/initdb ] ; then
63   echo 'No Postgres server was found, so the postgres database interface cannot be tested.'  
64   cleanup;
65   exit 77;
66 fi
67
68 mkdir -p $TEMPDIR
69
70 cd $TEMPDIR
71
72 activity="create cluster"
73 $pgpath/initdb  -D $TEMPDIR/cluster -A trust > /dev/null
74 if [ $? -ne 0 ] ; then no_result ; fi
75
76 activity="run server"
77 PGHOST=$TEMPDIR PGPORT=$port $pgpath/pg_ctl -D $TEMPDIR/cluster  start -w -o "-k $TEMPDIR -h ''" > /dev/null
78 if [ $? -ne 0 ] ; then no_result ; fi
79
80
81 activity="create database"
82 $pgpath/createdb  -h $TEMPDIR  -p $port $dbase > /dev/null 2> /dev/null
83 if [ $? -ne 0 ] ; then no_result ; fi
84
85
86 activity="populate database"
87 $pgpath/psql  -h $TEMPDIR -p $port  $dbase > /dev/null << EOF
88
89 CREATE TABLE empty (a int, b date, c numeric(23, 4));
90
91 -- a largeish table to check big queries work ok.
92 CREATE TABLE large (x int);
93 INSERT INTO large  (select * from generate_series(1, 1000));
94
95
96 CREATE TABLE thing (
97  bool    bool                      ,
98  bytea   bytea                     ,
99  char    char                      ,
100  int8    int8                      ,
101  int2    int2                      ,
102  int4    int4                      ,
103  numeric       numeric(50,6)       ,
104  text    text                      ,
105  oid     oid                       ,
106  float4  float4                    ,
107  float8  float8                    ,
108  money   money                     ,
109  pbchar  bpchar                    ,
110  varchar varchar                   ,
111  date    date                      ,
112  time    time                      ,
113  timestamp     timestamp           ,
114  timestamptz   timestamptz         ,
115  interval      interval            ,
116  timetz        timetz              
117 );
118
119 INSERT INTO thing VALUES (
120  false,
121  '0',
122  'a',
123  '0',
124  0,
125  0,
126  -256.098,
127  'this-long-text',
128  0,
129  0,
130  0,
131  '0.01',
132  'a',
133  'A',
134  '1-Jan-2000',
135  '00:00',
136  'January 8 04:05:06 1999',
137  'January 8 04:05:06 1999 PST',
138  '1 minutes',
139  '10:09 UTC+4'
140 );
141
142 INSERT INTO thing VALUES (
143  null,
144  null,
145  null,
146  null,
147  null,
148  null,
149  null,
150  null,
151  null,
152  null,
153  null,
154  null,
155  null,
156  null,
157  null,
158  null,
159  null,
160  null,
161  null,
162  null
163 );
164
165 INSERT INTO thing VALUES (
166  true,
167  '1',
168  'b',
169  '1',
170  1,
171  1,
172  65535.00001,
173  'that-long-text',
174  1,
175  1,
176  1,
177  '1.23',
178  'b',
179  'B',
180  '10-Jan-1963',
181  '01:05:02',
182  '10-Jan-1963 23:58:00',
183  '10-Jan-1963 23:58:00 CET',
184  '2 year 1 month 12 days 1 hours 3 minutes 4 seconds',
185  '01:05:02 UTC-7'
186 );
187 EOF
188 if [ $? -ne 0 ] ; then fail ; fi
189
190 activity="create program 1"
191 cat > $TESTFILE <<EOF
192 GET DATA /TYPE=psql 
193         /CONNECT="host=$TEMPDIR port=$port dbname=$dbase"
194         /UNENCRYPTED
195         /SQL="select * from thing".
196
197 DISPLAY DICTIONARY.
198
199 LIST.
200 EOF
201 if [ $? -ne 0 ] ; then no_result ; fi
202
203
204 activity="run program 1"
205 $SUPERVISOR $PSPP --testing-mode -o raw-ascii $TESTFILE
206 if [ $? -ne 0 ] ; then no_result ; fi
207
208 activity="compare output 1"
209 diff -c $TEMPDIR/pspp.csv - << 'EOF'
210 Variable,Description,,Position
211 bool,Format: F8.2,,1
212 ,Measure: Scale,,
213 ,Display Alignment: Right,,
214 ,Display Width: 8,,
215 bytea,Format: AHEX2,,2
216 ,Measure: Nominal,,
217 ,Display Alignment: Left,,
218 ,Display Width: 1,,
219 char,Format: A8,,3
220 ,Measure: Nominal,,
221 ,Display Alignment: Left,,
222 ,Display Width: 8,,
223 int8,Format: F8.2,,4
224 ,Measure: Scale,,
225 ,Display Alignment: Right,,
226 ,Display Width: 8,,
227 int2,Format: F8.2,,5
228 ,Measure: Scale,,
229 ,Display Alignment: Right,,
230 ,Display Width: 8,,
231 int4,Format: F8.2,,6
232 ,Measure: Scale,,
233 ,Display Alignment: Right,,
234 ,Display Width: 8,,
235 numeric,Format: E40.6,,7
236 ,Measure: Scale,,
237 ,Display Alignment: Right,,
238 ,Display Width: 8,,
239 text,Format: A16,,8
240 ,Measure: Nominal,,
241 ,Display Alignment: Left,,
242 ,Display Width: 16,,
243 oid,Format: F8.2,,9
244 ,Measure: Scale,,
245 ,Display Alignment: Right,,
246 ,Display Width: 8,,
247 float4,Format: F8.2,,10
248 ,Measure: Scale,,
249 ,Display Alignment: Right,,
250 ,Display Width: 8,,
251 float8,Format: F8.2,,11
252 ,Measure: Scale,,
253 ,Display Alignment: Right,,
254 ,Display Width: 8,,
255 money,Format: DOLLAR8.2,,12
256 ,Measure: Scale,,
257 ,Display Alignment: Right,,
258 ,Display Width: 8,,
259 pbchar,Format: A8,,13
260 ,Measure: Nominal,,
261 ,Display Alignment: Left,,
262 ,Display Width: 8,,
263 varchar,Format: A8,,14
264 ,Measure: Nominal,,
265 ,Display Alignment: Left,,
266 ,Display Width: 8,,
267 date,Format: DATE11,,15
268 ,Measure: Scale,,
269 ,Display Alignment: Right,,
270 ,Display Width: 8,,
271 time,Format: TIME11.0,,16
272 ,Measure: Scale,,
273 ,Display Alignment: Right,,
274 ,Display Width: 8,,
275 timestamp,Format: DATETIME22.0,,17
276 ,Measure: Scale,,
277 ,Display Alignment: Right,,
278 ,Display Width: 8,,
279 timestamptz,Format: DATETIME22.0,,18
280 ,Measure: Scale,,
281 ,Display Alignment: Right,,
282 ,Display Width: 8,,
283 interval,Format: DTIME13.0,,19
284 ,Measure: Scale,,
285 ,Display Alignment: Right,,
286 ,Display Width: 8,,
287 interval_months,Format: F3.0,,20
288 ,Measure: Scale,,
289 ,Display Alignment: Right,,
290 ,Display Width: 8,,
291 timetz,Format: TIME11.0,,21
292 ,Measure: Scale,,
293 ,Display Alignment: Right,,
294 ,Display Width: 8,,
295 timetz_zone,Format: F8.2,,22
296 ,Measure: Scale,,
297 ,Display Alignment: Right,,
298 ,Display Width: 8,,
299
300 Table: Data List
301 bool,bytea,char,int8,int2,int4,numeric,text,oid,float4,float8,money,pbchar,varchar,date,time,timestamp,timestamptz,interval,interval_months,timetz,timetz_zone
302 .00,30,a       ,.00,.00,.00,-2.560980E+002,this-long-text  ,.00,.00,.00,$.01,a       ,A       ,01-JAN-2000,0:00:00,08-JAN-1999 04:05:06,08-JAN-1999 12:05:06,0 00:01:00,0,10:09:00,4.00
303 .  ,20,,.  ,.  ,.  ,.          ,,.  ,.  ,.  ,.  ,,,.,.,.,.,.,.,.,.  
304 1.00,31,b       ,1.00,1.00,1.00,6.553500E+004,that-long-text  ,.00,1.00,1.00,$1.23,b       ,B       ,10-JAN-1963,1:05:02,10-JAN-1963 23:58:00,10-JAN-1963 22:58:00,12 01:03:04,25,1:05:02,-7.00
305 EOF
306 if [ $? -ne 0 ] ; then fail ; fi
307
308
309 activity="create program 2"
310 cat > $TESTFILE <<EOF
311 GET DATA /TYPE=psql 
312         /CONNECT="host=$TEMPDIR port=$port dbname=$dbase"
313         /UNENCRYPTED
314         /SQL="select * from empty".
315
316 DISPLAY DICTIONARY.
317
318 LIST.
319 EOF
320 if [ $? -ne 0 ] ; then no_result ; fi
321
322 activity="run program 2"
323 $SUPERVISOR $PSPP --testing-mode -o raw-ascii $TESTFILE
324 if [ $? -ne 0 ] ; then no_result ; fi
325
326 activity="compare output 2"
327 diff -c $TEMPDIR/pspp.csv - << 'EOF'
328 Variable,Description,,Position
329 a,Format: F8.2,,1
330 ,Measure: Scale,,
331 ,Display Alignment: Right,,
332 ,Display Width: 8,,
333 b,Format: DATE11,,2
334 ,Measure: Scale,,
335 ,Display Alignment: Right,,
336 ,Display Width: 8,,
337 c,Format: E40.2,,3
338 ,Measure: Scale,,
339 ,Display Alignment: Right,,
340 ,Display Width: 8,,
341 EOF
342 if [ $? -ne 0 ] ; then fail ; fi
343
344 activity="create program 3"
345 cat > $TESTFILE <<EOF
346 GET DATA /TYPE=psql 
347         /CONNECT="host=$TEMPDIR port=$port dbname=$dbase"
348         /UNENCRYPTED
349         /BSIZE = 27
350         /SQL="select * from large".
351
352 NUMERIC diff.
353 COMPUTE diff = x - lag (x).
354
355 TEMPORARY.
356 SELECT IF (diff <> 1).
357 LIST.
358
359 TEMPORARY.
360 N OF CASES 6.
361 LIST.
362
363 SORT CASES BY x (D).
364
365 TEMPORARY.
366 N OF CASES 6.
367 LIST.
368
369 EOF
370 if [ $? -ne 0 ] ; then no_result ; fi
371
372 activity="run program 3"
373 $SUPERVISOR $PSPP --testing-mode -o raw-ascii $TESTFILE
374 if [ $? -ne 0 ] ; then no_result ; fi
375
376 activity="compare output 3"
377 diff -c $TEMPDIR/pspp.csv - << 'EOF'
378 Table: Data List
379 x,diff
380 1.00,.  
381 2.00,1.00
382 3.00,1.00
383 4.00,1.00
384 5.00,1.00
385 6.00,1.00
386
387 Table: Data List
388 x,diff
389 1000.00,1.00
390 999.00,1.00
391 998.00,1.00
392 997.00,1.00
393 996.00,1.00
394 995.00,1.00
395 EOF
396 if [ $? -ne 0 ] ; then fail ; fi
397
398
399
400 # Check for a bug caused by having string variables in the database,
401 # all of which are null.
402
403 activity="populate database 4"
404 $pgpath/psql  -h $TEMPDIR -p $port  $dbase > /dev/null << EOF
405
406 -- a table which has a text field containing only null, or zero
407 -- length entries.
408
409 CREATE TABLE foo (int4  int4, text text);
410
411 INSERT INTO foo VALUES ('12', '');
412
413 INSERT INTO foo VALUES (null, '');
414
415 EOF
416 if [ $? -ne 0 ] ; then fail ; fi
417
418
419 activity="create program 4"
420 cat > $TESTFILE <<EOF
421 GET DATA /TYPE=psql 
422         /CONNECT="host=$TEMPDIR port=$port dbname=$dbase"
423         /UNENCRYPTED
424         /SQL="select * from foo".
425
426 DISPLAY DICTIONARY.
427
428 LIST.
429 EOF
430 if [ $? -ne 0 ] ; then no_result ; fi
431
432
433 activity="run program 4"
434 $SUPERVISOR $PSPP --testing-mode -o raw-ascii $TESTFILE
435 if [ $? -ne 0 ] ; then no_result ; fi
436
437
438 pass;