jzimmek-reportme 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.10
1
+ 0.0.11
@@ -12,10 +12,10 @@ module Reportme
12
12
  @source = block
13
13
  end
14
14
 
15
- def periods(*args)
16
- unless args.blank?
15
+ def periods(wanted_periods=[])
16
+ unless wanted_periods.blank?
17
17
  @periods.clear
18
- args.each do |period|
18
+ wanted_periods.each do |period|
19
19
  @periods << period
20
20
  end
21
21
  end
@@ -17,14 +17,23 @@ class ReportmeTest < Test::Unit::TestCase
17
17
  SQL
18
18
  end
19
19
 
20
- def create_visit_report_factory
20
+ def create_visit_report_factory(opts={})
21
21
 
22
- @rme = Reportme::ReportFactory.create do
22
+ defaults = {
23
+ :periods => [],
24
+ :since => DateTime.now
25
+ }
26
+
27
+ opts = defaults.merge(opts)
28
+
29
+ @rme = Reportme::ReportFactory.create opts[:since] do
23
30
  report :visits do
31
+ periods opts[:periods]
24
32
  source do |von, bis|
25
33
  <<-SQL
26
34
  select
27
- date('#{von}') as datum,
35
+ '#{von}' as von,
36
+ date(created_at) as datum,
28
37
  channel,
29
38
  count(1) as cnt
30
39
  from
@@ -38,7 +47,6 @@ class ReportmeTest < Test::Unit::TestCase
38
47
  end
39
48
  end
40
49
  end
41
-
42
50
  @rme
43
51
  end
44
52
 
@@ -51,8 +59,10 @@ class ReportmeTest < Test::Unit::TestCase
51
59
  end
52
60
 
53
61
  def teardown
54
- @rme.reset
55
- exec("truncate visits;");
62
+ unless @debug
63
+ @rme.reset if @rme
64
+ exec("truncate visits;");
65
+ end
56
66
  end
57
67
 
58
68
  should "create one visitor in the today report for channel sem" do
@@ -85,11 +95,75 @@ class ReportmeTest < Test::Unit::TestCase
85
95
  exec("insert into visits values (null, 'seo', date_sub(curdate(), interval 1 day));");
86
96
  exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 1 day));");
87
97
  exec("insert into visits values (null, 'seo', date_sub(curdate(), interval 1 day));");
88
- create_visit_report_factory.run
98
+ create_visit_report_factory(:periods => [:today, :day]).run
89
99
  assert_equal 2, one("select cnt from visits_day where channel = 'seo' and datum = date_sub(curdate(), interval 1 day)")["cnt"].to_i
90
100
  assert_equal 3, one("select cnt from visits_day where channel = 'sem' and datum = date_sub(curdate(), interval 1 day)")["cnt"].to_i
91
101
  end
92
102
 
103
+ should "report a week as 7 days since yesterday ignoring days before or after this" do
104
+
105
+ # today should be ignored
106
+ exec("insert into visits values (null, 'sem', curdate());");
107
+
108
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 1 day));");
109
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 2 day));");
110
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 3 day));");
111
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 4 day));");
112
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 5 day));");
113
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 6 day));");
114
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 7 day));");
115
+
116
+ # 8 days ago should be ignored
117
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 8 day));");
118
+
119
+ create_visit_report_factory(:periods => [:week]).run
120
+ assert_equal 7, one("select count(1) as cnt from visits_week where channel = 'sem' and von = date_sub(curdate(), interval 7 day)")["cnt"].to_i
121
+ end
122
+
123
+ should "create a daily report for the previous 3 days" do
124
+
125
+ #should be ignored
126
+ exec("insert into visits values (null, 'sem', curdate());");
127
+
128
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 1 day));");
129
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 2 day));");
130
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 3 day));");
131
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 4 day));");
132
+
133
+ # should be ignored
134
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 5 day));");
135
+
136
+ create_visit_report_factory(:since => 3.days.ago,:periods => [:day]).run
137
+ assert_equal 4, one("select count(1) as cnt from visits_day where von between date_sub(curdate(), interval 4 day) and date_sub(curdate(), interval 1 day)")["cnt"].to_i
138
+ end
139
+
140
+ should "create the weekly report by using 7 daily reports" do
141
+
142
+ # should be ignored in weekly
143
+ exec("insert into visits values (null, 'sem', curdate());");
144
+
145
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 1 day));");
146
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 2 day));");
147
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 3 day));");
148
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 4 day));");
149
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 5 day));");
150
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 6 day));");
151
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 7 day));");
152
+
153
+ # should be ignored in weekly
154
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 8 day));");
155
+ # should be ignored in weekly
156
+ exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 9 day));");
157
+
158
+ create_visit_report_factory(:since => 10.days.ago,:periods => [:day]).run
159
+
160
+ exec("truncate visits;")
161
+
162
+ create_visit_report_factory(:periods => [:week]).run
163
+
164
+ assert_equal 7, one("select count(1) as cnt from visits_week where von between date_sub(curdate(), interval 7 day) and date_sub(curdate(), interval 1 day)")["cnt"].to_i
165
+ end
166
+
93
167
  # should "handle today and day periods but nothing else" do
94
168
  # end
95
169
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jzimmek-reportme
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Zimmek