ruport-util 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
1
+ require 'test/helper'
2
+
3
+ testcase_requires 'hpricot'
4
+
5
+ describe 'OSD Formatter' do
6
+ before :all do
7
+ @csv = "first col,second col,third col\n" +
8
+ "first row,cell 1,cell 2\n" +
9
+ "second row,cell 3,cell 4\n" +
10
+ "third row,special >,special <\n" +
11
+ "fourth row,,after empty\n" +
12
+ "\n" +
13
+ "seventh row,nothing,more\n"
14
+ @table = Table(:string => @csv)
15
+ zipfile = @table.to_ods(:tempfile => true)
16
+ zip = Zip::ZipFile.open(zipfile.path)
17
+ @content = zip.read('content.xml')
18
+ zip.close
19
+ @doc = Hpricot(@content)
20
+ @rows = (@doc/'table:table-row')
21
+ end
22
+
23
+ it 'should have content' do
24
+ @content.should_not be_nil
25
+ end
26
+
27
+ it 'should have all rows' do
28
+ @rows.should have(7).rows
29
+ end
30
+
31
+ it 'should have the header' do
32
+ headers = (@rows.first/'text:p').map{|t| t.inner_html}
33
+ headers.should == @table.column_names
34
+ end
35
+
36
+ it 'should have the two rows with data' do
37
+ @rows[1..2].zip(@table.data).each do |h_row, r_row|
38
+ (h_row/'text:p').map{ |t| t.inner_html }.should == r_row.to_a
39
+ end
40
+ end
41
+
42
+ it 'should escape HTML entities' do
43
+ (@rows[3]/'text:p').map{|e| e.inner_html}.
44
+ should == %w[third\ row special\ &gt; special\ &lt;]
45
+ end
46
+ end
@@ -1,5 +1,6 @@
1
- require "test/init"
2
- require "ruport/util/graph"
1
+ require 'test/helper'
2
+
3
+ quiet { testcase_requires 'scruffy' }
3
4
 
4
5
  class MockGraphPlugin < Ruport::Formatter
5
6
  renders :mock, :for => Ruport::Renderer::Graph
@@ -14,36 +15,27 @@ class MockGraphPlugin < Ruport::Formatter
14
15
  end
15
16
  end
16
17
 
18
+ describe 'Graph Renderer' do
19
+ before :all do
20
+ @graph = Ruport::Renderer::Graph
21
+ @data = Ruport::Graph(:data => [[1,2,3],[4,5,6]])
22
+ end
17
23
 
18
- class TestGraphRenderer < Test::Unit::TestCase
19
-
20
- def test_basics
21
- out = Ruport::Renderer::Graph.render_mock do |r|
24
+ it 'should render' do
25
+ out = @graph.render_mock do |r|
22
26
  r.options do |l|
23
- assert_equal :line, l.style
27
+ l.style.should == :line
24
28
  end
25
29
  end
26
30
 
27
- assert_equal("preparebuildfinalize",out)
31
+ out.should == 'preparebuildfinalize'
28
32
  end
29
33
 
30
- end
31
-
32
-
33
- class TestSVGPlugin < Test::Unit::TestCase
34
-
35
- def test_output
36
- assert_not_nil Ruport::Renderer::Graph.render_svg { |r|
37
- r.data = Ruport::Graph(:data => [[1,2,3],[4,5,6]])
38
- }
34
+ it 'should render SVG' do
35
+ @graph.render_svg{|r| r.data = @data}.should_not be_nil
39
36
  end
40
37
 
41
- end
42
-
43
- class TestXMLSWFPlugin < Test::Unit::TestCase
44
-
45
- def test_output
46
-
38
+ it 'should render XMLSWF via block' do
47
39
  expected = <<-EOS
48
40
  <chart>
49
41
  <chart_type>line</chart_type>
@@ -66,11 +58,11 @@ class TestXMLSWFPlugin < Test::Unit::TestCase
66
58
  </chart_data>
67
59
  </chart>
68
60
  EOS
