ruport 1.0.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/query_test.rb CHANGED
@@ -1,4 +1,5 @@
1
- require "test/helpers"
1
+ #!/usr/bin/env ruby -w
2
+ require File.join(File.expand_path(File.dirname(__FILE__)), "helpers")
2
3
 
3
4
  begin
4
5
  require 'mocha'
@@ -141,7 +142,7 @@
141
142
  end
142
143
 
143
144
  def test_each_without_block
144
- assert_raise (LocalJumpError) { @query[:plain].each }
145
+ assert_raise(LocalJumpError) { @query[:plain].each }
145
146
  end
146
147
 
147
148
  def test_select_source
data/test/record_test.rb CHANGED
@@ -1,4 +1,5 @@
1
- require "test/helpers"
1
+ #!/usr/bin/env ruby -w
2
+ require File.join(File.expand_path(File.dirname(__FILE__)), "helpers")
2
3
 
3
4
  class TestRecord < Test::Unit::TestCase
4
5
 
@@ -1,4 +1,5 @@
1
- require 'test/helpers'
1
+ #!/usr/bin/env ruby -w
2
+ require File.join(File.expand_path(File.dirname(__FILE__)), "helpers")
2
3
 
3
4
  ###########################################################################
4
5
  #
@@ -68,6 +69,15 @@ class TestRenderer < Test::Unit::TestCase
68
69
  def test_trivial
69
70
  actual = OldSchoolRenderer.render(:text)
70
71
  assert_equal "header\nbody\nfooter\n", actual
72
+ end
73
+
74
+ context "when using templates" do
75
+ def specify_apply_template_should_be_called
76
+ Ruport::Formatter::Template.create(:stub)
77
+ Table(%w[a b c]).to_csv(:template => :stub) do |r|
78
+ r.formatter.expects(:apply_template)
79
+ end
80
+ end
71
81
  end
72
82
 
73
83
  def test_using_io
@@ -79,6 +89,21 @@ class TestRenderer < Test::Unit::TestCase
79
89
  assert_equal "", out.read
80
90
  end
81
91
 
92
+ def test_using_file
93
+ begin
94
+ require "mocha"
95
+ require "stubba"
96
+ rescue LoadError
97
+ $stderr.puts "Warning: Mocha not found -- skipping some Renderer tests"
98
+ end
99
+ if Object.const_defined?(:Mocha)
100
+ f = []
101
+ File.expects(:open).yields(f)
102
+ a = OldSchoolRenderer.render(:text, :file => "foo.text")
103
+ assert_equal "header\nbody\nfooter\n", f[0]
104
+ end
105
+ end
106
+
82
107
  def test_formats
83
108
  assert_equal( {}, Ruport::Renderer.formats )
84
109
  assert_equal( { :text => DummyText },OldSchoolRenderer.formats )
@@ -485,7 +510,7 @@ class TestRendererHooks < Test::Unit::TestCase
485
510
  include Ruport::Renderer::Hooks
486
511
  renders_as_table
487
512
 
488
- def renderable_data
513
+ def renderable_data(format)
489
514
  1
490
515
  end
491
516
  end
@@ -511,7 +536,28 @@ class TestRendererHooks < Test::Unit::TestCase
511
536
  assert_raises(ArgumentError) { DummyObject3.new.as(:csv) }
512
537
  end
513
538
 
539
+ class DummyObject4
540
+ include Ruport::Renderer::Hooks
541
+ renders_as_table
542
+
543
+ def renderable_data(format)
544
+ case format
545
+ when :html
546
+ 1
547
+ when :csv
548
+ 2
549
+ end
550
+ end
551
+ end
552
+
553
+ def specify_should_return_results_of_renderable_data_using_format
554
+ a = DummyObject4.new
555
+ rend = mock("renderer")
556
+ rend.expects(:data=).with(2)
557
+ Ruport::Renderer::Table.expects(:render).with(:csv,{}).yields(rend)
558
+ a.as(:csv)
559
+ end
514
560
 
