Merge pull request #1333 from tsigo/model_specs

Increased model spec coverage
This commit is contained in:
Dmitriy Zaporozhets 2012-08-29 09:00:02 -07:00
commit 5b0a998920
17 changed files with 222 additions and 338 deletions

View file

@ -18,11 +18,15 @@ FactoryGirl.define do
Faker::Lorem.sentence
end
sequence :name, aliases: [:file_name] do
Faker::Name.name
end
sequence(:url) { Faker::Internet.uri('http') }
factory :user, aliases: [:author, :assignee, :owner] do
email { Faker::Internet.email }
name { Faker::Name.name }
name
password "123456"
password_confirmation "123456"
@ -116,6 +120,11 @@ FactoryGirl.define do
author
title
content
file_name { Faker::Lorem.sentence }
file_name
end
factory :protected_branch do
name
project
end
end

View file

@ -1,24 +1,9 @@
# == Schema Information
#
# Table name: events
#
# id :integer(4) not null, primary key
# target_type :string(255)
# target_id :integer(4)
# title :string(255)
# data :text
# project_id :integer(4)
# created_at :datetime not null
# updated_at :datetime not null
# action :integer(4)
# author_id :integer(4)
#
require 'spec_helper'
describe Event do
describe "Associations" do
it { should belong_to(:project) }
it { should belong_to(:target) }
end
describe "Respond to" do
@ -29,16 +14,6 @@ describe Event do
it { should respond_to(:commits) }
end
describe "Creation" do
before do
@event = Factory :event
end
it "should create a valid event" do
@event.should be_valid
end
end
describe "Push event" do
before do
project = Factory :project

View file

@ -2,21 +2,16 @@ 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) }
it { should ensure_length_of(:description).is_within(0..2000) }
end
describe "Scope" do
it { Issue.should respond_to :closed }
it { Issue.should respond_to :opened }
describe 'modules' do
it { should include_module(IssueCommonality) }
it { should include_module(Upvote) }
end
subject { Factory.create(:issue) }
@ -61,57 +56,4 @@ describe Issue do
subject.is_being_reopened?.should be_false
end
end
describe "plus 1" do
subject { Factory.create(:issue) }
it "with no notes has a 0/0 score" do
subject.upvotes.should == 0
end
it "should recognize non-+1 notes" do
subject.notes << Factory(:note, note: "No +1 here")
subject.should have(1).note
subject.notes.first.upvote?.should be_false
subject.upvotes.should == 0
end
it "should recognize a single +1 note" do
subject.notes << Factory(:note, note: "+1 This is awesome")
subject.upvotes.should == 1
end
it "should recognize a multiple +1 notes" do
subject.notes << Factory(:note, note: "+1 This is awesome")
subject.notes << Factory(:note, note: "+1 I want this")
subject.upvotes.should == 2
end
end
describe ".search" do
let!(:issue) { Factory.create(:issue, title: "Searchable issue") }
it "matches by title" do
Issue.search('able').all.should == [issue]
end
end
end
# == Schema Information
#
# Table name: issues
#
# id :integer(4) not null, primary key
# title :string(255)
# assignee_id :integer(4)
# author_id :integer(4)
# project_id :integer(4)
# created_at :datetime not null
# updated_at :datetime not null
# closed :boolean(1) default(FALSE), not null
# position :integer(4) default(0)
# critical :boolean(1) default(FALSE), not null
# branch_name :string(255)
# description :text
# milestone_id :integer(4)
#

View file

@ -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
@ -44,17 +47,3 @@ describe Key do
end
end
end
# == Schema Information
#
# Table name: keys
#
# id :integer(4) not null, primary key
# user_id :integer(4)
# created_at :datetime not null
# updated_at :datetime not null
# key :text
# title :string(255)
# identifier :string(255)
# project_id :integer(4)
#

View file

