forked from travis-ci/travis-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository_spec.rb
201 lines (163 loc) · 7.18 KB
/
repository_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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
require 'spec_helper'
describe Repository do
include Support::ActiveRecord
describe 'validates' do
it 'uniqueness of :owner_name/:name' do
existing = Factory(:repository)
repository = Repository.new(existing.attributes)
repository.should_not be_valid
repository.errors['name'].should == ['has already been taken']
end
end
describe 'associations' do
describe 'owner' do
let(:user) { Factory(:user) }
let(:org) { Factory(:org) }
it 'can be a user' do
repository = Factory(:repository, :owner => user)
repository.reload.owner.should == user
end
it 'can be an organization' do
repository = Factory(:repository, :owner => org)
repository.reload.owner.should == org
end
end
end
describe 'class methods' do
describe 'find_by' do
let(:minimal) { Factory(:repository) }
it "should find a repository by it's id" do
Repository.find_by(:id => minimal.id).id.should == minimal.id
end
it "should find a repository by it's name and owner_name" do
repository = Repository.find_by(:name => minimal.name, :owner_name => minimal.owner_name)
repository.owner_name.should == minimal.owner_name
repository.name.should == minimal.name
end
it "should raise an error when a repository couldn't be found using params" do
expect {
Repository.find_by(:name => 'emptiness')
}.to raise_error(ActiveRecord::RecordNotFound)
end
end
describe 'timeline' do
it 'sorts the most repository with the most recent build to the top' do
repository_1 = Factory(:repository, :name => 'repository_1', :last_build_started_at => '2011-11-11')
repository_2 = Factory(:repository, :name => 'repository_2', :last_build_started_at => '2011-11-12')
repositories = Repository.timeline.all
repositories.first.id.should == repository_2.id
repositories.last.id.should == repository_1.id
end
end
describe 'search' do
before(:each) do
Factory(:repository, :name => 'repository_1', :last_build_started_at => '2011-11-11')
Factory(:repository, :name => 'repository_2', :last_build_started_at => '2011-11-12')
end
it 'performs searches case-insensitive' do
Repository.search('ePoS').to_a.count.should == 2
end
it 'performs searches with / entered' do
Repository.search('fuchs/').to_a.count.should == 2
end
it 'performs searches with \ entered' do
Repository.search('fuchs\\').to_a.count.should == 2
end
end
describe 'by_member' do
let(:user) { Factory(:user) }
let(:org) { Factory(:org) }
let(:repository_user) { Factory(:repository, :owner => user)}
let(:repository_org) { Factory(:repository, :owner => org, :name => 'globalize')}
before do
Permission.create!(:user => user, :repository => repository_user, :pull => true, :push => true)
Permission.create!(:user => user, :repository => repository_org, :pull => true)
end
it 'returns all repositories a user has rights to' do
Repository.by_member('svenfuchs').should have(2).items
end
end
describe 'counts_by_owner_names' do
let!(:repositories) do
Factory(:repository, :owner_name => 'svenfuchs', :name => 'minimal')
Factory(:repository, :owner_name => 'travis-ci', :name => 'travis-ci')
end
it 'returns repository counts per owner_name for the given owner_names' do
counts = Repository.counts_by_owner_names(%w(svenfuchs travis-ci))
counts.should == { 'svenfuchs' => 1, 'travis-ci' => 1 }
end
end
end
describe 'source_url' do
let(:repository) { Repository.new(:owner_name => 'travis-ci', :name => 'travis-ci') }
it 'returns the public git source url for a public repository' do
repository.private = false
repository.source_url.should == 'git://github.com/travis-ci/travis-ci.git'
end
it 'returns the private git source url for a private repository' do
repository.private = true
repository.source_url.should == 'git@github.com:travis-ci/travis-ci.git'
end
end
it "last_build returns the most recent build" do
repository = Factory(:repository)
attributes = { :repository => repository, :state => 'finished' }
Factory(:build, attributes)
Factory(:build, attributes)
build = Factory(:build, attributes)
repository.last_build.id.should == build.id
end
describe 'last_build_result_on' do
let(:build) { Factory(:build, :state => 'finished', :config => { 'rvm' => ['1.8.7', '1.9.2'], 'env' => ['DB=sqlite3', 'DB=postgresql'] }) }
let(:repository) { build.repository }
it 'returns last_build_result if params is empty' do
repository.expects(:last_build_result).returns(2)
repository.last_build_result_on({}).should == 2
end
it 'returns 0 (passing) if all specified builds are passing' do
build.matrix.each { |job| job.update_attribute(:result, job.config[:rvm] == '1.8.7' ? 0 : 1) }
repository.last_build_result_on('rvm' => '1.8.7').should == 0
end
it 'returns 1 (failing) if at least one specified build is failing' do
build.matrix.each_with_index { |build, ix| build.update_attribute(:result, ix == 0 ? 1 : 0) }
repository.last_build_result_on('rvm' => '1.8.7').should == 1
end
end
describe "keys" do
let(:repository) { Factory(:repository) }
it "should return the public key" do
repository.public_key.should == repository.key.public_key
end
it "should create a new key when the repository is created" do
repository = Repository.create!(:owner_name => 'travis-ci', :name => 'travis-ci')
repository.key.should_not be_nil
end
end
describe 'branches' do
let(:repository) { Factory(:repository) }
it 'retrieves branches only from last 25 builds' do
old_build = Factory(:build, :repository => repository, :commit => Factory(:commit, :branch => 'old-branch'))
24.times { Factory(:build, :repository => repository) }
Factory(:build, :repository => repository, :commit => Factory(:commit, :branch => 'production'))
repository.branches.size.should eql 2
repository.branches.should include("master")
repository.branches.should include("production")
repository.branches.should_not include("old-branch")
end
it 'is empty for empty repository' do
repository.branches.should eql []
end
end
describe 'last_finished_builds_by_branches' do
let(:repository) { Factory(:repository) }
it 'retrieves last builds on all branches' do
old_build = Factory(:build, :repository => repository, :state => 'finished', :commit => Factory(:commit, :branch => 'master'))
production_build = Factory(:build, :repository => repository, :state => 'finished', :commit => Factory(:commit, :branch => 'production'))
master_build = Factory(:build, :repository => repository, :state => 'finished', :commit => Factory(:commit, :branch => 'master'))
builds = repository.last_finished_builds_by_branches
builds.size.should == 2
builds.should include(master_build)
builds.should include(production_build)
end
end
end