lean-ruport 0.3.8

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of lean-ruport might be problematic. Click here for more details.

Files changed (50) hide show
  1. data/ACKNOWLEDGEMENTS +33 -0
  2. data/AUTHORS +19 -0
  3. data/CHANGELOG +206 -0
  4. data/COPYING +340 -0
  5. data/LICENSE +7 -0
  6. data/README +209 -0
  7. data/Rakefile +54 -0
  8. data/TODO +27 -0
  9. data/lib/ruport.rb +58 -0
  10. data/lib/ruport/config.rb +114 -0
  11. data/lib/ruport/data_row.rb +144 -0
  12. data/lib/ruport/data_set.rb +221 -0
  13. data/lib/ruport/format.rb +116 -0
  14. data/lib/ruport/format/builder.rb +89 -0
  15. data/lib/ruport/format/document.rb +77 -0
  16. data/lib/ruport/format/open_node.rb +36 -0
  17. data/lib/ruport/parser.rb +202 -0
  18. data/lib/ruport/query.rb +208 -0
  19. data/lib/ruport/query/sql_split.rb +33 -0
  20. data/lib/ruport/report.rb +116 -0
  21. data/lib/ruport/report/mailer.rb +48 -0
  22. data/test/samples/addressbook.csv +6 -0
  23. data/test/samples/car_ads.txt +505 -0
  24. data/test/samples/data.csv +3 -0
  25. data/test/samples/document.xml +22 -0
  26. data/test/samples/five_lines.txt +5 -0
  27. data/test/samples/five_paragraphs.txt +9 -0
  28. data/test/samples/ross_report.txt +58530 -0
  29. data/test/samples/ruport_test.sql +8 -0
  30. data/test/samples/stonecodeblog.sql +279 -0
  31. data/test/samples/test.sql +2 -0
  32. data/test/samples/test.yaml +3 -0
  33. data/test/tc_builder.rb +116 -0
  34. data/test/tc_config.rb +41 -0
  35. data/test/tc_data_row.rb +36 -0
  36. data/test/tc_data_set.rb +141 -0
  37. data/test/tc_database.rb +25 -0
  38. data/test/tc_document.rb +42 -0
  39. data/test/tc_element.rb +18 -0
  40. data/test/tc_page.rb +42 -0
  41. data/test/tc_query.rb +55 -0
  42. data/test/tc_reading.rb +60 -0
  43. data/test/tc_report.rb +31 -0
  44. data/test/tc_section.rb +45 -0
  45. data/test/tc_sql_split.rb +18 -0
  46. data/test/tc_state.rb +142 -0
  47. data/test/ts_all.rb +9 -0
  48. data/test/ts_format.rb +5 -0
  49. data/test/ts_parser.rb +10 -0
  50. metadata +102 -0
