ruport 1.7.1 → 1.8.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.
Files changed (43) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +38 -0
  3. data/HACKING +1 -17
  4. data/{README.rdoc → README.md} +30 -38
  5. data/Rakefile +0 -10
  6. data/examples/row_renderer.rb +1 -1
  7. data/examples/simple_pdf_lines.rb +1 -1
  8. data/examples/trac_ticket_status.rb +1 -1
  9. data/lib/ruport/controller.rb +1 -1
  10. data/lib/ruport/data/grouping.rb +7 -7
  11. data/lib/ruport/data/record.rb +4 -4
  12. data/lib/ruport/data/table.rb +9 -9
  13. data/lib/ruport/formatter/csv.rb +1 -1
  14. data/lib/ruport/formatter/markdown.rb +105 -0
  15. data/lib/ruport/formatter/prawn_pdf.rb +96 -9
  16. data/lib/ruport/formatter/text.rb +1 -1
  17. data/lib/ruport/formatter.rb +1 -2
  18. data/lib/ruport/version.rb +1 -1
  19. data/lib/ruport.rb +7 -11
  20. data/test/controller_test.rb +107 -109
  21. data/test/csv_formatter_test.rb +21 -21
  22. data/test/data_feeder_test.rb +39 -39
  23. data/test/expected_outputs/prawn_pdf_formatter/pdf_basic.pdf.test +265 -0
  24. data/test/grouping_test.rb +74 -74
  25. data/test/helpers.rb +16 -5
  26. data/test/html_formatter_test.rb +22 -22
  27. data/test/markdown_formatter_test.rb +142 -0
  28. data/test/prawn_pdf_formatter_test.rb +108 -0
  29. data/test/record_test.rb +82 -82
  30. data/test/table_pivot_test.rb +9 -2
  31. data/test/table_test.rb +33 -40
  32. data/test/template_test.rb +12 -12
  33. data/test/text_formatter_test.rb +34 -34
  34. data/util/bench/data/table/bench_column_manip.rb +0 -1
  35. data/util/bench/data/table/bench_dup.rb +0 -1
  36. data/util/bench/data/table/bench_init.rb +0 -1
  37. data/util/bench/data/table/bench_manip.rb +0 -1
  38. data/util/bench/formatter/bench_csv.rb +0 -1
  39. data/util/bench/formatter/bench_html.rb +0 -1
  40. data/util/bench/formatter/bench_pdf.rb +0 -1
  41. data/util/bench/formatter/bench_text.rb +0 -1
  42. metadata +30 -29
  43. data/lib/ruport/formatter/pdf.rb +0 -589
