forked from roo-rb/roo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_roo.rb
1784 lines (1638 loc) · 62.9 KB
/
test_roo.rb
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
#damit keine falschen Vermutungen aufkommen: Ich habe religioes rein gar nichts
# mit diesem Bibelbund zu tun, aber die hatten eine ziemlich grosse
# Spreadsheet-Datei mit ca. 3500 Zeilen oeffentlich im Netz, die sich ganz gut
# zum Testen eignete.
#
#--
# these test cases were developed to run under Linux OS, some commands
# (like 'diff') must be changed (or commented out ;-)) if you want to run
# the tests under another OS
#
#TODO
# Look at formulas in excel - does not work with date/time
# Dump warnings that come from the test to open files
# with the wrong spreadsheet class
STDERR.reopen "/dev/null","w"
TESTDIR = File.dirname(__FILE__)
require TESTDIR + '/test_helper.rb'
#require 'soap/rpc/driver'
require 'fileutils'
require 'timeout'
require 'logger'
$log = Logger.new(File.join(ENV['HOME'],"roo.log"))
$log.level = Logger::WARN
#$log.level = Logger::DEBUG
DISPLAY_LOG = false
DB_LOG = false
if DB_LOG
require 'activerecord'
end
include FileUtils
if DB_LOG
def activerecord_connect
ActiveRecord::Base.establish_connection(:adapter => "mysql",
:database => "test_runs",
:host => "localhost",
:username => "root",
:socket => "/var/run/mysqld/mysqld.sock")
end
class Testrun < ActiveRecord::Base
end
end
class Test::Unit::TestCase
def key_of(spreadsheetname)
begin
return {
'formula' => 'rt4Pw1WmjxFtyfrqqy94wPw',
"write.me" => 'r6m7HFlUOwst0RTUTuhQ0Ow',
'numbers1' => "rYraCzjxTtkxw1NxHJgDU8Q",
'borders' => "r_nLYMft6uWg_PT9Rc2urXw",
'simple_spreadsheet' => "r3aMMCBCA153TmU_wyIaxfw",
'testnichtvorhandenBibelbund.ods' => "invalidkeyforanyspreadsheet", # !!! intentionally false key
"only_one_sheet" => "rqRtkcPJ97nhQ0m9ksDw2rA",
'time-test' => 'r2XfDBJMrLPjmuLrPQQrEYw',
'datetime' => "r2kQpXWr6xOSUpw9MyXavYg",
'whitespace' => "rZyQaoFebVGeHKzjG6e9gRQ"
}[spreadsheetname]
# 'numbers1' => "o10837434939102457526.4784396906364855777",
# 'borders' => "o10837434939102457526.664868920231926255",
# 'simple_spreadsheet' => "ptu6bbahNZpYe-L1vEBmgGA",
# 'testnichtvorhandenBibelbund.ods' => "invalidkeyforanyspreadsheet", # !!! intentionally false key
# "only_one_sheet" => "o10837434939102457526.762705759906130135",
# "write.me" => 'ptu6bbahNZpY0N0RrxQbWdw&hl',
# 'formula' => 'o10837434939102457526.3022866619437760118',
# 'time-test' => 'ptu6bbahNZpYBMhk01UfXSg',
# 'datetime' => "ptu6bbahNZpYQEtZwzL_dZQ",
rescue
raise "unknown spreadsheetname: #{spreadsheetname}"
end
end
if DB_LOG
if ! (defined?(@connected) and @connected)
activerecord_connect
else
@connected = true
end
end
alias unlogged_run run
def run(result, &block)
t1 = Time.now
#RAILS_DEFAULT_LOGGER.debug "RUNNING #{self.class} #{@method_name} \t#{Time.now.to_s}"
if DISPLAY_LOG
print "RUNNING #{self.class} #{@method_name} \t#{Time.now.to_s}"
STDOUT.flush
end
unlogged_run result, &block
t2 = Time.now
if DISPLAY_LOG
puts "\t#{t2-t1} seconds"
end
if DB_LOG
domain = Testrun.create(
:class_name => self.class.to_s,
:test_name => @method_name,
:start => t1,
:duration => t2-t1
)
end
end
end
class File
def File.delete_if_exist(filename)
if File.exist?(filename)
File.delete(filename)
end
end
end
# :nodoc
class Fixnum
def minutes
self * 60
end
end
class TestRoo < Test::Unit::TestCase
OPENOFFICE = true # do Openoffice-Spreadsheet Tests?
EXCEL = true # do Excel Tests?
GOOGLE = false # do Google-Spreadsheet Tests?
EXCELX = true # do Excel-X Tests? (.xlsx-files)
ONLINE = true
LONG_RUN = false
GLOBAL_TIMEOUT = 48.minutes #*60 # 2*12*60 # seconds
def setup
#if DISPLAY_LOG
# puts " GLOBAL_TIMEOUT = #{GLOBAL_TIMEOUT}"
#end
end
def test_internal_minutes
assert_equal 42*60, 42.minutes
end
# call a block of code for each spreadsheet type
# and yield a reference to the roo object
def with_each_spreadsheet(options)
options[:format] ||= [:excel, :excelx, :openoffice, :google]
options[:format] = [options[:format]] if options[:format].class == Symbol
yield Roo::Spreadsheet.open(File.join(TESTDIR, options[:name] + '.xls')) if EXCEL && options[:format].include?(:excel)
yield Roo::Spreadsheet.open(File.join(TESTDIR, options[:name] + '.xlsx')) if EXCELX && options[:format].include?(:excelx)
yield Roo::Spreadsheet.open(File.join(TESTDIR, options[:name] + '.ods')) if OPENOFFICE && options[:format].include?(:openoffice)
yield Roo::Spreadsheet.open(key_of(options[:name]) || options[:name]) if GOOGLE && options[:format].include?(:google)
end
# Using Date.strptime so check that it's using the method
# with the value set in date_format
def test_date
with_each_spreadsheet(:name=>'numbers1', :format=>:google) do |oo|
# should default to DDMMYYYY
assert oo.date?("21/11/1962") == true
assert oo.date?("11/21/1962") == false
oo.date_format = '%m/%d/%Y'
assert oo.date?("21/11/1962") == false
assert oo.date?("11/21/1962") == true
oo.date_format = '%Y-%m-%d'
assert oo.date?("1962-11-21") == true
assert oo.date?("1962-21-11") == false
end
end
def test_classes
if OPENOFFICE
oo = Openoffice.new(File.join(TESTDIR,"numbers1.ods"))
assert_kind_of Openoffice, oo
end
if EXCEL
oo = Excel.new(File.join(TESTDIR,"numbers1.xls"))
assert_kind_of Excel, oo
end
if GOOGLE
oo = Google.new(key_of("numbers1"))
assert_kind_of Google, oo
end
if EXCELX
oo = Excelx.new(File.join(TESTDIR,"numbers1.xlsx"))
assert_kind_of Excelx, oo
end
end
def test_letters
assert_equal 1, GenericSpreadsheet.letter_to_number('A')
assert_equal 1, GenericSpreadsheet.letter_to_number('a')
assert_equal 2, GenericSpreadsheet.letter_to_number('B')
assert_equal 26, GenericSpreadsheet.letter_to_number('Z')
assert_equal 27, GenericSpreadsheet.letter_to_number('AA')
assert_equal 27, GenericSpreadsheet.letter_to_number('aA')
assert_equal 27, GenericSpreadsheet.letter_to_number('Aa')
assert_equal 27, GenericSpreadsheet.letter_to_number('aa')
end
def test_sheets
with_each_spreadsheet(:name=>'numbers1') do |oo|
assert_equal ["Tabelle1","Name of Sheet 2","Sheet3","Sheet4","Sheet5"], oo.sheets
assert_raise(RangeError) { oo.default_sheet = "no_sheet" }
assert_raise(TypeError) { oo.default_sheet = [1,2,3] }
oo.sheets.each { |sh|
oo.default_sheet = sh
assert_equal sh, oo.default_sheet
}
end
end
def test_cells
with_each_spreadsheet(:name=>'numbers1') do |oo|
assert_equal 1, oo.cell(1,1)
assert_equal 2, oo.cell(1,2)
assert_equal 3, oo.cell(1,3)
assert_equal 4, oo.cell(1,4)
assert_equal 5, oo.cell(2,1)
assert_equal 6, oo.cell(2,2)
assert_equal 7, oo.cell(2,3)
assert_equal 8, oo.cell(2,4)
assert_equal 9, oo.cell(2,5)
assert_equal "test", oo.cell(2,6)
assert_equal :string, oo.celltype(2,6)
assert_equal 11, oo.cell(2,7)
assert_equal :float, oo.celltype(2,7)
assert_equal 10, oo.cell(4,1)
assert_equal 11, oo.cell(4,2)
assert_equal 12, oo.cell(4,3)
assert_equal 13, oo.cell(4,4)
assert_equal 14, oo.cell(4,5)
assert_equal 10, oo.cell(4,'A')
assert_equal 11, oo.cell(4,'B')
assert_equal 12, oo.cell(4,'C')
assert_equal 13, oo.cell(4,'D')
assert_equal 14, oo.cell(4,'E')
assert_equal :date, oo.celltype(5,1)
assert_equal Date.new(1961,11,21), oo.cell(5,1)
assert_equal "1961-11-21", oo.cell(5,1).to_s
end
end
def test_celltype
with_each_spreadsheet(:name=>'numbers1') do |oo|
assert_equal :string, oo.celltype(2,6)
end
end
def test_cell_address
with_each_spreadsheet(:name=>'numbers1') do |oo|
assert_equal "tata", oo.cell(6,1)
assert_equal "tata", oo.cell(6,'A')
assert_equal "tata", oo.cell('A',6)
assert_equal "tata", oo.cell(6,'a')
assert_equal "tata", oo.cell('a',6)
assert_raise(ArgumentError) { assert_equal "tata", oo.cell('a','f') }
assert_raise(ArgumentError) { assert_equal "tata", oo.cell('f','a') }
assert_equal "thisisc8", oo.cell(8,3)
assert_equal "thisisc8", oo.cell(8,'C')
assert_equal "thisisc8", oo.cell('C',8)
assert_equal "thisisc8", oo.cell(8,'c')
assert_equal "thisisc8", oo.cell('c',8)
assert_equal "thisisd9", oo.cell('d',9)
assert_equal "thisisa11", oo.cell('a',11)
end
end
def test_office_version
with_each_spreadsheet(:name=>'numbers1', :format=>:openoffice) do |oo|
assert_equal "1.0", oo.officeversion
end
end
#TODO: inkonsequente Lieferung Fixnum/Float
def test_rows
with_each_spreadsheet(:name=>'numbers1') do |oo|
assert_equal 41, oo.cell('a',12)
assert_equal 42, oo.cell('b',12)
assert_equal 43, oo.cell('c',12)
assert_equal 44, oo.cell('d',12)
assert_equal 45, oo.cell('e',12)
assert_equal [41.0,42.0,43.0,44.0,45.0, nil, nil], oo.row(12)
assert_equal "einundvierzig", oo.cell('a',16)
assert_equal "zweiundvierzig", oo.cell('b',16)
assert_equal "dreiundvierzig", oo.cell('c',16)
assert_equal "vierundvierzig", oo.cell('d',16)
assert_equal "fuenfundvierzig", oo.cell('e',16)
assert_equal ["einundvierzig", "zweiundvierzig", "dreiundvierzig", "vierundvierzig", "fuenfundvierzig", nil, nil], oo.row(16)
end
end
def test_last_row
with_each_spreadsheet(:name=>'numbers1') do |oo|
assert_equal 18, oo.last_row
end
end
def test_last_column
with_each_spreadsheet(:name=>'numbers1') do |oo|
assert_equal 7, oo.last_column
end
end
def test_last_column_as_letter
with_each_spreadsheet(:name=>'numbers1') do |oo|
assert_equal 'G', oo.last_column_as_letter
end
end
def test_first_row
with_each_spreadsheet(:name=>'numbers1') do |oo|
assert_equal 1, oo.first_row
end
end
def test_first_column
with_each_spreadsheet(:name=>'numbers1') do |oo|
assert_equal 1, oo.first_column
end
end
def test_first_column_as_letter
with_each_spreadsheet(:name=>'numbers1') do |oo|
assert_equal 'A', oo.first_column_as_letter
end
end
def test_sheetname
with_each_spreadsheet(:name=>'numbers1') do |oo|
oo.default_sheet = "Name of Sheet 2"
assert_equal 'I am sheet 2', oo.cell('C',5)
assert_raise(RangeError) { oo.default_sheet = "non existing sheet name" }
assert_raise(RangeError) { oo.default_sheet = "non existing sheet name" }
assert_raise(RangeError) { dummy = oo.cell('C',5,"non existing sheet name")}
assert_raise(RangeError) { dummy = oo.celltype('C',5,"non existing sheet name")}
assert_raise(RangeError) { dummy = oo.empty?('C',5,"non existing sheet name")}
if oo.class == Excel
assert_raise(RuntimeError) { dummy = oo.formula?('C',5,"non existing sheet name")}
assert_raise(RuntimeError) { dummy = oo.formula('C',5,"non existing sheet name")}
else
assert_raise(RangeError) { dummy = oo.formula?('C',5,"non existing sheet name")}
assert_raise(RangeError) { dummy = oo.formula('C',5,"non existing sheet name")}
assert_raise(RangeError) { dummy = oo.set('C',5,42,"non existing sheet name")} unless oo.class == Google
assert_raise(RangeError) { dummy = oo.formulas("non existing sheet name")}
end
assert_raise(RangeError) { dummy = oo.to_yaml({},1,1,1,1,"non existing sheet name")}
end
end
def test_boundaries
with_each_spreadsheet(:name=>'numbers1') do |oo|
oo.default_sheet = "Name of Sheet 2"
assert_equal 2, oo.first_column
assert_equal 'B', oo.first_column_as_letter
assert_equal 5, oo.first_row
assert_equal 'E', oo.last_column_as_letter
assert_equal 14, oo.last_row
end
end
def test_multiple_letters
with_each_spreadsheet(:name=>'numbers1') do |oo|
oo.default_sheet = "Sheet3"
assert_equal "i am AA", oo.cell('AA',1)
assert_equal "i am AB", oo.cell('AB',1)
assert_equal "i am BA", oo.cell('BA',1)
assert_equal 'BA', oo.last_column_as_letter
assert_equal "i am BA", oo.cell(1,'BA')
end
end
def test_argument_error
with_each_spreadsheet(:name=>'numbers1') do |oo|
assert_nothing_raised(ArgumentError) { oo.default_sheet = "Tabelle1" }
end
end
def test_empty_eh
with_each_spreadsheet(:name=>'numbers1') do |oo|
assert oo.empty?('a',14)
assert ! oo.empty?('a',15)
assert oo.empty?('a',20)
end
end
def test_reload
with_each_spreadsheet(:name=>'numbers1') do |oo|
assert_equal 1, oo.cell(1,1)
oo.reload
assert_equal 1, oo.cell(1,1)
end
end
def test_bug_contiguous_cells
with_each_spreadsheet(:name=>'numbers1', :format=>:openoffice) do |oo|
oo.default_sheet = "Sheet4"
assert_equal Date.new(2007,06,16), oo.cell('a',1)
assert_equal 10, oo.cell('b',1)
assert_equal 10, oo.cell('c',1)
assert_equal 10, oo.cell('d',1)
assert_equal 10, oo.cell('e',1)
end
end
def test_bug_italo_ve
with_each_spreadsheet(:name=>'numbers1') do |oo|
oo.default_sheet = "Sheet5"
assert_equal 1, oo.cell('A',1)
assert_equal 5, oo.cell('b',1)
assert_equal 5, oo.cell('c',1)
assert_equal 2, oo.cell('a',2)
assert_equal 3, oo.cell('a',3)
end
end
def test_italo_table
with_each_spreadsheet(:name=>'simple_spreadsheet_from_italo', :format=>[:openoffice, :excel]) do |oo|
assert_equal '1', oo.cell('A',1)
assert_equal '1', oo.cell('B',1)
assert_equal '1', oo.cell('C',1)
assert_equal 1, oo.cell('A',2).to_i
assert_equal 2, oo.cell('B',2).to_i
assert_equal 1, oo.cell('C',2).to_i
assert_equal 1, oo.cell('A',3)
assert_equal 3, oo.cell('B',3)
assert_equal 1, oo.cell('C',3)
assert_equal 'A', oo.cell('A',4)
assert_equal 'A', oo.cell('B',4)
assert_equal 'A', oo.cell('C',4)
assert_equal 0.01, oo.cell('A',5)
assert_equal 0.01, oo.cell('B',5)
assert_equal 0.01, oo.cell('C',5)
assert_equal 0.03, oo.cell('a',5)+oo.cell('b',5)+oo.cell('c',5)
# Cells values in row 1:
assert_equal "1:string", oo.cell(1, 1)+":"+oo.celltype(1, 1).to_s
assert_equal "1:string",oo.cell(1, 2)+":"+oo.celltype(1, 2).to_s
assert_equal "1:string",oo.cell(1, 3)+":"+oo.celltype(1, 3).to_s
# Cells values in row 2:
assert_equal "1:string",oo.cell(2, 1)+":"+oo.celltype(2, 1).to_s
assert_equal "2:string",oo.cell(2, 2)+":"+oo.celltype(2, 2).to_s
assert_equal "1:string",oo.cell(2, 3)+":"+oo.celltype(2, 3).to_s
# Cells values in row 3:
assert_equal "1.0:float",oo.cell(3, 1).to_s+":"+oo.celltype(3, 1).to_s
assert_equal "3.0:float",oo.cell(3, 2).to_s+":"+oo.celltype(3, 2).to_s
assert_equal "1.0:float",oo.cell(3, 3).to_s+":"+oo.celltype(3, 3).to_s
# Cells values in row 4:
assert_equal "A:string",oo.cell(4, 1)+":"+oo.celltype(4, 1).to_s
assert_equal "A:string",oo.cell(4, 2)+":"+oo.celltype(4, 2).to_s
assert_equal "A:string",oo.cell(4, 3)+":"+oo.celltype(4, 3).to_s
# Cells values in row 5:
if oo.class == Openoffice
assert_equal "0.01:percentage",oo.cell(5, 1).to_s+":"+oo.celltype(5, 1).to_s
assert_equal "0.01:percentage",oo.cell(5, 2).to_s+":"+oo.celltype(5, 2).to_s
assert_equal "0.01:percentage",oo.cell(5, 3).to_s+":"+oo.celltype(5, 3).to_s
else
assert_equal "0.01:float",oo.cell(5, 1).to_s+":"+oo.celltype(5, 1).to_s
assert_equal "0.01:float",oo.cell(5, 2).to_s+":"+oo.celltype(5, 2).to_s
assert_equal "0.01:float",oo.cell(5, 3).to_s+":"+oo.celltype(5, 3).to_s
end
end
end
def test_formula_openoffice
with_each_spreadsheet(:name=>'formula', :format=>:openoffice) do |oo|
assert_equal 1, oo.cell('A',1)
assert_equal 2, oo.cell('A',2)
assert_equal 3, oo.cell('A',3)
assert_equal 4, oo.cell('A',4)
assert_equal 5, oo.cell('A',5)
assert_equal 6, oo.cell('A',6)
assert_equal 21, oo.cell('A',7)
assert_equal :formula, oo.celltype('A',7)
assert_equal "=[Sheet2.A1]", oo.formula('C',7)
assert_nil oo.formula('A',6)
assert_equal [[7, 1, "=SUM([.A1:.A6])"],
[7, 2, "=SUM([.$A$1:.B6])"],
[7, 3, "=[Sheet2.A1]"],
[8, 2, "=SUM([.$A$1:.B7])"],
], oo.formulas(oo.sheets.first)
# setting a cell
oo.set('A',15, 41)
assert_equal 41, oo.cell('A',15)
oo.set('A',16, "41")
assert_equal "41", oo.cell('A',16)
oo.set('A',17, 42.5)
assert_equal 42.5, oo.cell('A',17)
end
end
def test_formula_google
with_each_spreadsheet(:name=>'formula', :format=>:google) do |oo|
assert_equal 1, oo.cell('A',1)
assert_equal 2, oo.cell('A',2)
assert_equal 3, oo.cell('A',3)
assert_equal 4, oo.cell('A',4)
assert_equal 5, oo.cell('A',5)
assert_equal 6, oo.cell('A',6)
# assert_equal 21, oo.cell('A',7)
assert_equal 21.0, oo.cell('A',7) #TODO: better solution Fixnum/Float
assert_equal :formula, oo.celltype('A',7)
# assert_equal "=[Sheet2.A1]", oo.formula('C',7)
# !!! different from formulas in Openoffice
#was: assert_equal "=sheet2!R[-6]C[-2]", oo.formula('C',7)
# has Google changed their format of formulas/references to other sheets?
assert_equal "=Sheet2!R[-6]C[-2]", oo.formula('C',7)
assert_nil oo.formula('A',6)
# assert_equal [[7, 1, "=SUM([.A1:.A6])"],
# [7, 2, "=SUM([.$A$1:.B6])"],
# [7, 3, "=[Sheet2.A1]"],
# [8, 2, "=SUM([.$A$1:.B7])"],
# ], oo.formulas(oo.sheets.first)
# different format than in openoffice spreadsheets:
#was:
# assert_equal [[7, 1, "=SUM(R[-6]C[0]:R[-1]C[0])"],
# [7, 2, "=SUM(R1C1:R[-1]C[0])"],
# [7, 3, "=sheet2!R[-6]C[-2]"],
# [8, 2, "=SUM(R1C1:R[-1]C[0])"]],
# oo.formulas(oo.sheets.first)
assert_equal [[7, 1, "=SUM(R[-6]C:R[-1]C)"],
[7, 2, "=SUM(R1C1:R[-1]C)"],
[7, 3, "=Sheet2!R[-6]C[-2]"],
[8, 2, "=SUM(R1C1:R[-1]C)"]],
oo.formulas(oo.sheets.first)
end
end
def test_formula_excelx
with_each_spreadsheet(:name=>'formula', :format=>:excelx) do |oo|
assert_equal 1, oo.cell('A',1)
assert_equal 2, oo.cell('A',2)
assert_equal 3, oo.cell('A',3)
assert_equal 4, oo.cell('A',4)
assert_equal 5, oo.cell('A',5)
assert_equal 6, oo.cell('A',6)
assert_equal 21, oo.cell('A',7)
assert_equal :formula, oo.celltype('A',7)
#steht nicht in Datei, oder?
#nein, diesen Bezug habe ich nur in der Openoffice-Datei
#assert_equal "=[Sheet2.A1]", oo.formula('C',7)
assert_nil oo.formula('A',6)
# assert_equal [[7, 1, "=SUM([.A1:.A6])"],
# [7, 2, "=SUM([.$A$1:.B6])"],
#[7, 3, "=[Sheet2.A1]"],
#[8, 2, "=SUM([.$A$1:.B7])"],
#], oo.formulas(oo.sheets.first)
assert_equal [[7, 1, 'SUM(A1:A6)'],
[7, 2, 'SUM($A$1:B6)'],
# [7, 3, "=[Sheet2.A1]"],
# [8, 2, "=SUM([.$A$1:.B7])"],
], oo.formulas(oo.sheets.first)
# setting a cell
oo.set('A',15, 41)
assert_equal 41, oo.cell('A',15)
oo.set('A',16, "41")
assert_equal "41", oo.cell('A',16)
oo.set('A',17, 42.5)
assert_equal 42.5, oo.cell('A',17)
end
end
# Excel can only read the cell's value
def test_formula_excel
with_each_spreadsheet(:name=>'formula', :format=>:excel) do |oo|
assert_equal 21, oo.cell('A',7)
assert_equal 21, oo.cell('B',7)
end
end
def test_borders_sheets
with_each_spreadsheet(:name=>'borders') do |oo|
oo.default_sheet = oo.sheets[1]
assert_equal 6, oo.first_row
assert_equal 11, oo.last_row
assert_equal 4, oo.first_column
assert_equal 8, oo.last_column
oo.default_sheet = oo.sheets.first
assert_equal 5, oo.first_row
assert_equal 10, oo.last_row
assert_equal 3, oo.first_column
assert_equal 7, oo.last_column
oo.default_sheet = oo.sheets[2]
assert_equal 7, oo.first_row
assert_equal 12, oo.last_row
assert_equal 5, oo.first_column
assert_equal 9, oo.last_column
end
end
def yaml_entry(row,col,type,value)
"cell_#{row}_#{col}: \n row: #{row} \n col: #{col} \n celltype: #{type} \n value: #{value} \n"
end
def test_to_yaml
with_each_spreadsheet(:name=>'numbers1') do |oo|
assert_equal "--- \n"+yaml_entry(5,1,"date","1961-11-21"), oo.to_yaml({}, 5,1,5,1)
assert_equal "--- \n"+yaml_entry(8,3,"string","thisisc8"), oo.to_yaml({}, 8,3,8,3)
assert_equal "--- \n"+yaml_entry(12,3,"float",43.0), oo.to_yaml({}, 12,3,12,3)
assert_equal \
"--- \n"+yaml_entry(12,3,"float",43.0) +
yaml_entry(12,4,"float",44.0) +
yaml_entry(12,5,"float",45.0), oo.to_yaml({}, 12,3,12)
assert_equal \
"--- \n"+yaml_entry(12,3,"float",43.0)+
yaml_entry(12,4,"float",44.0)+
yaml_entry(12,5,"float",45.0)+
yaml_entry(15,3,"float",43.0)+
yaml_entry(15,4,"float",44.0)+
yaml_entry(15,5,"float",45.0)+
yaml_entry(16,3,"string","dreiundvierzig")+
yaml_entry(16,4,"string","vierundvierzig")+
yaml_entry(16,5,"string","fuenfundvierzig"), oo.to_yaml({}, 12,3)
end
end
def test_only_one_sheet
with_each_spreadsheet(:name=>'only_one_sheet') do |oo|
assert_equal 42, oo.cell('B',4)
assert_equal 43, oo.cell('C',4)
assert_equal 44, oo.cell('D',4)
oo.default_sheet = oo.sheets.first
assert_equal 42, oo.cell('B',4)
assert_equal 43, oo.cell('C',4)
assert_equal 44, oo.cell('D',4)
end
end
def test_excel_open_from_uri_and_zipped
if EXCEL
if ONLINE
begin
url = 'http://stiny-leonhard.de/bode-v1.xls.zip'
excel = Excel.new(url, :zip)
excel.default_sheet = excel.sheets.first
assert_equal 'ist "e" im Nenner von H(s)', excel.cell('b', 5)
ensure
excel.remove_tmp
end
end
end
end
def test_openoffice_open_from_uri_and_zipped
if OPENOFFICE
if ONLINE
begin
url = 'http://spazioinwind.libero.it/s2/rata.ods.zip'
sheet = Openoffice.new(url, :zip)
#has been changed: assert_equal 'ist "e" im Nenner von H(s)', sheet.cell('b', 5)
assert_in_delta 0.001, 505.14, sheet.cell('c', 33).to_f
ensure
sheet.remove_tmp
end
end
end
end
def test_excel_zipped
if EXCEL
begin
oo = Excel.new(File.join(TESTDIR,"bode-v1.xls.zip"), :zip)
assert oo
assert_equal 'ist "e" im Nenner von H(s)', oo.cell('b', 5)
ensure
oo.remove_tmp
end
end
end
def test_openoffice_zipped
if OPENOFFICE
begin
oo = Openoffice.new(File.join(TESTDIR,"bode-v1.ods.zip"), :zip)
assert oo
assert_equal 'ist "e" im Nenner von H(s)', oo.cell('b', 5)
ensure
oo.remove_tmp
end
end
end
def test_bug_ric
with_each_spreadsheet(:name=>'ric', :format=>:openoffice) do |oo|
assert oo.empty?('A',1)
assert oo.empty?('B',1)
assert oo.empty?('C',1)
assert oo.empty?('D',1)
expected = 1
letter = 'e'
while letter <= 'u'
assert_equal expected, oo.cell(letter,1)
letter.succ!
expected += 1
end
assert_equal 'J', oo.cell('v',1)
assert_equal 'P', oo.cell('w',1)
assert_equal 'B', oo.cell('x',1)
assert_equal 'All', oo.cell('y',1)
assert_equal 0, oo.cell('a',2)
assert oo.empty?('b',2)
assert oo.empty?('c',2)
assert oo.empty?('d',2)
assert_equal 'B', oo.cell('e',2)
assert_equal 'B', oo.cell('f',2)
assert_equal 'B', oo.cell('g',2)
assert_equal 'B', oo.cell('h',2)
assert_equal 'B', oo.cell('i',2)
assert_equal 'B', oo.cell('j',2)
assert_equal 'B', oo.cell('k',2)
assert_equal 'B', oo.cell('l',2)
assert_equal 'B', oo.cell('m',2)
assert_equal 'B', oo.cell('n',2)
assert_equal 'B', oo.cell('o',2)
assert_equal 'B', oo.cell('p',2)
assert_equal 'B', oo.cell('q',2)
assert_equal 'B', oo.cell('r',2)
assert_equal 'B', oo.cell('s',2)
assert oo.empty?('t',2)
assert oo.empty?('u',2)
assert_equal 0 , oo.cell('v',2)
assert_equal 0 , oo.cell('w',2)
assert_equal 15 , oo.cell('x',2)
assert_equal 15 , oo.cell('y',2)
end
end
def test_mehrteilig
with_each_spreadsheet(:name=>'Bibelbund1', :format=>:openoffice) do |oo|
assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
end
#if EXCELX
# #Datei gibt es noch nicht
# oo = Excelx.new(File.join(TESTDIR,"Bibelbund1.xlsx"))
# oo.default_sheet = oo.sheets.first
# assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
#end
end
def test_huge_document_to_csv
if LONG_RUN
with_each_spreadsheet(:name=>'Bibelbund') do |oo|
assert_nothing_raised(Timeout::Error) {
Timeout::timeout(GLOBAL_TIMEOUT) do |timeout_length|
File.delete_if_exist("/tmp/Bibelbund.csv")
assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
assert_equal "Tagebuch des Sekret\303\244rs. Nachrichten aus Chile", oo.cell(46,'A')
assert_equal "Tagebuch aus Chile Juli 1977", oo.cell(55,'A')
assert oo.to_csv("/tmp/Bibelbund.csv")
assert File.exists?("/tmp/Bibelbund.csv")
assert_equal "", `diff test/Bibelbund.csv /tmp/Bibelbund.csv`
end
}
end
end
end
def test_to_csv
with_each_spreadsheet(:name=>'numbers1') do |oo|
master = "#{TESTDIR}/numbers1.csv"
File.delete_if_exist("/tmp/numbers1.csv")
assert oo.to_csv("/tmp/numbers1.csv",oo.sheets.first)
assert File.exists?("/tmp/numbers1.csv")
assert_equal "", `diff #{master} /tmp/numbers1.csv`
assert oo.to_csv("/tmp/numbers1.csv")
assert File.exists?("/tmp/numbers1.csv")
assert_equal "", `diff #{master} /tmp/numbers1.csv`
end
end
def test_bug_mehrere_datum
with_each_spreadsheet(:name=>'numbers1') do |oo|
oo.default_sheet = 'Sheet5'
assert_equal :date, oo.celltype('A',4)
assert_equal :date, oo.celltype('B',4)
assert_equal :date, oo.celltype('C',4)
assert_equal :date, oo.celltype('D',4)
assert_equal :date, oo.celltype('E',4)
assert_equal Date.new(2007,11,21), oo.cell('A',4)
assert_equal Date.new(2007,11,21), oo.cell('B',4)
assert_equal Date.new(2007,11,21), oo.cell('C',4)
assert_equal Date.new(2007,11,21), oo.cell('D',4)
assert_equal Date.new(2007,11,21), oo.cell('E',4)
assert_equal :float, oo.celltype('A',5)
assert_equal :float, oo.celltype('B',5)
assert_equal :float, oo.celltype('C',5)
assert_equal :float, oo.celltype('D',5)
assert_equal :float, oo.celltype('E',5)
assert_equal 42, oo.cell('A',5)
assert_equal 42, oo.cell('B',5)
assert_equal 42, oo.cell('C',5)
assert_equal 42, oo.cell('D',5)
assert_equal 42, oo.cell('E',5)
assert_equal :string, oo.celltype('A',6)
assert_equal :string, oo.celltype('B',6)
assert_equal :string, oo.celltype('C',6)
assert_equal :string, oo.celltype('D',6)
assert_equal :string, oo.celltype('E',6)
assert_equal "ABC", oo.cell('A',6)
assert_equal "ABC", oo.cell('B',6)
assert_equal "ABC", oo.cell('C',6)
assert_equal "ABC", oo.cell('D',6)
assert_equal "ABC", oo.cell('E',6)
end
end
def test_multiple_sheets
with_each_spreadsheet(:name=>'numbers1') do |oo|
2.times do
oo.default_sheet = "Tabelle1"
assert_equal 1, oo.cell(1,1)
assert_equal 1, oo.cell(1,1,"Tabelle1")
assert_equal "I am sheet 2", oo.cell('C',5,"Name of Sheet 2")
sheetname = 'Sheet5'
assert_equal :date, oo.celltype('A',4,sheetname)
assert_equal :date, oo.celltype('B',4,sheetname)
assert_equal :date, oo.celltype('C',4,sheetname)
assert_equal :date, oo.celltype('D',4,sheetname)
assert_equal :date, oo.celltype('E',4,sheetname)
assert_equal Date.new(2007,11,21), oo.cell('A',4,sheetname)
assert_equal Date.new(2007,11,21), oo.cell('B',4,sheetname)
assert_equal Date.new(2007,11,21), oo.cell('C',4,sheetname)
assert_equal Date.new(2007,11,21), oo.cell('D',4,sheetname)
assert_equal Date.new(2007,11,21), oo.cell('E',4,sheetname)
assert_equal :float, oo.celltype('A',5,sheetname)
assert_equal :float, oo.celltype('B',5,sheetname)
assert_equal :float, oo.celltype('C',5,sheetname)
assert_equal :float, oo.celltype('D',5,sheetname)
assert_equal :float, oo.celltype('E',5,sheetname)
assert_equal 42, oo.cell('A',5,sheetname)
assert_equal 42, oo.cell('B',5,sheetname)
assert_equal 42, oo.cell('C',5,sheetname)
assert_equal 42, oo.cell('D',5,sheetname)
assert_equal 42, oo.cell('E',5,sheetname)
assert_equal :string, oo.celltype('A',6,sheetname)
assert_equal :string, oo.celltype('B',6,sheetname)
assert_equal :string, oo.celltype('C',6,sheetname)
assert_equal :string, oo.celltype('D',6,sheetname)
assert_equal :string, oo.celltype('E',6,sheetname)
assert_equal "ABC", oo.cell('A',6,sheetname)
assert_equal "ABC", oo.cell('B',6,sheetname)
assert_equal "ABC", oo.cell('C',6,sheetname)
assert_equal "ABC", oo.cell('D',6,sheetname)
assert_equal "ABC", oo.cell('E',6,sheetname)
oo.reload
end
end
end
def test_bug_empty_sheet
with_each_spreadsheet(:name=>'formula', :format=>[:openoffice, :excelx]) do |oo|
oo.default_sheet = 'Sheet3' # is an empty sheet
assert_nothing_raised(NoMethodError) { oo.to_csv(File.join("/","tmp","emptysheet.csv")) }
assert_equal "", `cat /tmp/emptysheet.csv`
end
end
def test_find_by_row_huge_document
if LONG_RUN
with_each_spreadsheet(:name=>'Bibelbund') do |oo|
Timeout::timeout(GLOBAL_TIMEOUT) do |timeout_length|
oo.default_sheet = oo.sheets.first
rec = oo.find 20
assert rec
# assert_equal "Brief aus dem Sekretariat", rec[0]
#p rec
assert_equal "Brief aus dem Sekretariat", rec[0]['TITEL']
rec = oo.find 22
assert rec
# assert_equal "Brief aus dem Skretariat. Tagung in Amberg/Opf.",rec[0]
assert_equal "Brief aus dem Skretariat. Tagung in Amberg/Opf.",rec[0]['TITEL']
end
end
end
end
def test_find_by_row
with_each_spreadsheet(:name=>'numbers1') do |oo|
oo.header_line = nil
rec = oo.find 16
assert rec
assert_nil oo.header_line
# keine Headerlines in diesem Beispiel definiert
assert_equal "einundvierzig", rec[0]
#assert_equal false, rec
rec = oo.find 15
assert rec
assert_equal 41,rec[0]
end
end
def test_find_by_conditions
if LONG_RUN
with_each_spreadsheet(:name=>'Bibelbund') do |oo|
assert_nothing_raised(Timeout::Error) {
Timeout::timeout(GLOBAL_TIMEOUT) do |timeout_length|
#-----------------------------------------------------------------
zeilen = oo.find(:all, :conditions => {
'TITEL' => 'Brief aus dem Sekretariat'
}
)
assert_equal 2, zeilen.size
assert_equal [{"VERFASSER"=>"Almassy, Annelene von",
"INTERNET"=>nil,
"SEITE"=>316.0,
"KENNUNG"=>"Aus dem Bibelbund",
"OBJEKT"=>"Bibel+Gem",
"PC"=>"#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
"NUMMER"=>"1982-3",
"TITEL"=>"Brief aus dem Sekretariat"},
{"VERFASSER"=>"Almassy, Annelene von",
"INTERNET"=>nil,
"SEITE"=>222.0,
"KENNUNG"=>"Aus dem Bibelbund",
"OBJEKT"=>"Bibel+Gem",
"PC"=>"#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
"NUMMER"=>"1983-2",
"TITEL"=>"Brief aus dem Sekretariat"}] , zeilen
#----------------------------------------------------------
zeilen = oo.find(:all,
:conditions => { 'VERFASSER' => 'Almassy, Annelene von' }
)
assert_equal 13, zeilen.size
#----------------------------------------------------------
zeilen = oo.find(:all, :conditions => {
'TITEL' => 'Brief aus dem Sekretariat',
'VERFASSER' => 'Almassy, Annelene von',
}
)
assert_equal 2, zeilen.size
assert_equal [{"VERFASSER"=>"Almassy, Annelene von",
"INTERNET"=>nil,
"SEITE"=>316.0,
"KENNUNG"=>"Aus dem Bibelbund",
"OBJEKT"=>"Bibel+Gem",
"PC"=>"#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
"NUMMER"=>"1982-3",
"TITEL"=>"Brief aus dem Sekretariat"},
{"VERFASSER"=>"Almassy, Annelene von",
"INTERNET"=>nil,
"SEITE"=>222.0,
"KENNUNG"=>"Aus dem Bibelbund",
"OBJEKT"=>"Bibel+Gem",
"PC"=>"#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
"NUMMER"=>"1983-2",
"TITEL"=>"Brief aus dem Sekretariat"}] , zeilen
# Result as an array
zeilen = oo.find(:all,
:conditions => {
'TITEL' => 'Brief aus dem Sekretariat',
'VERFASSER' => 'Almassy, Annelene von',
}, :array => true)
assert_equal 2, zeilen.size
assert_equal [
[
"Brief aus dem Sekretariat",
"Almassy, Annelene von",
"Bibel+Gem",
"1982-3",
316.0,
nil,
"#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
"Aus dem Bibelbund",
],
[
"Brief aus dem Sekretariat",
"Almassy, Annelene von",
"Bibel+Gem",
"1983-2",
222.0,
nil,
"#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
"Aus dem Bibelbund",
]] , zeilen
end # Timeout
} # nothing_raised
end
end
end
#TODO: temporaerer Test