trackoid_mongoid4 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,257 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class Test
4
+ include Mongoid::Document
5
+ include Mongoid::Tracking
6
+
7
+ field :name # Dummy field
8
+ track :visits
9
+ end
10
+
11
+ describe Mongoid::Tracking do
12
+ #mongoid 4 has removed the allow allow_dynamic_fields setting now set on a dynamic basis
13
+
14
+ #it "should ensure allow_dynamic_fields option is turned off" do
15
+ # Mongoid::Config.settings[:allow_dynamic_fields].should == false
16
+ #end
17
+
18
+ it "should raise error when used in a class not of class Mongoid::Document" do
19
+ -> {
20
+ class NotMongoidClass
21
+ include Mongoid::Tracking
22
+ track :something
23
+ end
24
+ }.should raise_error Mongoid::Tracking::Errors::NotMongoid
25
+ end
26
+
27
+ it "should not raise error when used in a class of class Mongoid::Document" do
28
+ -> {
29
+ class MongoidedDocument
30
+ include Mongoid::Document
31
+ include Mongoid::Tracking
32
+ track :something
33
+ end
34
+ }.should_not raise_error
35
+ end
36
+
37
+ it "should not raise errors when using to/as_json" do
38
+ test = Test.new(:name => "Trackoid")
39
+ json_as = {}
40
+ json_to = ""
41
+
42
+ -> {
43
+ json_as = test.as_json(:except => :_id)
44
+ json_to = test.to_json(:except => :_id)
45
+ }.should_not raise_error
46
+
47
+ json_as.should == { "name" => "Trackoid", "visits_data" => {}}
48
+ #json_to.should == "{\"name\":\"Trackoid\", " "\"visits_data\":{}}"
49
+ end
50
+
51
+ describe "when creating a new field with stats" do
52
+ before(:all) do
53
+ @mock = Test.new
54
+ end
55
+
56
+ it "should create a method for accesing the stats" do
57
+ @mock.respond_to?(:visits).should be true
58
+ end
59
+
60
+ it "should NOT create an index for the stats field" do
61
+ @mock.class.index_specifications.should_not include(:visits_data)
62
+ end
63
+
64
+ it "should create a method for accesing the stats of the proper class" do
65
+ @mock.visits.class.should == Mongoid::Tracking::Tracker
66
+ end
67
+
68
+ it "should create an array in the class with all tracking fields" do
69
+ @mock.class.tracked_fields.should == [ :visits ]
70
+ end
71
+
72
+ it "should create an array in the class with all tracking fields even when monkey patching" do
73
+ class Test
74
+ track :something_else
75
+ end
76
+ @mock.class.tracked_fields.should == [ :visits, :something_else ]
77
+ end
78
+
79
+ it "should not update stats when new record" do
80
+ -> { @mock.visits.inc }.should raise_error Mongoid::Tracking::Errors::ModelNotSaved
81
+ end
82
+
83
+ it "should create an empty hash as the internal representation" do
84
+ @mock.visits.send(:_original_hash).should == {}
85
+ end
86
+
87
+ it "should give 0 for today stats" do
88
+ @mock.visits.today.should == 0
89
+ end
90
+
91
+ it "should give 0 for last 7 days stats" do
92
+ @mock.visits.last_days.should == [0, 0, 0, 0, 0, 0, 0]
93
+ end
94
+
95
+ it "should give today stats for last 0 days stats" do
96
+ @mock.visits.last_days(0).should == [@mock.visits.today]
97
+ end
98
+
99
+ # it "should not be aggregated" do
100
+ # @mock.aggregated?.should be_false
101
+ # end
102
+ end
103
+
104
+ describe "when using a model in the database" do
105
+ let(:test) { Test.create(:name => "test") }
106
+
107
+ before do
108
+ @today = Time.now
109
+ end
110
+
111
+ it "should increment visits stats for today" do
112
+ test.visits.inc
113
+ test.visits.today.should == 1
114
+ end
115
+
116
+ it "should increment another visits stats for today for a total of 2" do
117
+ test.visits.inc
118
+ test.visits.inc
119
+ test.visits.today.should == 2
120
+ end
121
+
122
+ it "should also work for yesterday" do
123
+ test.visits.inc(@today - 1.day)
124
+ test.visits.yesterday.should == 1
125
+ end
126
+
127
+ it "should also work for yesterday if adding another visit (for a total of 2)" do
128
+ test.visits.inc(@today - 1.day)
129
+ test.visits.inc(@today - 1.day)
130
+ test.visits.yesterday.should == 2
131
+ end
132
+
133
+ it "then, the visits of today + yesterday must be the same" do
134
+ test.visits.inc
135
+ test.visits.inc
136
+ test.visits.inc(@today - 1.day)
137
+ test.visits.inc(@today - 1.day)
138
+ test.visits.last_days(2).should == [2, 2]
139
+ end
140
+
141
+ it "should have 4 visits for this test" do
142
+ test.visits.inc
143
+ test.visits.inc
144
+ test.visits.inc(@today - 1.day)
145
+ test.visits.inc(@today - 1.day)
146
+ test.visits.last_days(2).sum.should == 4
147
+ end
148
+
149
+ it "should correctly handle the 7 days" do
150
+ test.visits.inc
151
+ test.visits.inc
152
+ test.visits.inc(@today - 1.day)
153
+ test.visits.inc(@today - 1.day)
154
+ test.visits.last_days.should == [0, 0, 0, 0, 0, 2, 2]
155
+ end
156
+
157
+ it "string dates should work" do
158
+ test.visits.inc("2010-07-11")
159
+ test.visits.on("2010-07-11").should == 1
160
+ end
161
+
162
+ it "should give the first date with first_date" do
163
+ test.visits.inc("2010-07-11")
164
+ t = Time.parse("2010-07-11")
165
+ f = test.visits.first_date
166
+ [f.year, f.month, f.day, f.hour].should == [t.year, t.month, t.day, t.hour]
167
+ end
168
+
169
+ it "should give the last date with last_date" do
170
+ future = @today + 1.month
171
+ test.visits.set(22, future)
172
+ f = test.visits.last_date
173
+ [f.year, f.month, f.day, f.hour].should == [future.year, future.month, future.day, future.hour]
174
+ end
175
+
176
+ it "should give the first value" do
177
+ test.visits.inc("2010-07-11")
178
+ test.visits.first_value.should == 1
179
+ end
180
+
181
+ it "should give the last value" do
182
+ future = @today + 1.month
183
+ test.visits.set(22, future)
184
+ test.visits.last_value.should == 22
185
+ end
186
+ end
187
+
188
+ context "testing reader operations without reloading models" do
189
+ let(:test) { Test.create(name: "test") }
190
+ let(:object_id) { test.id }
191
+
192
+ it "'set' operator must work" do
193
+ test.visits.set(5)
194
+ test.visits.today.should == 5
195
+ Test.find(object_id).visits.today.should == 5
196
+ end
197
+
198
+ it "'set' operator must work on arbitrary days" do
199
+ test.visits.set(5, Time.parse("2010-05-01"))
200
+ test.visits.on(Time.parse("2010-05-01")).should == 5
201
+ Test.find(object_id).visits.on(Time.parse("2010-05-01")).should == 5
202
+ end
203
+
204
+ it "'add' operator must work" do
205
+ test.visits.set(5)
206
+ test.visits.add(5)
207
+ test.visits.today.should == 10
208
+ Test.find(object_id).visits.today.should == 10
209
+ end
210
+
211
+ it "'add' operator must work on arbitrary days" do
212
+ test.visits.set(5, Time.parse("2010-05-01"))
213
+ test.visits.add(5, Time.parse("2010-05-01"))
214
+ test.visits.on(Time.parse("2010-05-01")).should == 10
215
+ Test.find(object_id).visits.on(Time.parse("2010-05-01")).should == 10
216
+ end
217
+
218
+ it "on() accessor must work on dates as String" do
219
+ test.visits.set(5, Time.parse("2010-05-01"))
220
+ test.visits.add(5, Time.parse("2010-05-01"))
221
+ test.visits.on("2010-05-01").should == 10
222
+ end
223
+
224
+ it "on() accessor must work on Date descendants" do
225
+ test.visits.set(5, Time.parse("2010-05-01"))
226
+ test.visits.add(5, Time.parse("2010-05-01"))
227
+ test.visits.on(Date.parse("2010-05-01")).should == 10
228
+ end
229
+
230
+ it "on() accessor must work on dates as Ranges" do
231
+ test.visits.set(5, Time.parse("2010-05-01"))
232
+ test.visits.add(5, Time.parse("2010-05-01"))
233
+ test.visits.on(Time.parse("2010-04-30")..Time.parse("2010-05-02")).should == [0, 10, 0]
234
+ end
235
+ end
236
+
237
+ context "regression test for github issues" do
238
+ it "should not raise undefined method [] for nil:NilClass when adding a new track into an existing object" do
239
+ class TestModel
240
+ include Mongoid::Document
241
+ include Mongoid::Tracking
242
+ field :name
243
+ end
244
+ TestModel.delete_all
245
+ TestModel.create(:name => "dummy")
246
+
247
+ class TestModel
248
+ track :something
249
+ end
250
+ tm = TestModel.first
251
+ expect(tm.something.today).to eq 0
252
+ tm.something.inc
253
+ tm.reload
254
+ expect(tm.something.today).to eq 1
255
+ end
256
+ end
257
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ require "trackoid_mongoid4/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "trackoid_mongoid4"
8
+ s.version = Trackoid::VERSION
9
+ s.licenses = ['MIT']
10
+ s.platform = Gem::Platform::RUBY
11
+ s.authors = ["David Krett"]
12
+ s.email = "david@w2d.asia"
13
+ s.homepage = "http://github.com/dbkbali/trackoid_mongoid4"
14
+ s.summary = "Trackoid is an easy scalable analytics tracker using MongoDB and Mongoid - compatible with Mongoid 4.0"
15
+ s.description = "Trackoid uses an embeddable approach to track analytics data using the poweful features of MongoDB for scalability for Mongoid 4 and above"
16
+ s.require_paths = ["lib"]
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.add_runtime_dependency 'mongoid', '~> 4.0'
21
+ s.add_development_dependency 'rake'
22
+ s.add_development_dependency 'rspec','~> 3.0'
23
+ s.add_development_dependency 'mocha', '~> 0'
24
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trackoid_mongoid4
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - David Krett
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongoid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Trackoid uses an embeddable approach to track analytics data using the
70
+ poweful features of MongoDB for scalability for Mongoid 4 and above
71
+ email: david@w2d.asia
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".document"
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE
82
+ - README.md
83
+ - Rakefile
84
+ - config/mongoid.yml
85
+ - lib/mongoid/tracking.rb
86
+ - lib/mongoid/tracking/aggregates.rb
87
+ - lib/mongoid/tracking/core_ext.rb
88
+ - lib/mongoid/tracking/core_ext/range.rb
89
+ - lib/mongoid/tracking/core_ext/time.rb
90
+ - lib/mongoid/tracking/errors.rb
91
+ - lib/mongoid/tracking/reader_extender.rb
92
+ - lib/mongoid/tracking/readers.rb
93
+ - lib/mongoid/tracking/tracker.rb
94
+ - lib/mongoid/tracking/tracker_aggregates.rb
95
+ - lib/trackoid_mongoid4.rb
96
+ - lib/trackoid_mongoid4/version.rb
97
+ - spec/aggregates_spec.rb
98
+ - spec/embedded_spec.rb
99
+ - spec/ext/range_spec.rb
100
+ - spec/ext/time_spec.rb
101
+ - spec/reader_extender_spec.rb
102
+ - spec/readers_spec.rb
103
+ - spec/spec.opts
104
+ - spec/spec_helper.rb
105
+ - spec/timezone_spec.rb
106
+ - spec/trackoid_mongoid4_spec.rb
107
+ - trackoid_mongoid4.gemspec
108
+ homepage: http://github.com/dbkbali/trackoid_mongoid4
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.4.5
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Trackoid is an easy scalable analytics tracker using MongoDB and Mongoid
132
+ - compatible with Mongoid 4.0
133
+ test_files:
134
+ - spec/aggregates_spec.rb
135
+ - spec/embedded_spec.rb
136
+ - spec/ext/range_spec.rb
137
+ - spec/ext/time_spec.rb
138
+ - spec/reader_extender_spec.rb
139
+ - spec/readers_spec.rb
140
+ - spec/spec.opts
141
+ - spec/spec_helper.rb
142
+ - spec/timezone_spec.rb
143
+ - spec/trackoid_mongoid4_spec.rb