elskwid-munger 0.1.4.5 → 0.1.4.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +7 -32
- data/munger.gemspec +44 -34
- data/{spec/munger/render/csv_spec.rb → test/munger/csv_test.rb} +9 -9
- data/test/munger/data_ar_test.rb +194 -0
- data/test/munger/data_test.rb +185 -0
- data/test/munger/html_test.rb +87 -0
- data/test/munger/item_test.rb +43 -0
- data/{spec/munger/data/new_spec.rb → test/munger/new_test.rb} +9 -9
- data/test/munger/render_test.rb +37 -0
- data/test/munger/report_test.rb +156 -0
- data/{spec/munger/render/text_spec.rb → test/munger/text_test.rb} +9 -9
- data/test/test_helper.rb +60 -0
- metadata +88 -50
- data/spec/munger/data_spec.rb +0 -183
- data/spec/munger/item_spec.rb +0 -43
- data/spec/munger/render/html_spec.rb +0 -87
- data/spec/munger/render_spec.rb +0 -38
- data/spec/munger/report_spec.rb +0 -155
- data/spec/spec_helper.rb +0 -107
data/spec/munger/render_spec.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + "/../spec_helper"
|
2
|
-
|
3
|
-
describe Munger::Render do
|
4
|
-
include MungerSpecHelper
|
5
|
-
|
6
|
-
before(:each) do
|
7
|
-
@data = Munger::Data.new(:data => test_data)
|
8
|
-
@report = Munger::Report.new(:data => @data).process
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should render html" do
|
12
|
-
html = Munger::Render.to_html(@report)
|
13
|
-
html.should have_tag('table')
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should render sortable html" do
|
17
|
-
html = Munger::Render.to_sortable_html(@report, :sort => "name", :order => "asc", :url => "test")
|
18
|
-
html.should have_tag("table")
|
19
|
-
html.should have_tag("th.unsorted")
|
20
|
-
html.should have_tag("th.sorted")
|
21
|
-
html.should have_tag("th.sorted-asc")
|
22
|
-
html.should have_tag('a[@href="test?order=asc&sort=score"', /score/)
|
23
|
-
html.should have_tag('a[@href="test?order=desc&sort=name"', /name/)
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should render text" do
|
27
|
-
text = Munger::Render.to_text(@report)
|
28
|
-
text.should_not have_tag('table')
|
29
|
-
text.split("\n").should have_at_least(5).items
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should render xls"
|
33
|
-
|
34
|
-
it "should render csv"
|
35
|
-
|
36
|
-
it "should render pdf"
|
37
|
-
|
38
|
-
end
|
data/spec/munger/report_spec.rb
DELETED
@@ -1,155 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + "/../spec_helper"
|
2
|
-
|
3
|
-
describe Munger::Report do
|
4
|
-
include MungerSpecHelper
|
5
|
-
|
6
|
-
before(:each) do
|
7
|
-
@data = Munger::Data.new(:data => test_data)
|
8
|
-
@report = Munger::Report.new(:data => @data)
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should accept a Munger::Data object" do
|
12
|
-
Munger::Report.new(:data => @data).should be_valid
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should accept a array of hashes" do
|
16
|
-
Munger::Report.new(:data => test_data).should be_valid
|
17
|
-
Munger::Report.new(:data => invalid_test_data).should_not be_valid
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should be able to sort fields by array" do
|
21
|
-
@report.sort = 'name'
|
22
|
-
data = @report.process.process_data
|
23
|
-
data.map { |a| a[:data].name }[0, 4].join(',').should eql('Alice,Alice,Alice,Chaz')
|
24
|
-
|
25
|
-
@report.sort = ['name', 'age']
|
26
|
-
data = @report.process.process_data
|
27
|
-
data.map { |a| a[:data].age }[0, 4].join(',').should eql('33,33,34,28')
|
28
|
-
|
29
|
-
@report.sort = [['name', :asc], ['age', :desc]]
|
30
|
-
data = @report.process.process_data
|
31
|
-
data.map { |a| a[:data].age }[0, 4].join(',').should eql('34,33,33,28')
|
32
|
-
end
|
33
|
-
|
34
|
-
it "should be able to custom sort fields" do
|
35
|
-
@report.sort = [['name', Proc.new {|a, b| a[2] <=> b[2]} ]]
|
36
|
-
data = @report.process.process_data
|
37
|
-
data.map { |a| a[:data].name }[0, 4].join(',').should eql('Chaz,Rich,Alice,Alice')
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should be able to order columns" do
|
41
|
-
@report.columns([:name, :age, :score])
|
42
|
-
@report.columns.should eql([:name, :age, :score])
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should be able to alias column titles" do
|
46
|
-
titles = {:name => 'My Name', :age => 'The Age', :score => 'Super Score'}
|
47
|
-
@report.column_titles = titles
|
48
|
-
@report.column_titles.should eql(titles)
|
49
|
-
end
|
50
|
-
|
51
|
-
it "should default to all columns" do
|
52
|
-
@report.columns.map { |c| c.to_s }.sort.join(',').should eql('age,day,name,score')
|
53
|
-
end
|
54
|
-
|
55
|
-
|
56
|
-
it "should be able to subgroup data" do
|
57
|
-
@report.sort('name').subgroup('name').process
|
58
|
-
@report.get_subgroup_rows.should have(6).items
|
59
|
-
end
|
60
|
-
|
61
|
-
it "should be able to add subgroup headers" do
|
62
|
-
@report.sort('score').subgroup('score', :with_headers => true)
|
63
|
-
@report.aggregate(:sum => :score).process
|
64
|
-
puts Munger::Render.to_text(@report)
|
65
|
-
end
|
66
|
-
|
67
|
-
it "should add the grouping name on the group line somewhere"
|
68
|
-
|
69
|
-
it "should be able to subgroup in multiple dimensions"
|
70
|
-
|
71
|
-
it "should be able to aggregate columns into subgroup rows" do
|
72
|
-
@report.sort('name').subgroup('name').aggregate(:sum => :score).process
|
73
|
-
@report.get_subgroup_rows(1).should have(6).items
|
74
|
-
@report.get_subgroup_rows(0).should have(1).items
|
75
|
-
@report.get_subgroup_rows(0).first[:data][:score].should eql(151)
|
76
|
-
end
|
77
|
-
|
78
|
-
it "should be able to aggregate multiple columns into subgroup rows" do
|
79
|
-
@report.sort('name').subgroup('name').aggregate(:sum => [:score, :age]).process
|
80
|
-
data = @report.get_subgroup_rows(0).first[:data]
|
81
|
-
data[:score].should eql(151)
|
82
|
-
data[:age].should eql(294)
|
83
|
-
|
84
|
-
@report.sort('name').subgroup('name').aggregate(:sum => :score, :average => :age).process
|
85
|
-
data = @report.get_subgroup_rows(0).first[:data]
|
86
|
-
data[:score].should eql(151)
|
87
|
-
data[:age].should eql(29)
|
88
|
-
end
|
89
|
-
|
90
|
-
it "should be able to aggregate with :average" do
|
91
|
-
@report.sort('name').subgroup('name').aggregate(:average => :score).process
|
92
|
-
@report.get_subgroup_rows(0).first[:data][:score].should eql(15)
|
93
|
-
end
|
94
|
-
|
95
|
-
it "should be able to aggregate with :custom" do
|
96
|
-
@report.sort('name').subgroup('name')
|
97
|
-
@report.aggregate(Proc.new { |d| d.inject { |t, a| 2 * (t + a) } } => :score).process
|
98
|
-
@report.get_subgroup_rows(0).first[:data][:score].should eql(19508)
|
99
|
-
end
|
100
|
-
|
101
|
-
it "should be able to style cells" do
|
102
|
-
@report.process
|
103
|
-
@report.style_cells('highlight') { |c, r| c == 32 }
|
104
|
-
styles = @report.process_data.select { |r| r[:meta][:cell_styles] }
|
105
|
-
styles.should have(2).items
|
106
|
-
end
|
107
|
-
|
108
|
-
it "should be able to style cells in certain columns" do
|
109
|
-
@report.process
|
110
|
-
@report.style_cells('highlight', :only => :age) { |c, r| c == 32 }
|
111
|
-
@report.style_cells('big', :except => [:name, :day]) { |c, r| c.size > 2 }
|
112
|
-
styles = @report.process_data.select { |r| r[:meta][:cell_styles] }
|
113
|
-
styles.should have(10).items
|
114
|
-
|
115
|
-
janet = @report.process_data.select { |r| r[:data].name == 'Janet' }.first
|
116
|
-
jstyles = janet[:meta][:cell_styles]
|
117
|
-
|
118
|
-
jstyles[:age].sort.join(',').should eql('big,highlight')
|
119
|
-
jstyles[:score].should eql(["big"])
|
120
|
-
end
|
121
|
-
|
122
|
-
it "should be able to style rows" do
|
123
|
-
@report.process
|
124
|
-
@report.style_rows('over_thirty') { |row| row.age > 29 }
|
125
|
-
@report.style_cells('highlight', :only => :age) { |c, r| c == 32 }
|
126
|
-
|
127
|
-
janet = @report.process_data.select { |r| r[:data].name == 'Janet' }.first[:meta]
|
128
|
-
janet[:row_styles].should eql(["over_thirty"])
|
129
|
-
janet[:cell_styles].should have(1).item
|
130
|
-
janet[:cell_styles][:age].should eql(["highlight"])
|
131
|
-
end
|
132
|
-
|
133
|
-
it "should know when it is processed" do
|
134
|
-
@report.should_not be_processed
|
135
|
-
@report.process
|
136
|
-
@report.should be_processed
|
137
|
-
end
|
138
|
-
|
139
|
-
it "should be able to style columns"
|
140
|
-
|
141
|
-
it "should be able to attach formatting independent of content"
|
142
|
-
# so can format numbers without hurting ability to aggregate correctly
|
143
|
-
# or add hyperlinks using data from columns not being shown
|
144
|
-
|
145
|
-
it "should be able to add and retrieve column formatters" do
|
146
|
-
@report.column_formatters = {:name => :to_s}
|
147
|
-
@report.process
|
148
|
-
@report.column_formatters.should have(1).item
|
149
|
-
@report.column_formatter(:name).should eql(:to_s)
|
150
|
-
end
|
151
|
-
|
152
|
-
it "should be able to aggregate rows into new column"
|
153
|
-
|
154
|
-
|
155
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,107 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + "/../lib/munger")
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
require 'rspec'
|
5
|
-
require 'fileutils'
|
6
|
-
require 'logger'
|
7
|
-
require 'pp'
|
8
|
-
require 'date'
|
9
|
-
|
10
|
-
require 'rspec-hpricot-matchers'
|
11
|
-
RSpec.configure do |config|
|
12
|
-
config.include(HpricotSpec::Matchers)
|
13
|
-
end
|
14
|
-
|
15
|
-
module MungerSpecHelper
|
16
|
-
def test_data
|
17
|
-
[{:name => 'Scott', :age => 23, :day => 1, :score => 12},
|
18
|
-
{:name => 'Chaz', :age => 28, :day => 1, :score => 12},
|
19
|
-
{:name => 'Scott', :age => 23, :day => 2, :score => 1},
|
20
|
-
{:name => 'Janet', :age => 32, :day => 2, :score => 24},
|
21
|
-
{:name => 'Rich', :age => 32, :day => 2, :score => 14},
|
22
|
-
{:name => 'Gordon', :age => 33, :day => 1, :score => 21},
|
23
|
-
{:name => 'Scott', :age => 23, :day => 1, :score => 31},
|
24
|
-
{:name => 'Alice', :age => 33, :day => 1, :score => 12},
|
25
|
-
{:name => 'Alice', :age => 34, :day => 2, :score => 12},
|
26
|
-
{:name => 'Alice', :age => 33, :day => 2, :score => 12}
|
27
|
-
]
|
28
|
-
end
|
29
|
-
|
30
|
-
def more_test_data
|
31
|
-
[{:name => 'David', :age => 40, :day => 1, :score => 12},
|
32
|
-
{:name => 'Michael', :age => 32, :day => 2, :score => 20},
|
33
|
-
{:name => 'David', :age => 40, :day => 2, :score => 13},
|
34
|
-
{:name => 'Michael', :age => 28, :day => 1, :score => 15}]
|
35
|
-
end
|
36
|
-
|
37
|
-
def invalid_test_data
|
38
|
-
['one', 'two', 'three']
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_ar_data
|
42
|
-
test_data.map{|r| ARLike.new(r)}
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
##
|
49
|
-
# rSpec Hash additions.
|
50
|
-
#
|
51
|
-
# From
|
52
|
-
# * http://wincent.com/knowledge-base/Fixtures_considered_harmful%3F
|
53
|
-
# * Neil Rahilly
|
54
|
-
|
55
|
-
class Hash
|
56
|
-
|
57
|
-
##
|
58
|
-
# Filter keys out of a Hash.
|
59
|
-
#
|
60
|
-
# { :a => 1, :b => 2, :c => 3 }.except(:a)
|
61
|
-
# => { :b => 2, :c => 3 }
|
62
|
-
|
63
|
-
def except(*keys)
|
64
|
-
self.reject { |k,v| keys.include?(k || k.to_sym) }
|
65
|
-
end
|
66
|
-
|
67
|
-
##
|
68
|
-
# Override some keys.
|
69
|
-
#
|
70
|
-
# { :a => 1, :b => 2, :c => 3 }.with(:a => 4)
|
71
|
-
# => { :a => 4, :b => 2, :c => 3 }
|
72
|
-
|
73
|
-
def with(overrides = {})
|
74
|
-
self.merge overrides
|
75
|
-
end
|
76
|
-
|
77
|
-
##
|
78
|
-
# Returns a Hash with only the pairs identified by +keys+.
|
79
|
-
#
|
80
|
-
# { :a => 1, :b => 2, :c => 3 }.only(:a)
|
81
|
-
# => { :a => 1 }
|
82
|
-
|
83
|
-
def only(*keys)
|
84
|
-
self.reject { |k,v| !keys.include?(k || k.to_sym) }
|
85
|
-
end
|
86
|
-
|
87
|
-
end
|
88
|
-
|
89
|
-
##
|
90
|
-
# Gives us a hash that acts like an ActiveRecord dataset (sort of)
|
91
|
-
#
|
92
|
-
class ARLike
|
93
|
-
|
94
|
-
attr_accessor :attributes
|
95
|
-
|
96
|
-
def initialize(attributes)
|
97
|
-
@attributes = attributes
|
98
|
-
end
|
99
|
-
|
100
|
-
def [](key)
|
101
|
-
attributes[key]
|
102
|
-
end
|
103
|
-
|
104
|
-
def []=(key, value)
|
105
|
-
attributes[key] = value
|
106
|
-
end
|
107
|
-
end
|