ruport 0.7.2 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/AUTHORS +7 -3
  2. data/Rakefile +8 -9
  3. data/TODO +16 -0
  4. data/examples/RWEmerson.jpg +0 -0
  5. data/examples/centered_pdf_text_box.rb +66 -0
  6. data/examples/invoice.rb +35 -25
  7. data/examples/invoice_report.rb +1 -1
  8. data/examples/line_plotter.rb +1 -1
  9. data/examples/pdf_table_with_title.rb +42 -0
  10. data/lib/ruport.rb +5 -7
  11. data/lib/ruport.rb.rej +41 -0
  12. data/lib/ruport.rb~ +85 -0
  13. data/lib/ruport/attempt.rb +59 -59
  14. data/lib/ruport/config.rb +15 -4
  15. data/lib/ruport/data.rb +0 -2
  16. data/lib/ruport/data/groupable.rb +25 -16
  17. data/lib/ruport/data/record.rb +128 -102
  18. data/lib/ruport/data/table.rb +352 -199
  19. data/lib/ruport/data/taggable.rb +18 -7
  20. data/lib/ruport/format/html.rb +3 -1
  21. data/lib/ruport/format/latex.rb +1 -1
  22. data/lib/ruport/format/latex.rb.rej +26 -0
  23. data/lib/ruport/format/latex.rb~ +47 -0
  24. data/lib/ruport/format/pdf.rb +111 -28
  25. data/lib/ruport/format/pdf.rb.rej +168 -0
  26. data/lib/ruport/format/pdf.rb~ +189 -0
  27. data/lib/ruport/format/plugin.rb +0 -5
  28. data/lib/ruport/format/svg.rb +4 -4
  29. data/lib/ruport/format/xml.rb +3 -3
  30. data/lib/ruport/generator.rb +66 -27
  31. data/lib/ruport/mailer.rb +4 -1
  32. data/lib/ruport/query.rb +13 -1
  33. data/lib/ruport/renderer.rb +89 -17
  34. data/lib/ruport/renderer/graph.rb +5 -5
  35. data/lib/ruport/renderer/table.rb +8 -9
  36. data/lib/ruport/report.rb +2 -6
  37. data/test/test_config.rb +88 -76
  38. data/test/{test_text_table.rb → test_format_text.rb} +4 -2
  39. data/test/test_groupable.rb +15 -13
  40. data/test/test_query.rb +6 -3
  41. data/test/test_record.rb +57 -33
  42. data/test/test_renderer.rb +77 -0
  43. data/test/test_report.rb +188 -181
  44. data/test/test_ruport.rb +5 -6
  45. data/test/test_table.rb +290 -190
  46. data/test/test_table_renderer.rb +56 -8
  47. data/test/test_taggable.rb +7 -8
  48. data/test/unit.log +259 -7
  49. metadata +22 -19
  50. data/lib/ruport/data/collection.rb +0 -65
  51. data/lib/ruport/data/set.rb +0 -148
  52. data/test/test_collection.rb +0 -30
  53. data/test/test_set.rb +0 -118
@@ -1,4 +1,6 @@
1
- class TextPluginTest < Test::Unit::TestCase
1
+ require "test/unit"
2
+ require "ruport"
3
+ class TestFormatText < Test::Unit::TestCase
2
4
 
3
5
  def test_basic
4
6
 
@@ -21,7 +23,7 @@ class TextPluginTest < Test::Unit::TestCase
21
23
  a.as(:text) { |e| e.layout.alignment = :center })
22
24
 
23
25
  tf = "+------------+\n"
24
- a.column_names = %w[a bark]
26
+ a.column_names = %w[a bark]
25
27
  assert_equal("#{tf}| a | bark |\n#{tf}| 1 | 2 |\n"+
26
28
  "| 300 | 4 |\n#{tf}",
27
29
  a.as(:text) { |e| e.layout.alignment = :center })
@@ -9,34 +9,36 @@ end
9
9
  class TestGroupable < Test::Unit::TestCase
10
10
  def test_simple
