ruport 0.4.23 → 0.4.99
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +16 -8
- data/CHANGELOG +30 -1
- data/README +144 -114
- data/Rakefile +12 -4
- data/TODO +4 -7
- data/bin/rope +21 -28
- data/examples/line_graph.rb +36 -0
- data/examples/sample_invoice_report.rb +1 -1
- data/examples/simple_graph.rb +8 -0
- data/lib/SVG/Graph/Bar.rb +137 -0
- data/lib/SVG/Graph/BarBase.rb +140 -0
- data/lib/SVG/Graph/BarHorizontal.rb +136 -0
- data/lib/SVG/Graph/Graph.rb +977 -0
- data/lib/SVG/Graph/Line.rb +444 -0
- data/lib/SVG/Graph/Pie.rb +394 -0
- data/lib/SVG/Graph/Plot.rb +494 -0
- data/lib/SVG/Graph/Schedule.rb +373 -0
- data/lib/SVG/Graph/TimeSeries.rb +241 -0
- data/lib/ruport.rb +2 -2
- data/lib/ruport/config.rb +47 -3
- data/lib/ruport/data/collection.rb +17 -1
- data/lib/ruport/data/record.rb +101 -8
- data/lib/ruport/data/set.rb +81 -2
- data/lib/ruport/data/set.rb.rej +147 -0
- data/lib/ruport/data/set.rb~ +73 -0
- data/lib/ruport/data/table.rb +127 -2
- data/lib/ruport/data/taggable.rb +21 -2
- data/lib/ruport/format.rb +36 -44
- data/lib/ruport/format/engine.rb +21 -1
- data/lib/ruport/format/plugin.rb +64 -1
- data/lib/ruport/mailer.rb +70 -36
- data/lib/ruport/meta_tools.rb +15 -6
- data/lib/ruport/query.rb +1 -1
- data/lib/ruport/rails/reportable.rb +23 -1
- data/lib/ruport/report.rb +11 -11
- data/lib/ruport/report/invoice.rb +16 -0
- data/lib/ruport/system_extensions.rb +3 -55
- data/test/{tc_database.rb → _test_database.rb} +0 -0
- data/test/{tc_config.rb → test_config.rb} +0 -0
- data/test/{tc_format.rb → test_format.rb} +1 -0
- data/test/{tc_format_engine.rb → test_format_engine.rb} +14 -2
- data/test/test_graph.rb +101 -0
- data/test/{tc_invoice.rb → test_invoice.rb} +7 -1
- data/test/test_mailer.rb +108 -0
- data/test/test_meta_tools.rb +14 -0
- data/test/{tc_plugin.rb → test_plugin.rb} +12 -1
- data/test/{tc_query.rb → test_query.rb} +0 -0
- data/test/{tc_record.rb → test_record.rb} +9 -0
- data/test/{tc_report.rb → test_report.rb} +2 -1
- data/test/{tc_ruport.rb → test_ruport.rb} +0 -0
- data/test/test_set.rb +118 -0
- data/test/test_set.rb.rej +16 -0
- data/test/{tc_set.rb → test_set.rb~} +17 -0
- data/test/{tc_sql_split.rb → test_sql_split.rb} +0 -0
- data/test/{tc_table.rb → test_table.rb} +15 -0
- data/test/{tc_taggable.rb → test_taggable.rb} +0 -0
- data/test/unit.log +361 -0
- metadata +52 -30
- data/examples/bar.pdf +0 -193
- data/examples/f.log +0 -5
- data/examples/foo.pdf +0 -193
- data/lib/ruport/format/document.rb +0 -78
- data/lib/ruport/format/open_node.rb +0 -38
- data/test/tc_data_row.rb +0 -132
- data/test/tc_data_set.rb +0 -386
- data/test/tc_document.rb +0 -42
- data/test/tc_element.rb +0 -18
- data/test/tc_page.rb +0 -42
- data/test/tc_section.rb +0 -45
- data/test/ts_all.rb +0 -12
- data/test/ts_format.rb +0 -7
@@ -1,78 +0,0 @@
|
|
1
|
-
# FIXME: Copyright notice
|
2
|
-
# HERE THERE BE DRAGONS!
|
3
|
-
require "ostruct"
|
4
|
-
module Ruport
|
5
|
-
class Format
|
6
|
-
class Document < OpenStruct
|
7
|
-
include Enumerable
|
8
|
-
|
9
|
-
def initialize(name,options={})
|
10
|
-
super(options)
|
11
|
-
self.name = name
|
12
|
-
self.pages ||= []
|
13
|
-
end
|
14
|
-
|
15
|
-
def each
|
16
|
-
self.pages.each { |p| yield(p) }
|
17
|
-
end
|
18
|
-
|
19
|
-
def add_page(name,options={})
|
20
|
-
options[:document] = self
|
21
|
-
self.pages << Format::Page.new(name,options)
|
22
|
-
end
|
23
|
-
|
24
|
-
def <<(page)
|
25
|
-
page.document = self
|
26
|
-
self.pages << page.dup
|
27
|
-
end
|
28
|
-
|
29
|
-
def [](page_name)
|
30
|
-
return self.pages[page_name] if page_name.kind_of? Integer
|
31
|
-
self.pages.find { |p| p.name.eql?(page_name) }
|
32
|
-
end
|
33
|
-
|
34
|
-
def clone
|
35
|
-
cloned = self.clone
|
36
|
-
cloned.pages = self.pages.clone
|
37
|
-
return cloned
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
class Page < Format::OpenNode
|
42
|
-
|
43
|
-
def initialize(name,options={})
|
44
|
-
super(:page,:document,:sections,name,options)
|
45
|
-
end
|
46
|
-
|
47
|
-
def add_section(name,options={})
|
48
|
-
add_child(Format::Section,name,options)
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
class Section < Format::OpenNode
|
54
|
-
|
55
|
-
def initialize(name, options={})
|
56
|
-
super(:section,:page,:elements,name,options)
|
57
|
-
end
|
58
|
-
|
59
|
-
def add_element(name,options={})
|
60
|
-
add_child(Format::Element,name,options)
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
class Element < OpenStruct
|
66
|
-
|
67
|
-
def initialize(name,options={})
|
68
|
-
super(options)
|
69
|
-
self.name = name
|
70
|
-
end
|
71
|
-
|
72
|
-
def to_s
|
73
|
-
self.content
|
74
|
-
end
|
75
|
-
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
#FIXME: COPYRIGHT NOTICE
|
2
|
-
|
3
|
-
require "ostruct"
|
4
|
-
module Ruport
|
5
|
-
class Format
|
6
|
-
class OpenNode < OpenStruct
|
7
|
-
include Enumerable
|
8
|
-
def initialize(my_name, parent_name, children_name, name, options={})
|
9
|
-
@my_children_name = children_name
|
10
|
-
@my_parent_name = parent_name
|
11
|
-
@my_name = my_name
|
12
|
-
super(options)
|
13
|
-
self.name = name
|
14
|
-
send(@my_children_name) ||
|
15
|
-
send("#{@my_children_name}=".to_sym,{})
|
16
|
-
end
|
17
|
-
|
18
|
-
def each(&p)
|
19
|
-
send(@my_children_name).values.each(&p)
|
20
|
-
end
|
21
|
-
|
22
|
-
def add_child(klass,name,options={})
|
23
|
-
options[@my_name] = self
|
24
|
-
self << klass.new(name, options)
|
25
|
-
end
|
26
|
-
|
27
|
-
def <<(child)
|
28
|
-
child.send("#{@my_name}=".to_sym, self)
|
29
|
-
self.send(@my_children_name)[child.name] = child.dup
|
30
|
-
end
|
31
|
-
|
32
|
-
def [](child_name)
|
33
|
-
self.send(@my_children_name)[child_name]
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
data/test/tc_data_row.rb
DELETED
@@ -1,132 +0,0 @@
|
|
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_accessor_style
|
25
|
-
row = @rows[0]
|
26
|
-
assert_equal 1, row.foo
|
27
|
-
assert_equal 2, row.bar
|
28
|
-
row.bar = 17
|
29
|
-
assert_equal [1,17], row.to_a
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_tagging
|
33
|
-
@rows[0].tag_as :foo
|
34
|
-
assert_equal(true, @rows[0].has_tag?(:foo) )
|
35
|
-
assert_equal( false,@rows[1].has_tag?(:foo) )
|
36
|
-
assert_equal(false,@rows[0].has_tag?(:bar) )
|
37
|
-
assert_equal({:foo => true},@rows[0].tags)
|
38
|
-
@rows[0].tag_as :apple
|
39
|
-
assert_equal({:foo => true, :apple => true},@rows[0].tags)
|
40
|
-
assert_equal({},@rows[2].tags)
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_constructor
|
44
|
-
row = Ruport::DataRow.new(%w[a b c], :data => [1,2,3])
|
45
|
-
row2 = Ruport::DataRow.new(%w[a b c], :data => {"a" => 1, "b" => 2, "c" => 3})
|
46
|
-
row3 = Ruport::DataRow.new(%w[a b c], :data => row2)
|
47
|
-
row4 = Ruport::DataRow.new(%w[a b c], {:data => row3, :tags => [:awesome, :cool]})
|
48
|
-
|
49
|
-
assert_equal(row, row2)
|
50
|
-
assert_equal(row2, row3)
|
51
|
-
|
52
|
-
assert_equal(%w[a b c], row.fields)
|
53
|
-
assert_equal(1, row[0])
|
54
|
-
assert_equal(1, row["a"])
|
55
|
-
assert_equal(2, row[1])
|
56
|
-
assert_equal(2, row["b"])
|
57
|
-
assert_equal(3, row[2])
|
58
|
-
assert_equal(3, row["c"])
|
59
|
-
assert_equal([:awesome,:cool], row4.tags)
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_brackets
|
64
|
-
r1 = @rows[0]
|
65
|
-
r2 = @rows[3]
|
66
|
-
|
67
|
-
assert_equal( 1, r1[0] )
|
68
|
-
assert_equal( 2, r1[1] )
|
69
|
-
assert_equal( 1, r1["foo"] )
|
70
|
-
assert_equal( 2, r1["bar"] )
|
71
|
-
|
72
|
-
assert_equal( 7, r2[0] )
|
73
|
-
assert_equal( 8, r2["bar"] )
|
74
|
-
assert_equal( 8, r2[:bar] )
|
75
|
-
|
76
|
-
r1[1] = "apple"
|
77
|
-
r1[:foo] = "banana"
|
78
|
-
|
79
|
-
assert_equal( r1["foo"], "banana" )
|
80
|
-
assert_equal( r1[:bar], "apple" )
|
81
|
-
|
82
|
-
assert_nothing_raised { r1[:apples] }
|
83
|
-
assert_equal( r1[:apples], nil )
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_equality
|
87
|
-
assert( Ruport::DataRow.new(%w[ a b ], :data => [1,2]) ==
|
88
|
-
Ruport::DataRow.new(%w[ a b ], :data => [1,2]) )
|
89
|
-
assert( Ruport::DataRow.new(%w[ a b ], :data => [1,2]) !=
|
90
|
-
Ruport::DataRow.new(%w[ c d ], :data => [1,2]) )
|
91
|
-
a = Ruport::DataRow.new(%w[ a b c ], :data => [1,2,3])
|
92
|
-
a.tag_as :apple
|
93
|
-
b = Ruport::DataRow.new(%w[ a b c ], :data => [1,2,3])
|
94
|
-
assert( a == b )
|
95
|
-
assert( a.eql?(b) )
|
96
|
-
|
97
|
-
end
|
98
|
-
|
99
|
-
def test_addition
|
100
|
-
row1 = Ruport::DataRow.new %w[ a b c ], :data => [1,2,3]
|
101
|
-
row2 = Ruport::DataRow.new %w[ d e f ], :data => [4,5,6]
|
102
|
-
|
103
|
-
expected = Ruport::DataRow.new %w[ a b c d e f ], :data => [1,2,3,4,5,6]
|
104
|
-
|
105
|
-
assert_equal( expected, row1 + row2 )
|
106
|
-
end
|
107
|
-
|
108
|
-
def test_clone
|
109
|
-
row1 = Ruport::DataRow.new %w[ a b c ], :data => [1,2,3]
|
110
|
-
row2 = row1.clone
|
111
|
-
|
112
|
-
assert( row1.object_id != row2.object_id )
|
113
|
-
row1.tag_as :original
|
114
|
-
assert( row1.has_tag?(:original) )
|
115
|
-
assert( ! row2.has_tag?(:original) )
|
116
|
-
end
|
117
|
-
|
118
|
-
#FIXME: add edge cases
|
119
|
-
def test_to_h
|
120
|
-
row1 = Ruport::DataRow.new %w[ a b c ], :data => [1,2,3]
|
121
|
-
assert_instance_of(Hash,row1.to_h)
|
122
|
-
assert_equal({ "a" => 1, "b" => 2, "c" => 3},row1.to_h)
|
123
|
-
end
|
124
|
-
|
125
|
-
def test_ensure_arrays_not_modified
|
126
|
-
arr = [1,2,3]
|
127
|
-
row1 = Ruport::DataRow.new %w[ a b c ], :data => arr
|
128
|
-
assert_equal( [1,2,3], arr )
|
129
|
-
end
|
130
|
-
|
131
|
-
end
|
132
|
-
|
data/test/tc_data_set.rb
DELETED
@@ -1,386 +0,0 @@
|
|
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 ] << { "col1" => "d", "col3" => "e"}
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_new
|
18
|
-
fields = %w[ col1 col2 col3 ]
|
19
|
-
my_data = DataSet.new(fields)
|
20
|
-
assert_equal(fields,my_data.fields)
|
21
|
-
|
22
|
-
my_filled_data = DataSet.new( fields, :data => [[1,2,3],[4,5,6]] )
|
23
|
-
|
24
|
-
assert_equal( [[1,2,3],[4,5,6]], my_filled_data.map { |r| r.to_a } )
|
25
|
-
|
26
|
-
hash_filling = [ { "col1" => 9, "col2" => 6, "col3" => 0 },
|
27
|
-
{ "col1" => 54, "col3" => 1 } ]
|
28
|
-
|
29
|
-
my_filled_data = DataSet.new(fields, :data => hash_filling)
|
30
|
-
|
31
|
-
assert_equal( [[9,6,0],[54,nil,1]], my_filled_data.map { |r| r.to_a } )
|
32
|
-
|
33
|
-
cloned_set = @data.clone
|
34
|
-
|
35
|
-
assert_equal( [ %w[a b c], ["d","","e"] ], cloned_set.map { |r| r.to_a } )
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_fields
|
39
|
-
assert_equal(%w[ col1 col2 col3 ], @data.fields )
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_default
|
43
|
-
@data.default = "x"
|
44
|
-
@data << { }
|
45
|
-
assert_equal( ['x','x','x'],
|
46
|
-
@data[2].to_a )
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_rename_columns
|
50
|
-
@data.rename_columns "col1" => "Column 1",
|
51
|
-
"col2" => "Column 2",
|
52
|
-
"col3" => "Column 3"
|
53
|
-
assert_equal %w[Column\ 1 Column\ 2 Column\ 3], @data.fields
|
54
|
-
@data.each do |r|
|
55
|
-
assert_equal %w[Column\ 1 Column\ 2 Column\ 3], r.fields
|
56
|
-
end
|
57
|
-
assert_equal "b", @data[0]["Column 2"]
|
58
|
-
assert_equal "e", @data[1]["Column 3"]
|
59
|
-
@data.rename_columns %w[one two three]
|
60
|
-
assert_equal %w[one two three], @data.fields
|
61
|
-
@data.each do |r|
|
62
|
-
assert_equal %w[one two three], r.fields
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_delete_if
|
67
|
-
@data.delete_if { |r| r.any? { |e| e.empty? } }
|
68
|
-
assert_equal([%w[a b c]],@data.to_a)
|
69
|
-
end
|
70
|
-
|
71
|
-
def test_brackets
|
72
|
-
row0 = { "col1" => "a", "col2" => "b", "col3" => "c" }
|
73
|
-
row1 = { "col1" => "d", "col2" => "", "col3" => "e" }
|
74
|
-
row0.each do |key,value|
|
75
|
-
assert_equal( value, @data[0][key] )
|
76
|
-
end
|
77
|
-
row1.each do |key,value|
|
78
|
-
assert_equal( value, @data[1][key] )
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
def test_eql?
|
83
|
-
data2 = DataSet.new
|
84
|
-
data2.fields = %w[ col1 col2 col3 ]
|
85
|
-
data2.default = ""
|
86
|
-
data2 << %w[ a b c ]
|
87
|
-
data2 << { "col1" => "d", "col3" => "e" }
|
88
|
-
|
89
|
-
#FIXME: This looks like some shady discrete math assignment
|
90
|
-
assert(@data.eql?(data2) && data2.eql?(@data))
|
91
|
-
data2 << [2, 3, 4]
|
92
|
-
assert(!( @data.eql?(data2) || data2.eql?(@data) ))
|
93
|
-
@data << [2, 3, 4]
|
94
|
-
assert(@data.eql?(data2) && data2.eql?(@data))
|
95
|
-
@data << [8, 9, 10]
|
96
|
-
assert(!( @data.eql?(data2) || data2.eql?(@data) ))
|
97
|
-
data2 << [8, 9, 10]
|
98
|
-
assert(@data.eql?(data2) && data2.eql?(@data))
|
99
|
-
end
|
100
|
-
|
101
|
-
def test_shaped_like?
|
102
|
-
a = DataSet.new
|
103
|
-
a.fields = %w[ col1 col2 col3 ]
|
104
|
-
assert(@data.shaped_like?(a))
|
105
|
-
assert(a.shaped_like?(@data))
|
106
|
-
end
|
107
|
-
|
108
|
-
def test_union
|
109
|
-
a = DataSet.new
|
110
|
-
a.fields = %w[ col1 col2 col3 ]
|
111
|
-
a << %w[ a b c ]
|
112
|
-
a << %w[ x y z ]
|
113
|
-
b = a | @data
|
114
|
-
assert_kind_of(DataSet, b)
|
115
|
-
assert_equal(b.data.length, 3)
|
116
|
-
assert_equal([ %w[a b c], %w[x y z], ["d","","e"] ], b.to_a)
|
117
|
-
assert_equal((a | @data), a.union(@data))
|
118
|
-
end
|
119
|
-
|
120
|
-
def test_difference
|
121
|
-
a = DataSet.new
|
122
|
-
a.fields = %w[ col1 col2 col3 ]
|
123
|
-
a << %w[ a b c ]
|
124
|
-
a << %w[ x y z ]
|
125
|
-
b = a - @data
|
126
|
-
assert_kind_of(DataSet, b)
|
127
|
-
assert_equal(b.data.length, 1)
|
128
|
-
assert_equal([ %w[x y z] ], b.to_a)
|
129
|
-
assert_equal((a - @data), a.difference(@data))
|
130
|
-
end
|
131
|
-
|
132
|
-
def test_intersection
|
133
|
-
a = DataSet.new
|
134
|
-
a.fields = %w[ col1 col2 col3 ]
|
135
|
-
a << %w[ a b c ]
|
136
|
-
a << %w[ x y z ]
|
137
|
-
b = a & @data
|
138
|
-
assert_kind_of(DataSet, b)
|
139
|
-
assert_equal(b.data.length, 1)
|
140
|
-
assert_equal([ %w[a b c] ], b.to_a)
|
141
|
-
assert_equal((a & @data), a.intersection(@data))
|
142
|
-
end
|
143
|
-
|
144
|
-
def test_concatenation
|
145
|
-
a = DataSet.new
|
146
|
-
a.fields = %w[ col1 col2 col3 ]
|
147
|
-
a << %w[ x y z ]
|
148
|
-
newdata = @data.concat(a)
|
149
|
-
assert_equal([ %w[a b c], ["d","","e"], %w[x y z] ], newdata.to_a)
|
150
|
-
end
|
151
|
-
|
152
|
-
def test_append_datarow
|
153
|
-
row = DataRow.new(%w[ x y z ], :data => %w[ col1 col2 col3 ])
|
154
|
-
@data << row
|
155
|
-
assert_equal(@data[2], row)
|
156
|
-
end
|
157
|
-
|
158
|
-
def test_append_datarows
|
159
|
-
row = DataRow.new(%w[ x y z ], :data => %w[ col1 col2 col3 ])
|
160
|
-
row2 = DataRow.new(%w[ u v w ], :data => %w[ col1, col2, col3 ])
|
161
|
-
@data << [row, row2]
|
162
|
-
assert_equal(@data[2], row)
|
163
|
-
assert_equal(@data[3], row2)
|
164
|
-
end
|
165
|
-
|
166
|
-
def test_empty?
|
167
|
-
a = DataSet.new
|
168
|
-
a.fields = %w[a b c]
|
169
|
-
assert_equal(true,a.empty?)
|
170
|
-
assert_equal(false,@data.empty?)
|
171
|
-
|
172
|
-
a << [1,2,3]
|
173
|
-
assert_equal(false,a.empty?)
|
174
|
-
end
|
175
|
-
|
176
|
-
def test_length
|
177
|
-
assert_equal(2, @data.length)
|
178
|
-
@data << [1,2,3]
|
179
|
-
assert_equal(3, @data.length)
|
180
|
-
end
|
181
|
-
|
182
|
-
def test_load
|
183
|
-
loaded_data = DataSet.load("test/samples/data.csv", :default => "")
|
184
|
-
assert_equal(@data,loaded_data)
|
185
|
-
|
186
|
-
loaded_data = DataSet.load("test/samples/data.csv", :has_names => false)
|
187
|
-
|
188
|
-
assert_equal(nil,loaded_data.fields)
|
189
|
-
assert_equal([%w[col1 col2 col3],%w[a b c],["d",nil,"e"]],loaded_data.to_a)
|
190
|
-
assert_equal(%w[a b c],loaded_data[1].to_a)
|
191
|
-
|
192
|
-
loaded_data = DataSet.load("test/samples/data.csv") do |set,row|
|
193
|
-
set << row if row.include? 'b'
|
194
|
-
end
|
195
|
-
assert_equal([%w[a b c]],loaded_data.to_a)
|
196
|
-
end
|
197
|
-
|
198
|
-
def test_to_csv
|
199
|
-
loaded_data = DataSet.load("test/samples/data.csv" )
|
200
|
-
csv = loaded_data.to_csv
|
201
|
-
assert_equal("col1,col2,col3\na,b,c\nd,,e\n",csv)
|
202
|
-
end
|
203
|
-
|
204
|
-
def test_to_html
|
205
|
-
assert_equal("<table>\n\t\t<tr>\n\t\t\t<th>col1 </th>\n\t\t\t"+
|
206
|
-
"<th>col2 </th>\n\t\t\t<th>col3</th>\n\t\t</tr>\n"+
|
207
|
-
"\t\t<tr>\n\t\t\t<td>a</td>\n\t\t\t<td>b</td>\n\t\t\t"+
|
208
|
-
"<td>c</td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td>d</td>\n\t"+
|
209
|
-
"\t\t<td> </td>\n\t\t\t<td>e</td>\n\t\t</tr>"+
|
210
|
-
"\n\t</table>", @data.to_html )
|
211
|
-
end
|
212
|
-
|
213
|
-
def test_select_columns
|
214
|
-
|
215
|
-
b = @data.select_columns(0,2)
|
216
|
-
assert_equal(%w[col1 col3], b.fields)
|
217
|
-
assert_equal([%w[a c],%w[d e]],b.to_a)
|
218
|
-
|
219
|
-
assert_equal( [["a"],["d"]],
|
220
|
-
@data.select_columns("col1").to_a )
|
221
|
-
assert_equal( [["b"],[""]] ,
|
222
|
-
@data.select_columns("col2").to_a )
|
223
|
-
assert_equal( [["c"],["e"]],
|
224
|
-
@data.select_columns("col3").to_a )
|
225
|
-
assert_equal( [["a","b","c"],["d","","e"]],
|
226
|
-
@data.select_columns("col1","col2","col3").to_a )
|
227
|
-
assert_equal( [["c","a"],["e","d"]],
|
228
|
-
@data.select_columns("col3","col1").to_a )
|
229
|
-
|
230
|
-
end
|
231
|
-
|
232
|
-
def test_select_columns!
|
233
|
-
a = [[1,2],[3,4]].to_ds(%w[a b])
|
234
|
-
a.select_columns!(*%w[b a])
|
235
|
-
|
236
|
-
assert_equal(%w[b a],a.fields)
|
237
|
-
assert_equal([[2,1],[4,3]],a.to_a)
|
238
|
-
|
239
|
-
a.select_columns!('a')
|
240
|
-
|
241
|
-
assert_equal(%w[a], a.fields)
|
242
|
-
assert_equal([[1],[3]],a.to_a)
|
243
|
-
|
244
|
-
a.select_columns!('a','q')
|
245
|
-
assert_equal(%w[a q], a.fields)
|
246
|
-
assert_equal([[1,nil],[3,nil]],a.to_a)
|
247
|
-
|
248
|
-
a[0]['q'] =2
|
249
|
-
assert_equal([[1,2],[3,nil]],a.to_a)
|
250
|
-
|
251
|
-
end
|
252
|
-
|
253
|
-
def test_as
|
254
|
-
data = DataSet.new
|
255
|
-
data.fields = %w[ col1 col2 col3]
|
256
|
-
data << %w[ a b c]
|
257
|
-
data << %w[ d e f]
|
258
|
-
assert_equal("col1,col2,col3\na,b,c\nd,e,f\n",
|
259
|
-
data.as(:csv) )
|
260
|
-
|
261
|
-
assert_equal("<table>\n\t\t<tr>\n\t\t\t<th>col1 </th>"+
|
262
|
-
"\n\t\t\t<th>col2 </th>\n\t\t\t<th>col3</th>"+
|
263
|
-
"\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td>a</td>\n\t\t\t"+
|
264
|
-
"<td>b</td>\n\t\t\t<td>c</td>\n\t\t</tr>\n\t\t<tr>"+
|
265
|
-
"\n\t\t\t<td>d</td>\n\t\t\t<td>e</td>\n\t\t\t<td>f</td>"+
|
266
|
-
"\n\t\t</tr>\n\t</table>", data.as(:html) )
|
267
|
-
end
|
268
|
-
|
269
|
-
def test_sigma
|
270
|
-
data = Ruport::DataSet.new(%w[x], :data => [[3],[5],[8],[2]])
|
271
|
-
|
272
|
-
sum = data.sigma { |r| r["x"] if (r["x"] % 2).zero? }
|
273
|
-
assert_equal(10, sum)
|
274
|
-
|
275
|
-
sum = data.sigma { |r| r["x"] }
|
276
|
-
assert_equal(18,sum)
|
277
|
-
|
278
|
-
sum = data.sigma("x")
|
279
|
-
assert_equal(18,sum)
|
280
|
-
|
281
|
-
#check alias
|
282
|
-
sum = data.sum { |r| r["x"] }
|
283
|
-
assert_equal(18,sum)
|
284
|
-
end
|
285
|
-
|
286
|
-
def test_add_columns
|
287
|
-
d = @data.add_columns("a","b")
|
288
|
-
assert_equal %w[col1 col2 col3 a b], d.fields
|
289
|
-
assert_equal %w[col1 col2 col3], @data.fields
|
290
|
-
assert_equal %w[col1 col2 col3 a b], d[0].fields
|
291
|
-
assert_equal %w[col1 col2 col3], @data[0].fields
|
292
|
-
assert_equal nil, d[0]["a"]
|
293
|
-
d[0]["a"] = "foo"
|
294
|
-
assert_equal %w[a b c foo] + [nil], d[0].to_a
|
295
|
-
|
296
|
-
d.add_columns!("c")
|
297
|
-
assert_equal %w[col1 col2 col3 a b c], d.fields
|
298
|
-
assert_equal %w[col1 col2 col3 a b c], d[0].fields
|
299
|
-
end
|
300
|
-
|
301
|
-
def test_remove_columns
|
302
|
-
|
303
|
-
d = @data.remove_columns(0)
|
304
|
-
assert_equal(%w[col2 col3],d.fields)
|
305
|
-
assert_equal([%w[b c],["","e"]], d.to_a)
|
306
|
-
|
307
|
-
data = @data.remove_columns("col1","col3")
|
308
|
-
assert_equal(["col2"],data.fields)
|
309
|
-
assert_equal([["b"],[""]], data.to_a)
|
310
|
-
|
311
|
-
data.remove_columns!("col2")
|
312
|
-
assert_equal([],data.fields)
|
313
|
-
assert(data.empty?)
|
314
|
-
|
315
|
-
@data.remove_columns!("col1")
|
316
|
-
assert_equal(%w[col2 col3], @data.fields)
|
317
|
-
end
|
318
|
-
|
319
|
-
def test_bracket_equals
|
320
|
-
expected = DataRow.new(%w[col1 col2 col3], :data => %w[apple banana orange])
|
321
|
-
|
322
|
-
raw = [ %w[apple banana orange],
|
323
|
-
{ "col1" => "apple", "col2" => "banana", "col3" => "orange" },
|
324
|
-
DataRow.new(%w[col1 col2 col3], :data => %w[apple banana orange])
|
325
|
-
]
|
326
|
-
raw.each do |my_row|
|
327
|
-
@data[1] = my_row
|
328
|
-
assert_instance_of(DataRow, @data[1])
|
329
|
-
assert_equal(expected, @data[1])
|
330
|
-
end
|
331
|
-
|
332
|
-
assert_raise(ArgumentError) { @data[1] = "apple" }
|
333
|
-
end
|
334
|
-
|
335
|
-
def test_clone
|
336
|
-
data2 = @data.clone
|
337
|
-
assert( @data.object_id != data2.object_id )
|
338
|
-
assert_equal( @data, data2 )
|
339
|
-
data2 << %w[ f o o ]
|
340
|
-
assert( @data != data2 )
|
341
|
-
assert( data2 != @data )
|
342
|
-
end
|
343
|
-
|
344
|
-
def test_array_hack
|
345
|
-
assert_nothing_raised {
|
346
|
-
[ { :a => :b, :c => :d }, { :e => :f, :g => :h } ].to_ds(%w[a e])
|
347
|
-
}
|
348
|
-
assert_nothing_raised {
|
349
|
-
[ [1,2,3], [4,5,6], [7,8,9] ].to_ds(%w[a b c])
|
350
|
-
}
|
351
|
-
assert_raise(ArgumentError) {
|
352
|
-
%w[d e f].to_ds(%w[a b c])
|
353
|
-
}
|
354
|
-
assert_raise(TypeError) {
|
355
|
-
[1,2,3].to_ds(%w[foo bar soup])
|
356
|
-
}
|
357
|
-
|
358
|
-
assert_equal( DataSet.new(%w[a b], :data => [[1,2],[3,4]]),
|
359
|
-
[[1,2],[3,4]].to_ds(%w[a b]) )
|
360
|
-
|
361
|
-
assert_equal( DataSet.new(%w[a b], :data => [{ "a" => 1 },{ "b" => 2}]),
|
362
|
-
[{"a" => 1}, {"b" => 2}].to_ds(%w[a b]) )
|
363
|
-
assert_equal( DataSet.new(%w[a b], :data => [DataRow.new(%w[a b], :data => [1,2])]),
|
364
|
-
[DataRow.new(%w[a b], :data => [1,2])].to_ds(%w[a b]) )
|
365
|
-
|
366
|
-
# FIXME: Decide whether this test should pass or fail.
|
367
|
-
# assert_equal( DataSet.new(%w[a b], [DataRow.new(%w[a b], [1,2])]),
|
368
|
-
# [DataRow.new(%w[a q], [1,2])].to_ds(%w[a b]) )
|
369
|
-
|
370
|
-
end
|
371
|
-
|
372
|
-
def test_append_chain
|
373
|
-
data2 = DataSet.new(%w[col1 col2 col3])
|
374
|
-
data2.default=""
|
375
|
-
data2 << %w[ a b c ] << { "col1" => "d", "col3" => "e" }
|
376
|
-
assert_equal @data, data2
|
377
|
-
end
|
378
|
-
|
379
|
-
def test_no_fields
|
380
|
-
data2 = DataSet.new
|
381
|
-
data2 << [1,2,3]
|
382
|
-
data2 << [4,5,6]
|
383
|
-
assert_equal([1,2,3],data2[0].to_a)
|
384
|
-
assert_equal([4,5,6],data2[1].to_a)
|
385
|
-
end
|
386
|
-
end
|