69
- assert_equal expected,
70
- Ruport::Renderer::Graph.render_xml_swf { |r|
71
- r.data = Ruport::Graph(:data => [[1,2,3],[4,5,6]])
72
- }
73
-
61
+
62
+ @graph.render_xml_swf{|r| r.data = @data}.should == expected
63
+ end
64
+
65
+ it 'should render XMLSWF via add_line' do
74
66
  expected = <<-EOS
75
67
  <chart>
76
68
  <chart_type>line</chart_type>
@@ -98,8 +90,6 @@ EOS
98
90
  graph.add_line [1,2,3], :name => "Alpha"
99
91
  graph.add_line [4,5,6], :name => "Beta"
100
92
 
101
- assert_equal expected, graph.to_xml_swf
102
-
93
+ graph.to_xml_swf.should == expected
103
94
  end
104
-
105
95
  end
data/test/test_invoice.rb CHANGED
@@ -1,31 +1,33 @@
1
- require "test/init"
2
- require "ruport/util"
1
+ require 'test/helper'
3
2
 
4
- class TestInvoice < Test::Unit::TestCase
3
+ describe 'Invoice' do
4
+ before :all do
5
+ @invoice = Ruport::Renderer::Invoice
6
+ @data = Table(%w[a b c]) << [1,2,3]
7
+ end
8
+
9
+ it 'should raise if the required options are not set' do
10
+ lambda{ @invoice.render_pdf }.
11
+ should raise_error(Ruport::Renderer::RequiredOptionNotSet)
12
+ end
13
+
14
+ it "shouldn't raise if options are given correct in hash form" do
15
+ lambda do
16
+ @invoice.render_pdf :data => @data,
17
+ :customer_info => 'blah', :company_info => 'also blah',
18
+ :order_info => 'Incredibly Blah', :comments => 'Super Blah'
19
+ end.should_not raise_error
20
+ end
5
21
 
6
- def test_required_options
7
- assert_raises(Ruport::Renderer::RequiredOptionNotSet) {
8
- Ruport::Renderer::Invoice.render_pdf
9
- }
10
-
11
- #hash form
12
- assert_nothing_raised {
13
- Ruport::Renderer::Invoice.render_pdf :data => Table(%w[a b c]) << [1,2,3],
14
- :customer_info => "blah", :company_info => "also blah",
15
- :order_info => "Incredibly Blah", :comments => "Super Blah"
16
- }
17
-
18
- #block form
19
- assert_nothing_raised {
20
- Ruport::Renderer::Invoice.render_pdf do |r|
21
- r.data = Table(%w[a b c]) << [1,2,3]
22
- r.customer_info = "blah"
23
- r.company_info = "also blah"
24
- r.order_info = "Incredibly Blah"
25
- r.comments = "Super blah"
26
- end
27
- }
22
+ it "shouldn't raise if options are given correct in block form" do
23
+ lambda do
24
+ @invoice.render_pdf do |r|
25
+ r.customer_info = 'blah'
26
+ r.company_info = 'also blah'
27
+ r.order_info = 'Incredibly Blah'
28
+ r.comments = 'Super Blah'
29
+ r.data = @data
30
+ end
31
+ end.should_not raise_error
28
32
  end
29
-
30
-
31
- end
33
+ end
data/test/test_mailer.rb CHANGED
@@ -1,206 +1,163 @@
1
- require "test/unit"
2
- begin; require "rubygems"; rescue LoadError; nil end
3
- require "ruport"
4
- require "ruport/util/mailer"
5
- begin
6
- require 'mocha'
7
- require 'stubba'
8
- require 'net/smtp'
9
- rescue LoadError
10
- $stderr.puts "Warning: Mocha not found -- skipping some Mailer tests"
11
- end
12
-
13
- class TestMailer < Test::Unit::TestCase
1
+ require 'test/helper'
2
+ require 'net/smtp'
14
3
 
15
- def setup
4
+ describe 'Mailer' do
5
+ before :all do
16
6
  @default_opts = {
17
- :host => "mail.example.com",
18
- :address => "sue@example.com",
19
- :user => "inky",
7
+ :user => "inky",
8
+ :host => "mail.example.com",
9
+ :address => "sue@example.com",
20
10
  :password => "chunky"
21
11
  }