@ -1,76 +1,13 @@
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 "plus 1" do
subject { Factory.create(:merge_request) }
it "with no notes has a 0/0 score" do
subject.upvotes.should == 0
end
it "should recognize non-+1 notes" do
subject.notes << Factory(:note, note: "No +1 here")
subject.should have(1).note
subject.notes.first.upvote?.should be_false
subject.upvotes.should == 0
end
it "should recognize a single +1 note" do
subject.notes << Factory(:note, note: "+1 This is awesome")
subject.upvotes.should == 1
end
it "should recognize a multiple +1 notes" do
subject.notes << Factory(:note, note: "+1 This is awesome")
subject.notes << Factory(:note, note: "+1 I want this")
subject.upvotes.should == 2
end
end
describe ".search" do
let!(:issue) { Factory.create(:issue, title: "Searchable issue") }
it "matches by title" do
Issue.search('able').all.should == [issue]
end
describe 'modules' do
it { should include_module(IssueCommonality) }
it { should include_module(Upvote) }
end
end
# == Schema Information
#
# Table name: merge_requests
#
# id :integer(4) not null, primary key
# target_branch :string(255) not null
# source_branch :string(255) not null
# project_id :integer(4) not null
# author_id :integer(4)
# assignee_id :integer(4)
# title :string(255)
# closed :boolean(1) default(FALSE), not null
# created_at :datetime not null
# updated_at :datetime not null
# st_commits :text(2147483647
# st_diffs :text(2147483647
# merged :boolean(1) default(FALSE), not null
# state :integer(4) default(1), not null
#

View file

@ -1,17 +1,3 @@
# == Schema Information
#
# Table name: milestones
#
# id :integer(4) not null, primary key
# title :string(255) not null
# project_id :integer(4) not null
# description :text
# due_date :date
# closed :boolean(1) default(FALSE), not null
# created_at :datetime not null
# updated_at :datetime not null
#
require 'spec_helper'
describe Milestone do

View file

@ -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
@ -130,19 +132,3 @@ describe Note do
end
end
end
# == Schema Information
#
# Table name: notes
#
# id :integer(4) not null, primary key
# note :text
# noteable_id :string(255)
# noteable_type :string(255)
# author_id :integer(4)
# created_at :datetime not null
# updated_at :datetime not null
# project_id :integer(4)
# attachment :string(255)
# line_code :string(255)
#

View file

@ -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
@ -236,23 +267,3 @@ describe Project do
end
end
end
# == Schema Information
#
# Table name: projects
#
# id :integer(4) not null, primary key
# name :string(255)
# path :string(255)
# description :text
# created_at :datetime not null
# updated_at :datetime not null
# private_flag :boolean(1) default(TRUE), not null
# code :string(255)
# owner_id :integer(4)
# default_branch :string(255) default("master"), not null
# issues_enabled :boolean(1) default(TRUE), not null
# wall_enabled :boolean(1) default(TRUE), not null
# merge_requests_enabled :boolean(1) default(TRUE), not null
# wiki_enabled :boolean(1) default(TRUE), not null
#

View file

@ -1,19 +1,6 @@
# == Schema Information
#
# Table name: protected_branches
#
# id :integer(4) not null, primary key
# project_id :integer(4) not null
# name :string(255) not null
# created_at :datetime not null
# updated_at :datetime not null
#
require 'spec_helper'
describe ProtectedBranch do
let(:project) { Factory(:project) }
describe 'Associations' do
it { should belong_to(:project) }
end
@ -24,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

View file

@ -3,29 +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
# == Schema Information
#
# Table name: snippets
#
# id :integer(4) not null, primary key
# title :string(255)
# content :text
# author_id :integer(4) not null
# project_id :integer(4) not null
# created_at :datetime not null
# updated_at :datetime not null
# file_name :string(255)
# expires_at :datetime
#

View file

@ -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
@ -51,33 +64,3 @@ describe User do
user.authentication_token.should_not == ""
end
end
# == Schema Information
#
# Table name: users
#
# id :integer(4) not null, primary key
# email :string(255) default(""), not null
# encrypted_password :string(128) default(""), not null
# reset_password_token :string(255)
# reset_password_sent_at :datetime
# remember_created_at :datetime
# sign_in_count :integer(4) default(0)
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string(255)
# last_sign_in_ip :string(255)
# created_at :datetime not null
# updated_at :datetime not null
# name :string(255)
# admin :boolean(1) default(FALSE), not null
# projects_limit :integer(4) default(10)
# skype :string(255) default(""), not null
# linkedin :string(255) default(""), not null
# twitter :string(255) default(""), not null
# authentication_token :string(255)
# dark_scheme :boolean(1) default(FALSE), not null
# theme_id :integer(4) default(1), not null
# bio :string(255)
# blocked :boolean(1) default(FALSE), not null
#

View file