515
- end
561
+ end
516
562
 
517
563
  end
@@ -1,4 +1,5 @@
1
- require "test/helpers"
1
+ #!/usr/bin/env ruby -w
2
+ require File.join(File.expand_path(File.dirname(__FILE__)), "helpers")
2
3
  class TestSqlSplit < Test::Unit::TestCase
3
4
  include Ruport
4
5
 
@@ -10,7 +11,8 @@ class TestSqlSplit < Test::Unit::TestCase
10
11
  end
11
12
 
12
13
  def test_sql_split_complex
13
- sql = File.read 'test/samples/ruport_test.sql'
14
+ sql = File.read File.join(File.expand_path(File.dirname(__FILE__)),
15
+ *%w[samples ruport_test.sql])
14
16
  split = Query::SqlSplit.new sql
15
17
  assert_equal( 'SELECT * FROM ruport_test', split.last )
16
18
  end
data/test/table_test.rb CHANGED
@@ -1,4 +1,6 @@
1
- require "test/helpers"
1
+ #!/usr/bin/env ruby -w
2
+ require File.join(File.expand_path(File.dirname(__FILE__)), "helpers")
3
+ TEST_SAMPLES = File.join(File.expand_path(File.dirname(__FILE__)), "samples")
2
4
 
3
5
  class Person < Ruport::Data::Record
4
6
 
@@ -35,6 +37,62 @@ class TestTable < Test::Unit::TestCase
35
37
  end
36
38
  end
37
39
 
40
+ context "when filtering data" do
41
+
42
+ def setup
43
+ @data = [[1,2,3],[4,5,6],[7,8,9]]
44
+ end
45
+
46
+ def specify_filters_should_discard_unmatched_rows
47
+ table = Ruport::Data::Table.new(:column_names => %w[a b c],
48
+ :data => [[1,2,3],[4,5,6],[7,8,9]],
49
+ :filters => [ lambda { |r| r.a % 2 == 1 } ] )
50
+ assert_equal Table(%w[a b c]) << [1,2,3] << [7,8,9], table
51
+ end
52
+
53
+ def specify_filters_should_work_on_csvs
54
+ only_ids_less_than_3 = lambda { |r| r["id"].to_i < 3 }
55
+ table = Table(File.join(TEST_SAMPLES,"addressbook.csv"),
56
+ :filters => [only_ids_less_than_3])
57
+ assert_equal ["1","2"], table.map { |r| r["id"] }
58
+ end
59
+ end
60
+
61
+ context "when transforming data" do
62
+
63
+ def setup
64
+ @data = [[1,2,3],[4,5,6],[7,8,9]]
65
+ end
66
+
67
+ def specify_transforms_should_modify_table_data
68
+
69
+ stringify_c = lambda { |r| r.c = r.c.to_s }
70
+ add_two_to_all_int_cols = lambda { |r|
71
+ r.each_with_index do |c,i|
72
+ if Fixnum === c
73
+ r[i] += 2
74
+ end
75
+ end
76
+
77
+ }
78
+
79
+ table = Ruport::Data::Table.new(:column_names => %w[a b c],
80
+ :data => @data,
81
+ :transforms => [stringify_c,
82
+ add_two_to_all_int_cols])
83
+ assert_equal Table(%w[a b c]) << [3,4,"3"] << [6,7,"6"] << [9,10,"9"],
84
+ table
85
+
86
+ end
87
+
88
+ def specify_transforms_should_work_on_csvs
89
+ ids_to_i = lambda { |r| r["id"] = r["id"].to_i }
90
+ table = Table(File.join(TEST_SAMPLES,"addressbook.csv"),
91
+ :filters => [ids_to_i])
92
+ assert_equal [1,2,3,4,5], table.map { |r| r["id"] }
93
+ end
94
+ end
95
+
38
96
  def test_to_group