@@ -0,0 +1,36 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ require "test/unit"
4
+ require "ruport"
5
+
6
+ class TestDataRow < Test::Unit::TestCase
7
+
8
+ include Ruport
9
+
10
+ def setup
11
+ @rows = DataSet.new
12
+ @rows.fields = %w[ foo bar ]
13
+ @rows << [ 1 , 2 ]
14
+ @rows << [ 3 , 4 ]
15
+ @rows << [ 5 , 6 ]
16
+ @rows << { "foo" => 7, "bar" => 8 }
17
+ @rows << [ 9, 10 ]
18
+ end
19
+
20
+ def test_to_s
21
+ assert_equal("[1,2]",@rows[0].to_s)
22
+ end
23
+
24
+ def test_tagging
25
+ @rows[0].tag_as :foo
26
+ assert_equal(true, @rows[0].has_tag?(:foo) )
27
+ assert_equal( false,@rows[1].has_tag?(:foo) )
28
+ assert_equal(false,@rows[0].has_tag?(:bar) )
29
+ assert_equal({:foo => true},@rows[0].tags)
30
+ @rows[0].tag_as :apple
31
+ assert_equal({:foo => true, :apple => true},@rows[0].tags)
32
+ assert_equal({},@rows[2].tags)
33
+ end
34
+
35
+ end
36
+
@@ -0,0 +1,141 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ require "test/unit"
4
+ require "ruport"
5
+
6
+ class TestDataSet < Test::Unit::TestCase
7
+
8
+ include Ruport
9
+
10
+ def setup
11
+ @data = DataSet.new
12
+ @data.fields = %w[ col1 col2 col3 ]
13
+ @data.default = ""
14
+ @data << %w[ a b c ]
15
+ @data << { "col1" => "d", "col3" => "e"}
16
+ end
17
+
18
+ def test_new
19
+ fields = %w[ col1 col2 col3 ]
20
+ my_data = DataSet.new(fields)
21
+ assert_equal(fields,my_data.fields)
22
+
23
+ my_filled_data = DataSet.new( fields, [[1,2,3],[4,5,6]] )
24
+
25
+ assert_equal( [[1,2,3],[4,5,6]], my_filled_data.map { |r| r.to_a } )
26
+
27
+ hash_filling = [ { "col1" => 9, "col2" => 6, "col3" => 0 },
28
+ { "col1" => 54, "col3" => 1 } ]
29
+
30
+ my_filled_data = DataSet.new(fields,hash_filling)
31
+
32
+ assert_equal( [[9,6,0],[54,nil,1]], my_filled_data.map { |r| r.to_a } )
33
+
34
+ cloned_set = @data.clone
35
+
36
+ assert_equal( [ %w[a b c], ["d","","e"] ], cloned_set.map { |r| r.to_a } )
37
+ end
38
+
39
+ def test_fields
40
+ assert_equal(%w[ col1 col2 col3 ], @data.fields )
41
+ end
42
+
43
+ def test_default
44
+ @data.default = "x"
45
+ @data << { }
46
+ assert_equal( ['x','x','x'],
47
+ @data[2].to_a )
48
+ end
49
+
50
+ def test_brackets
51
+ row0 = { "col1" => "a", "col2" => "b", "col3" => "c" }
52
+ row1 = { "col1" => "d", "col2" => "", "col3" => "e" }
53
+ row0.each do |key,value|
54
+ assert_equal( value, @data[0][key] )
55
+ end
56
+ row1.each do |key,value|
57
+ assert_equal( value, @data[1][key] )
58
+ end
59
+ end
60
+
61
+ def test_eql?
62
+ data2 = DataSet.new
63
+ data2.fields = %w[ col1 col2 col3 ]
64
+ data2.default = ""
65
+ data2 << %w[ a b c ]
66
+ data2 << { "col1" => "d", "col3" => "e" }
67
+
68
+ #FIXME: This looks like some shady discrete math assignment
69
+ assert(@data.eql?(data2) && data2.eql?(@data))
70
+ data2 << [2, 3, 4]
71
+ assert(!( @data.eql?(data2) || data2.eql?(@data) ))
72
+ @data << [2, 3, 4]
73
+ assert(@data.eql?(data2) && data2.eql?(@data))
74
+ @data << [8, 9, 10]
75
+ assert(!( @data.eql?(data2) || data2.eql?(@data) ))
76
+ data2 << [8, 9, 10]
77
+ assert(@data.eql?(data2) && data2.eql?(@data))
78
+ end
79
+
80
+ def test_empty?
81
+ a = DataSet.new
82
+ a.fields = %w[a b c]
83
+ assert_equal(true,a.empty?)
84
+ assert_equal(false,@data.empty?)
85
+
86
+ a << [1,2,3]
87
+ assert_equal(false,a.empty?)
88
+ end
89
+
90
+ def test_load
91
+ loaded_data = DataSet.load("test/samples/data.csv")
92
+ @data.each_with_index do |row,r_index|
93
+ row.each_with_index do |field,f_index|
94
+ assert_equal(field,loaded_data[r_index][f_index])
95
+ end
96
+ end
97
+ end
98
+
99
+ def test_to_csv
100
+ loaded_data = DataSet.load("test/samples/data.csv" )
101
+ csv = loaded_data.to_csv
102
+ assert_equal("col1,col2,col3\na,b,c\nd,\"\",e\n",csv)
103
+ end
104
+
105
+ def test_to_html
106
+ assert_equal("<table>\n <tr>\n <th>col1</th><th>col2</th>" +
107
+ "<th>col3</th>\n </tr>\n" +
108
+ " <tr>\n <td>a</td><td>b</td><td>c</td>\n </tr>\n" +
109
+ " <tr>\n <td>d</td><td></td><td>e</td>\n </tr>\n" +
110
+ "</table>", @data.to_html )
111
+ end
112
+
113
+ def test_select_columns
114
+ assert_equal( [["a"],["d"]],
115
+ @data.select_columns("col1").map { |r| r.to_a } )
116
+ assert_equal( [["b"],[""]] ,
117
+ @data.select_columns("col2").map { |r| r.to_a } )
118
+ assert_equal( [["c"],["e"]],
119
+ @data.select_columns("col3").map { |r| r.to_a } )
120
+ assert_equal( [["a","b","c"],["d","","e"]],
121
+ @data.select_columns("col1","col2","col3").map { |r| r.to_a })
122
+ assert_equal( [["c","a"],["e","d"]],
123
+ @data.select_columns("col3","col1").map { |r| r.to_a } )
124
+ end
125
+
126
+ def test_render
127
+ data = DataSet.new
128
+ data.fields = %w[ col1 col2 col3]
129
+ data << %w[ a b c]
130
+ data << %w[ d e f]
131
+ assert_equal("col1,col2,col3\na,b,c\nd,e,f\n",
132
+ data.as(:csv) )
133
+
134
+ assert_equal("<table>\n <tr>\n <th>col1</th><th>col2</th>" +
135
+ "<th>col3</th>\n </tr>\n" +
136
+ " <tr>\n <td>a</td><td>b</td><td>c</td>\n </tr>\n" +
137
+ " <tr>\n <td>d</td><td>e</td><td>f</td>\n </tr>\n" +
138
+ "</table>", data.as(:html) )
139
+ end
140
+
141
+ end
@@ -0,0 +1,25 @@
1
+ require "test/unit"
2
+ require "ruportlib"
3
+
4
+ class TestSqlSplit < Test::Unit::TestCase
5
+ def teardown
6
+ FileUtils.rm '/tmp/compare.sql' if File.exist?( '/tmp/compare.sql' )
7
+ end
8
+
9
+ def test_stonecodeblog_sql
10
+ user = 'test'
11
+ password = 'password'
12
+ dbh = DBI.connect( "dbi:Mysql:test:localhost", user, password )
13
+ dbh.do 'drop database if exists stonecodeblog'
14
+ orig_sql = 'test/samples/stonecodeblog.sql'
15
+ sql = File.read orig_sql
16
+ split = Ruport::Report::SqlSplit.new sql
17
+ split.each do |sql| dbh.do( sql ); end
18
+ tmp_sql = '/tmp/compare.sql'
19
+ md_command =
20
+ "mysqldump -u#{ user } -p#{ password } --databases stonecodeblog"
21
+ `#{ md_command } > #{ tmp_sql }`
22
+ diff = `diff #{ orig_sql } #{ tmp_sql }`
23
+ assert( diff == '', diff[0..500] )
24
+ end
25
+ end
@@ -0,0 +1,42 @@
1
+ require "test/unit"
2
+ require "ruport"
3
+
4
+ class TestDocument < Test::Unit::TestCase
5
+
6
+ include Ruport
7
+
8
+ def setup
9
+ @empty_doc = Format::Document.new :test_doc1
10
+ many_pages = [:p1,:p2,:p3,:p4].map { |p| Format::Page.new(p) }
11
+ @populated_doc = Format::Document.new :test_doc2, :pages => many_pages
12
+ end
13
+
14
+ def test_basics
15
+ assert_equal(:test_doc1,@empty_doc.name)
16
+ assert_equal(:test_doc2,@populated_doc.name)
17
+ assert_equal([],@empty_doc.pages)
18
+ assert_equal([:p1,:p2,:p3,:p4],@populated_doc.pages.map { |p| p.name })
19
+ end
20
+
21
+ def test_each
22
+ page_names = [:p1,:p2,:p3,:p4]
23
+
24
+ @populated_doc.each { |p| assert_equal(page_names.shift,p.name) }
25
+ assert_equal([],page_names)
26
+
27
+ @populated_doc.pages << Format::Page.new(:p5)
28
+ page_names = [:p1,:p2,:p3,:p4,:p5]
29
+
30
+ @populated_doc.each { |p| assert_equal(page_names.shift,p.name) }
31
+ assert_equal([],page_names)
32
+ end
33
+
34
+ def test_add_page
35
+ @empty_doc.add_page :p1
36
+ @populated_doc.add_page :p5, :some_trait => "cool"
37
+ assert(@empty_doc.find { |p| p.name.eql?(:p1) })
38
+ assert(@populated_doc.find { |p| p.name.eql?(:p5) and
39
+ p.some_trait.eql?("cool") })
40
+ end
41
+
42
+ end
@@ -0,0 +1,18 @@
1
+ require "test/unit"
2
+ require "ruport"
3
+
4
+ class TestElement < Test::Unit::TestCase
5
+ include Ruport
6
+ def setup
7
+ @empty_element = Format::Element.new :test_element
8
+ @populated_element = Format::Element.new :test_element2,
9
+ :content => "Hello, Element!"
10
+ end
11
+
12
+ def test_basics
13
+ assert_equal(:test_element, @empty_element.name)
14
+ assert_equal(:test_element2, @populated_element.name)
15
+ assert_equal("Hello, Element!", @populated_element.content)
16
+ end
17
+
18
+ end
@@ -0,0 +1,42 @@
1
+ require "test/unit"
2
+ require "ruport"
3
+
4
+ class TestPage < Test::Unit::TestCase
5
+ include Ruport
6
+ def setup
7
+ @empty_page = Format::Page.new :test_page1
8
+ sections = { :s1 => Format::Section.new(:s1), :s2 => Format::Section.new(:s2) }
9
+ @populated_page = Format::Page.new :test_page2,
10
+ :sections => sections
11
+ end
12
+
13
+ def test_basics
14
+ assert_equal(:test_page1, @empty_page.name)
15
+ assert_equal(:test_page2, @populated_page.name)
16
+ assert_equal([],[:s1,:s2]-@populated_page.map { |s| s.name })
17
+ assert_equal({},@empty_page.sections)
18
+ end
19
+
20
+ def test_each
21
+ section_names = [:s1,:s2]
22
+ @populated_page.each { |s| section_names -= [s.name] }
23
+ assert_equal([],section_names)
24
+ @populated_page << Format::Section.new(:s3)
25
+ section_names = [:s1,:s2,:s3]
26
+ @populated_page.each { |s| section_names -= [s.name] }
27
+ assert_equal([],section_names)
28
+ end
29
+
30
+ def test_add_function
31
+ @populated_page.add_section :s3
32
+ @populated_page.add_section :s4, :content => "Hello from Section!"
33
+ assert(@populated_page.find { |s| s.name.eql?(:s3) })
34
+ assert(@populated_page.find { |s| s.name.eql?(:s4) and
35
+ s.content.eql?("Hello from Section!")} )
36
+
37
+ end
38
+ def test_brackets
39
+ assert_equal(:s1,@populated_page[:s1].name)
40
+ assert_equal(:s2,@populated_page[:s2].name)
41
+ end
42
+ end
@@ -0,0 +1,55 @@
1
+ require "test/unit"
2
+ require "ruport"
3
+ class TestQuery < Test::Unit::TestCase
4
+
5
+
6
+ def setup
7
+ Ruport::Config.source :default,
8
+ :dsn => "ruport:test", :user => "greg", :password => "apple"
9
+
10
+ Ruport::Config.source :alternate,
11
+ :dsn => "ruport:test2", :user => "sandal", :password => "harmonix"
12
+
13
+ @query1 = Ruport::Query.new "select * from foo", :cache_enabled => true
14
+ @query1.cached_data = [[1,2,3],[4,5,6],[7,8,9]]
15
+ end
16
+
17
+
18
+ def test_result
19
+ assert_nothing_raised { @query1.result }
20
+ assert_equal([[1,2,3],[4,5,6],[7,8,9]],@query1.result)
21
+ end
22
+
23
+ def test_each
24
+ data = [[1,2,3],[4,5,6],[7,8,9]]
25
+ @query1.each do |r|
26
+ assert_equal(data.shift,r)
27
+ end
28
+ data = [1,4,7]
29
+ @query1.each do |r|
30
+ assert_equal(data.shift,r.first)
31
+ end
32
+ assert_raise (LocalJumpError) { @query1.each }
33
+ end
34
+
35
+ def test_select_source
36
+
37
+ assert_equal( "ruport:test", @query1.instance_eval("@dsn") )
38
+ assert_equal( "greg", @query1.instance_eval("@user") )
39
+ assert_equal( "apple", @query1.instance_eval("@password") )
40
+
41
+ @query1.select_source :alternate
42
+
43
+ assert_equal( "ruport:test2", @query1.instance_eval("@dsn") )
44
+ assert_equal( "sandal", @query1.instance_eval("@user") )
45
+ assert_equal( "harmonix", @query1.instance_eval("@password") )
46
+
47
+ @query1.select_source :default
48
+
49
+ assert_equal( "ruport:test", @query1.instance_eval("@dsn") )
50
+ assert_equal( "greg", @query1.instance_eval("@user") )
51
+ assert_equal( "apple", @query1.instance_eval("@password") )
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,60 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # tc_reading.rb
4
+ #
5
+ # Created by James Edward Gray II on 2005-08-14.
6
+ # Copyright 2005 Gray Productions. All rights reserved.
7
+
8
+ require "test/unit"
9
+
10
+ require "ruport/parser"
11
+
12
+ class TestReading < Test::Unit::TestCase
13
+ def test_line_by_line_read
14
+ path = File.join(File.dirname(__FILE__), "samples/five_lines.txt")
15
+ lines = File.readlines(path)
16
+ test = self
17
+
18
+ Ruport::Parser.new(path) do
19
+ read { |line| test.assert_equal(lines.shift, line) }
20
+ end
21
+ end
22
+
23
+ def test_paragraph_read
24
+ path = File.join(File.dirname(__FILE__), "samples/five_paragraphs.txt")
25
+ paragraphs = File.readlines(path, "")
26
+ test = self
27
+
28
+ Ruport::Parser.new(path, "") do
29
+ read { |paragraph| test.assert_equal(paragraphs.shift, paragraph) }
30
+ end
31
+ end
32
+
33
+ def test_restricted_reading
34
+ path = File.join(File.dirname(__FILE__), "samples/five_lines.txt")
35
+ numbers = %w{two three}
36
+ test = self
37
+
38
+ Ruport::Parser.new(path) do
39
+ read(/ (t\w+)\./) do |number|
40
+ test.assert_equal("This is line #{numbers.first}.\n", @read)
41
+ test.assert_equal(numbers.shift, number)
42
+ end
43
+ end
44
+ end
45
+
46
+ def test_data_saving_and_retrieving
47
+ path = File.join(File.dirname(__FILE__), "samples/five_lines.txt")
48
+
49
+ data = Ruport::Parser.new( path ) do
50
+ read(/ (o\w+)\./) { |number| @number = number }
51
+ read { |line| (@lines ||= Array.new) << line }
52
+ end
53
+
54
+ assert_equal("one", data[:number])
55
+ assert_equal("one", data.number)
56
+ assert_equal(path, data.path)
57
+ assert_equal(5, data.lines.size)
58
+ assert_equal(File.readlines(path), data.lines)
59
+ end
60
+ end
@@ -0,0 +1,31 @@
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
+ class TestReport < Test::Unit::TestCase
9
+ include Ruport
10
+
11
+ def setup
12
+ @report = Report.new
13
+ end
14
+
15
+ def test_render
16
+ result = @report.render "<%= 2 + 3%>",
17
+ :filters => [:erb]
18
+ assert_equal("5",result)
19
+
20
+ if defined? RedCloth
21
+ result = @report.render '"foo":http://foo.com',
22
+ :filters => [:red_cloth]
23
+
24
+ assert_equal('<p><a href="http://foo.com">foo</a></p>',result)
25
+ result = @report.render %{"<%= 2 + 3%>":http://foo.com },
26
+ :filters => [:erb, :red_cloth]
27
+ assert_equal('<p><a href="http://foo.com">5</a></p>',result)
28
+ end
29
+ end
30
+
31
+ end