forked from harelba/q
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-suite
More file actions
executable file
·2023 lines (1456 loc) · 77.7 KB
/
test-suite
File metadata and controls
executable file
·2023 lines (1456 loc) · 77.7 KB
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#
#
# Simplistic test suite for q.
#
# Currently takes into account the project folder structure for running, so it needs
# to be executed from the current folder
#
#
import unittest
import json
from json import JSONEncoder
from subprocess import PIPE, Popen, STDOUT
import sys
import os
import time
from tempfile import NamedTemporaryFile
import locale
import pprint
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])),'..','bin'))
from qtextasdata import QTextAsData,QOutput,QOutputPrinter,QInputParams
# q uses this encoding as the default output encoding. Some of the tests use it in order to
# make sure that the output is correctly encoded
SYSTEM_ENCODING = locale.getpreferredencoding()
def run_command(cmd_to_run):
p = Popen(cmd_to_run, stdout=PIPE, stderr=PIPE, shell=True)
o, e = p.communicate()
# remove last newline
o = o.rstrip()
e = e.strip()
# split rows
if o != '':
o = o.split(os.linesep)
else:
o = []
if e != '':
e = e.split(os.linesep)
else:
e = []
return (p.returncode, o, e)
uneven_ls_output = """drwxr-xr-x 2 root root 4096 Jun 11 2012 /selinux
drwxr-xr-x 2 root root 4096 Apr 19 2013 /mnt
drwxr-xr-x 2 root root 4096 Apr 24 2013 /srv
drwx------ 2 root root 16384 Jun 21 2013 /lost+found
lrwxrwxrwx 1 root root 33 Jun 21 2013 /initrd.img.old -> /boot/initrd.img-3.8.0-19-generic
drwxr-xr-x 2 root root 4096 Jun 21 2013 /cdrom
drwxr-xr-x 3 root root 4096 Jun 21 2013 /home
lrwxrwxrwx 1 root root 29 Jun 21 2013 /vmlinuz -> boot/vmlinuz-3.8.0-19-generic
lrwxrwxrwx 1 root root 32 Jun 21 2013 /initrd.img -> boot/initrd.img-3.8.0-19-generic
"""
find_output = """8257537 32 drwxrwxrwt 218 root root 28672 Mar 1 11:00 /tmp
8299123 4 drwxrwxr-x 2 harel harel 4096 Feb 27 10:06 /tmp/1628a3fd-b9fe-4dd1-bcdc-7eb869fe7461/supervisor/stormdist/testTopology3fad644a-54c0-4def-b19e-77ca97941595-1-1393513576
8263229 964 -rw-rw-r-- 1 mapred mapred 984569 Feb 27 10:06 /tmp/1628a3fd-b9fe-4dd1-bcdc-7eb869fe7461/supervisor/stormdist/testTopology3fad644a-54c0-4def-b19e-77ca97941595-1-1393513576/stormcode.ser
8263230 4 -rw-rw-r-- 1 harel harel 1223 Feb 27 10:06 /tmp/1628a3fd-b9fe-4dd1-bcdc-7eb869fe7461/supervisor/stormdist/testTopology3fad644a-54c0-4def-b19e-77ca97941595-1-1393513576/stormconf.ser
8299113 4 drwxrwxr-x 2 harel harel 4096 Feb 27 10:16 /tmp/1628a3fd-b9fe-4dd1-bcdc-7eb869fe7461/supervisor/localstate
8263406 4 -rw-rw-r-- 1 harel harel 2002 Feb 27 10:16 /tmp/1628a3fd-b9fe-4dd1-bcdc-7eb869fe7461/supervisor/localstate/1393514168746
8263476 0 -rw-rw-r-- 1 harel harel 0 Feb 27 10:16 /tmp/1628a3fd-b9fe-4dd1-bcdc-7eb869fe7461/supervisor/localstate/1393514168746.version
8263607 0 -rw-rw-r-- 1 harel harel 0 Feb 27 10:16 /tmp/1628a3fd-b9fe-4dd1-bcdc-7eb869fe7461/supervisor/localstate/1393514169735.version
8263533 0 -rw-rw-r-- 1 harel harel 0 Feb 27 10:16 /tmp/1628a3fd-b9fe-4dd1-bcdc-7eb869fe7461/supervisor/localstate/1393514172733.version
8263604 0 -rw-rw-r-- 1 harel harel 0 Feb 27 10:16 /tmp/1628a3fd-b9fe-4dd1-bcdc-7eb869fe7461/supervisor/localstate/1393514175754.version
"""
header_row = 'name,value1,value2'
sample_data_rows = ['a,1,0', 'b,2,0', 'c,,0']
sample_data_rows_with_empty_string = ['a,aaa,0', 'b,bbb,0', 'c,,0']
sample_data_no_header = "\n".join(sample_data_rows) + "\n"
sample_data_with_empty_string_no_header = "\n".join(
sample_data_rows_with_empty_string) + "\n"
sample_data_with_header = header_row + "\n" + sample_data_no_header
sample_data_with_missing_header_names = "name,value1\n" + sample_data_no_header
sample_quoted_data = '''non_quoted regular_double_quoted double_double_quoted escaped_double_quoted multiline_double_double_quoted multiline_escaped_double_quoted
control-value-1 "control-value-2" control-value-3 "control-value-4" control-value-5 "control-value-6"
non-quoted-value "this is a quoted value" "this is a ""double double"" quoted value" "this is an escaped \\"quoted value\\"" "this is a double double quoted ""multiline
value""." "this is an escaped \\"multiline
value\\"."
control-value-1 "control-value-2" control-value-3 "control-value-4" control-value-5 "control-value-6"
'''
double_double_quoted_data = '''regular_double_quoted double_double_quoted
"this is a quoted value" "this is a quoted value with ""double double quotes"""
'''
escaped_double_quoted_data = '''regular_double_quoted escaped_double_quoted
"this is a quoted value" "this is a quoted value with \\"escaped double quotes\\""
'''
combined_quoted_data = '''regular_double_quoted double_double_quoted escaped_double_quoted
"this is a quoted value" "this is a quoted value with ""double double quotes""" "this is a quoted value with \\"escaped double quotes\\""
'''
sample_quoted_data2 = '"quoted data" 23\nunquoted-data 54'
one_column_data = '''data without commas 1
data without commas 2
'''
# Values with leading whitespace
sample_data_rows_with_spaces = ['a,1,0', ' b, 2,0', 'c,,0']
sample_data_with_spaces_no_header = "\n".join(
sample_data_rows_with_spaces) + "\n"
header_row_with_spaces = 'name,value 1,value2'
sample_data_with_spaces_with_header = header_row_with_spaces + \
"\n" + sample_data_with_spaces_no_header
long_value1 = "23683289372328372328373"
int_value = "2328372328373"
sample_data_with_long_values = "%s\n%s\n%s" % (long_value1,int_value,int_value)
def one_column_warning(e):
return e[0].startswith('Warning: column count is one')
class AbstractQTestCase(unittest.TestCase):
def create_file_with_data(self, data, encoding='utf-8'):
tmpfile = NamedTemporaryFile(delete=False)
if encoding != 'none' and encoding is not None:
tmpfile.write(data.encode(encoding))
else:
tmpfile.write(data)
tmpfile.close()
return tmpfile
def cleanup(self, tmpfile):
os.remove(tmpfile.name)
class BasicTests(AbstractQTestCase):
def test_basic_aggregation(self):
retcode, o, e = run_command(
'seq 1 10 | ../bin/q "select sum(c1),avg(c1) from -"')
self.assertTrue(retcode == 0)
self.assertTrue(len(o) == 1)
self.assertTrue(len(e) == 1)
s = sum(xrange(1, 11))
self.assertTrue(o[0] == '%s %s' % (s, s / 10.0))
self.assertTrue(one_column_warning(e))
def test_gzipped_file(self):
tmpfile = self.create_file_with_data(
'\x1f\x8b\x08\x08\xf2\x18\x12S\x00\x03xxxxxx\x003\xe42\xe22\xe62\xe12\xe52\xe32\xe7\xb2\xe0\xb2\xe424\xe0\x02\x00\xeb\xbf\x8a\x13\x15\x00\x00\x00', encoding='none')
cmd = '../bin/q -z "select sum(c1),avg(c1) from %s"' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertTrue(retcode == 0)
self.assertTrue(len(o) == 1)
self.assertTrue(len(e) == 1)
s = sum(xrange(1, 11))
self.assertTrue(o[0] == '%s %s' % (s, s / 10.0))
self.assertTrue(one_column_warning(e))
self.cleanup(tmpfile)
def test_attempt_to_unzip_stdin(self):
tmpfile = self.create_file_with_data(
'\x1f\x8b\x08\x08\xf2\x18\x12S\x00\x03xxxxxx\x003\xe42\xe22\xe62\xe12\xe52\xe32\xe7\xb2\xe0\xb2\xe424\xe0\x02\x00\xeb\xbf\x8a\x13\x15\x00\x00\x00', encoding='none')
cmd = 'cat %s | ../bin/q -z "select sum(c1),avg(c1) from -"' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertTrue(retcode != 0)
self.assertTrue(len(o) == 0)
self.assertTrue(len(e) == 1)
self.assertEquals(e[0],'Cannot decompress standard input. Pipe the input through zcat in order to decompress.')
self.cleanup(tmpfile)
def test_delimition_mistake_with_header(self):
tmpfile = self.create_file_with_data(sample_data_no_header)
cmd = '../bin/q -d " " "select * from %s" -H' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertNotEquals(retcode, 0)
self.assertEquals(len(o), 0)
self.assertEquals(len(e), 3)
self.assertTrue(e[0].startswith(
"Warning: column count is one - did you provide the correct delimiter"))
self.assertTrue(e[1].startswith("Bad header row"))
self.assertTrue("Column name cannot contain commas" in e[2])
self.cleanup(tmpfile)
def test_regexp_int_data_handling(self):
tmpfile = self.create_file_with_data(sample_data_no_header)
cmd = '../bin/q -d , "select c2 from %s where regexp(\'^1\',c2)"' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(o), 1)
self.assertEquals(len(e), 0)
self.assertEquals(o[0],"1")
self.cleanup(tmpfile)
def test_regexp_null_data_handling(self):
tmpfile = self.create_file_with_data(sample_data_no_header)
cmd = '../bin/q -d , "select count(*) from %s where regexp(\'^\',c2)"' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(o), 1)
self.assertEquals(len(e), 0)
self.assertEquals(o[0],"2")
self.cleanup(tmpfile)
def test_select_one_column(self):
tmpfile = self.create_file_with_data(sample_data_no_header)
cmd = '../bin/q -d , "select c1 from %s"' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(o), 3)
self.assertEquals(len(e), 0)
self.assertEquals(" ".join(o), 'a b c')
self.cleanup(tmpfile)
def test_tab_delimition_parameter(self):
tmpfile = self.create_file_with_data(
sample_data_no_header.replace(",", "\t"))
cmd = '../bin/q -t "select c1,c2,c3 from %s"' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(o), 3)
self.assertEquals(len(e), 0)
self.assertEquals(o[0], sample_data_rows[0].replace(",", "\t"))
self.assertEquals(o[1], sample_data_rows[1].replace(",", "\t"))
self.assertEquals(o[2], sample_data_rows[2].replace(",", "\t"))
self.cleanup(tmpfile)
def test_tab_delimition_parameter__with_manual_override_attempt(self):
tmpfile = self.create_file_with_data(
sample_data_no_header.replace(",", "\t"))
cmd = '../bin/q -t -d , "select c1,c2,c3 from %s"' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(o), 3)
self.assertEquals(len(e), 0)
self.assertEquals(o[0], sample_data_rows[0].replace(",", "\t"))
self.assertEquals(o[1], sample_data_rows[1].replace(",", "\t"))
self.assertEquals(o[2], sample_data_rows[2].replace(",", "\t"))
self.cleanup(tmpfile)
def test_output_delimiter(self):
tmpfile = self.create_file_with_data(sample_data_no_header)
cmd = '../bin/q -d , -D "|" "select c1,c2,c3 from %s"' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(o), 3)
self.assertEquals(len(e), 0)
self.assertEquals(o[0], sample_data_rows[0].replace(",", "|"))
self.assertEquals(o[1], sample_data_rows[1].replace(",", "|"))
self.assertEquals(o[2], sample_data_rows[2].replace(",", "|"))
self.cleanup(tmpfile)
def test_output_delimiter_tab_parameter(self):
tmpfile = self.create_file_with_data(sample_data_no_header)
cmd = '../bin/q -d , -T "select c1,c2,c3 from %s"' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(o), 3)
self.assertEquals(len(e), 0)
self.assertEquals(o[0], sample_data_rows[0].replace(",", "\t"))
self.assertEquals(o[1], sample_data_rows[1].replace(",", "\t"))
self.assertEquals(o[2], sample_data_rows[2].replace(",", "\t"))
self.cleanup(tmpfile)
def test_output_delimiter_tab_parameter__with_manual_override_attempt(self):
tmpfile = self.create_file_with_data(sample_data_no_header)
cmd = '../bin/q -d , -T -D "|" "select c1,c2,c3 from %s"' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(o), 3)
self.assertEquals(len(e), 0)
self.assertEquals(o[0], sample_data_rows[0].replace(",", "\t"))
self.assertEquals(o[1], sample_data_rows[1].replace(",", "\t"))
self.assertEquals(o[2], sample_data_rows[2].replace(",", "\t"))
self.cleanup(tmpfile)
def test_stdin_input(self):
cmd = 'printf "%s" | ../bin/q -d , "select c1,c2,c3 from -"' % sample_data_no_header
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(o), 3)
self.assertEquals(len(e), 0)
self.assertEquals(o[0], sample_data_rows[0])
self.assertEquals(o[1], sample_data_rows[1])
self.assertEquals(o[2], sample_data_rows[2])
def test_column_separation(self):
tmpfile = self.create_file_with_data(sample_data_no_header)
cmd = '../bin/q -d , "select c1,c2,c3 from %s"' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(o), 3)
self.assertEquals(len(e), 0)
self.assertEquals(o[0], sample_data_rows[0])
self.assertEquals(o[1], sample_data_rows[1])
self.assertEquals(o[2], sample_data_rows[2])
self.cleanup(tmpfile)
def test_column_analysis(self):
tmpfile = self.create_file_with_data(sample_data_no_header)
cmd = '../bin/q -d , "select c1 from %s" -A' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(o[0], 'Table for file: %s' % tmpfile.name)
self.assertEquals(o[1].strip(), '`c1` - text')
self.assertEquals(o[2].strip(), '`c2` - int')
self.assertEquals(o[3].strip(), '`c3` - int')
self.cleanup(tmpfile)
def test_column_analysis_no_header(self):
tmpfile = self.create_file_with_data(sample_data_no_header)
cmd = '../bin/q -d , "select c1 from %s" -A' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(o[0], 'Table for file: %s' % tmpfile.name)
self.assertEquals(o[1].strip(), '`c1` - text')
self.assertEquals(o[2].strip(), '`c2` - int')
self.assertEquals(o[3].strip(), '`c3` - int')
def test_header_exception_on_numeric_header_data(self):
tmpfile = self.create_file_with_data(sample_data_no_header)
cmd = '../bin/q -d , "select * from %s" -A -H' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertNotEquals(retcode, 0)
self.assertEquals(len(o), 0)
self.assertEquals(len(e), 3)
self.assertTrue(
'Bad header row: Header must contain only strings' in e[0])
self.assertTrue("Column name must be a string" in e[1])
self.assertTrue("Column name must be a string" in e[2])
self.cleanup(tmpfile)
def test_column_analysis_with_header(self):
tmpfile = self.create_file_with_data(sample_data_with_header)
cmd = '../bin/q -d , "select c1 from %s" -A -H' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertNotEquals(retcode, 0)
self.assertEquals(len(o),4)
self.assertEquals(len(e),2)
self.assertEquals(o[0], 'Table for file: %s' % tmpfile.name)
self.assertEquals(o[1].strip(), '`name` - text')
self.assertEquals(o[2].strip(), '`value1` - int')
self.assertEquals(o[3].strip(), '`value2` - int')
self.assertEquals(e[0].strip(),'query error: no such column: c1')
self.assertTrue(e[1].startswith('Warning - There seems to be a '))
self.cleanup(tmpfile)
def test_data_with_header(self):
tmpfile = self.create_file_with_data(sample_data_with_header)
cmd = '../bin/q -d , "select name from %s" -H' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(o), 3)
self.assertEquals(" ".join(o), "a b c")
self.cleanup(tmpfile)
def test_output_header_when_input_header_exists(self):
tmpfile = self.create_file_with_data(sample_data_with_header)
cmd = '../bin/q -d , "select name from %s" -H -O' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(o), 4)
self.assertEquals(o[0],'name')
self.assertEquals(o[1],'a')
self.assertEquals(o[2],'b')
self.assertEquals(o[3],'c')
self.cleanup(tmpfile)
def test_generated_column_name_warning_when_header_line_exists(self):
tmpfile = self.create_file_with_data(sample_data_with_header)
cmd = '../bin/q -d , "select c3 from %s" -H' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertNotEquals(retcode, 0)
self.assertEquals(len(o), 0)
self.assertEquals(len(e), 2)
self.assertTrue('no such column: c3' in e[0])
self.assertEquals(
e[1], 'Warning - There seems to be a "no such column" error, and -H (header line) exists. Please make sure that you are using the column names from the header line and not the default (cXX) column names')
self.cleanup(tmpfile)
def test_column_analysis_with_unexpected_header(self):
tmpfile = self.create_file_with_data(sample_data_with_header)
cmd = '../bin/q -d , "select c1 from %s" -A' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(o), 4)
self.assertEquals(len(e), 1)
self.assertEquals(o[0], 'Table for file: %s' % tmpfile.name)
self.assertEquals(o[1].strip(), '`c1` - text')
self.assertEquals(o[2].strip(), '`c2` - text')
self.assertEquals(o[3].strip(), '`c3` - text')
self.assertEquals(
e[0], 'Warning - There seems to be header line in the file, but -H has not been specified. All fields will be detected as text fields, and the header line will appear as part of the data')
self.cleanup(tmpfile)
def test_empty_data(self):
tmpfile = self.create_file_with_data('')
cmd = '../bin/q -d , "select c1 from %s"' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(o), 0)
self.assertEquals(len(e), 1)
self.assertTrue('Warning - data is empty' in e[0])
self.cleanup(tmpfile)
def test_empty_data_with_header_param(self):
tmpfile = self.create_file_with_data('')
cmd = '../bin/q -d , "select c1 from %s" -H' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(o), 0)
self.assertEquals(len(e), 1)
self.assertTrue('Warning - data is empty' in e[0])
self.cleanup(tmpfile)
def test_one_row_of_data_without_header_param(self):
tmpfile = self.create_file_with_data(header_row)
cmd = '../bin/q -d , "select c2 from %s"' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(o), 1)
self.assertEquals(len(e), 0)
self.assertEquals(o[0], 'value1')
self.cleanup(tmpfile)
def test_one_row_of_data_with_header_param(self):
tmpfile = self.create_file_with_data(header_row)
cmd = '../bin/q -d , "select c2 from %s" -H' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(o), 0)
self.assertEquals(len(e), 1)
self.assertTrue('Warning - data is empty' in e[0])
self.cleanup(tmpfile)
def test_dont_leading_keep_whitespace_in_values(self):
tmpfile = self.create_file_with_data(sample_data_with_spaces_no_header)
cmd = '../bin/q -d , "select c1 from %s"' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(e), 0)
self.assertEquals(len(o), 3)
self.assertEquals(o[0], 'a')
self.assertEquals(o[1], 'b')
self.assertEquals(o[2], 'c')
self.cleanup(tmpfile)
def test_keep_leading_whitespace_in_values(self):
tmpfile = self.create_file_with_data(sample_data_with_spaces_no_header)
cmd = '../bin/q -d , "select c1 from %s" -k' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(e), 0)
self.assertEquals(len(o), 3)
self.assertEquals(o[0], 'a')
self.assertEquals(o[1], ' b')
self.assertEquals(o[2], 'c')
self.cleanup(tmpfile)
def test_no_impact_of_keeping_leading_whitespace_on_integers(self):
tmpfile = self.create_file_with_data(sample_data_with_spaces_no_header)
cmd = '../bin/q -d , "select c2 from %s" -k -A' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(e), 0)
self.assertEquals(len(o), 4)
self.assertEquals(o[0], 'Table for file: %s' % tmpfile.name)
self.assertEquals(o[1].strip(), '`c1` - text')
self.assertEquals(o[2].strip(), '`c2` - int')
self.assertEquals(o[3].strip(), '`c3` - int')
self.cleanup(tmpfile)
def test_spaces_in_header_row(self):
tmpfile = self.create_file_with_data(
header_row_with_spaces + "\n" + sample_data_no_header)
cmd = '../bin/q -d , "select name,\`value 1\` from %s" -H' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(e), 0)
self.assertEquals(len(o), 3)
self.assertEquals(o[0], 'a,1')
self.assertEquals(o[1], 'b,2')
self.assertEquals(o[2], 'c,')
self.cleanup(tmpfile)
def test_column_analysis_for_spaces_in_header_row(self):
tmpfile = self.create_file_with_data(
header_row_with_spaces + "\n" + sample_data_no_header)
cmd = '../bin/q -d , "select name,\`value 1\` from %s" -H -A' % tmpfile.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(e), 0)
self.assertEquals(len(o), 4)
self.assertEquals(o[0], 'Table for file: %s' % tmpfile.name)
self.assertEquals(o[1].strip(), '`name` - text')
self.assertEquals(o[2].strip(), '`value 1` - int')
self.assertEquals(o[3].strip(), '`value2` - int')
self.cleanup(tmpfile)
def test_no_query_in_command_line(self):
cmd = '../bin/q -d , ""'
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 1)
self.assertEquals(len(e), 1)
self.assertEquals(len(o), 0)
self.assertEquals(e[0],'Query cannot be empty (query number 1)')
def test_empty_query_in_command_line(self):
cmd = '../bin/q -d , " "'
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 1)
self.assertEquals(len(e), 1)
self.assertEquals(len(o), 0)
self.assertEquals(e[0],'Query cannot be empty (query number 1)')
def test_failure_in_query_stops_processing_queries(self):
cmd = '../bin/q -d , "select 500" "select 300" "wrong-query" "select 8000"'
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 1)
self.assertEquals(len(e), 1)
self.assertEquals(len(o), 2)
self.assertEquals(o[0],'500')
self.assertEquals(o[1],'300')
def test_multiple_queries_in_command_line(self):
cmd = '../bin/q -d , "select 500" "select 300+100" "select 300" "select 200"'
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(e), 0)
self.assertEquals(len(o), 4)
self.assertEquals(o[0],'500')
self.assertEquals(o[1],'400')
self.assertEquals(o[2],'300')
self.assertEquals(o[3],'200')
def test_literal_calculation_query(self):
cmd = '../bin/q -d , "select 1+40/6"'
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(e), 0)
self.assertEquals(len(o), 1)
self.assertEquals(o[0],'7')
def test_literal_calculation_query_float_result(self):
cmd = '../bin/q -d , "select 1+40/6.0"'
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(e), 0)
self.assertEquals(len(o), 1)
self.assertEquals(o[0],'7.66666666667')
def test_use_query_file(self):
tmp_data_file = self.create_file_with_data(sample_data_with_header)
tmp_query_file = self.create_file_with_data("select name from %s" % tmp_data_file.name)
cmd = '../bin/q -d , -q %s -H' % tmp_query_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(e), 0)
self.assertEquals(len(o), 3)
self.assertEquals(o[0], 'a')
self.assertEquals(o[1], 'b')
self.assertEquals(o[2], 'c')
self.cleanup(tmp_data_file)
self.cleanup(tmp_query_file)
def test_use_query_file_with_incorrect_query_encoding(self):
tmp_data_file = self.create_file_with_data(sample_data_with_header)
tmp_query_file = self.create_file_with_data("select name,'Hr\xc3\xa1\xc4\x8d' from %s" % tmp_data_file.name,encoding=None)
cmd = '../bin/q -d , -q %s -H -Q ascii' % tmp_query_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode,3)
self.assertEquals(len(o),0)
self.assertEquals(len(e),1)
self.assertTrue(e[0].startswith('Could not decode query number 1 using the provided query encoding (ascii)'))
self.cleanup(tmp_data_file)
self.cleanup(tmp_query_file)
def test_output_header_with_non_ascii_names(self):
tmp_data_file = self.create_file_with_data(sample_data_with_header)
tmp_query_file = self.create_file_with_data("select name,'Hr\xc3\xa1\xc4\x8d' Hr\xc3\xa1\xc4\x8d from %s" % tmp_data_file.name,encoding=None)
cmd = '../bin/q -d , -q %s -H -Q utf-8 -O' % tmp_query_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode,0)
self.assertEquals(len(o),4)
self.assertEquals(len(e),0)
self.assertEquals(o[0].decode(SYSTEM_ENCODING), u'name,Hr\xe1\u010d')
self.assertEquals(o[1].decode(SYSTEM_ENCODING), u'a,Hr\xe1\u010d')
self.assertEquals(o[2].decode(SYSTEM_ENCODING), u'b,Hr\xe1\u010d')
self.assertEquals(o[3].decode(SYSTEM_ENCODING), u'c,Hr\xe1\u010d')
self.cleanup(tmp_data_file)
self.cleanup(tmp_query_file)
def test_use_query_file_with_query_encoding(self):
tmp_data_file = self.create_file_with_data(sample_data_with_header)
tmp_query_file = self.create_file_with_data("select name,'Hr\xc3\xa1\xc4\x8d' from %s" % tmp_data_file.name,encoding=None)
cmd = '../bin/q -d , -q %s -H -Q utf-8' % tmp_query_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(e), 0)
self.assertEquals(len(o), 3)
self.assertEquals(o[0].decode(SYSTEM_ENCODING), u'a,Hr\xe1\u010d')
self.assertEquals(o[1].decode(SYSTEM_ENCODING), u'b,Hr\xe1\u010d')
self.assertEquals(o[2].decode(SYSTEM_ENCODING), u'c,Hr\xe1\u010d')
self.cleanup(tmp_data_file)
self.cleanup(tmp_query_file)
def test_use_query_file_and_command_line(self):
tmp_data_file = self.create_file_with_data(sample_data_with_header)
tmp_query_file = self.create_file_with_data("select name from %s" % tmp_data_file.name)
cmd = '../bin/q -d , -q %s -H "select * from ppp"' % tmp_query_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 1)
self.assertEquals(len(e), 1)
self.assertEquals(len(o), 0)
self.assertTrue(e[0].startswith("Can't provide both a query file and a query on the command line"))
self.cleanup(tmp_data_file)
self.cleanup(tmp_query_file)
def test_select_output_encoding(self):
tmp_data_file = self.create_file_with_data(sample_data_with_header)
tmp_query_file = self.create_file_with_data("select 'Hr\xc3\xa1\xc4\x8d' from %s" % tmp_data_file.name,encoding=None)
for target_encoding in ['utf-8','ibm852']:
cmd = '../bin/q -d , -q %s -H -Q utf-8 -E %s' % (tmp_query_file.name,target_encoding)
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 0)
self.assertEquals(len(e), 0)
self.assertEquals(len(o), 3)
self.assertEquals(o[0].decode(target_encoding), u'Hr\xe1\u010d')
self.assertEquals(o[1].decode(target_encoding), u'Hr\xe1\u010d')
self.assertEquals(o[2].decode(target_encoding), u'Hr\xe1\u010d')
self.cleanup(tmp_data_file)
self.cleanup(tmp_query_file)
def test_select_failed_output_encoding(self):
tmp_data_file = self.create_file_with_data(sample_data_with_header)
tmp_query_file = self.create_file_with_data("select 'Hr\xc3\xa1\xc4\x8d' from %s" % tmp_data_file.name,encoding=None)
cmd = '../bin/q -d , -q %s -H -Q utf-8 -E ascii' % tmp_query_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 3)
self.assertEquals(len(e), 1)
self.assertEquals(len(o), 0)
self.assertTrue(e[0].startswith('Cannot encode data'))
self.cleanup(tmp_data_file)
self.cleanup(tmp_query_file)
def test_use_query_file_with_empty_query(self):
tmp_query_file = self.create_file_with_data(" ")
cmd = '../bin/q -d , -q %s -H' % tmp_query_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 1)
self.assertEquals(len(e), 1)
self.assertEquals(len(o), 0)
self.assertTrue(e[0].startswith("Query cannot be empty"))
self.cleanup(tmp_query_file)
def test_use_non_existent_query_file(self):
cmd = '../bin/q -d , -q non-existent-query-file -H'
retcode, o, e = run_command(cmd)
self.assertEquals(retcode, 1)
self.assertEquals(len(e), 1)
self.assertEquals(len(o), 0)
self.assertTrue(e[0].startswith("Could not read query from file"))
def test_non_quoted_values_in_quoted_data(self):
tmp_data_file = self.create_file_with_data(sample_quoted_data)
cmd = '../bin/q -d " " "select c1 from %s"' % tmp_data_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode,0)
self.assertEquals(len(e),0)
self.assertEquals(len(o),4)
self.assertTrue(o[0],'non_quoted')
self.assertTrue(o[1],'control-value-1')
self.assertTrue(o[2],'non-quoted-value')
self.assertTrue(o[3],'control-value-1')
self.cleanup(tmp_data_file)
def test_regular_quoted_values_in_quoted_data(self):
tmp_data_file = self.create_file_with_data(sample_quoted_data)
cmd = '../bin/q -d " " "select c2 from %s"' % tmp_data_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode,0)
self.assertEquals(len(e),0)
self.assertEquals(len(o),4)
self.assertTrue(o[0],'regular_double_quoted')
self.assertTrue(o[1],'control-value-2')
self.assertTrue(o[2],'this is a quoted value')
self.assertTrue(o[3],'control-value-2')
self.cleanup(tmp_data_file)
def test_double_double_quoted_values_in_quoted_data(self):
tmp_data_file = self.create_file_with_data(sample_quoted_data)
cmd = '../bin/q -d " " "select c3 from %s"' % tmp_data_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode,0)
self.assertEquals(len(e),0)
self.assertEquals(len(o),4)
self.assertTrue(o[0],'double_double_quoted')
self.assertTrue(o[1],'control-value-3')
self.assertTrue(o[2],'this is a "double double" quoted value')
self.assertTrue(o[3],'control-value-3')
self.cleanup(tmp_data_file)
def test_escaped_double_quoted_values_in_quoted_data(self):
tmp_data_file = self.create_file_with_data(sample_quoted_data)
cmd = '../bin/q -d " " "select c4 from %s"' % tmp_data_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode,0)
self.assertEquals(len(e),0)
self.assertEquals(len(o),4)
self.assertTrue(o[0],'escaped_double_quoted')
self.assertTrue(o[1],'control-value-4')
self.assertTrue(o[2],'this is an escaped "quoted value"')
self.assertTrue(o[3],'control-value-4')
self.cleanup(tmp_data_file)
def test_none_input_quoting_mode_in_relaxed_mode(self):
tmp_data_file = self.create_file_with_data(sample_quoted_data2)
cmd = '../bin/q -d " " -m relaxed -D , -w none "select * from %s"' % tmp_data_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode,0)
self.assertEquals(len(e),0)
self.assertEquals(len(o),2)
self.assertEquals(o[0],'"quoted,data",23')
self.assertEquals(o[1],'unquoted-data,54,')
self.cleanup(tmp_data_file)
def test_none_input_quoting_mode_in_strict_mode(self):
tmp_data_file = self.create_file_with_data(sample_quoted_data2)
cmd = '../bin/q -d " " -m strict -D , -w none "select * from %s"' % tmp_data_file.name
retcode, o, e = run_command(cmd)
self.assertNotEquals(retcode,0)
self.assertEquals(len(e),1)
self.assertEquals(len(o),0)
self.assertTrue(e[0].startswith('Strict mode. Column Count is expected to identical'))
self.cleanup(tmp_data_file)
def test_minimal_input_quoting_mode(self):
tmp_data_file = self.create_file_with_data(sample_quoted_data2)
cmd = '../bin/q -d " " -D , -w minimal "select * from %s"' % tmp_data_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode,0)
self.assertEquals(len(e),0)
self.assertEquals(len(o),2)
self.assertEquals(o[0],'quoted data,23')
self.assertEquals(o[1],'unquoted-data,54')
self.cleanup(tmp_data_file)
def test_all_input_quoting_mode(self):
tmp_data_file = self.create_file_with_data(sample_quoted_data2)
cmd = '../bin/q -d " " -D , -w all "select * from %s"' % tmp_data_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode,0)
self.assertEquals(len(e),0)
self.assertEquals(len(o),2)
self.assertEquals(o[0],'quoted data,23')
self.assertEquals(o[1],'unquoted-data,54')
self.cleanup(tmp_data_file)
def test_incorrect_input_quoting_mode(self):
tmp_data_file = self.create_file_with_data(sample_quoted_data2)
cmd = '../bin/q -d " " -D , -w unknown_wrapping_mode "select * from %s"' % tmp_data_file.name
retcode, o, e = run_command(cmd)
self.assertNotEquals(retcode,0)
self.assertEquals(len(e),1)
self.assertEquals(len(o),0)
self.assertTrue(e[0].startswith('Input quoting mode can only be one of all,none,minimal'))
self.assertTrue('unknown_wrapping_mode' in e[0])
self.cleanup(tmp_data_file)
def test_none_output_quoting_mode(self):
tmp_data_file = self.create_file_with_data(sample_quoted_data2)
cmd = '../bin/q -d " " -D , -w all -W none "select * from %s"' % tmp_data_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode,0)
self.assertEquals(len(e),0)
self.assertEquals(len(o),2)
self.assertEquals(o[0],'quoted data,23')
self.assertEquals(o[1],'unquoted-data,54')
self.cleanup(tmp_data_file)
def test_minimal_output_quoting_mode__without_need_to_quote_in_output(self):
tmp_data_file = self.create_file_with_data(sample_quoted_data2)
cmd = '../bin/q -d " " -D , -w all -W minimal "select * from %s"' % tmp_data_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode,0)
self.assertEquals(len(e),0)
self.assertEquals(len(o),2)
self.assertEquals(o[0],'quoted data,23')
self.assertEquals(o[1],'unquoted-data,54')
self.cleanup(tmp_data_file)
def test_minimal_output_quoting_mode__with_need_to_quote_in_output(self):
tmp_data_file = self.create_file_with_data(sample_quoted_data2)
# output delimiter is set to space, so the output will contain it
cmd = '../bin/q -d " " -D " " -w all -W minimal "select * from %s"' % tmp_data_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode,0)
self.assertEquals(len(e),0)
self.assertEquals(len(o),2)
self.assertEquals(o[0],'"quoted data" 23')
self.assertEquals(o[1],'unquoted-data 54')
self.cleanup(tmp_data_file)
def test_nonnumeric_output_quoting_mode(self):
tmp_data_file = self.create_file_with_data(sample_quoted_data2)
cmd = '../bin/q -d " " -D , -w all -W nonnumeric "select * from %s"' % tmp_data_file.name
retcode, o, e = run_command(cmd)
self.assertEquals(retcode,0)
self.assertEquals(len(e),0)
self.assertEquals(len(o),2)
self.assertEquals(o[0],'"quoted data",23')