22
12
 
23
13
  @other_opts = {
24
- :host => "moremail.example.com",
25
- :address => "clyde@example.com",
26
- :user => "blinky",
14
+ :user => "blinky",
15
+ :host => "moremail.example.com",
16
+ :address => "clyde@example.com",
27
17
  :password => "bacon"
28
18
  }
29
19
 
30
- Ruport::Mailer.add_mailer :default, @default_opts
31
- Ruport::Mailer.add_mailer :other, @other_opts
20
+ @mailer = Ruport::Mailer
32
21
 
33
- @default_mailer = Ruport::Mailer.new
34
- @other_mailer = Ruport::Mailer.new :other
35
- end
36
-
37
- def assert_mailer_equal(expected, mailer)
38
- assert_equal expected[:host], mailer.instance_variable_get(:@host)
39
- assert_equal expected[:address], mailer.instance_variable_get(:@address)
40
- assert_equal expected[:user], mailer.instance_variable_get(:@user)
41
- assert_equal expected[:password], mailer.instance_variable_get(:@password)
42
- end
22
+ @mailer.add_mailer :default, @default_opts
23
+ @mailer.add_mailer :other, @other_opts
43
24
 
44
- def test_send_to
45
- return unless Object.const_defined? :Mocha
46
- setup_mock_report_mailer
47
- @report = Ruport::Report.new
48
- assert_equal "250 ok", @report.send_to("clyde@example.com") { |mail|
49
- mail.subject = "Test Report"
50
- mail.text = "Test"
51
- }
52
- end
25
+ @default_mailer = @mailer.new :default
26
+ @other_mailer = @mailer.new :other
53
27
 
54
- def test_mail_defaults
55
- assert_equal( Ruport::Mailer.mailers[:default],
56
- Ruport::Mailer.default_mailer)
28
+ @mail_fields = { :to => 'clyde@example.com',
29
+ :from => 'sue@example.com',
30
+ :subject => 'Hello',
31
+ :text => 'This is a test' }
57
32
  end
58
33
 
34
+ def values_for(mailer, *keys)
35
+ keys = keys.flatten.map{|k| k.to_s}
36
+ keys.map{|k| mailer.instance_variable_get("@#{k}") }
37
+ end
59
38
 
60
-
61
- def test_default_constructor
62
- assert_mailer_equal @default_opts, @default_mailer
39
+ def check(mailer, expected)
40
+ keys = [:host, :address, :user, :password]
41
+ values = values_for(mailer, *keys)
42
+ values.should == expected.values_at(*keys)
63
43
  end
64
-
65
- def test_constructor_with_mailer_label
66
- assert_mailer_equal @other_opts, @other_mailer
44
+
45
+ def mock_report_mailer
46
+ @smtp = mock('SMTP')
47
+ @smtp.stub!(:send_message).and_return('250 ok')
48
+ Net::SMTP.should_receive(:start).and_return("250 ok")
67
49
  end
68
50
 
69
- def test_constructor_without_mailer
70
- Ruport::Mailer.mailers[:default] = nil
71
- assert_raise(RuntimeError) { Ruport::Mailer.new }
51
+ def mock_mailer(count, mailer = @default_mailer)
52
+ values = values_for(mailer, :host, :port, :host, :user, :password, :auth)
53
+
54
+ @smtp = mock('SMTP')
55
+
56
+ Net::SMTP.should_receive(:start).
57
+ with(*values).
58
+ exactly(count).times.
59
+ and_yield(@smtp)
60
+ @smtp.should_receive(:send_message).
61
+ with( an_instance_of(String),
62
+ an_instance_of(String),
63
+ an_instance_of(String) ).any_number_of_times.
64
+ and_return('250 ok')
65
+ @smtp.should_receive(:send_message).
66
+ with(an_instance_of(String),an_instance_of(String), nil).any_number_of_times.
67
+ and_raise(Net::SMTPSyntaxError)
72
68
  end
73
-
74
- def test_select_mailer
75
- mailer = Ruport::Mailer.new :default
76
- assert_mailer_equal @default_opts, mailer
77
69
 
