ruport 0.4.9 → 0.4.11

Sign up to get free protection for your applications and to get access to all the features.
data/test/tc_builder.rb DELETED
@@ -1,135 +0,0 @@
1
- require "test/unit"
2
- require "ruport"
3
- class TestBuilder < Test::Unit::TestCase
4
- include Ruport
5
- def setup
6
- data = DataSet.new
7
- data.fields = %w[ col1 col2 col3 ]
8
- data.default = ""; data << %w[ a b c ]; data << %w[ d e f ]
9
- @builder = Format::Builder.new( data )
10
- end
11
-
12
- def test_register_rendering_method
13
- assert_raise(NoMethodError) {
14
- @builder.render_sample
15
- }
16
- Format::Builder.register_rendering_method :sample do
17
- "sample"
18
- end
19
- assert_nothing_raised { @builder.render_sample }
20
- assert_equal( "sample", @builder.render_sample )
21
- Format::Builder.register_rendering_method :data_snatcher do
22
- @data
23
- end
24
-
25
- assert_equal(DataSet.new( %w[ col1 col2 col3 ],
26
- :data => [ %w[ a b c ], %w[ d e f ]]),
27
- @builder.render_data_snatcher)
28
-
29
- end
30
-
31
- def test_render_html
32
- @builder.format = :html
33
- assert_equal("<table>\n <tr>\n <th>col1</th><th>col2</th>" +
34
- "<th>col3</th>\n </tr>\n" +
35
- " <tr>\n <td>a</td><td>b</td><td>c</td>\n </tr>\n" +
36
- " <tr>\n <td>d</td><td>e</td><td>f</td>\n </tr>\n" +
37
- "</table>", @builder.render )
38
- end
39
-
40
- def test_render_text
41
- @builder.format = :text
42
- assert_equal( "fields: ( col1, col2, col3 )\n" +
43
- "row0: ( a, b, c )\n" +
44
- "row1: ( d, e, f )\n", @builder.render )
45
- end
46
-
47
- def test_render_csv
48
- @builder.format = :csv
49
- assert_equal("col1,col2,col3\na,b,c\nd,e,f\n", @builder.render)
50
- end
51
-
52
- def test_partial_rendering
53
- my_data = DataSet.new
54
- my_data.fields = %w[ col1 col2 col3 ]
55
- [ %w[ a b c], %w[ d e f ], %w[ g h i ], %w[ j k l ] ].each do |row|
56
- my_data << row
57
- end
58
-
59
- my_builder = Format::Builder.new( my_data )
60
- my_builder.range = 1..2
61
-
62
- my_builder.format = :csv
63
- assert_equal("col1,col2,col3\nd,e,f\ng,h,i\n", my_builder.render)
64
-
65
- my_builder.format = :html
66
- assert_equal( "<table>\n <tr>\n <th>col1</th><th>col2</th>"+
67
- "<th>col3</th>\n </tr>\n"+
68
- " <tr>\n <td>d</td><td>e</td><td>f</td>\n </tr>\n" +
69
- " <tr>\n <td>g</td><td>h</td><td>i</td>\n </tr>\n" +
70
- "</table>", my_builder.render )
71
-
72
- my_builder.format = :text
73
- assert_equal( "fields: ( col1, col2, col3 )\n" +
74
- "row1: ( d, e, f )\nrow2: ( g, h, i )\n",
75
- my_builder.render )
76
- end
77
-
78
- def test_complete_output
79
- @builder.output_type = :complete
80
- @builder.format = :html
81
- assert_equal( "<html><head><title></title></head><body>\n"+
82
- "<table>\n <tr>\n <th>col1</th><th>col2</th>" +
83
- "<th>col3</th>\n </tr>\n" +
84
- " <tr>\n <td>a</td><td>b</td><td>c</td>\n </tr>\n" +
85
- " <tr>\n <td>d</td><td>e</td><td>f</td>\n </tr>\n" +
86
- "</table></body></html>\n", @builder.render )
87
- end
88
-
89
- def test_header
90
- @builder.header = "This is a sample header"
91
- @builder.format = :csv
92
- assert_equal( "This is a sample header\n\n" +
93
- "col1,col2,col3\na,b,c\nd,e,f\n", @builder.render )
94
- @builder.format = :html
95
- assert_equal( "<table>\n <tr>\n <th colspan=3>"+
96
- "This is a sample header</th>\n </tr>\n"+
97
- " <tr>\n <th>col1</th><th>col2</th>" +
98
- "<th>col3</th>\n </tr>\n" +
99
- " <tr>\n <td>a</td><td>b</td><td>c</td>\n </tr>\n" +
100
- " <tr>\n <td>d</td><td>e</td><td>f</td>\n </tr>\n" +
101
- "</table>", @builder.render )
102
- @builder.format = :text
103
- assert_equal( "This is a sample header\n" +
104
- "fields: ( col1, col2, col3 )\n" +
105
- "row0: ( a, b, c )\n" +
106
- "row1: ( d, e, f )\n", @builder.render )
107
-
108
-
109
- end
110
-
111
- def test_footer
112
- @builder.footer = "This is a sample footer"
113
- @builder.format = :csv
114
- assert_equal( "col1,col2,col3\na,b,c\nd,e,f\n\n"+
115
- "This is a sample footer\n", @builder.render )
116
- @builder.format = :html
117
- assert_equal( "<table>\n <tr>\n <th>col1</th><th>col2</th>" +
118
- "<th>col3</th>\n </tr>\n" +
119
- " <tr>\n <td>a</td><td>b</td><td>c</td>\n </tr>\n" +
120
- " <tr>\n <td>d</td><td>e</td><td>f</td>\n </tr>\n" +
121
- " <tr>\n <th colspan=3>"+
122
- "This is a sample footer</th>\n </tr>\n"+
123
- "</table>", @builder.render )
124
- @builder.format = :text
125
- assert_equal( "fields: ( col1, col2, col3 )\n" +
126
- "row0: ( a, b, c )\n" +
127
- "row1: ( d, e, f )\n" +
128
- "This is a sample footer\n", @builder.render )
129
-
130
- end
131
-
132
- end
133
-
134
-
135
-
data/test/tc_data_set.rb~ DELETED
@@ -1,326 +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, :content => [[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, :content => 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_delete_if
50
- @data.delete_if { |r| r.any? { |e| e.empty? } }
51
- assert_equal([%w[a b c]],@data.to_a)
52
- end
53
-
54
- def test_brackets
55
- row0 = { "col1" => "a", "col2" => "b", "col3" => "c" }
56
- row1 = { "col1" => "d", "col2" => "", "col3" => "e" }
57
- row0.each do |key,value|
58
- assert_equal( value, @data[0][key] )
59
- end
60
- row1.each do |key,value|
61
- assert_equal( value, @data[1][key] )
62
- end
63
- end
64
-
65
- def test_eql?
66
- data2 = DataSet.new
67
- data2.fields = %w[ col1 col2 col3 ]
68
- data2.default = ""
69
- data2 << %w[ a b c ]
70
- data2 << { "col1" => "d", "col3" => "e" }
71
-
72
- #FIXME: This looks like some shady discrete math assignment
73
- assert(@data.eql?(data2) && data2.eql?(@data))
74
- data2 << [2, 3, 4]
75
- assert(!( @data.eql?(data2) || data2.eql?(@data) ))
76
- @data << [2, 3, 4]
77
- assert(@data.eql?(data2) && data2.eql?(@data))
78
- @data << [8, 9, 10]
79
- assert(!( @data.eql?(data2) || data2.eql?(@data) ))
80
- data2 << [8, 9, 10]
81
- assert(@data.eql?(data2) && data2.eql?(@data))
82
- end
83
-
84
- def test_shaped_like?
85
- a = DataSet.new
86
- a.fields = %w[ col1 col2 col3 ]
87
- assert(@data.shaped_like?(a))
88
- assert(a.shaped_like?(@data))
89
- end
90
-
91
- def test_union
92
- a = DataSet.new
93
- a.fields = %w[ col1 col2 col3 ]
94
- a << %w[ a b c ]
95
- a << %w[ x y z ]
96
- b = a | @data
97
- assert_kind_of(DataSet, b)
98
- assert_equal(b.data.length, 3)
99
- assert_equal([ %w[a b c], %w[x y z], ["d","","e"] ], b.to_a)
100
- assert_equal((a | @data), a.union(@data))
101
- end
102
-
103
- def test_difference
104
- a = DataSet.new
105
- a.fields = %w[ col1 col2 col3 ]
106
- a << %w[ a b c ]
107
- a << %w[ x y z ]
108
- b = a - @data
109
- assert_kind_of(DataSet, b)
110
- assert_equal(b.data.length, 1)
111
- assert_equal([ %w[x y z] ], b.to_a)
112
- assert_equal((a - @data), a.difference(@data))
113
- end
114
-
115
- def test_intersection
116
- a = DataSet.new
117
- a.fields = %w[ col1 col2 col3 ]
118
- a << %w[ a b c ]
119
- a << %w[ x y z ]
120
- b = a & @data
121
- assert_kind_of(DataSet, b)
122
- assert_equal(b.data.length, 1)
123
- assert_equal([ %w[a b c] ], b.to_a)
124
- assert_equal((a & @data), a.intersection(@data))
125
- end
126
-
127
- def test_concatenation
128
- a = DataSet.new
129
- a.fields = %w[ col1 col2 col3 ]
130
- a << %w[ x y z ]
131
- newdata = @data.concat(a)
132
- assert_equal([ %w[a b c], ["d","","e"], %w[x y z] ], newdata.to_a)
133
- end
134
-
135
- def test_append_datarow
136
- row = DataRow.new(%w[ x y z ],%w[ col1 col2 col3 ])
137
- @data << row
138
- assert_equal(@data[2], row)
139
- end
140
-
141
- def test_append_datarows
142
- row = DataRow.new(%w[ x y z ],%w[ col1 col2 col3 ])
143
- row2 = DataRow.new(%w[ u v w ], %w[ col1, col2, col3 ])
144
- @data << [row, row2]
145
- assert_equal(@data[2], row)
146
- assert_equal(@data[3], row2)
147
- end
148
-
149
- def test_empty?
150
- a = DataSet.new
151
- a.fields = %w[a b c]
152
- assert_equal(true,a.empty?)
153
- assert_equal(false,@data.empty?)
154
-
155
- a << [1,2,3]
156
- assert_equal(false,a.empty?)
157
- end
158
-
159
- def test_length
160
- assert_equal(2, @data.length)
161
- @data << [1,2,3]
162
- assert_equal(3, @data.length)
163
- end
164
-
165
- def test_load
166
- loaded_data = DataSet.load("test/samples/data.csv")
167
- assert_equal(@data,loaded_data)
168
-
169
- loaded_data = DataSet.load("test/samples/data.csv") do |set,row|
170
- set << row if row.include? 'b'
171
- end
172
- assert_equal([%w[a b c]],loaded_data.to_a)
173
- end
174
-
175
- def test_to_csv
176
- loaded_data = DataSet.load("test/samples/data.csv" )
177
- csv = loaded_data.to_csv
178
- assert_equal("col1,col2,col3\na,b,c\nd,\"\",e\n",csv)
179
- end
180
-
181
- def test_to_html
182
- assert_equal("<table>\n\t\t<tr>\n\t\t\t<th>col1 </th>\n\t\t\t"+
183
- "<th>col2 </th>\n\t\t\t<th>col3</th>\n\t\t</tr>\n"+
184
- "\t\t<tr>\n\t\t\t<td>a</td>\n\t\t\t<td>b</td>\n\t\t\t"+
185
- "<td>c</td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td>d</td>\n\t"+
186
- "\t\t<td>e</td>\n\t\t</tr>\n\t</table>", @data.to_html )
187
- end
188
-
189
- def test_select_columns
190
- assert_equal( [["a"],["d"]],
191
- @data.select_columns("col1").map { |r| r.to_a } )
192
- assert_equal( [["b"],[""]] ,
193
- @data.select_columns("col2").map { |r| r.to_a } )
194
- assert_equal( [["c"],["e"]],
195
- @data.select_columns("col3").map { |r| r.to_a } )
196
- assert_equal( [["a","b","c"],["d","","e"]],
197
- @data.select_columns("col1","col2","col3").map { |r| r.to_a })
198
- assert_equal( [["c","a"],["e","d"]],
199
- @data.select_columns("col3","col1").map { |r| r.to_a } )
200
- end
201
-
202
- def test_select_columns!
203
- a = [[1,2],[3,4]].to_ds(%w[a b])
204
- a.select_columns!(*%w[b a])
205
-
206
- assert_equal(%w[b a],a.fields)
207
- assert_equal([[2,1],[4,3]],a.to_a)
208
-
209
- a.select_columns!('a')
210
-
211
- assert_equal(%w[a], a.fields)
212
- assert_equal([[1],[3]],a.to_a)
213
-
214
- a.select_columns!('a','q')
215
- assert_equal(%w[a q], a.fields)
216
- assert_equal([[1,nil],[3,nil]],a.to_a)
217
-
218
- a[0]['q'] =2
219
- assert_equal([[1,2],[3,nil]],a.to_a)
220
-
221
- end
222
-
223
- def test_as
224
- data = DataSet.new
225
- data.fields = %w[ col1 col2 col3]
226
- data << %w[ a b c]
227
- data << %w[ d e f]
228
- assert_equal("col1,col2,col3\na,b,c\nd,e,f\n",
229
- data.as(:csv) )
230
-
231
- assert_equal("<table>\n\t\t<tr>\n\t\t\t<th>col1 </th>"+
232
- "\n\t\t\t<th>col2 </th>\n\t\t\t<th>col3</th>"+
233
- "\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td>a</td>\n\t\t\t"+
234
- "<td>b</td>\n\t\t\t<td>c</td>\n\t\t</tr>\n\t\t<tr>"+
235
- "\n\t\t\t<td>d</td>\n\t\t\t<td>e</td>\n\t\t\t<td>f</td>"+
236
- "\n\t\t</tr>\n\t</table>", data.as(:html) )
237
- end
238
-
239
- def test_sigma
240
- data = Ruport::DataSet.new(%w[x], :content => [[3],[5],[8],[2]])
241
-
242
- sum = data.sigma { |r| r["x"] if (r["x"] % 2).zero? }
243
- assert_equal(10, sum)
244
-
245
- sum = data.sigma { |r| r["x"] }
246
- assert_equal(18,sum)
247
-
248
- #check alias
249
- sum = data.sum { |r| r["x"] }
250
- assert_equal(18,sum)
251
-
252
- end
253
-
254
- def test_remove_columns
255
- data = @data.remove_columns("col1","col3")
256
- assert_equal(["col2"],data.fields)
257
-
258
- data.remove_columns!("col2")
259
- assert_equal([],data.fields)
260
- assert(data.empty?)
261
- end
262
-
263
- def test_bracket_equals
264
- expected = DataRow.new(%w[apple banana orange], %w[col1 col2 col3])
265
-
266
- raw = [ %w[apple banana orange],
267
- { "col1" => "apple", "col2" => "banana", "col3" => "orange" },
268
- DataRow.new(%w[apple banana orange], %w[col1 col2 col3])
269
- ]
270
- raw.each do |my_row|
271
- @data[1] = my_row
272
- assert_instance_of(DataRow, @data[1])
273
- assert_equal(expected, @data[1])
274
- end
275
-
276
- assert_raise(ArgumentError) { @data[1] = "apple" }
277
- end
278
-
279
- def test_clone
280
- data2 = @data.clone
281
- assert( @data.object_id != data2.object_id )
282
- assert_equal( @data, data2 )
283
- data2 << %w[ f o o ]
284
- assert( @data != data2 )
285
- assert( data2 != @data )
286
- end
287
-
288
- def test_array_hack
289
- assert_nothing_raised {
290
- [ { :a => :b, :c => :d }, { :e => :f, :g => :h } ].to_ds(%w[a e])
291
- }
292
- assert_nothing_raised {
293
- [ [1,2,3], [4,5,6], [7,8,9] ].to_ds(%w[a b c])
294
- }
295
- assert_raise(ArgumentError) {
296
- %w[d e f].to_ds(%w[a b c])
297
- }
298
- assert_raise(ArgumentError) {
299
- [:foo,:bar,:dog].to_ds(%w[tree cat animal])
300
- }
301
- assert_raise(ArgumentError) {
302
- [1,2,3].to_ds(%w[foo bar soup])
303
- }
304
-
305
- assert_equal( DataSet.new(%w[a b], :content => [[1,2],[3,4]]),
306
- [[1,2],[3,4]].to_ds(%w[a b]) )
307
-
308
- assert_equal( DataSet.new(%w[a b], :content => [{ "a" => 1 },{ "b" => 2}]),
309
- [{"a" => 1}, {"b" => 2}].to_ds(%w[a b]) )
310
- assert_equal( DataSet.new(%w[a b], :content => [DataRow.new([1,2],%w[a b])]),
311
- [DataRow.new([1,2],%w[a b])].to_ds(%w[a b]) )
312
-
313
- # FIXME: Decide whether this test should pass or fail.
314
- # assert_equal( DataSet.new(%w[a b], [DataRow.new([1,2],%w[a b])]),
315
- # [DataRow.new([1,2],%w[a q])].to_ds(%w[a b]) )
316
-
317
- end
318
-
319
- def test_append_chain
320
- data2 = DataSet.new(%w[col1 col2 col3])
321
- data2.default=""
322
- data2 << %w[ a b c ] << { "col1" => "d", "col3" => "e" }
323
- assert_equal @data, data2
324
- end
325
-
326
- end