11
11
  a = [[1,2],[3,4],[5,6],[7,8]].to_table(%w[a b])
12
- a[0].tag(:foo); a[1].tag(:bar);
13
- a[2].tag(:bar); a[3].tag(:foo);
12
+ a[0].tag("grp_foo"); a[1].tag("grp_bar");
13
+ a[2].tag("grp_bar"); a[3].tag("grp_foo");
14
14
 
15
15
 
16
16
  expected = Ruport::Data::Record.new( [ [[1,2],[7,8]].to_table(%w[a b]),
17
17
  [[3,4],[5,6]].to_table(%w[a b]) ],
18
- :attributes => ["foo","bar"] )
19
- assert_equal expected, a.group_by_tag
18
+ :attributes => %w[foo bar] )
19
+ assert_equal expected, a.groups
20
20
 
21
- a[0].tag(:bar)
21
+ a[0].tag("grp_bar")
22
22
 
23
23
  expected = Ruport::Data::Record.new( [ [[1,2],[7,8]].to_table(%w[a b]),
24
24
  [[1,2],[3,4],[5,6]].to_table(%w[a b]) ],
25
25
  :attributes => ["foo","bar"] )
26
- assert_equal expected, a.group_by_tag
26
+ assert_equal expected, a.groups
27
27
  end
28
28
 
29
- def test_create_tag_group
29
+ def test_create_group
30
30
  a = [[1,2,3],[4,5,6],[7,8,9]].to_table(%w[a b c])
31
31
  expected = Ruport::Data::Record.new( [ [[1,2,3],[7,8,9]].to_table(%w[a b c]),
32
32
  [[4,5,6]].to_table(%w[a b c]) ],
33
33
  :attributes => %w[starts_odd starts_even])
34
- a.create_tag_group(:starts_odd) { |r| (r[0] % 2) != 0 }
35
- a.create_tag_group(:starts_even) { |r| (r[0] % 2).zero? }
34
+ a.create_group("starts_odd") { |r| (r[0] % 2) != 0 }
35
+ a.create_group(:starts_even) { |r| (r[0] % 2).zero? }
36
36
 
37
- assert_equal expected, a.group_by_tag
38
- end
37
+ assert_equal expected, a.groups
38
+ assert_equal([[1,2,3],[7,8,9]].to_table(%w[a b c]), a.group("starts_odd"))
39
+ assert_equal([[1,2,3],[7,8,9]].to_table(%w[a b c]), a.group(:starts_odd))
40
+ assert_equal([[4,5,6]].to_table(%w[a b c]), a.group("starts_even"))
41
+
42
+ end
39
43
 
40
44
  end
41
-
42
-
data/test/test_query.rb CHANGED
@@ -77,11 +77,13 @@
77
77
  assert_nothing_raised { query.result }
78
78
  assert_equal @data[1], get_raw(query.result)
79
79
  assert_equal @data[2], get_raw(query.result)
80
+
80
81
  end
81
82
 
82
83
  def test_result_cache_enabled
83
84
  return unless Object.const_defined? :Mocha
84
85
  query = @query[:cached]
86
+
85
87
  setup_mock_dbi(1)
86
88
 
87
89
  assert_nothing_raised { query.result }
@@ -136,8 +138,9 @@
136
138
  return unless Object.const_defined? :Mocha
137
139
  query = @query[:cached]
138
140
  setup_mock_dbi(2)
139
-
141
+
140
142
  assert_equal @data[0], get_raw(query.result)
143
+
141
144
  query.clear_cache
142
145
  assert_equal nil, query.cached_data
143
146
  assert_equal @data[1], get_raw(query.result)
@@ -303,7 +306,7 @@
303
306
  @dbh = mock("database_handle")
304
307
  @sth = mock("statement_handle")
305
308
  def @dbh.execute(*a, &b); execute__(*a, &b); ensure; sth__.finish if b; end
306
- def @sth.each; data__.each { |x| yield(x) }; end
309
+ def @sth.each; data__.each { |x| yield(x.dup) }; end
307
310
  def @sth.fetch_all; data__; end