@ -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
@ -16,15 +20,3 @@ describe UsersProject do
it { should respond_to(:user_email) }
end
end
# == Schema Information
#
# Table name: users_projects
#
# id :integer(4) not null, primary key
# user_id :integer(4) not null
# project_id :integer(4) not null
# created_at :datetime not null
# updated_at :datetime not null
# project_access :integer(4) default(0), not null
#

View file

@ -52,14 +52,3 @@ describe ProjectHook do
end
end
end
# == Schema Information
#
# Table name: web_hooks
#
# id :integer(4) not null, primary key
# url :string(255)
# project_id :integer(4)
# created_at :datetime not null
# updated_at :datetime not null
#

View file

@ -4,27 +4,13 @@ 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
it { Factory(:wiki).should be_valid }
end
# == Schema Information
#
# Table name: wikis
#
# id :integer(4) not null, primary key
# title :string(255)
# content :text
# project_id :integer(4)
# created_at :datetime not null
# updated_at :datetime not null
# slug :string(255)
# user_id :integer(4)
#

View file

@ -0,0 +1,69 @@
require 'spec_helper'
describe Issue, "IssueCommonality" do
let(:issue) { create(:issue) }
describe "Associations" do
it { should belong_to(:project) }
it { should belong_to(:author) }
it { should belong_to(:assignee) }
it { should have_many(:notes).dependent(:destroy) }
end
describe "Validation" do
it { should validate_presence_of(:project_id) }
it { should validate_presence_of(:author_id) }
it { should validate_presence_of(:title) }
it { should ensure_length_of(:title).is_at_least(0).is_at_most(255) }
end
describe "Scope" do
it { described_class.should respond_to(:opened) }
it { described_class.should respond_to(:closed) }
it { described_class.should respond_to(:assigned) }
end
it "has an :author_id_of_changes accessor" do
issue.should respond_to(:author_id_of_changes)
issue.should respond_to(:author_id_of_changes=)
end
describe ".search" do
let!(:searchable_issue) { create(:issue, title: "Searchable issue") }
it "matches by title" do
described_class.search('able').all.should == [searchable_issue]
end
end
describe "#today?" do
it "returns true when created today" do
# Avoid timezone differences and just return exactly what we want
Date.stub(:today).and_return(issue.created_at.to_date)
issue.today?.should be_true
end
it "returns false when not created today" do
Date.stub(:today).and_return(Date.yesterday)
issue.today?.should be_false
end
end
describe "#new?" do
it "returns true when created today and record hasn't been updated" do
issue.stub(:today?).and_return(true)
issue.new?.should be_true
end
it "returns false when not created today" do
issue.stub(:today?).and_return(false)
issue.new?.should be_false
end
it "returns false when record has been updated" do
issue.stub(:today?).and_return(true)
issue.touch
issue.new?.should be_false
end
end
end

27
spec/roles/upvote_spec.rb Normal file
View file

@ -0,0 +1,27 @@
require 'spec_helper'
describe Issue, "Upvote" do
let(:issue) { create(:issue) }
it "with no notes has a 0/0 score" do
issue.upvotes.should == 0
end
it "should recognize non-+1 notes" do
issue.notes << create(:note, note: "No +1 here")
issue.should have(1).note
issue.notes.first.upvote?.should be_false
issue.upvotes.should == 0
end
it "should recognize a single +1 note" do
issue.notes << create(:note, note: "+1 This is awesome")
issue.upvotes.should == 1
end
it "should recognize multiple +1 notes" do
issue.notes << create(:note, note: "+1 This is awesome")
issue.notes << create(:note, note: "+1 I want this")
issue.upvotes.should == 2
end
end

View file

@ -28,6 +28,16 @@ RSpec::Matchers.define :be_404_for do |user|
end
end
RSpec::Matchers.define :include_module do |expected|
match do
described_class.included_modules.include?(expected)
end
failure_message_for_should do
"expected #{described_class} to include the #{expected} module"
end
end
module UrlAccess
def url_allowed?(user, url)
emulate_user(user)
@ -57,3 +67,17 @@ module UrlAccess
login_with(user) if user
end
end
# Extend shoulda-matchers
module Shoulda::Matchers::ActiveModel
class EnsureLengthOfMatcher
# Shortcut for is_at_least and is_at_most
def is_within(range)
if range.exclude_end?
is_at_least(range.first) && is_at_most(range.last - 1)
else
is_at_least(range.first) && is_at_most(range.last)
end
end
end
end