repository_spec.rb
4.73 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
125
126
127
128
129
130
131
132
require 'rails_helper'
describe Repository do
describe 'methods' do
describe 'attributes' do
subject { FactoryGirl.build(:repository) }
context 'when there are attributes' do
let!(:repository_attributes) { FactoryGirl.build(:repository_attributes) }
before :each do
RepositoryAttributes.expects(:find_by_repository_id).returns(repository_attributes)
end
it 'is expected to return the repository attributes' do
expect(subject.attributes).to eq(repository_attributes)
end
end
context 'when there are no attributes' do
before :each do
RepositoryAttributes.expects(:find_by_repository_id).returns(nil)
end
it 'is expected to return the repository attributes' do
expect(subject.attributes).to be_nil
end
end
end
end
describe 'class method' do
describe 'latest' do
let!(:repository) { FactoryGirl.build(:repository) }
let!(:another_repository) { FactoryGirl.build(:repository, id: 2) }
let!(:repository_attributes) { FactoryGirl.build(:repository_attributes) }
before :each do
repository.expects(:attributes).returns(repository_attributes)
another_repository.expects(:attributes).returns(repository_attributes)
end
context 'without private repositories' do
before :each do
Repository.expects(:all).returns([repository, another_repository])
end
it 'is expected to return the two repositories ordered' do
expect(Repository.latest(2)).to eq([another_repository, repository])
end
context 'when no parameter is passed' do
it 'is expected to return just the most recent repository' do
expect(Repository.latest).to eq([another_repository])
end
end
end
context 'with private repositories' do
let(:private_repository) { FactoryGirl.build(:repository, id: 3) }
let(:private_attributes) { FactoryGirl.build(:repository_attributes, :private) }
before :each do
private_repository.expects(:attributes).returns(private_attributes)
Repository.expects(:all).returns([repository, another_repository, private_repository])
end
it 'is expected to return only the public ones' do
expect(Repository.latest(2)).to eq([another_repository, repository])
end
end
end
describe 'public_or_owned_by_user' do
let!(:user) { FactoryGirl.build(:user, :with_id) }
let!(:owned_private_attrs) { FactoryGirl.build(:repository_attributes, :private, user_id: user.id) }
let!(:owned_public_attrs) { FactoryGirl.build(:repository_attributes, user_id: user.id) }
let!(:not_owned_private_attrs) { FactoryGirl.build(:repository_attributes, :private, user_id: user.id + 1) }
let!(:not_owned_public_attrs) { FactoryGirl.build(:repository_attributes, user_id: user.id + 1) }
let!(:public_attrs) { [owned_public_attrs, not_owned_public_attrs] }
let(:public_repositories) { public_attrs.map(&:repository) }
let!(:owned_or_public_attrs) { public_attrs + [owned_private_attrs] }
let!(:owned_or_public_repositories) { owned_or_public_attrs.map(&:repository) }
let(:all_repositories) { owned_or_public_repositories + [not_owned_private_attrs.repository] }
context 'when repositories exist' do
before :each do
all_repositories.each do |repository|
described_class.stubs(:find).with(repository.id).returns(repository)
end
RepositoryAttributes.expects(:where).with(public: true).returns(public_attrs)
end
context 'when user is not provided' do
it 'is expected to find all public repositories' do
expect(described_class.public_or_owned_by_user).to eq(public_repositories)
end
end
context 'when user is provided' do
before do
RepositoryAttributes.expects(:where).with(user_id: user.id, public: false).returns([owned_private_attrs])
end
it 'is expected to find all public and owned repositories' do
expect(described_class.public_or_owned_by_user(user)).to eq(owned_or_public_repositories)
end
end
end
context 'when no repositories exist' do
before :each do
all_repositories.each do |repository|
described_class.stubs(:find).with(repository.id).raises(Likeno::Errors::RecordNotFound)
end
RepositoryAttributes.expects(:where).with(public: true).returns(public_attrs)
end
it 'is expected to be empty' do
expect(described_class.public_or_owned_by_user).to be_empty
end
end
end
end
end