@@ -0,0 +1,108 @@
1
+ #!/usr/bin/env ruby -w
2
+
3
+ require_relative 'helpers'
4
+
5
+ class TestRenderPDFTable < Minitest::Test
6
+
7
+ def setup
8
+ Ruport::Formatter::Template.create(:simple) do |format|
9
+ format.page = {
10
+ size: "LETTER",
11
+ layout: :landscape
12
+ }
13
+ format.text = {
14
+ font_size: 16
15
+ }
16
+ format.table = {
17
+ show_headings: false
18
+ }
19
+ format.column = {
20
+ alignment: :center,
21
+ width: 50
22
+ }
23
+ format.heading = {
24
+ alignment: :right,
25
+ bold: false,
26
+ title: "Test"
27
+ }
28
+ format.grouping = {
29
+ style: :separated
30
+ }
31
+ end
32
+ end
33
+
34
+ def test_render_pdf_errors
35
+ # can't render without column names
36
+ data = Ruport.Table([], data: [[1,2],[3,4]])
37
+ assert_raises(Ruport::FormatterError) do
38
+ data.to_prawn_pdf
39
+ end
40
+
41
+ data.column_names = %w[a b]
42
+
43
+ data.to_prawn_pdf
44
+ end
45
+
46
+ def test_render_pdf_basic
47
+ expected_output = IO.binread(File.join(__dir__, 'expected_outputs/prawn_pdf_formatter/pdf_basic.pdf.test')).bytes
48
+
49
+ data = Ruport.Table(%w[a b c], data: [[1,2,3]])
50
+ # debugging:
51
+ # data.to_prawn_pdf(:file => File.join(__dir__, 'expected_outputs/prawn_pdf_formatter/pdf_actual.pdf.test'))
52
+ actual_output = data.to_prawn_pdf.bytes
53
+
54
+ assert_equal expected_output, actual_output
55
+ end
56
+
57
+ # this is mostly to check that the transaction hack gets called
58
+ def test_relatively_large_pdf
59
+ table = Ruport.Table(File.join(File.expand_path(File.dirname(__FILE__)), "../test/",
60
+ %w[samples dates.csv]))
61
+ table.reduce(0..99)
62
+ table.to_prawn_pdf
63
+ end
64
+
65
+ #--------BUG TRAPS--------#
66
+
67
+ # PDF::SimpleTable does not handle symbols as column names
68
+ # Ruport should smartly fix this surprising behaviour (#283)
69
+ def test_tables_should_render_with_symbol_column_name
70
+ data = Ruport.Table([:a,:b,:c], data: [[1,2,3],[4,5,6]])
71
+ data.to_prawn_pdf
72
+ end
73
+
74
+ end
75
+
76
+ class TestRenderPDFGrouping < Minitest::Test
77
+
78
+ #--------BUG TRAPS----------#
79
+
80
+ def test_grouping_should_have_consistent_font_size
81
+ a = Ruport.Table(%w[a b c]) << %w[eye like chicken] << %w[eye like liver] <<
82
+ %w[meow mix meow ] << %w[mix please deliver ]
83
+ b = Grouping(a, by: "a")
84
+ splat = b.to_prawn_pdf.split("\n")
85
+ splat.grep(/meow/).each do |m|
86
+ assert_equal '10.0', m.split[5]
87
+ end
88
+ splat.grep(/mix/).each do |m|
89
+ assert_equal '10.0', m.split[5]
90
+ end
91
+ splat.grep(/eye/).each do |m|
92
+ assert_equal '10.0', m.split[5]
93
+ end
94
+ end
95
+
96
+ end
97
+
98
+ class TestPDFFormatterHelpers < Minitest::Test
99
+
100
+ def test_move_up
101
+ a = Ruport::Formatter::PrawnPDF.new
102
+ a.move_cursor_to(500)
103
+ a.move_up(50)
104
+ assert_equal(550,a.cursor)
105
+ a.move_down(100)
106
+ assert_equal(450,a.cursor)
107
+ end
108
+ end
data/test/record_test.rb CHANGED
@@ -1,75 +1,75 @@
1
- #!/usr/bin/env ruby -w
1
+ #!/usr/bin/env ruby -w
2
2
  require File.join(File.expand_path(File.dirname(__FILE__)), "helpers")
3
3
 
4
4
  class TestRecord < Minitest::Test
5
5
 
6
6
  include Ruport::Data
7
-
7
+
8
8
  def setup
9
9
  @attributes = %w[a b c d]
10
- @record = Ruport::Data::Record.new [1,2,3,4], :attributes => @attributes
11
- end
12
-
13
- describe "when initializing with an array with attributes" do
14
- def specify_key_access_should_work
10
+ @record = Ruport::Data::Record.new [1,2,3,4], :attributes => @attributes
11
+ end
12
+
13
+ context "when initializing with an array with attributes" do
14
+ should "test_specify_key_access_should_work" do
15
15
  assert_equal 1, @record["a"]
16
16
  assert_equal 4, @record["d"]
17
17
  assert_equal 2, @record.b
18
18
  assert_equal 3, @record.c
19
- assert_raises(NoMethodError) { @record.f }
20
- end
21
-
22
- def specify_ordinal_access_should_work
19
+ assert_raises(NoMethodError) { @record.f }
20
+ end
21
+
22
+ should "test_specify_ordinal_access_should_work" do
23
23
  assert_equal 1, @record[0]
24
24
  assert_equal 2, @record[1]
25
25
  assert_equal 3, @record[2]
26
26
  assert_equal 4, @record[3]
27
27
  end
