forked from civiccc/modesty
/
metric_spec.rb
163 lines (136 loc) · 4.51 KB
/
metric_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
require 'modesty'
describe Modesty::Metric, "Creating Metrics" do
before :all do
Modesty.set_store :redis, :mock => true
end
before :each do
Modesty.metrics.clear
end
it "can create a metric without a block" do
m = Modesty.new_metric(:foo)
m.slug.should == :foo
Modesty.metrics[:foo].should == m
end
it "can create a metric with a block" do
m = Modesty.new_metric :foo do |m|
m.description "Foo"
end
m.slug.should == :foo
m.description.should == "Foo"
Modesty.metrics[:foo].should == m
end
it "can create submetrics" do
Modesty.new_metric :foo do |foo|
foo.description "Foo"
foo.submetric :bar do |bar|
bar.description "Bar"
bar.submetric :baz do |baz|
baz.description "Baz"
end
end
end
Modesty.metrics.should include :foo
Modesty.metrics.should include :foo/:bar
Modesty.metrics.should include :foo/:bar/:baz
Modesty.metrics[:foo].parent.should == nil
Modesty.metrics[:foo/:bar].parent.should_not == nil
Modesty.metrics[:foo/:bar].parent.slug.should == :foo
Modesty.metrics[:foo/:bar/:baz].parent.should_not == nil
Modesty.metrics[:foo/:bar/:baz].parent.slug.should == :foo/:bar
end
end
describe Modesty::Metric, "Tracking Metrics" do
before :each do
Modesty.data.flushdb
end
before :all do
Modesty.set_store :redis, :mock => true
Modesty.metrics.clear
Modesty.new_metric :foo do |foo|
foo.description "Foo"
foo.submetric :bar do |bar|
bar.description "Bar"
bar.submetric :baz do |baz|
baz.description "Baz"
end
end
end
Modesty.data.flushdb
end
it "can track a metric" do
(1..100).each do |i|
lambda { Modesty.track! :foo }.should_not raise_error
Modesty.metrics[:foo].count.to_i.should == i
end
end
it "can track a metric with a count" do
(1..100).each do |i|
lambda { Modesty.track! :foo, 3 }.should_not raise_error
Modesty.metrics[:foo].count.to_i.should == i*3
end
end
it "can fetch a metric for a date" do
now = Time.now
Time.stub!(:now).and_return(now-1.day)
25.times {|i| Modesty.track! :foo}
Modesty.metrics[:foo].count((now-1.day).to_date).should == 25
end
it "can fetch a metric over a date range" do
now = Time.now
Time.stub!(:now).and_return(now-1.day)
25.times {|i| Modesty.track! :foo}
Time.stub!(:now).and_return(now)
50.times {|i| Modesty.track! :foo}
Modesty.metrics[:foo].count((now-1.day).to_date..(now.to_date)).should == [25, 50]
end
it "can fetch a metric over a date range as array" do
now = Time.now
Time.stub!(:now).and_return(now-1.day)
25.times {|i| Modesty.track! :foo}
Time.stub!(:now).and_return(now)
50.times {|i| Modesty.track! :foo}
Modesty.metrics[:foo].count((now-1.day).to_date, now.to_date).should == [25, 50]
end
it "can track one submetric" do
(1..100).each do |i|
lambda { Modesty.track! :foo/:bar }.should_not raise_error
Modesty.metrics[:foo].count.to_i.should == i
Modesty.metrics[:foo/:bar].count.to_i.should == i
Modesty.metrics[:foo/:bar/:baz].count.to_i.should == 0
end
end
it "can track more than one submetric" do
(1..100).each do |i|
lambda { Modesty.track! :foo/:bar/:baz }.should_not raise_error
Modesty.metrics[:foo].count.to_i.should == i
Modesty.metrics[:foo/:bar].count.to_i.should == i
Modesty.metrics[:foo/:bar/:baz].count.to_i.should == i
end
end
it "raises Modesty::NoMetricError if it can't find your metric" do
lambda { Modesty.track! :oh_noes }.should raise_error Modesty::Metric::Error
end
it "can track with custom data" do
m = Modesty.metrics[:foo/:bar]
lambda do
Modesty.track! :foo/:bar, :with => {:zing => 56}
end.should_not raise_error
m.unique(:zings).should == 1
m.all(:zings).should include 56
lambda do
Modesty.track! :foo/:bar, :with => {:zing => 97}, :count => 4
end.should_not raise_error
m.unique(:zings).should == 2
m.all(:zings).should include 97
lambda do
Modesty.track! :foo/:bar, 7, :with => {:zing => 97}
end.should_not raise_error
m.unique(:zings).should == 2
m.all(:zings).count.should == 2
m.unique(:zings, Date.parse('1/1/2002')).should == 0
m.unique(:zings, :all).should == 2
m.distribution_by(:zings).should == {56=>{1=>1}, 97=>{7=>1, 4=>1}}
m.distribution.should == {1 => 1, 7 => 1, 4 => 1}
m.distribution(:all).should == {1 => 1, 7 => 1, 4 => 1}
end
end