forked from datamapper/dm-validations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuniqueness_validator_spec.rb
More file actions
124 lines (95 loc) · 3.79 KB
/
Copy pathuniqueness_validator_spec.rb
File metadata and controls
124 lines (95 loc) · 3.79 KB
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
require 'spec_helper'
require 'integration/uniqueness_validator/spec_helper'
describe 'uniqueness_validator/uniqueness_validator_spec' do
describe 'DataMapper::Validations::Fixtures::Department' do
before :all do
DataMapper::Validations::Fixtures::Department.destroy!
DataMapper::Validations::Fixtures::Department.create(:name => "HR").should be_saved
end
describe "with unique name" do
before do
@model = DataMapper::Validations::Fixtures::Department.new(:name => "R & D")
end
it_should_behave_like "valid model"
end
describe "with a duplicate name" do
before do
@model = DataMapper::Validations::Fixtures::Department.new(:name => "HR")
end
it_should_behave_like "invalid model"
end
describe "with duplicate name in a child class" do
before do
@model = DataMapper::Validations::Fixtures::BigDepartment.new(:name => "HR")
end
it_should_behave_like "invalid model"
end
end
describe 'DataMapper::Validations::Fixtures::Organisation' do
before :all do
DataMapper::Validations::Fixtures::Organisation.destroy!
@model = DataMapper.repository do
DataMapper::Validations::Fixtures::Organisation.create(:name => 'Apple', :domain => 'apple.com')
end
end
describe "with missing domain" do
before do
@model.domain = nil
end
it_should_behave_like "valid model"
end
describe "with a duplicate domain" do
before do
@model = DataMapper::Validations::Fixtures::Organisation.new(:name => 'Fake Apple', :domain => 'apple.com')
end
it_should_behave_like "invalid model"
it "has a meaningful error message" do
@model.valid?
@model.errors.on(:domain).should == [ 'Domain is already taken' ]
end
end
it "shouldn't fail on itself when checking for records with identical fields" do
@model.name = "Steve Job's Pony Express"
@model.should be_valid
end
end
describe 'DataMapper::Validations::Fixtures::User' do
before :all do
DataMapper::Validations::Fixtures::Organisation.destroy!
DataMapper::Validations::Fixtures::Department.destroy!
DataMapper::Validations::Fixtures::User.destroy!
DataMapper.repository do
@organization = DataMapper::Validations::Fixtures::Organisation.create(:name => 'Org 101', :domain => '101')
@dept = DataMapper::Validations::Fixtures::Department.create(:name => 'accounting')
@user = DataMapper::Validations::Fixtures::User.create(:organisation => @organization, :user_name => 'guy', :department => @dept)
@organization.should be_saved
@dept.should be_saved
@user.should be_saved
end
end
describe "with username not valid across the organization" do
before do
@model = DataMapper::Validations::Fixtures::User.new(:organisation => @organization, :user_name => 'guy')
end
it "is not valid for signing up" do
@model.should_not be_valid_for_signing_up_for_organization_account
end
it "has a meaningful error message" do
@model.valid?(:signing_up_for_organization_account)
@model.errors.on(:user_name).should == [ 'User name is already taken' ]
end
end
describe "with username not valid across the department" do
before do
@model = DataMapper::Validations::Fixtures::User.new(:user_name => 'guy', :department => @dept)
end
it "is not valid for setting up the account" do
@model.should_not be_valid_for_signing_up_for_department_account
end
it "has a meaningful error message" do
@model.valid?(:signing_up_for_department_account)
@model.errors.on(:user_name).should == [ 'User name is already taken' ]
end
end
end
end