Merge remote-tracking branch 'origin/master' into sheet
[pspp] / tests / language / data-io / get-data-psql.at
1 dnl PSPP - a program for statistical analysis.
2 dnl Copyright (C) 2017 Free Software Foundation, Inc.
3 dnl 
4 dnl This program is free software: you can redistribute it and/or modify
5 dnl it under the terms of the GNU General Public License as published by
6 dnl the Free Software Foundation, either version 3 of the License, or
7 dnl (at your option) any later version.
8 dnl 
9 dnl This program is distributed in the hope that it will be useful,
10 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
11 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 dnl GNU General Public License for more details.
13 dnl 
14 dnl You should have received a copy of the GNU General Public License
15 dnl along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 dnl
17 AT_BANNER([GET DATA /TYPE=PSQL])
18
19 m4_define([INIT_PSQL], 
20   [AT_SKIP_IF([test "$PSQL_SUPPORT" = no])
21    PATH=$PG_PATH:$PATH
22    export PATH
23    PGDATA=`pwd`/cluster
24    export PGDATA
25    PGPORT=$PG_PORT
26    export PGPORT
27    socket_dir=`mktemp -d`
28    PGHOST="$socket_dir"
29    export PGHOST
30    AT_CHECK([initdb -A trust], [0], [ignore])
31    AT_CHECK([pg_ctl start -w -o "-k $socket_dir -h ''"], [0], [ignore])
32    trap 'CLEANUP_PSQL' 0
33    AT_CHECK([createdb -h "$socket_dir" -p $PG_PORT $PG_DBASE],
34       [0], [ignore], [ignore])
35    AT_DATA([populate.sql], 
36      [CREATE TABLE empty (a int, b date, c numeric(23, 4));
37
38       -- a largeish table to check big queries work ok.
39       CREATE TABLE large (x int);
40       INSERT INTO large  (select * from generate_series(1, 1000));
41
42
43       CREATE TABLE thing (
44        bool    bool                      ,
45        bytea   bytea                     ,
46        char    char                      ,
47        int8    int8                      ,
48        int2    int2                      ,
49        int4    int4                      ,
50        numeric       numeric(50,6)       ,
51        text    text                      ,
52        oid     oid                       ,
53        float4  float4                    ,
54        float8  float8                    ,
55        money   money                     ,
56        pbchar  bpchar                    ,
57        varchar varchar                   ,
58        date    date                      ,
59        time    time                      ,
60        timestamp     timestamp           ,
61        timestamptz   timestamptz         ,
62        interval      interval            ,
63        timetz        timetz              
64       );
65
66       INSERT INTO thing VALUES (
67        false,
68        '0',
69        'a',
70        '0',
71        0,
72        0,
73        -256.098,
74        'this-long-text',
75        0,
76        0,
77        0,
78        '0.01',
79        'a',
80        'A',
81        '1-Jan-2000',
82        '00:00',
83        'January 8 04:05:06 1999',
84        'January 8 04:05:06 1999 PST',
85        '1 minutes',
86        '10:09 UTC+4'
87       );
88
89       INSERT INTO thing VALUES (
90        null,
91        null,
92        null,
93        null,
94        null,
95        null,
96        null,
97        null,
98        null,
99        null,
100        null,
101        null,
102        null,
103        null,
104        null,
105        null,
106        null,
107        null,
108        null,
109        null
110       );
111
112       INSERT INTO thing VALUES (
113        true,
114        '1',
115        'b',
116        '1',
117        1,
118        1,
119        65535.00001,
120        'that-long-text',
121        1,
122        1,
123        1,
124        '1.23',
125        'b',
126        'B',
127        '10-Jan-1963',
128        '01:05:02',
129        '10-Jan-1963 23:58:00',
130        '10-Jan-1963 23:58:00 CET',
131        '2 year 1 month 12 days 1 hours 3 minutes 4 seconds',
132        '01:05:02 UTC-7'
133       );
134 ])
135    AT_CHECK([psql -h "$socket_dir" -p $PG_PORT $PG_DBASE < populate.sql],
136       [0], [ignore])])
137
138 m4_define([CLEANUP_PSQL], [pg_ctl stop -W -o "-k $socket_dir -h ''"])
139
140 AT_SETUP([GET DATA /TYPE=PSQL])
141 INIT_PSQL
142
143 dnl Test with an ordinary query.
144 AT_CHECK([cat > ordinary-query.sps <<EOF
145 GET DATA /TYPE=psql 
146         /CONNECT="host=$socket_dir port=$PGPORT dbname=$PG_DBASE"
147         /UNENCRYPTED
148         /SQL="select * from thing".
149
150 DISPLAY DICTIONARY.
151
152 LIST.
153 EOF
154 ])
155 AT_CHECK([pspp -o pspp.csv ordinary-query.sps])
156 AT_CHECK([cat pspp.csv], [0], [dnl
157 Variable,Description,Position
158 bool,Format: F8.2,1
159 bytea,Format: AHEX2,2
160 char,Format: A8,3
161 int8,Format: F8.2,4
162 int2,Format: F8.2,5
163 int4,Format: F8.2,6
164 numeric,Format: E40.6,7
165 text,Format: A16,8
166 oid,Format: F8.2,9
167 float4,Format: F8.2,10
168 float8,Format: F8.2,11
169 money,Format: DOLLAR8.2,12
170 pbchar,Format: A8,13
171 varchar,Format: A8,14
172 date,Format: DATE11,15
173 time,Format: TIME11.0,16
174 timestamp,Format: DATETIME22.0,17
175 timestamptz,Format: DATETIME22.0,18
176 interval,Format: DTIME13.0,19
177 interval_months,Format: F3.0,20
178 timetz,Format: TIME11.0,21
179 timetz_zone,Format: F8.2,22
180
181 Table: Data List
182 bool,bytea,char,int8,int2,int4,numeric,text,oid,float4,float8,money,pbchar,varchar,date,time,timestamp,timestamptz,interval,interval_months,timetz,timetz_zone
183 .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
184 .  ,20,,.  ,.  ,.  ,.          ,,.  ,.  ,.  ,.  ,,,.,.,.,.,.,.,.,.  @&t@
185 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
186 ])
187
188 dnl Test query with empty result set.
189 AT_CHECK([cat > empty-result.sps <<EOF
190 GET DATA /TYPE=psql 
191         /CONNECT="host=$socket_dir port=$PGPORT dbname=$PG_DBASE"
192         /UNENCRYPTED
193         /SQL="select * from empty".
194
195 DISPLAY DICTIONARY.
196
197 LIST.
198 EOF
199 ])
200 AT_CHECK([pspp -o pspp.csv empty-result.sps])
201 AT_CHECK([cat pspp.csv], [0], [dnl
202 Variable,Description,Position
203 a,Format: F8.2,1
204 b,Format: DATE11,2
205 c,Format: E40.2,3
206 ])
207
208 dnl Test query with large result set.
209 AT_CHECK([cat > large-result.sps <<EOF
210 GET DATA /TYPE=psql 
211         /CONNECT="host=$socket_dir port=$PGPORT dbname=$PG_DBASE"
212         /UNENCRYPTED
213         /SQL="select * from large".
214
215 NUMERIC diff.
216 COMPUTE diff = x - lag (x).
217
218 TEMPORARY.
219 SELECT IF (diff <> 1).
220 LIST.
221
222 TEMPORARY.
223 N OF CASES 6.
224 LIST.
225
226 SORT CASES BY x (D).
227
228 TEMPORARY.
229 N OF CASES 6.
230 LIST.
231 EOF
232 ])
233 AT_CHECK([pspp -o pspp.csv large-result.sps])
234 AT_CHECK([cat pspp.csv], [0], [dnl
235 Table: Data List
236 x,diff
237 1.00,.  @&t@
238 2.00,1.00
239 3.00,1.00
240 4.00,1.00
241 5.00,1.00
242 6.00,1.00
243
244 Table: Data List
245 x,diff
246 1000.00,1.00
247 999.00,1.00
248 998.00,1.00
249 997.00,1.00
250 996.00,1.00
251 995.00,1.00
252 ])
253
254 dnl Check for a bug caused by having string variables in the database,
255 dnl all of which are null.
256 AT_DATA([all-null-string.sql],
257   [-- a table which has a text field containing only null, or zero
258    -- length entries.
259
260    CREATE TABLE foo (int4  int4, text text);
261
262    INSERT INTO foo VALUES ('12', '');
263
264    INSERT INTO foo VALUES (null, '');
265 ])
266 AT_CHECK([psql -h "$socket_dir" -p $PG_PORT $PG_DBASE < all-null-string.sql],
267   [0], [ignore])
268 AT_CAPTURE_FILE([get-data.sps])
269 AT_CHECK([cat > get-data.sps <<EOF
270 GET DATA /TYPE=psql 
271         /CONNECT="host=$socket_dir port=$PGPORT dbname=$PG_DBASE"
272         /UNENCRYPTED
273         /SQL="select * from foo".
274
275 DISPLAY DICTIONARY.
276
277 LIST.
278 EOF
279 ])
280 AT_CHECK([pspp -o pspp.csv get-data.sps])
281 AT_CAPTURE_FILE([pspp.csv])
282 rm -rf "$socket_dir"
283 AT_CLEANUP