forked from mongoid/mongoid-history
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation_failure_spec.rb
76 lines (62 loc) · 2.05 KB
/
validation_failure_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
require 'spec_helper'
describe Mongoid::History::Tracker do
before :each do
class Element
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::History::Trackable
field :title
field :body
validates :title, presence: true
if Mongoid::Compatibility::Version.mongoid7_or_newer?
has_many :items, dependent: :restrict_with_exception
else
has_many :items, dependent: :restrict
end
track_history on: [:body]
end
class Item
include Mongoid::Document
include Mongoid::Timestamps
belongs_to :element
end
class Prompt < Element
end
class User
include Mongoid::Document
end
end
after :each do
Object.send(:remove_const, :Element)
Object.send(:remove_const, :Item)
Object.send(:remove_const, :Prompt)
Object.send(:remove_const, :User)
end
let(:user) { User.create! }
it 'does not track delete when parent class validation fails' do
prompt = Prompt.new(title: 'first', modifier: user)
expect { prompt.save! }.to change(Tracker, :count).by(1)
expect do
expect { prompt.update_attributes!(title: nil, body: 'one') }
.to raise_error(Mongoid::Errors::Validations)
end.to change(Tracker, :count).by(0)
end
it 'does not track delete when parent class restrict dependency fails' do
prompt = Prompt.new(title: 'first', modifier: user)
prompt.items << Item.new
expect { prompt.save! }.to change(Tracker, :count).by(1)
expect(prompt.version).to eq(1)
expect do
expect { prompt.destroy }.to raise_error(Mongoid::Errors::DeleteRestriction)
end.to change(Tracker, :count).by(0)
end
it 'does not track delete when restrict dependency fails' do
elem = Element.new(title: 'first', modifier: user)
elem.items << Item.new
expect { elem.save! }.to change(Tracker, :count).by(1)
expect(elem.version).to eq(1)
expect do
expect { elem.destroy }.to raise_error(Mongoid::Errors::DeleteRestriction)
end.to change(Tracker, :count).by(0)
end
end