28
- end
29
-
30
- describe "when initializing with an array without attributes" do
31
- def specify_ordinal_access_should_work
28
+ end
29
+
30
+ context "when initializing with an array without attributes" do
31
+ should "test_specify_ordinal_access_should_work" do
32
32
  record = Ruport::Data::Record.new [1,2,3,4]
33
33
  assert_equal 1, record[0]
34
34
  assert_equal 2, record[1]
35
35
  assert_equal 3, record[2]
36
- assert_equal 4, record[3]
37
- end
38
- end
39
-
36
+ assert_equal 4, record[3]
37
+ end
38
+ end
39
+
40
40
  describe "when initializing with a hash without attributes" do
41
- def setup
41
+ def setup
42
42
  @record = Ruport::Data::Record.new({:a => 1, :b => 2, :c => 3},{})
43
- end
44
-
45
- def specify_key_access_should_work
43
+ end
44
+
45
+ def test_specify_key_access_should_work
46
46
  assert_equal 1, @record[:a]
47
47
  assert_equal 2, @record[:b]
48
48
  assert_equal 3, @record[:c]
49
- assert_equal 3, @record.c
50
- end
49
+ assert_equal 3, @record.c
50
+ end
51
51
  end
52
-
52
+
53
53
  describe "when initializing with a hash with attributes" do
54
54
  def setup