308
311
 
309
312
  DBI.expects(:connect).
@@ -332,7 +335,7 @@
332
335
  end
333
336
 
334
337
  def get_raw(table)
335
- table.collect { |row| row.to_a }
338
+ table.map { |row| row.to_a }
336
339
  end
337
340
 
338
341
  end
data/test/test_record.rb CHANGED
@@ -22,6 +22,7 @@ class TestRecord < Test::Unit::TestCase
22
22
  assert_equal 4, @record["d"]
23
23
  assert_equal 2, @record.b
24
24
  assert_equal 3, @record.c
25
+ assert_raise(NoMethodError) { @record.f }
25
26
  end
26
27
 
27
28
  def test_hash_constructor
@@ -29,6 +30,7 @@ class TestRecord < Test::Unit::TestCase
29
30
  assert_equal 1, record[:a]
30
31
  assert_equal 2, record[:b]
31
32
  assert_equal 3, record[:c]
33
+ assert_equal 3, record.c
32
34
  end
33
35
 
34
36
  def test_hash_constructor_with_attributes
@@ -36,6 +38,7 @@ class TestRecord < Test::Unit::TestCase
36
38
  assert_equal 1, record[:a]
37
39
  assert_equal 2, record[:b]
38
40
  assert_equal 3, record[:c]
41
+ assert_equal 3, record.c
39
42
 
40
43
  assert_equal 3, record[0]
41
44
  assert_equal 2, record[1]
@@ -52,6 +55,7 @@ class TestRecord < Test::Unit::TestCase
52
55
  assert_equal 2, @record["b"]
53
56
  assert_equal 3, @record["c"]
54
57
  assert_equal 4, @record["d"]
58
+ assert_equal 4, @record.d
55
59
  end
56
60
 
57
61
  def test_bracket_equals
@@ -93,7 +97,15 @@ class TestRecord < Test::Unit::TestCase
93
97
  def test_to_h
94
98
  assert_nothing_raised { @record.to_h }
95
99
  assert_equal({ "a" => 1, "b" => 2, "c" => 3, "d" => 4 }, @record.to_h)
96
- end
100
+ end
101
+
102
+ def test_rename_attribute
103
+ @record.rename_attribute("b","x")
104
+ assert_equal %w[a x c d], @record.attributes
105
+ assert_equal 2, @record["x"]
106
+ assert_equal 2, @record.x
107
+ assert_equal 2, @record[1]
108
+ end
97
109
 
98
110
  def test_equality
99
111
 
@@ -127,29 +139,29 @@ class TestRecord < Test::Unit::TestCase
127
139
  end
128
140
 
129
141
  def test_reordering
130
- r = @record.reorder "a","d","b","c"
131
- assert_equal [1,4,2,3], r.data
142
+ r = @record.dup.reorder "a","d","b","c"
143
+ assert_equal [1,4,2,3], r.to_a
132
144
  assert_equal %w[a d b c], r.attributes
133
145
 
134
- assert_equal [1,2,3,4], @record.data
146
+ assert_equal [1,2,3,4], @record.to_a
135
147
  assert_equal %w[a b c d], @record.attributes
136
148
 
137
- @record.reorder! "a","d","b","c"
138
- assert_equal [1,4,2,3], @record.data
149
+ @record.reorder "a","d","b","c"
150
+ assert_equal [1,4,2,3], @record.to_a
139
151
  assert_equal %w[a d b c], @record.attributes
140
152
 
141
- @record.reorder! 3,1,2
142
- assert_equal [3,4,2], @record.data
153
+ @record.reorder 3,1,2
154
+ assert_equal [3,4,2], @record.to_a
143
155
  assert_equal %w[c d b], @record.attributes
144
156
 
145
- r.reorder! %w[a b c]
146
- assert_equal [1,2,3], r.data
157
+ r.reorder %w[a b c]
158
+ assert_equal [1,2,3], r.to_a
147
159
  assert_equal %w[a b c], r.attributes
