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