39
97
  a =[[1,2,3],[4,5,6]].to_table(%w[a b c]).to_group("Packrats")
40
98
  b = Ruport::Data::Group.new( :data => [[1,2,3],[4,5,6]],
@@ -106,8 +164,8 @@ class TestTable < Test::Unit::TestCase
106
164
  end
107
165
 
108
166
  def test_reorder
109
- table = Ruport::Data::Table.load("test/samples/data.csv")
110
- table.reorder *%w[col1 col3]
167
+ table = Ruport::Data::Table.load(File.join(TEST_SAMPLES,"data.csv"))
168
+ table.reorder(*%w[col1 col3])
111
169
  assert_equal %w[col1 col3], table.column_names
112
170
  rows = [%w[a c], %w[d e]]
113
171
  table.each { |r| assert_equal rows.shift, r.to_a
@@ -126,59 +184,93 @@ class TestTable < Test::Unit::TestCase
126
184
  assert_equal b.column_names.object_id,
127
185
  r.instance_eval{@attributes}.object_id
128
186
  }
129
- end
187
+ end
130
188
 
131
- def test_sort_rows_by
132
- table = Ruport::Data::Table.new :column_names => %w[a b c]
133
- table << [1,2,3] << [6,1,8] << [9,1,4]
189
+ context "when sorting rows" do
134
190
 
135
- table2 = Ruport::Data::Table.new :column_names => [:a, :b, :c]
136
- table2 << [1,2,3] << [6,1,8] << [9,1,4]
137
-
138
- sorted_table_a = Ruport::Data::Table.new :column_names => %w[a b c]
139
- sorted_table_a << [1,2,3] << [6,1,8] << [9,1,4]
140
-
141
- sorted_table_b = Ruport::Data::Table.new :column_names => %w[a b c]
142
- sorted_table_b << [6,1,8] << [9,1,4] << [1,2,3]
191
+ def setup
192
+ @table = Table(%w[a b c]) << [1,2,3] << [6,1,8] << [9,1,4]
193
+ @table_with_nils = Table(%w[a b c]) << [1,nil,3] << [9,3,4] << [6,1,8]
194
+ end
143
195
 
144
- sorted_table_bc = Ruport::Data::Table.new :column_names => %w[a b c]
145
- sorted_table_bc << [9,1,4] << [6,1,8] << [1,2,3]
196
+ def specify_should_sort_in_reverse_order_on_descending
197
+ t = @table.sort_rows_by("a", :order => :descending )
198
+ assert_equal Table(%w[a b c]) << [9,1,4] << [6,1,8] << [1,2,3], t
199
+
200
+ t = @table.sort_rows_by("c", :order => :descending )
201
+ assert_equal Table(%w[a b c]) << [6,1,8] << [9,1,4] << [1,2,3], t
202
+ end
146
203
 
147
- sorted_table_bs = Ruport::Data::Table.new :column_names => [:a, :b, :c]
148
- sorted_table_bs << [6,1,8] << [9,1,4] << [1,2,3]
149
-
150
- assert_equal sorted_table_a, table.sort_rows_by {|r| r['a']}
151
- assert_equal sorted_table_b, table.sort_rows_by(['b'])
152
- assert_equal sorted_table_bc, table.sort_rows_by(['b', 'c'])
153
- assert_equal sorted_table_bs, table2.sort_rows_by(:b)
154
- end
155
-
156
- def test_sort_rows_by!
157
- table = Ruport::Data::Table.new :column_names => %w[a b c]
158
- table << [1,2,3] << [6,1,8] << [9,1,4]
204
+ def specify_show_put_rows_with_nil_columns_after_sorted_rows
205
+ # should not effect when using columns that are all populated
206
+ t = @table_with_nils.sort_rows_by("a")
207
+ assert_equal Table(%w[a b c]) << [1,nil,3] << [6,1,8] << [9,3,4], t
208
+
209
+ t = @table_with_nils.sort_rows_by("b")
210
+ assert_equal Table(%w[a b c]) << [6,1,8] << [9,3,4] << [1,nil,3], t
211
+
212
+ t = @table_with_nils.sort_rows_by("b", :order => :descending)
213
+ assert_equal Table(%w[a b c]) << [1,nil,3] << [9,3,4] << [6,1,8], t
214
+ end
215
+
216
+ def specify_in_place_sort_should_allow_order_by
217
+ @table.sort_rows_by!("a", :order => :descending )
218
+ assert_equal Table(%w[a b c]) << [9,1,4] << [6,1,8] << [1,2,3], @table
219
+ end
220
+
221
+ def specify_sort_rows_by
222
+ table = Ruport::Data::Table.new :column_names => %w[a b c]
223
+ table << [1,2,3] << [6,1,8] << [9,1,4]
224
+
225
+ table2 = Ruport::Data::Table.new :column_names => [:a, :b, :c]
226
+ table2 << [1,2,3] << [6,1,8] << [9,1,4]
159
227
 
160
- sorted_table_a = Ruport::Data::Table.new :column_names => %w[a b c]
161
- sorted_table_a << [1,2,3] << [6,1,8] << [9,1,4]
228
+ sorted_table_a = Ruport::Data::Table.new :column_names => %w[a b c]
229
+ sorted_table_a << [1,2,3] << [6,1,8] << [9,1,4]
162
230
 
163
- sorted_table_b = Ruport::Data::Table.new :column_names => %w[a b c]
164
- sorted_table_b << [6,1,8] << [9,1,4] << [1,2,3]
231
+ sorted_table_b = Ruport::Data::Table.new :column_names => %w[a b c]
232
+ sorted_table_b << [6,1,8] << [9,1,4] << [1,2,3]
233
+
234
+ sorted_table_bc = Ruport::Data::Table.new :column_names => %w[a b c]
235
+ sorted_table_bc << [9,1,4] << [6,1,8] << [1,2,3]
165
236
 
166
- sorted_table_bc = Ruport::Data::Table.new :column_names => %w[a b c]
167
- sorted_table_bc << [9,1,4] << [6,1,8] << [1,2,3]
237
+ sorted_table_bs = Ruport::Data::Table.new :column_names => [:a, :b, :c]
238
+ sorted_table_bs << [6,1,8] << [9,1,4] << [1,2,3]
239
+
240
+ assert_equal sorted_table_a, table.sort_rows_by {|r| r['a']}
241
+ assert_equal sorted_table_b, table.sort_rows_by(['b'])
242
+ assert_equal sorted_table_bc, table.sort_rows_by(['b', 'c'])
243
+ assert_equal sorted_table_bs, table2.sort_rows_by(:b)
244
+ end
245
+
246
+ def specify_sort_rows_by!
247
+ table = Ruport::Data::Table.new :column_names => %w[a b c]
248
+ table << [1,2,3] << [6,1,8] << [9,1,4]
249
+
250
+ sorted_table_a = Ruport::Data::Table.new :column_names => %w[a b c]
251
+ sorted_table_a << [1,2,3] << [6,1,8] << [9,1,4]
252
+
253
+ sorted_table_b = Ruport::Data::Table.new :column_names => %w[a b c]
254
+ sorted_table_b << [6,1,8] << [9,1,4] << [1,2,3]
168
255
 
169
- table_a = table.dup
170
- table_a.sort_rows_by! { |r| r['a'] }
256
+ sorted_table_bc = Ruport::Data::Table.new :column_names => %w[a b c]
257
+ sorted_table_bc << [9,1,4] << [6,1,8] << [1,2,3]
171
258
 
172
- table_b = table.dup
173
- table_b.sort_rows_by!("b")
259
+ table_a = table.dup
260
+ table_a.sort_rows_by! { |r| r['a'] }
174
261
 
175
- table_bc = table.dup
176
- table_bc.sort_rows_by!(['b', 'c'])
262
+ table_b = table.dup
263
+ table_b.sort_rows_by!("b")
264
+
265
+ table_bc = table.dup
266
+ table_bc.sort_rows_by!(['b', 'c'])
177
267
 
178
- assert_equal sorted_table_a, table_a
179
- assert_equal sorted_table_b, table_b
180
- assert_equal sorted_table_bc, table_bc
181
- end
268
+ assert_equal sorted_table_a, table_a
269
+ assert_equal sorted_table_b, table_b
270
+ assert_equal sorted_table_bc, table_bc
271
+ end
272
+
273
+ end
182
274
 
183
275
  def test_array_hack
184
276
  t = [[1,2],[3,4],[5,6]].to_table
@@ -601,7 +693,7 @@ end
601
693
  class TestTableFromCSV < Test::Unit::TestCase
602
694
 
603
695
  def test_csv_load
604
- table = Ruport::Data::Table.load("test/samples/data.csv")
696
+ table = Ruport::Data::Table.load(File.join(TEST_SAMPLES,"data.csv"))
605
697
  assert_equal %w[col1 col2 col3], table.column_names
606
698
  rows = [%w[a b c],["d",nil,"e"]]
607
699
  table.each { |r| assert_equal rows.shift, r.to_a
@@ -609,14 +701,14 @@ class TestTableFromCSV < Test::Unit::TestCase
609
701
  expected = [%w[1 2 3],%w[4 5 6]].to_table(%w[a b c])
610
702
 
611
703
  # ticket:94
612
- table = Ruport::Data::Table.load( "test/samples/data.tsv",
704
+ table = Ruport::Data::Table.load( File.join(TEST_SAMPLES,"data.tsv"),
613
705
  :csv_options => { :col_sep => "\t" } )
614
706
  assert_equal expected, table
615
707
 
616
708
 
617
709
  expected = ['c','e']
618
710
 
619
- table = Ruport::Data::Table.load( "test/samples/data.csv", :csv_options =>
711
+ table = Ruport::Data::Table.load( File.join(TEST_SAMPLES,"data.csv"), :csv_options =>
620
712
  { :headers => true, :header_converters => :symbol } ) do |s,r|
621
713
  assert_equal expected.shift, r[:col3]
622
714
  end
@@ -626,13 +718,13 @@ class TestTableFromCSV < Test::Unit::TestCase
626
718
 
627
719
  expected = ['c','e']
628
720
 
629
- Ruport::Data::Table.load( "test/samples/data.csv",
721
+ Ruport::Data::Table.load( File.join(TEST_SAMPLES,"data.csv"),
630
722
  :records => true ) do |s,r|
631
723
  assert_equal expected.shift, r.col3
632
724
  assert_kind_of Ruport::Data::Record, r
633
725
  end
634
726
 
635
- table = Ruport::Data::Table.load( "test/samples/data.csv",
727
+ table = Ruport::Data::Table.load( File.join(TEST_SAMPLES, "data.csv"),
636
728
  :has_names => false )
637
729
  assert_equal([],table.column_names)
638
730
  assert_equal([%w[col1 col2 col3],%w[a b c],["d",nil,"e"]].to_table, table)
@@ -683,8 +775,8 @@ class TestTableFromCSV < Test::Unit::TestCase
683
775
  }
684
776
  assert_equal [%w[a b],%w[a b],%w[1 2], %w[1 2],
685
777
  %w[3 4],%w[3 4]].to_table, t
686
- x = Ruport::Data::Table.load("test/samples/data.csv") { |s,r|
687
- assert_kind_of Ruport::Data::Table, s
778
+ x = Ruport::Data::Table.load(File.join(TEST_SAMPLES,"data.csv")) { |s,r|
779
+ assert_kind_of Ruport::Data::Feeder, s
688
780
  assert_kind_of Array, r
689
781
  s << r
690
782
  s << r
@@ -693,13 +785,14 @@ class TestTableFromCSV < Test::Unit::TestCase
693
785
  end
694
786
 
695
787
  def test_ensure_csv_loading_accepts_table_options
696
- a = Table("test/samples/addressbook.csv",:record_class => DuckRecord)
788
+ a = Table(File.join(TEST_SAMPLES,"addressbook.csv"),
789
+ :record_class => DuckRecord)
697
790
  a.each { |r| assert_kind_of(DuckRecord,r) }
698
791
  end
699
792
 
700
793
  def test_ensure_table_from_csv_accepts_record_class_in_block_usage
701
- a = Table("test/samples/addressbook.csv",:record_class => DuckRecord,
702
- :records => true) do |s,r|
794
+ a = Table(File.join(TEST_SAMPLES,"addressbook.csv"),
795
+ :record_class => DuckRecord, :records => true) do |s,r|
703
796
  assert_kind_of(DuckRecord,r)
704
797
  end
705
798
  end
@@ -711,14 +804,15 @@ class TestTableKernelHack < Test::Unit::TestCase
711
804
  def test_simple
712
805
  assert_equal [].to_table(%w[a b c]), Table(%w[a b c])
713
806
  assert_equal [].to_table(%w[a b c]), Table("a","b","c")
714
- assert_equal Ruport::Data::Table.load("test/samples/addressbook.csv"),
715
- Table("test/samples/addressbook.csv")
716
807
  assert_equal Ruport::Data::Table.load(
717
- "test/samples/addressbook.csv", :has_names => false),
718
- Table('test/samples/addressbook.csv', :has_names => false)
808
+ File.join(TEST_SAMPLES,"addressbook.csv")),
809
+ Table(File.join(TEST_SAMPLES,"addressbook.csv"))
810
+ assert_equal Ruport::Data::Table.load(
811
+ File.join(TEST_SAMPLES,"addressbook.csv"), :has_names => false),
812
+ Table(File.join(TEST_SAMPLES,"addressbook.csv"), :has_names => false)
719
813
  Table("a","b","c") do |t|
720
814
  t << [1,2,3]
721
- assert_equal([[1,2,3]].to_table(%w[a b c]), t)
815
+ assert_equal([[1,2,3]].to_table(%w[a b c]), t.data)
722
816
  end
723
817
 
724
818
  assert_equal Table("a"), Table(%w[a])
@@ -727,16 +821,16 @@ class TestTableKernelHack < Test::Unit::TestCase
727
821
 
728
822
  def test_iterators
729
823
 
730
- Table("test/samples/addressbook.csv") do |s,r|
824
+ Table(File.join(TEST_SAMPLES,"addressbook.csv")) do |s,r|
731
825
  assert_kind_of(Array,r)
732
- assert_kind_of(Ruport::Data::Table,s)
826
+ assert_kind_of(Ruport::Data::Feeder,s)
733
827
  end
734
828
 
735
829
  n = 0
736
830
 
737
831
  Table(:string => "a,b,c\n1,2,3\n4,5,6\n") do |s,r|
738
832
  assert_kind_of(Array,r)
739
- assert_kind_of(Ruport::Data::Table,s)
833
+ assert_kind_of(Ruport::Data::Feeder,s)
740
834
  n += 1
741
835
  end
742
836
 
@@ -745,8 +839,8 @@ class TestTableKernelHack < Test::Unit::TestCase
745
839
  end
746
840
 
747
841
  def test_with_file_arg
748
- assert_equal Table("test/samples/addressbook.csv"),
749
- Table(:file => "test/samples/addressbook.csv")
842
+ assert_equal Table(File.join(TEST_SAMPLES,"addressbook.csv")),
843
+ Table(:file => File.join(TEST_SAMPLES,"addressbook.csv"))
750
844
  end
751
845
 
752
846
  def test_with_string_arg