78
- mailer.send(:select_mailer, :other)
79
- assert_mailer_equal @other_opts, mailer
70
+ it 'should have default mailer' do
71
+ @mailer.mailers[:default].should == @mailer.default_mailer
80
72
  end
81
73
 
82
- def test_mailer_config_errors
74
+ it 'should have equal options inside and outside of default mailer' do
75
+ check @default_mailer, @default_opts
76
+ end
83
77
 
84
- assert_raise(Ruport::Mailer::InvalidMailerConfigurationError) {
85
- Ruport::Mailer.add_mailer :bar, :user => :foo, :address => "foo@bar.com"
86
- }
87
- assert_nothing_raised { Ruport::Mailer.add_mailer :bar, :host => "localhost" }
78
+ it 'should have equal options inside and outside of other mailer' do
79
+ check @other_mailer, @other_opts
88
80
  end
89
81
 
82
+ it 'should raise if no default mailer is set' do
83
+ default = @mailer.mailers.delete :default
90
84
 
85
+ lambda{ @mailer.new }.
86
+ should raise_error(RuntimeError, 'you need to specify a mailer to use')
91
87
 
92
- def test_to
93
- @default_mailer.instance_eval "@mail.to = ['foo@bar.com']"
94
- assert_equal ['foo@bar.com'], @default_mailer.to
88
+ @mailer.mailers[:default] = default
95
89
  end
96
90
 
97
- def test_to_equals
98
- @default_mailer.to = ['foo@bar.com']
99
- assert_equal ['foo@bar.com'], @default_mailer.to
100
- end
91
+ it 'should raise if configuration is invalid' do
92
+ lambda{ @mailer.add_mailer :bar, :user => :foo, :address => 'foo@bar.com' }.
93
+ should raise_error(Ruport::Mailer::InvalidMailerConfigurationError)
101
94
 
102
- def test_from
103
- @default_mailer.instance_eval "@mail.from = ['foo@bar.com']"
104
- assert_equal ['foo@bar.com'], @default_mailer.from
95
+ lambda{ @mailer.add_mailer :bar, :host => 'localhost' }.
96
+ should_not raise_error
105
97
  end
106
98
 
107
- def test_from_equals
108
- @default_mailer.from = ['foo@bar.com']
109
- assert_equal ['foo@bar.com'], @default_mailer.from
99
+ it 'should send emails with default' do
100
+ mock_mailer 1
101
+ @default_mailer.deliver(@mail_fields).should == '250 ok'
110
102
  end
111
103
 
112
- def test_subject
113
- @default_mailer.instance_eval "@mail.subject = ['RuportDay!']"
114
- assert_equal ['RuportDay!'], @default_mailer.subject
104
+ it 'should send emails with other default' do
105
+ mock_mailer 1, @other_mailer
106
+ @other_mailer.deliver(@mail_fields).should == '250 ok'
115
107
  end
116
108
 
117
- def test_subject_equals
118
- @default_mailer.subject = ['RuportDay!']
119
- assert_equal ['RuportDay!'], @default_mailer.subject
109
+ it 'should send mail without to field' do
110
+ mock_mailer 1
111
+ hash = @mail_fields.dup
112
+ hash.delete :to
113
+ lambda{ @default_mailer.deliver(hash) }.
114
+ should raise_error(Net::SMTPSyntaxError)
120
115
  end
121
116
 
122
- def test_send_mail_with_default
123
- return unless Object.const_defined? :Mocha
124
- setup_mock_mailer(1)
125
- assert_equal "250 ok",
126
- @default_mailer.deliver(:to => "clyde@example.com",
127
- :from => "sue@example.com",
128
- :subject => "Hello",
129
- :text => "This is a test.")
117
+ it 'should send mail with HTML' do
118
+ mock_mailer 1
119
+ hash = @mail_fields.dup
120
+ hash.delete(:text)
121
+ hash[:html] = '<p>This is a test.</p>'
122
+ @default_mailer.deliver(hash).should == '250 ok'
130
123
  end
131
124
 