148
160
 
149
- assert_raise(ArgumentError) { r.reorder "foo" }
150
- assert_raise(ArgumentError) { r.reorder 0,5 }
151
- assert_nothing_raised { r.reorder 0 }
152
- assert_nothing_raised { r.reorder "a","b" }
161
+ assert_raise(ArgumentError) { r.dup.reorder "foo" }
162
+ assert_raise(ArgumentError) { r.dup.reorder 0,5 }
163
+ assert_nothing_raised { r.dup.reorder 0 }
164
+ assert_nothing_raised { r.dup.reorder "a","b" }
153
165
  end
154
166
 
155
167
  def test_dup
@@ -162,11 +174,11 @@ class TestRecord < Test::Unit::TestCase
162
174
 
163
175
  rec2.tag(:apple)
164
176
 
165
- assert_equal [1,2,3,4], rec1.data
166
- assert_equal [5,7,9,4], rec2.data
177
+ assert_equal [1,2,3,4], rec1.to_a
178
+ assert_equal [5,7,9,4], rec2.to_a
167
179
 
168
- assert_equal [], rec1.tags
169
- assert_equal [:apple], rec2.tags
180
+ assert_equal Set.new([]), rec1.tags
181
+ assert_equal Set.new([:apple]), rec2.tags
170
182
  end
171
183
 
172
184
  def test_records_with_same_attrs_and_data_hash_the_same
@@ -184,23 +196,35 @@ class TestRecord < Test::Unit::TestCase
184
196
  assert r.hash != t.hash
185
197
  end
186
198
 
187
- # ticket:71
188
- def test_record_has_indifferent_access
189
- t = Record.new [1,2,3], :attributes => %w[a b c]
190
- assert_equal [1,2,3], [t[0],t[1],t[2]]
191
- assert_equal [1,2,3], [t["a"],t["b"],t["c"]]
192
- assert_equal [1,2,3], [t.a,t.b,t.c]
199
+ def test_length_and_size
200
+ r = Record.new({:a => 1, :b => 2, :c => 3})
201
+ assert_equal 3,r.length
202
+ assert_equal 3,r.size
203
+ end
193
204
 
194
- assert_equal [1,2,3], [t[:a],t[:b],t[:c]]
195
-
196
- t[:c] = 4
197
- assert_equal [1,2,4], [t["a"],t["b"],t["c"]]
205
+ def test_ensure_records_dup_source_data
206
+ a = [1,2,3]
207
+ b = Record.new(a)
208
+ b[0] += 1
209
+ assert_equal 2, b[0]
210
+ assert_equal 1, a[0]
198
211
 
199
- x = Record.new [1,2,3], :attributes => [:a,:b,:c]
200
- assert_equal [1,2,3], [x[0],x[1],x[2]]
201
- assert_equal [1,2,3], [x[:a],x[:b],x[:c]]
212
+ a = { "a" => 1, "b" => 2, "c" => 3 }
213
+ b = Record.new(a)
214
+ b["a"] += 1
215
+ assert_equal 2, b["a"]
216
+ assert_equal 1, a["a"]
217
+ end
202
218
 
203
- assert_equal [1,2,3], [x["a"],x["b"],x["c"]]
204
- assert_equal [1,2,3], [x.a,x.b,x.c]
219
+ def test_reindex
220
+ assert_equal %w[a b c d], @record.attributes
221
+ old_object_id = @record.instance_variable_get(:@attributes).object_id
222
+
223
+ @record.send(:reindex, %w[apple banana courier django])
224
+ assert_equal %w[apple banana courier django], @record.attributes
225
+
226
+ new_object_id = @record.instance_variable_get(:@attributes).object_id
227
+ assert_equal old_object_id, new_object_id
205
228
  end
229
+
206
230
  end
@@ -2,15 +2,25 @@ require 'test/unit'
2
2
  require 'ruport'
3
3
 
4
4
  class DummyText < Ruport::Format::Plugin
5
+ def prepare_document
6
+ output << "p"
7
+ end
8
+
5
9
  def build_header
