Skip to content

Commit c838f86

Browse files
committed
恢复 BaseModel 和 SoftDelete 的测试用例;
1 parent 4475cd9 commit c838f86

File tree

4 files changed

+109
-3
lines changed

4 files changed

+109
-3
lines changed

app/models/concerns/base_model.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module BaseModel
33

44
included do
55
scope :recent, -> { order(id: :desc) }
6-
scope :exclude_ids, ->(ids) { where(:id.nin => ids.map(&:to_i)) }
6+
scope :exclude_ids, ->(ids) { where.not(id: ids.map(&:to_i)) }
77
scope :by_week, -> { where("created_at > ?", 7.days.ago.utc) }
88

99
delegate :url_helpers, to: 'Rails.application.routes'

app/models/concerns/soft_delete.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ module SoftDelete
1010
def destroy
1111
run_callbacks(:destroy) do
1212
if persisted?
13-
update_attribute(:deleted_at, Time.now.utc)
14-
update_attribute(:updated_at, Time.now.utc)
13+
update_attributes(deleted_at: Time.now.utc, updated_at: Time.now.utc)
1514
end
1615

1716
@destroyed = true
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'rails_helper'
2+
3+
describe BaseModel, type: :model do
4+
ActiveRecord::Base.connection.create_table(:monkeys, force: true) do |t|
5+
t.string :name
6+
t.timestamps null: false
7+
end
8+
9+
class Monkey < ActiveRecord::Base
10+
include BaseModel
11+
end
12+
13+
after(:each) do
14+
Monkey.delete_all
15+
end
16+
17+
it 'should have recent scope method' do
18+
monkey = Monkey.create(name: 'Caesar', id: 1)
19+
ghost = Monkey.create(name: 'Wukong', id: 2)
20+
21+
expect(Monkey.recent.to_a).to eq([ghost, monkey])
22+
end
23+
24+
it 'should have exclude_ids scope method' do
25+
ids = Array(1..10)
26+
ids.each { |i| Monkey.create(name: "entry##{i}", id: i) }
27+
28+
result1 = Monkey.exclude_ids(ids.to(4).map(&:to_s)).map(&:name)
29+
result2 = Monkey.exclude_ids(ids.from(5)).map(&:name)
30+
31+
expect(result1).to eq(ids.from(5).map { |i| "entry##{i}" })
32+
expect(result2).to eq(ids.to(4).map { |i| "entry##{i}" })
33+
end
34+
35+
it 'should have find_by_id class methods' do
36+
monkey = Monkey.create(name: 'monkey', id: 1)
37+
expect(Monkey.find_by_id(1)).to eq(monkey)
38+
expect(Monkey.find_by_id('1')).to eq(monkey)
39+
expect(Monkey.find_by_id(2)).to be_nil
40+
end
41+
42+
it 'should have by_week method' do
43+
Monkey.create(name: 'Caesar', created_at: 2.weeks.ago.utc)
44+
Monkey.create(name: 'Caesar1', created_at: 3.days.ago.utc)
45+
Monkey.create(name: 'Caesar1', created_at: Time.now.utc)
46+
expect(Monkey.by_week.count).to eq(2)
47+
end
48+
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require 'rails_helper'
2+
3+
describe SoftDelete, type: :model do
4+
ActiveRecord::Base.connection.create_table(:walking_deads, force: true) do |t|
5+
t.string :name
6+
t.string :tag
7+
t.datetime :deleted_at
8+
t.timestamps null: false
9+
end
10+
11+
class WalkingDead < ActiveRecord::Base
12+
include BaseModel
13+
include SoftDelete
14+
15+
after_destroy do
16+
self.tag = "after_destroy #{name}"
17+
end
18+
end
19+
20+
let!(:rick) { WalkingDead.create! name: 'Rick Grimes' }
21+
22+
it 'should affect default count' do
23+
expect do
24+
rick.destroy
25+
end.to change(WalkingDead, :count).by(-1)
26+
end
27+
28+
it 'should not affect unscoped count' do
29+
expect do
30+
rick.destroy
31+
end.to_not change(WalkingDead.unscoped, :count)
32+
end
33+
34+
it 'should update the deleted_at field' do
35+
expect do
36+
rick.destroy
37+
end.to change { rick.deleted_at }.from(nil)
38+
end
39+
40+
it 'should use deleted?' do
41+
expect do
42+
rick.destroy
43+
end.to change { rick.deleted? }.from(false).to(true)
44+
end
45+
46+
it 'should mark as destroyed and get proper query result' do
47+
rick.destroy
48+
expect(rick).to be_destroyed
49+
50+
expect(WalkingDead.where(name: rick.name).count).to eq(0)
51+
expect(WalkingDead.unscoped.where(name: rick.name).first).to eq(rick)
52+
end
53+
54+
it 'is run callback after destroy' do
55+
rick.name = 'foo'
56+
rick.destroy
57+
expect(rick.tag).to eq('after_destroy foo')
58+
end
59+
end

0 commit comments

Comments
 (0)