55
- @record = Record.new({:a => 1, :b => 2, :c => 3 },
55
+ @record = Record.new({:a => 1, :b => 2, :c => 3 },
56
56
  :attributes => [:c,:b,:a])
57
- end
58
-
59
- def specify_key_access_should_work
57
+ end
58
+
59
+ def test_specify_key_access_should_work
60
60
  assert_equal 1, @record[:a]
61
61
  assert_equal 2, @record[:b]
62
62
  assert_equal 3, @record[:c]
63
- assert_equal 3, @record.c
63
+ assert_equal 3, @record.c
64
64
  end
65
-
66
- def specify_ordinal_access_should_work
65
+
66
+ def test_specify_ordinal_access_should_work
67
67
  assert_equal 3, @record[0]
68
- assert_equal 2, @record[1]
69
- assert_equal 1, @record[2]
68
+ assert_equal 2, @record[1]
69
+ assert_equal 1, @record[2]
70
70
  end
71
71
  end
72
-
72
+
73
73
  def test_bracket_equals
74
74
  @record[1] = "godzilla"
75
75
  @record["d"] = "mothra"
@@ -80,16 +80,16 @@ class TestRecord < Minitest::Test
80
80
  assert_equal @record[3], "mothra"
81
81
  assert_equal @record["d"], "mothra"
82
82
  end
83
-
83
+
84
84
  def test_accessors
85
85
  assert_equal @record.a, @record["a"]
86
86
  assert_equal @record.b, @record["b"]
87
87
  assert_equal @record.c, @record["c"]
88
88
  assert_equal @record.d, @record["d"]
89
- end
90
-
89
+ end
90
+
91
91
  def test_can_has_id
92
- record = Ruport::Data::Record.new(:id => 12345)
92
+ record = Ruport::Data::Record.new(:id => 12345)
93
93
  assert_equal 12345, record.id
94
94
  end
95
95
 
@@ -98,7 +98,7 @@ class TestRecord < Minitest::Test
98
98
  @record.e
99
99
  end
100
100
  end
101
-
101
+
102
102
  def test_attribute_setting
103
103
  @record.a = 10
104
104
  @record.b = 20
@@ -114,22 +114,22 @@ class TestRecord < Minitest::Test
114
114
  def test_to_hash
115
115
  @record.to_hash
116
116
  assert_equal({ "a" => 1, "b" => 2, "c" => 3, "d" => 4 }, @record.to_hash)
117
- end
118
-
117
+ end
118
+
119
119
  def test_rename_attribute
120
120
  @record.rename_attribute("b","x")
121
121
  assert_equal %w[a x c d], @record.attributes
122
122
  assert_equal 2, @record["x"]
123
123
  assert_equal 2, @record.x
124
124
  assert_equal 2, @record[1]
125
- end
125
+ end
126
126
 
127
127
  def test_equality
128
128
 
129
129
  dc = %w[a b c d]
130
130
  dc2 = %w[a b c d]
131
131
  dc3 = %w[a b c]
132
-
132
+
133
133
  rec1 = Record.new [1,2,3,4]
134
134
  rec2 = Record.new [1,2,3,4]
135
135
  rec3 = Record.new [1,2]
@@ -137,7 +137,7 @@ class TestRecord < Minitest::Test
137
137
  rec5 = Record.new [1,2,3,4], :attributes => dc2
138
138
  rec6 = Record.new [1,2,3,4], :attributes => dc3
139
139
  rec7 = Record.new [1,2], :attributes => dc
140
-
140
+
141
141
  [:==, :eql?].each do |op|
142
142
  assert rec1.send(op, rec2)
143
143
  assert rec4.send(op, rec5)
@@ -162,7 +162,7 @@ class TestRecord < Minitest::Test
162
162
 
163
163
  assert_equal [1,2,3,4], @record.to_a
164
164
  assert_equal %w[a b c d], @record.attributes
165
-
165
+
166
166
  @record.reorder "a","d","b","c"
167
167
  assert_equal [1,4,2,3], @record.to_a
168
168
  assert_equal %w[a d b c], @record.attributes
@@ -199,12 +199,12 @@ class TestRecord < Minitest::Test
199
199
  s = Record.new :attributes => %w[a b], :data => [1,2]
200
200
  assert_equal r.hash, s.hash
201
201
  end
202
-
202
+
203
203
  def test_records_with_differing_attrs_and_data_hash_differently
204
204
  r = Record.new [1,2],:attributes => %w[a b]
205
205
  s = Record.new [nil,nil],:attributes => %w[a b]
206
206
  assert r.hash != s.hash
207
-
207
+
208
208
  t = Record.new [1,3],:attributes => %w[a b]
209
209
  assert r.hash != t.hash
210
210
  end
@@ -225,7 +225,7 @@ class TestRecord < Minitest::Test
225
225
  new_object_id = @record.instance_variable_get(:@attributes).object_id
226
226
  assert_equal a.object_id, new_object_id
227
227
  end
228
-
228
+
229
229
  #----------------------------------------------------------------------
230
230
  # BUG Traps
231
231
  #----------------------------------------------------------------------
@@ -245,35 +245,35 @@ class TestRecord < Minitest::Test
245
245
  end
246
246
 
247
247
  # Ticket #172
248
- def test_ensure_get_really_indifferent
248
+ def test_ensure_get_really_indifferent
249
249
  a = Record.new({"a" => 1, "b" => 2})
250
250
  assert_equal(2,a.get("b"))
251
251
  assert_equal(2,a.get(:b))
252
- a = Record.new({:a => 1, :b => 2})
252
+ a = Record.new({:a => 1, :b => 2})
253
253
  assert_equal(2,a.get("b"))
254
254
  assert_equal(2,a.get(:b))
255
255
  end
256
-
256
+
257
257
  def test_ensure_get_throws_argument_error
258
258
  a = Record.new({"a" => 1, "b" => 2})
259
259
  assert_raises(ArgumentError) { a.get([]) }
260
260
  end
261
-
261
+
262
262
  def test_ensure_delete_removes_attribute
263
263
  a = Record.new({"a" => 1, "b" => 2})
264
264
  assert_equal({"a" => 1, "b" => 2}, a.data)
265
265
  assert_equal(["a","b"], a.attributes)
266
-
266
+
267
267
  a.send(:delete, "a")
268
268
  assert_equal({"b" => 2}, a.data)
269
269
  assert_equal(["b"], a.attributes)
270
270
  end
271
-
271
+
272
272
  def test_ensure_bracket_equals_updates_attributes
273
273
  a = Record.new({"a" => 1, "b" => 2})
274
274
  assert_equal({"a" => 1, "b" => 2}, a.data)
275
275
  assert_equal(["a","b"], a.attributes)
276
-
276
+
277
277
  a["b"] = 3
278
278
  assert_equal({"a" => 1, "b" => 3}, a.data)
279
279
  assert_equal(["a","b"], a.attributes)
@@ -289,44 +289,44 @@ class TestRecord < Minitest::Test
289
289
  a = MyRecordSub.new [1,2,3]
290
290
  assert_equal "1,2,3\n", a.to_csv
291
291
  end
292
-
293
- describe "when rendering records" do
294
-
295
- def specify_record_as_should_work
292
+
293
+ context "when rendering records" do
294
+
295
+ should "test_specify_record_as_should_work" do
296
296
  rendered_row = @record.as(:text)
297
297
  assert_equal("| 1 | 2 | 3 | 4 |\n", rendered_row)
298
298
  end
299
299
 
300
- def specify_record_to_format_should_work_without_options
300
+ should "test_specify_record_to_format_should_work_without_options" do
301
301
  rendered_row = @record.to_text
302
- assert_equal("| 1 | 2 | 3 | 4 |\n", rendered_row)
303
- end
304
-
305
- def specify_record_to_format_should_work_with_options
302
+ assert_equal("| 1 | 2 | 3 | 4 |\n", rendered_row)
303
+ end
304
+
305
+ should "test_specify_record_to_format_should_work_with_options" do
306
306
  rendered_row = @record.to_csv(:format_options => { :col_sep => "\t"})
307
- assert_equal("1\t2\t3\t4\n",rendered_row)
308
- end
309
-
307
+ assert_equal("1\t2\t3\t4\n",rendered_row)
308
+ end
309
+
310
310
  describe "when given bad format names" do
311
- def setup
312
- @a = Record.new({ "a" => 1, "b" => 2 })
311
+ def setup
312
+ @a = Record.new({ "a" => 1, "b" => 2 })
313
313
  end
314
314
 
315
- def specify_as_should_throw_proper_errors
316
- assert_raises(Ruport::Controller::UnknownFormatError) { @a.as(:nothing) }
317
- end
318
-
319
- def specify_to_format_should_throw_proper_errors
315
+ def test_specify_as_should_throw_proper_errors
316
+ assert_raises(Ruport::Controller::UnknownFormatError) { @a.as(:nothing) }
317
+ end
318
+
319
+ def test_specify_to_format_should_throw_proper_errors
320
320
  assert_raises(Ruport::Controller::UnknownFormatError) { @a.to_nothing }
321
- end
322
- end
323
-
321
+ end
322
+ end
323
+
324
324
  ## -- BUG TRAPS --------------------
325
-
326
- def specify_attributes_should_not_be_broken_by_to_hack
325
+
326
+ should "test_specify_attributes_should_not_be_broken_by_to_hack" do
327
327
  record = Ruport::Data::Record.new [1,2], :attributes => %w[a to_something]
328
328
  assert_equal 2, record.to_something
329
- end
329
+ end
330
330
  end
331
331
 
332
332
  end
@@ -21,6 +21,13 @@ class TablePivotSimpleCaseTest < Minitest::Test
21
21
  assert_equal(expected, @pivoted)
22
22
  end
23
23
 
24
+ def test_pivot_empty_table
25
+ tab = Ruport.Table ["id", "num_sessions", "status", "diagnosis"]
26
+ tab << ["one", 10, :aborted, :good]
27
+ tab << ["two", 10, :success, :bad]
28
+
29
+ tab.pivot("status", :group_by=>"diagnosis", :values=>"num_sessions", :operation=>:mean)
30
+ end
24
31
  end
25
32
 
26
33
  class PivotConvertRowOrderToGroupOrderTest < Minitest::Test
@@ -55,7 +62,7 @@ class PivotConvertRowOrderToGroupOrderTest < Minitest::Test
55
62
  end
56
63
 
57
64
  def test_nil
58
- assert_equal(nil, convert(nil))
65
+ assert_nil convert(nil)
59
66
  end
60
67
 
61
68
  end
@@ -125,7 +132,7 @@ class PivotPreservesOrdering < Minitest::Test
125
132
  ].each {|e| table << e}
126
133
  table.add_column('pivotme') {|row| 10 - row.group.to_i}
127
134
  pivoted = table.pivot('pivotme', :group_by => 'group', :values => 'a',
128
- :pivot_order => proc {|row, pivot| pivot })
135
+ :pivot_order => proc {|_row, pivot| pivot })
129
136
  assert_equal(['group', 7, 8, 9], pivoted.column_names)
130
137
  end
131
138