6
10
  output << "header\n"
7
11
  end
12
+
8
13
  def build_body
9
14
  output << "body\n"
10
15
  end
16
+
11
17
  def build_footer
12
18
  output << "footer\n"
13
19
  end
20
+
21
+ def finalize_document
22
+ output << "f"
23
+ end
14
24
  end
15
25
 
16
26
 
@@ -27,6 +37,22 @@ class TrivialRenderer < Ruport::Renderer
27
37
 
28
38
  end
29
39
 
40
+ class RendererWithHelpers < Ruport::Renderer
41
+ include Ruport::Renderer::Helpers
42
+
43
+ add_format DummyText, :text
44
+
45
+ prepare :document
46
+
47
+ option :subtitle
48
+
49
+ stage :header
50
+ stage :body
51
+ stage :footer
52
+
53
+ finalize :document
54
+ end
55
+
30
56
 
31
57
  class TestRenderer < Test::Unit::TestCase
32
58
 
@@ -45,6 +71,57 @@ class TestRenderer < Test::Unit::TestCase
45
71
  assert_nil Ruport::Renderer.try_require(:not_a_plugin)
46
72
  end
47
73
 
74
+ def test_stage_helper
75
+ assert RendererWithHelpers.stages.include?('body')
76
+ end
77
+
78
+ def test_finalize_helper
79
+ assert_equal :document, RendererWithHelpers.final_stage
80
+ end
81
+
82
+ def test_prepare_helper
83
+ assert_equal :document, RendererWithHelpers.first_stage
84
+ end
85
+
86
+ def test_finalize_again
87
+ assert_raise(RuntimeError) { RendererWithHelpers.finalize :report }
88
+ end
89
+
90
+ def test_prepare_again
91
+ assert_raise(RuntimeError) { RendererWithHelpers.prepare :foo }
92
+ end
93
+
94
+ def test_renderer_using_helpers
95
+ actual = RendererWithHelpers.render(:text)
96
+ assert_equal "pheader\nbody\nfooter\nf", actual
97
+
98
+ actual = RendererWithHelpers.render_text
99
+ assert_equal "pheader\nbody\nfooter\nf", actual
100
+ end
101
+
102
+ def test_option_helper
103
+ RendererWithHelpers.render_text do |r|
104
+ r.subtitle = "Test Report"
105
+ assert_equal "Test Report", r.options.subtitle
106
+ end
107
+ end
108
+
109
+ def test_required_option_helper
110
+ RendererWithHelpers.required_option :title
111
+
112
+ RendererWithHelpers.render_text do |r|
113
+ r.title = "Test Report"
114
+ assert_equal "Test Report", r.options.title
115
+ end
116
+ end
117
+
118
+ def test_without_required_option
119
+ RendererWithHelpers.required_option :title
120
+
121
+ assert_raise(RuntimeError) { RendererWithHelpers.render(:text) }
122
+ end
123
+
124
+
48
125
  def test_method_missing
49
126
  actual = TrivialRenderer.render_text
50
127
  assert_equal "header\nbody\nfooter\n", actual