132
- def test_send_mail_with_other
133
- return unless Object.const_defined? :Mocha
134
- setup_mock_mailer(1, @other_mailer)
135
- assert_equal "250 ok",
136
- @other_mailer.deliver(:to => "sue@example.com",
137
- :from => "clyde@example.com",
138
- :subject => "Hello",
139
- :text => "This is a test.")
125
+ it 'should send mail with attachment' do
126
+ mock_mailer 1
127
+ @default_mailer.attach 'test/samples/data.csv'
128
+ @default_mailer.deliver(@mail_fields).should == '250 ok'
140
129
  end
141
130
 
142
- def test_send_mail_without_to
143
- return unless Object.const_defined? :Mocha
144
- setup_mock_mailer(1)
145
- assert_raise(Net::SMTPSyntaxError) {
146
- @default_mailer.deliver(:from => "sue@example.com",
147
- :subject => "Hello",
148
- :text => "This is a test.")
149
- }
131
+ it 'should have information about the mail' do
132
+ dfm = @default_mailer
133
+
134
+ { :to => ['foo@bar.com'],
135
+ :from => ['foo@bar.com'],
136
+ :subject => ['RuportDay!']
137
+ }.each do |meth, value|
138
+ assign_meth = "#{meth}="
139
+
140
+ dfm.instance_eval{ @mail.send(assign_meth, value) }
141
+ dfm.send(meth).should == value
142
+
143
+ dfm.send(assign_meth, value)
144
+ dfm.send(meth).should == value
145
+ end
150
146
  end
151
147
 
152
- def test_send_html_mail
153
- return unless Object.const_defined? :Mocha
154
- setup_mock_mailer(1)
155
- assert_equal "250 ok",
156
- @default_mailer.deliver(:to => "clyde@example.com",
157
- :from => "sue@example.com",
158
- :subject => "Hello",
159
- :html => "<p>This is a test.</p>")
148
+ it 'should be able to select a mailer' do
149
+ mailer = @mailer.new(:default)
150
+ check mailer, @default_opts
151
+ mailer.send(:select_mailer, :other)
152
+ check mailer, @other_opts
160
153
  end
161
154
 
162
- def test_send_mail_with_attachment
163
- return unless Object.const_defined? :Mocha
164
- setup_mock_mailer(1)
165
- @default_mailer.attach 'test/samples/data.csv'
166
- assert_equal "250 ok",
167
- @default_mailer.deliver(:to => "clyde@example.com",
168
- :from => "sue@example.com",
169
- :subject => "Hello",
170
- :text => "This is a test.")
171
- end
172
-
173
- private
174
-
175
- def setup_mock_mailer(count, mailer=@default_mailer)
176
- host = mailer.instance_variable_get(:@host)
177
- port = mailer.instance_variable_get(:@port)
178
- user = mailer.instance_variable_get(:@user)
179
- password = mailer.instance_variable_get(:@password)
180
- auth = mailer.instance_variable_get(:@auth)
181
-
182
- @smtp = mock('smtp')
183
-
184
- Net::SMTP.expects(:start).
185
- with(host,port,host,user,password,auth).
186
- yields(@smtp).
187
- returns("250 ok").times(count)
188
- @smtp.stubs(:send_message).
189
- with {|*params| !params[2].nil? }.
190
- returns("250 ok")
191
- @smtp.stubs(:send_message).
192
- with {|*params| params[2].nil? }.
193
- raises(Net::SMTPSyntaxError)
194
- end
195
-
196
- def setup_mock_report_mailer
197
- @smtp = mock('smtp')
198
-
199
- Net::SMTP.expects(:start).
200
- yields(@smtp).
201
- returns("250 ok").at_least_once
202
- @smtp.stubs(:send_message).
203
- returns("250 ok")
204
- end
155
+ it 'should send reports with default mailer' do
156
+ report = Ruport::Report.new
157
+ mock_report_mailer
158
+ report.send_to(@other_opts[:address]){|mail|
159
+ mail.subject = 'Test Report'
160
+ mail.text = 'Test'
161
+ }.should == '250 ok'
162
+ end
205
163
  end
206
-