cirandas.net

ref: master

vendor/plugins/action_tracker/test/action_tracker_model_test.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
require 'test_helper'

ActiveRecord::Base.establish_connection({
  :adapter => "sqlite3",
  :database => ":memory:"
})

ActiveRecord::Schema.define do
  create_table :some_table, :force => true do |t|
    t.column :some_column, :integer
  end
  create_table :action_tracker do |t|
    t.belongs_to :user, :polymorphic => true
    t.belongs_to :target, :polymorphic => true
    t.text :params
    t.string :verb
    t.timestamps
  end
end

class SomeModel < ActiveRecord::Base
  self.table_name = :some_table
  acts_as_trackable
end

class ActionTrackerModelTest < ActiveSupport::TestCase

  def setup
    ActionTrackerConfig.verbs = { :some_verb => { :description => "Did something" } }
    @mymodel = SomeModel.create!
    @othermodel = SomeModel.create!
    @tracked_action = ActionTracker::Record.create! :verb => :some_verb, :params => { :user => "foo" }, :user => @mymodel, :target => @othermodel
  end

  def test_has_relationship
    assert @mymodel.respond_to?(:tracked_actions)
  end

  def test_params_is_a_hash
    assert_kind_of Hash, @tracked_action.params
  end

  def test_has_a_polymorphic_relation_with_user
    assert_equal @mymodel.id, @tracked_action.user_id
    assert_equal "SomeModel", @tracked_action.user_type
    assert_equal @mymodel, @tracked_action.user
    assert_equal [@tracked_action], @mymodel.tracked_actions
  end

  def test_has_a_polymorphic_relation_with_target
    assert_equal @othermodel.id, @tracked_action.target_id
    assert_equal "SomeModel", @tracked_action.target_type
    assert_equal @othermodel, @tracked_action.target
  end

  def test_should_stringify_verb_before_validation
    ta = ActionTracker::Record.create! :user => SomeModel.create!, :verb => :some_verb
    assert_equal "some_verb", ta.verb
  end

  def test_verb_is_mandatory
    ta = ActionTracker::Record.new
    ta.valid?
    assert ta.errors.on(:verb)
    assert_raise ActiveRecord::RecordInvalid do
      ta.save!
    end
  end

  def test_user_is_mandatory
    ta = ActionTracker::Record.new :user_type => 'SomeModel', :verb => :some_verb
    ta.valid?
    assert ta.errors.on(:user)
    assert_raise ActiveRecord::RecordInvalid do
      ta.save!
    end

    ta = ActionTracker::Record.new :user_id => 2, :verb => :some_verb
    ta.valid?
    assert ta.errors.on(:user)
    assert_raise ActiveRecord::RecordInvalid do
      ta.save!
    end
  end

  def test_user_exists_indeed
    ta = ActionTracker::Record.new(:verb => :some_verb)
    ta.valid?
    assert ta.errors.on(:user)
    user = SomeModel.create!
    ta.user = user
    assert ta.valid?
    user.destroy
    ta.valid?
    assert ta.errors.on(:user)
  end

  def test_verb_must_be_declared_previously
    ActionTrackerConfig.verbs = { :some_verb => { :description => "Did something" } }
    assert_raise ActiveRecord::RecordInvalid do
      ta = ActionTracker::Record.create! :verb => :undeclared_verb
    end
    ActionTrackerConfig.verbs = { :declared_verb => { :description => "Did something" } }
    assert_nothing_raised do
      ta = ActionTracker::Record.create! :user => SomeModel.create!, :verb => :declared_verb
    end
  end

  def test_update_or_create_create_if_there_is_no_last
    ActionTrackerConfig.verbs = { :some => { :description => "Something", :type => :updatable } }
    ActionTracker::Record.delete_all
    assert_difference "ActionTracker::Record.count" do
      ta = ActionTracker::Record.update_or_create :verb => :some, :user => @mymodel
      ta.save; ta.reload
      assert_kind_of ActionTracker::Record, ta
    end
    assert_equal "some", ActionTracker::Record.last.verb
    assert_equal @mymodel, ActionTracker::Record.last.user
  end

  def test_update_or_create_create_if_timeout
    ActionTrackerConfig.verbs = { :some => { :description => "Something", :type => :updatable } }
    ActionTrackerConfig.timeout = 5.minutes
    ActionTracker::Record.delete_all
    ta = nil
    assert_difference "ActionTracker::Record.count" do
      ta = ActionTracker::Record.update_or_create :verb => :some, :user => @mymodel
      ta.save; ta.reload
    end
    assert_kind_of ActionTracker::Record, ta
    ta.updated_at = Time.now.ago(6.minutes)
    ta.send :update_without_callbacks
    t = ta.reload.updated_at
    assert_difference "ActionTracker::Record.count" do
      ta2 = ActionTracker::Record.update_or_create :verb => :some, :user => @mymodel
      ta2.save; ta2.reload
      assert_kind_of ActionTracker::Record, ta2
    end
    assert_equal t, ta.reload.updated_at
  end

  def test_update_or_create_update_if_no_timeout
    ActionTrackerConfig.verbs = { :some => { :description => "Something", :type => :updatable } }
    ActionTrackerConfig.timeout = 7.minutes
    ActionTracker::Record.delete_all
    ta = nil
    assert_difference "ActionTracker::Record.count" do
      ta = ActionTracker::Record.update_or_create :verb => :some, :user => @mymodel, :params => { :foo => 2 }
      ta.save; ta.reload
    end
    assert_kind_of ActionTracker::Record, ta
    assert_equal 2, ta.get_foo
    ta.updated_at = Time.now.ago(6.minutes)
    ta.send :update_without_callbacks
    t = ta.reload.updated_at
    assert_no_difference "ActionTracker::Record.count" do
      ta2 = ActionTracker::Record.update_or_create :verb => :some, :user => @mymodel, :params => { :foo => 3 }
      ta2.save; ta2.reload
      assert_kind_of ActionTracker::Record, ta2
    end
    assert_not_equal t, ta.reload.updated_at
    assert_equal 3, ta.reload.get_foo
  end

  def test_should_update_or_create_method_create_a_new_tracker_with_different_dispacthers
    ActionTrackerConfig.verbs = { :some => { :description => "Something", :type => :updatable } }
    ActionTracker::Record.delete_all
    ta = nil
    assert_difference "ActionTracker::Record.count" do
      ta = ActionTracker::Record.update_or_create :verb => :some, :user => @mymodel, :target => @mymodel, :params => { :foo => 2 }
      ta.save; ta.reload
    end
    assert_kind_of ActionTracker::Record, ta
    t = ta.reload.updated_at
    sleep(1)
    assert_no_difference "ActionTracker::Record.count" do
      ta2 = ActionTracker::Record.update_or_create :verb => :some, :user => @mymodel, :target => @mymodel, :params => { :foo => 3 }
      ta2.save; ta2.reload
      assert_kind_of ActionTracker::Record, ta2
    end
    assert_equal 3, ta.reload.get_foo
    assert_not_equal t, ta.reload.updated_at

    assert_kind_of ActionTracker::Record, ta
    t = ta.reload.updated_at
    assert_difference "ActionTracker::Record.count" do
      ta2 = ActionTracker::Record.update_or_create :verb => :some, :user => @mymodel, :target => @othermodel, :params => { :foo => 4 }
      ta2.save; ta2.reload
      assert_kind_of ActionTracker::Record, ta2
    end
    assert_equal t, ta.reload.updated_at
    assert_equal 3, ta.reload.get_foo
  end

  def test_add_or_create_create_if_timeout
    ActionTrackerConfig.verbs = { :some => { :description => "Something", :type => :groupable } }
    ActionTrackerConfig.timeout = 5.minutes
    ActionTracker::Record.delete_all
    ta = nil
    assert_difference "ActionTracker::Record.count" do
      ta = ActionTracker::Record.add_or_create :verb => :some, :user => @mymodel, :params => { :foo => "bar" }
      ta.save; ta.reload
    end
    assert_kind_of ActionTracker::Record, ta
    assert_equal ["bar"], ta.reload.params[:foo]
    ta.created_at = Time.now.ago(6.minutes)
    ta.send :update_without_callbacks
    t = ta.reload.updated_at
    assert_difference "ActionTracker::Record.count" do
      ta2 = ActionTracker::Record.add_or_create :verb => :some, :user => @mymodel, :params => { :foo => "test" }
      ta2.save; ta2.reload
      assert_kind_of ActionTracker::Record, ta2
    end
    assert_equal t, ta.reload.updated_at
    assert_equal ["test"], ActionTracker::Record.last.params[:foo]
  end

  def test_add_or_create_update_if_no_timeout
    ActionTrackerConfig.verbs = { :some => { :description => "Something", :type => :updatable } }
    ActionTrackerConfig.timeout = 7.minutes
    ActionTracker::Record.delete_all
    ta = nil
    assert_difference "ActionTracker::Record.count" do
      ta = ActionTracker::Record.add_or_create :verb => :some, :user => @mymodel, :params => { :foo => "test 1", :bar => 2 }
      ta.save; ta.reload
    end
    assert_kind_of ActionTracker::Record, ta
    assert_equal ["test 1"], ta.params[:foo]
    assert_equal [2], ta.params[:bar]
    ta.updated_at = Time.now.ago(6.minutes)
    ta.send :update_without_callbacks
    t = ta.reload.updated_at
    assert_no_difference "ActionTracker::Record.count" do
      ta2 = ActionTracker::Record.add_or_create :verb => :some, :user => @mymodel, :params => { :foo => "test 2", :bar => 1 }
      ta2.save; ta2.reload
      assert_kind_of ActionTracker::Record, ta2
    end
    assert_equal ["test 1", "test 2"], ActionTracker::Record.last.params[:foo]
    assert_equal [2, 1], ActionTracker::Record.last.params[:bar]
    assert_not_equal t, ta.reload.updated_at
    assert_no_difference "ActionTracker::Record.count" do
      ta = ActionTracker::Record.add_or_create :verb => :some, :user => @mymodel, :params => { :foo => "test 1", :bar => 1 }
      ta.save; ta.reload
    end
    assert_equal ["test 1", "test 2", "test 1"], ActionTracker::Record.last.params[:foo]
    assert_equal [2, 1, 1], ActionTracker::Record.last.params[:bar]
  end

  def test_add_or_create_create_if_no_timeout_and_different_target
    ActionTrackerConfig.verbs = { :some => { :description => "Something", :type => :updatable } }
    ActionTrackerConfig.timeout = 7.minutes
    ActionTracker::Record.delete_all
    ta = nil
    assert_difference "ActionTracker::Record.count" do
      ta = ActionTracker::Record.add_or_create :verb => :some, :user => @mymodel, :target => @mymodel, :params => { :foo => "test 1", :bar => 2 }
      ta.save; ta.reload
    end
    assert_kind_of ActionTracker::Record, ta
    assert_equal ["test 1"], ta.params[:foo]
    assert_equal [2], ta.params[:bar]
    ta.updated_at = Time.now.ago(6.minutes)
    ta.send :update_without_callbacks
    t = ta.reload.updated_at
    assert_difference "ActionTracker::Record.count" do
      ta2 = ActionTracker::Record.add_or_create :verb => :some, :user => @mymodel, :target => @othermodel, :params => { :foo => "test 2", :bar => 1 }
      ta2.save; ta2.reload
      assert_kind_of ActionTracker::Record, ta2
    end
    assert_equal ["test 1"], ta.params[:foo]
    assert_equal [2], ta.params[:bar]
  end

  def test_time_spent
    ActionTracker::Record.delete_all
    ActionTrackerConfig.verbs = { :some => { :description => "Something", :type => :updatable } }
    t = ActionTracker::Record.update_or_create :verb => :some, :user => @mymodel
    t.save; t.reload
    t.created_at = t.created_at.ago(2.days)
    t.updated_at = t.created_at.tomorrow
    t.send :update_without_callbacks
    ActionTrackerConfig.timeout = 5.minutes
    t = ActionTracker::Record.update_or_create :verb => :some, :user => @mymodel
    t.save; t.reload
    t.created_at = t.updated_at.ago(2.hours)
    t.send :update_without_callbacks
    assert_equal 2, ActionTracker::Record.count
    assert_equal 26.hours, ActionTracker::Record.time_spent
  end

  def test_duration
    ActionTracker::Record.delete_all
    ActionTrackerConfig.verbs = { :some => { :description => "Something", :type => :updatable } }
    ActionTrackerConfig.timeout = 5.minutes
    t = ActionTracker::Record.update_or_create :verb => :some, :user => @mymodel; t.save
    t = ActionTracker::Record.update_or_create :verb => :some, :user => @mymodel; t.save
    t.reload
    t.created_at = t.updated_at.ago(2.minutes)
    t.send :update_without_callbacks
    assert_equal 1, ActionTracker::Record.count
    assert_equal 2.minutes, t.reload.duration
  end

  def test_describe
    ActionTracker::Record.delete_all
    ActionTrackerConfig.verbs = { :some => { :description => "Who done this is from class {{user.class.to_s}} and half of its value is {{params[:value].to_i/2}}" } }
    ActionTrackerConfig.timeout = 5.minutes
    t = ActionTracker::Record.create! :verb => :some, :user => @mymodel, :params => { :value => "10" }
    assert_equal "Who done this is from class SomeModel and half of its value is 5", t.describe
  end

  def test_description
    ActionTrackerConfig.verbs = { :some => { :description => "Got {{it}}" } }
    t = ActionTracker::Record.create! :user => SomeModel.create!, :verb => :some
    assert_equal "Got {{it}}", t.description
    ActionTrackerConfig.verbs = { :some => nil }
    t = ActionTracker::Record.create! :user => SomeModel.create!, :verb => :some
    assert_equal "", t.description
  end

  def test_subject
    ActionTrackerConfig.verbs = { :some => { :description => "Some" } }
    u = SomeModel.create!
    t = ActionTracker::Record.create! :verb => :some, :user => u
    assert_equal u, t.subject
  end

  def test_predicate
    ActionTrackerConfig.verbs = { :some => { :description => "Some" } }
    t = ActionTracker::Record.create! :user => SomeModel.create!, :verb => :some, :params => nil
    assert_equal({}, t.predicate)
    t = ActionTracker::Record.create! :user => SomeModel.create!, :verb => :some, :params => { :foo => "bar" }
    assert_equal({ :foo => "bar" }, t.predicate)
  end

  def test_phrase
    ActionTrackerConfig.verbs = { :some => { :description => "Some" } }
    u = SomeModel.create!
    t = ActionTracker::Record.create! :verb => :some, :params => { :foo => "bar" }, :user => u
    assert_equal({ :subject => u, :verb => "some", :predicate => { :foo => "bar" }}, t.phrase)
  end

  def test_method_missing
    ActionTrackerConfig.verbs = { :some => { :description => "Some" } }
    t = ActionTracker::Record.create! :user => SomeModel.create!, :verb => :some, :params => { :foo => "test 1", "bar" => "test 2" }
    assert_nil t.get_test
    assert_equal "test 1", t.get_foo
    assert_equal "test 2", t.get_bar
    assert_raise NoMethodError do
      t.another_unknown_method
    end
  end

  def test_collect_group_with_index
    ActionTrackerConfig.verbs = { :some => { :description => "Some" }, :type => :groupable }
    t = ActionTracker::Record.create! :user => SomeModel.create!, :verb => :some, :params => { "test" => ["foo", "bar"] }
    assert_equal(["foo 1", "bar 2"], t.collect_group_with_index(:test){|x, i| "#{x} #{i+1}" })
  end

  def test_recent_filter_actions
    ActionTracker::Record.destroy_all
    t1 = ActionTracker::Record.create!(:user => SomeModel.create!, :verb => :some_verb, :created_at => Time.now)
    t2 = ActionTracker::Record.create!(:user => SomeModel.create!, :verb => :some_verb, :created_at => ActionTracker::Record::RECENT_DELAY.days.ago - 1.day)
    assert_equal [t1], ActionTracker::Record.recent.all
  end
end