data/test/test_report.rb CHANGED
@@ -1,181 +1,188 @@
1
- #tc_report.rb
2
- #
3
- # Created by Gregory Thomas Brown on 2005-08-09
4
- # Copyright 2005 (Gregory Brown) All rights reserved.
5
-
6
- require "test/unit"
7
- require "ruport"
8
- begin; require "rubygems"; rescue LoadError; nil; end
9
-
10
- begin
11
- require 'mocha'
12
- require 'stubba'
13
- require 'net/smtp'
14
- rescue LoadError
15
- $stderr.puts "Warning: Mocha not found -- skipping some Report tests"
16
- end
17
-
18
- class TestReport < Test::Unit::TestCase
19
- include Ruport
20
-
21
- def setup
22
- @report = Report.new
23
- Ruport::Config.source :default, :dsn => "ruport:test",
24
- :user => "foo", :password => "bar"
25
- @query1 = Ruport::Query.new "select * from foo", :cache_enabled => true
26
- @query1.cached_data = [[1,2,3],[4,5,6],[7,8,9]].to_table(%w[a b c])
27
- end
28
-
29
- def test_erb
30
- @report = Report.new
31
- @report.results = "foo"
32
- assert_equal "foo", @report.erb("<%= @results %>")
33
- assert_equal "foo\n4\n---\n", @report.erb("test/samples/foo.rtxt")
34
- end
35
-
36
- def test_textile
37
- @report = Report.new
38
- assert_equal "<p><strong>foo</strong></p>", @report.textile("*foo*")
39
- end
40
-
41
- def test_query
42
- assert_kind_of Ruport::Data::Table,
43
- @report.query("blah",:query_obj => @query1)
44
- expected = [[1,2,3],[4,5,6],[7,8,9]]
45
- @report.query("blah",:query_obj => @query1, :yield_type => :by_row) { |r|
46
- assert_equal expected.shift, r.data
47
- assert_equal %w[a b c], r.attributes
48
- }
49
- assert_equal "a,b,c\n1,2,3\n4,5,6\n7,8,9\n",
50
- @report.query("blah",:query_obj => @query1, :as => :csv)
51
- end
52
-
53
- class MyReport < Report; end
54
-
55
- def test_klass_methods
56
- rep_klass = MyReport.dup
57
- rep_klass.send(:prepare) { self.file = "foo.csv" }
58
- rep_klass.send(:generate) { "hello dolly" }
59
- rep_klass.send(:cleanup) { @foo = "bar" }
60
- report = rep_klass.new
61
- report.run { |rep|
62
- assert_equal("foo.csv",rep.file)
63
- assert_equal("hello dolly",rep.results)
64
- assert_equal(nil,rep.instance_eval("@foo"))
65
- }
66
- assert_equal("bar",report.instance_eval("@foo"))
67
- end
68
-
69
- def test_multi_reports
70
- rep_klass = MyReport.dup
71
-
72
- report1 = rep_klass.new
73
- report2 = rep_klass.new
74
-
75
- report1.file = "foo"
76
- report2.file = "bar"
77
-
78
- rep_klass.send(:generate) { file }
79
-
80
- expected = %w[foo bar]
81
-
82
- rep_klass.run :reports => [report1,report2] do |rep|
83
- assert_equal expected.shift, rep.results
84
- end
85
-
86
- end
87
-
88
-
89
- def test_timeout
90
- rep_klass = MyReport.dup
91
- rep_klass.send(:generate) { raise }
92
-
93
- assert_raises(RuntimeError){
94
- rep_klass.run(:tries => 3, :interval => 1, :log_level => :log_only)
95
- }
96
-
97
- rep_klass.send(:generate) { sleep 1.1 }
98
-
99
- assert_raises(Timeout::Error) {
100
- rep_klass.run( :tries => 2,
101
- :timeout => 1,
102
- :interval => 1,
103
- :log_level => :log_only)
104
- }
105
-
106
- end
107
-
108
- def test_return_value
109
- rep_klass = MyReport.dup
110
- rep_klass.send(:generate) { "hello dolly" }
111
-
112
- # single report
113
- assert_equal "hello dolly", rep_klass.run
114
-
115
- # multiple reports
116
- assert_equal ["hello dolly", "hello dolly"],
117
- rep_klass.run(:reports => [rep_klass.new,rep_klass.new])
118
- end
119
-
120
- def test_send_to
121
- return unless Object.const_defined? :Mocha
122
- setup_mock_mailer
123
- @report = Report.new
124
- assert_equal "250 ok", @report.send_to("clyde@example.com") { |mail|
125
- mail.subject = "Test Report"
126
- mail.text = "Test"
127
- }
128
- end
129
-
130
- def test_write_to_file
131
- return unless Object.const_defined? :Mocha
132
- setup_mock_file
133
- @report = Report.new
134
- assert @report.write("foo.csv")
135
- end
136
-
137
- def test_append_to_file
138
- return unless Object.const_defined? :Mocha
139
- setup_mock_file
140
- @report = Report.new
141
- assert @report.append("foo.csv")
142
- end
143
-
144
- def test_load_csv
145
- expected = [%w[a b c],['d', nil, 'e']].to_table(%w[col1 col2 col3])
146
-
147
- @report = Report.new
148
- table = @report.load_csv("test/samples/data.csv")
149
-
150
- assert_equal expected, table
151
- end
152
-
153
- def test_load_csv_as_array
154
- expected = [%w[a b c],['d', nil, 'e']]
155
-
156
- @report = Report.new
157
- array = @report.load_csv("test/samples/data.csv", :as => :array)
158
-
159
- assert_equal expected, array
160
- end
161
-
162
- private
163
-
164
- def setup_mock_mailer
165
- @smtp = mock('smtp')
166
-
167
- Net::SMTP.expects(:start).
168
- yields(@smtp).
169
- returns("250 ok").at_least_once
170
- @smtp.stubs(:send_message).
171
- returns("250 ok")
172
- end
173
-
174
- def setup_mock_file
175
- @file = mock("file")
176
-
177
- File.expects(:open).yields(@file).returns(@file).at_least_once
178
- @file.expects(:<<).returns(@file).at_least_once
179
- end
180
-
181
- end
1
+ #tc_report.rb
2
+ #
3
+ # Created by Gregory Thomas Brown on 2005-08-09
4
+ # Copyright 2005 (Gregory Brown) All rights reserved.
5
+
6
+ require "test/unit"
7
+ require "ruport"
8
+ begin; require "rubygems"; rescue LoadError; nil; end
9
+
10
+ begin
11
+ require 'mocha'
12
+ require 'stubba'
13
+ require 'net/smtp'
14
+ rescue LoadError
15
+ $stderr.puts "Warning: Mocha not found -- skipping some Report tests"
16
+ end
17
+
18
+ class TestReport < Test::Unit::TestCase
19
+ include Ruport
20
+
21
+ def setup
22
+ @report = Report.new
23
+ Ruport::Config.source :default, :dsn => "ruport:test",
24
+ :user => "foo", :password => "bar"
25
+ @query1 = Ruport::Query.new "select * from foo", :cache_enabled => true
26
+ @query1.cached_data = [[1,2,3],[4,5,6],[7,8,9]].to_table(%w[a b c])
27
+ end
28
+
29
+ def test_erb
30
+ @report = Report.new
31
+ @report.results = "foo"
32
+ assert_equal "foo", @report.erb("<%= @results %>")
33
+ assert_equal "foo\n4\n---\n", @report.erb("test/samples/foo.rtxt")
34
+ end
35
+
36
+ def test_textile
37
+ @report = Report.new
38
+ assert_equal "<p><strong>foo</strong></p>", @report.textile("*foo*")
39
+ end
40
+
41
+ def test_query
42
+ assert_kind_of Ruport::Data::Table,
43
+ @report.query("blah",:query_obj => @query1)
44
+ expected = [[1,2,3],[4,5,6],[7,8,9]]
45
+ @report.query("blah",:query_obj => @query1, :yield_type => :by_row) { |r|
46
+ assert_equal expected.shift, r.to_a
47
+ assert_equal %w[a b c], r.attributes
48
+ }
49
+ assert_equal "a,b,c\n1,2,3\n4,5,6\n7,8,9\n",
50
+ @report.query("blah",:query_obj => @query1, :as => :csv)
51
+ end
52
+
53
+ class MyReport < Report; end
54
+
55
+ def test_klass_methods
56
+ rep_klass = MyReport.dup
57
+ rep_klass.send(:prepare) { self.file = "foo.csv" }
58
+ rep_klass.send(:generate) { "hello dolly" }
59
+ rep_klass.send(:cleanup) { @foo = "bar" }
60
+ report = rep_klass.new
61
+ report.run { |rep|
62
+ assert_equal("foo.csv",rep.file)
63
+ assert_equal("hello dolly",rep.results)
64
+ assert_equal(nil,rep.instance_eval("@foo"))
65
+ }
66
+ assert_equal("bar",report.instance_eval("@foo"))
67
+ end
68
+
69
+ def test_multi_reports
70
+ rep_klass = MyReport.dup
71
+
72
+ report1 = rep_klass.new
73
+ report2 = rep_klass.new
74
+
75
+ report1.file = "foo"
76
+ report2.file = "bar"
77
+
78
+ rep_klass.send(:generate) { file }
79
+
80
+ expected = %w[foo bar]
81
+
82
+ rep_klass.run :reports => [report1,report2] do |rep|
83
+ assert_equal expected.shift, rep.results
84
+ end
85
+
86
+ end
87
+
88
+
89
+ def test_timeout
90
+ rep_klass = MyReport.dup
91
+ rep_klass.send(:generate) { raise }
92
+
93
+ assert_raises(RuntimeError){
94
+ rep_klass.run(:tries => 3, :interval => 1, :log_level => :log_only)
95
+ }
96
+
97
+ rep_klass.send(:generate) { sleep 1.1 }
98
+
99
+ assert_raises(Timeout::Error) {
100
+ rep_klass.run( :tries => 2,
101
+ :timeout => 1,
102
+ :interval => 1,
103
+ :log_level => :log_only)
104
+ }
105
+
106
+ end
107
+
108
+ def test_return_value
109
+ rep_klass = MyReport.dup
110
+ rep_klass.send(:generate) { "hello dolly" }
111
+
112
+ # single report
113
+ assert_equal "hello dolly", rep_klass.run
114
+
115
+ # multiple reports
116
+ assert_equal ["hello dolly", "hello dolly"],
117
+ rep_klass.run(:reports => [rep_klass.new,rep_klass.new])
118
+ end
119
+
120
+ def test_send_to
121
+ return unless Object.const_defined? :Mocha
122
+ setup_mock_mailer
123
+ @report = Report.new
124
+ assert_equal "250 ok", @report.send_to("clyde@example.com") { |mail|
125
+ mail.subject = "Test Report"
126
+ mail.text = "Test"
127
+ }
128
+ end
129
+
130
+ def test_write_to_file
131
+ return unless Object.const_defined? :Mocha
132
+ file = mock("file")
133
+
134
+ File.expects(:open).
135
+ with("foo.csv","w").yields(file).returns(file).at_least_once
136
+
137
+ file.expects(:<<).
138
+ with("results").returns(file).at_least_once
139
+
140
+ @report = Report.new
141
+ assert @report.write("foo.csv", "results")
142
+ end
143
+
144
+ def test_append_to_file
145
+ return unless Object.const_defined? :Mocha
146
+ file = mock("file")
147
+
148
+ File.expects(:open).
149
+ with("foo.csv","a").yields(file).returns(file).at_least_once
150
+
151
+ file.expects(:<<).
152
+ with("results").returns(file).at_least_once
153
+
154
+ @report = Report.new
155
+ assert @report.append("foo.csv", "results")
156
+ end
157
+
158
+ def test_load_csv
159
+ expected = [%w[a b c],['d', nil, 'e']].to_table(%w[col1 col2 col3])
160
+
161
+ @report = Report.new
162
+ table = @report.load_csv("test/samples/data.csv")
163
+
164
+ assert_equal expected, table
165
+ end
166
+
167
+ def test_load_csv_as_array
168
+ expected = [%w[a b c],['d', nil, 'e']]
169
+
170
+ @report = Report.new
171
+ array = @report.load_csv("test/samples/data.csv", :as => :array)
172
+
173
+ assert_equal expected, array
174
+ end
175
+
176
+ private
177
+
178
+ def setup_mock_mailer
179
+ @smtp = mock('smtp')
180
+
181
+ Net::SMTP.expects(:start).
182
+ yields(@smtp).
183
+ returns("250 ok").at_least_once
184
+ @smtp.stubs(:send_message).
185
+ returns("250 ok")
186
+ end
187
+
188
+ end