diff options
author | Robert Speicher <rspeicher@gmail.com> | 2012-08-29 11:36:02 -0400 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2012-08-29 11:36:02 -0400 |
commit | 97423a0bed1148f65f9ed2bd8bf540e764d9576c (patch) | |
tree | d91a67a6a863a97b8cf2acd76b01c18de08c2dc5 /spec/models | |
parent | 2bdea8651fa3bd82418986ada84828c22d451405 (diff) | |
download | gitlab-ce-97423a0bed1148f65f9ed2bd8bf540e764d9576c.tar.gz |
Add more coverage for model validations and associations
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/event_spec.rb | 1 | ||||
-rw-r--r-- | spec/models/issue_spec.rb | 12 | ||||
-rw-r--r-- | spec/models/key_spec.rb | 5 | ||||
-rw-r--r-- | spec/models/merge_request_spec.rb | 14 | ||||
-rw-r--r-- | spec/models/note_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/project_spec.rb | 45 | ||||
-rw-r--r-- | spec/models/protected_branch_spec.rb | 20 | ||||
-rw-r--r-- | spec/models/snippet_spec.rb | 11 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 17 | ||||
-rw-r--r-- | spec/models/users_project_spec.rb | 4 | ||||
-rw-r--r-- | spec/models/wiki_spec.rb | 2 |
11 files changed, 85 insertions, 48 deletions
diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb index ef6e3ed21a4..aaffda3199e 100644 --- a/spec/models/event_spec.rb +++ b/spec/models/event_spec.rb @@ -3,6 +3,7 @@ require 'spec_helper' describe Event do describe "Associations" do it { should belong_to(:project) } + it { should belong_to(:target) } end describe "Respond to" do diff --git a/spec/models/issue_spec.rb b/spec/models/issue_spec.rb index 5c22d0ccbcc..69829a4d13d 100644 --- a/spec/models/issue_spec.rb +++ b/spec/models/issue_spec.rb @@ -2,21 +2,11 @@ require 'spec_helper' describe Issue do describe "Associations" do - it { should belong_to(:project) } - it { should belong_to(:author) } - it { should belong_to(:assignee) } it { should belong_to(:milestone) } end describe "Validation" do - it { should validate_presence_of(:title) } - it { should validate_presence_of(:author_id) } - it { should validate_presence_of(:project_id) } - end - - describe "Scope" do - it { Issue.should respond_to :closed } - it { Issue.should respond_to :opened } + it { should ensure_length_of(:description).is_within(0..2000) } end describe 'modules' do diff --git a/spec/models/key_spec.rb b/spec/models/key_spec.rb index 6efb21e9086..85cd291d681 100644 --- a/spec/models/key_spec.rb +++ b/spec/models/key_spec.rb @@ -2,12 +2,15 @@ require 'spec_helper' describe Key do describe "Associations" do - it { should belong_to(:user) or belong_to(:project) } + it { should belong_to(:user) } + it { should belong_to(:project) } end describe "Validation" do it { should validate_presence_of(:title) } it { should validate_presence_of(:key) } + it { should ensure_length_of(:title).is_within(0..255) } + it { should ensure_length_of(:key).is_within(0..5000) } end describe "Methods" do diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb index 78ec01100cc..d1253b35ae5 100644 --- a/spec/models/merge_request_spec.rb +++ b/spec/models/merge_request_spec.rb @@ -1,23 +1,9 @@ require 'spec_helper' describe MergeRequest do - describe "Associations" do - it { should belong_to(:project) } - it { should belong_to(:author) } - it { should belong_to(:assignee) } - end - describe "Validation" do it { should validate_presence_of(:target_branch) } it { should validate_presence_of(:source_branch) } - it { should validate_presence_of(:title) } - it { should validate_presence_of(:author_id) } - it { should validate_presence_of(:project_id) } - end - - describe "Scope" do - it { MergeRequest.should respond_to :closed } - it { MergeRequest.should respond_to :opened } end describe 'modules' do diff --git a/spec/models/note_spec.rb b/spec/models/note_spec.rb index c4010e64929..ffaf442d9a4 100644 --- a/spec/models/note_spec.rb +++ b/spec/models/note_spec.rb @@ -3,6 +3,8 @@ require 'spec_helper' describe Note do describe "Associations" do it { should belong_to(:project) } + it { should belong_to(:noteable) } + it { should belong_to(:author).class_name('User') } end describe "Validation" do diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index eac8c3b5966..b947eeb290a 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2,23 +2,52 @@ require 'spec_helper' describe Project do describe "Associations" do + it { should belong_to(:owner).class_name('User') } it { should have_many(:users) } - it { should have_many(:protected_branches).dependent(:destroy) } it { should have_many(:events).dependent(:destroy) } - it { should have_many(:wikis).dependent(:destroy) } it { should have_many(:merge_requests).dependent(:destroy) } - it { should have_many(:users_projects).dependent(:destroy) } it { should have_many(:issues).dependent(:destroy) } + it { should have_many(:milestones).dependent(:destroy) } + it { should have_many(:users_projects).dependent(:destroy) } it { should have_many(:notes).dependent(:destroy) } it { should have_many(:snippets).dependent(:destroy) } - it { should have_many(:hooks).dependent(:destroy) } it { should have_many(:deploy_keys).dependent(:destroy) } + it { should have_many(:hooks).dependent(:destroy) } + it { should have_many(:wikis).dependent(:destroy) } + it { should have_many(:protected_branches).dependent(:destroy) } end describe "Validation" do + let!(:project) { create(:project) } + it { should validate_presence_of(:name) } + it { should validate_uniqueness_of(:name) } + it { should ensure_length_of(:name).is_within(0..255) } + it { should validate_presence_of(:path) } + it { should validate_uniqueness_of(:path) } + it { should ensure_length_of(:path).is_within(0..255) } + # TODO: Formats + + it { should ensure_length_of(:description).is_within(0..2000) } + it { should validate_presence_of(:code) } + it { should validate_uniqueness_of(:code) } + it { should ensure_length_of(:code).is_within(1..255) } + # TODO: Formats + + it { should validate_presence_of(:owner) } + + it "should not allow new projects beyond user limits" do + project.stub(:owner).and_return(double(can_create_project?: false, projects_limit: 1)) + project.should_not be_valid + project.errors[:base].first.should match(/Your own projects limit is 1/) + end + + it "should not allow 'gitolite-admin' as repo name" do + should allow_value("blah").for(:path) + should_not allow_value("gitolite-admin").for(:path) + end end describe "Respond to" do @@ -73,9 +102,11 @@ describe Project do it { should respond_to(:trigger_post_receive) } end - it "should not allow 'gitolite-admin' as repo name" do - should allow_value("blah").for(:path) - should_not allow_value("gitolite-admin").for(:path) + describe 'modules' do + it { should include_module(Repository) } + it { should include_module(PushObserver) } + it { should include_module(Authority) } + it { should include_module(Team) } end it "should return valid url to repo" do diff --git a/spec/models/protected_branch_spec.rb b/spec/models/protected_branch_spec.rb index 77a214ffed2..9180bc3bca6 100644 --- a/spec/models/protected_branch_spec.rb +++ b/spec/models/protected_branch_spec.rb @@ -1,8 +1,6 @@ require 'spec_helper' describe ProtectedBranch do - let(:project) { Factory(:project) } - describe 'Associations' do it { should belong_to(:project) } end @@ -13,26 +11,26 @@ describe ProtectedBranch do end describe 'Callbacks' do - subject { ProtectedBranch.new(project: project, name: 'branch_name') } + let(:branch) { build(:protected_branch) } it 'call update_repository after save' do - subject.should_receive(:update_repository) - subject.save + branch.should_receive(:update_repository) + branch.save end it 'call update_repository after destroy' do - subject.should_receive(:update_repository) - subject.destroy + branch.save + branch.should_receive(:update_repository) + branch.destroy end end describe '#commit' do - subject { ProtectedBranch.new(project: project, name: 'cant_touch_this') } + let(:branch) { create(:protected_branch) } it 'commits itself to its project' do - project.should_receive(:commit).with('cant_touch_this') - - subject.commit + branch.project.should_receive(:commit).with(branch.name) + branch.commit end end end diff --git a/spec/models/snippet_spec.rb b/spec/models/snippet_spec.rb index 2a63584e429..ffb861c4910 100644 --- a/spec/models/snippet_spec.rb +++ b/spec/models/snippet_spec.rb @@ -3,14 +3,21 @@ require 'spec_helper' describe Snippet do describe "Associations" do it { should belong_to(:project) } - it { should belong_to(:author) } + it { should belong_to(:author).class_name('User') } + it { should have_many(:notes).dependent(:destroy) } end describe "Validation" do - it { should validate_presence_of(:title) } it { should validate_presence_of(:author_id) } it { should validate_presence_of(:project_id) } + + it { should validate_presence_of(:title) } + it { should ensure_length_of(:title).is_within(0..255) } + it { should validate_presence_of(:file_name) } + it { should ensure_length_of(:title).is_within(0..255) } + it { should validate_presence_of(:content) } + it { should ensure_length_of(:content).is_within(0..10_000) } end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index a3443dc723d..ca34f07df7f 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -2,13 +2,26 @@ require 'spec_helper' describe User do describe "Associations" do - it { should have_many(:projects) } it { should have_many(:users_projects).dependent(:destroy) } + it { should have_many(:projects) } + it { should have_many(:my_own_projects).class_name('Project') } + it { should have_many(:keys).dependent(:destroy) } + it { should have_many(:events).class_name('Event').dependent(:destroy) } + it { should have_many(:recent_events).class_name('Event') } it { should have_many(:issues).dependent(:destroy) } + it { should have_many(:notes).dependent(:destroy) } it { should have_many(:assigned_issues).dependent(:destroy) } it { should have_many(:merge_requests).dependent(:destroy) } it { should have_many(:assigned_merge_requests).dependent(:destroy) } - it { should have_many(:notes).dependent(:destroy) } + end + + describe 'validations' do + it { should validate_presence_of(:projects_limit) } + it { should validate_numericality_of(:projects_limit) } + it { should allow_value(0).for(:projects_limit) } + it { should_not allow_value(-1).for(:projects_limit) } + + it { should ensure_length_of(:bio).is_within(0..255) } end describe "Respond to" do diff --git a/spec/models/users_project_spec.rb b/spec/models/users_project_spec.rb index 5103ae0077b..3197ba6eb6b 100644 --- a/spec/models/users_project_spec.rb +++ b/spec/models/users_project_spec.rb @@ -7,7 +7,11 @@ describe UsersProject do end describe "Validation" do + let!(:users_project) { create(:users_project) } + it { should validate_presence_of(:user_id) } + it { should validate_uniqueness_of(:user_id).scoped_to(:project_id) } + it { should validate_presence_of(:project_id) } end diff --git a/spec/models/wiki_spec.rb b/spec/models/wiki_spec.rb index 8035280daa8..de6ce426331 100644 --- a/spec/models/wiki_spec.rb +++ b/spec/models/wiki_spec.rb @@ -4,10 +4,12 @@ describe Wiki do describe "Associations" do it { should belong_to(:project) } it { should belong_to(:user) } + it { should have_many(:notes).dependent(:destroy) } end describe "Validation" do it { should validate_presence_of(:title) } + it { should ensure_length_of(:title).is_within(1..250) } it { should validate_presence_of(:content) } it { should validate_presence_of(:user_id) } end |