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
202
203
204
205
206
207
208
209
210
211
212
213
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::Catalog::Resource, feature_category: :pipeline_composition do
let_it_be(:today) { Time.zone.now }
let_it_be(:yesterday) { today - 1.day }
let_it_be(:tomorrow) { today + 1.day }
let_it_be_with_reload(:project) { create(:project, name: 'A') }
let_it_be(:project_2) { build(:project, name: 'Z') }
let_it_be(:project_3) { build(:project, name: 'L', description: 'Z') }
let_it_be_with_reload(:resource) { create(:ci_catalog_resource, project: project, latest_released_at: tomorrow) }
let_it_be(:resource_2) { create(:ci_catalog_resource, project: project_2, latest_released_at: today) }
let_it_be(:resource_3) { create(:ci_catalog_resource, project: project_3, latest_released_at: nil) }
let_it_be(:release1) { create(:release, project: project, released_at: yesterday) }
let_it_be(:release2) { create(:release, project: project, released_at: today) }
let_it_be(:release3) { create(:release, project: project, released_at: tomorrow) }
it { is_expected.to belong_to(:project) }
it do
is_expected.to(
have_many(:components).class_name('Ci::Catalog::Resources::Component').with_foreign_key(:catalog_resource_id)
)
end
it { is_expected.to have_many(:versions).class_name('Ci::Catalog::Resources::Version') }
it { is_expected.to delegate_method(:avatar_path).to(:project) }
it { is_expected.to delegate_method(:star_count).to(:project) }
it { is_expected.to delegate_method(:forks_count).to(:project) }
it { is_expected.to define_enum_for(:state).with_values({ draft: 0, published: 1 }) }
describe '.for_projects' do
it 'returns catalog resources for the given project IDs' do
resources_for_projects = described_class.for_projects(project.id)
expect(resources_for_projects).to contain_exactly(resource)
end
end
describe '.search' do
it 'returns catalog resources whose name or description match the search term' do
resources = described_class.search('Z')
expect(resources).to contain_exactly(resource_2, resource_3)
end
end
describe '.order_by_created_at_desc' do
it 'returns catalog resources sorted by descending created at' do
ordered_resources = described_class.order_by_created_at_desc
expect(ordered_resources.to_a).to eq([resource_3, resource_2, resource])
end
end
describe '.order_by_created_at_asc' do
it 'returns catalog resources sorted by ascending created at' do
ordered_resources = described_class.order_by_created_at_asc
expect(ordered_resources.to_a).to eq([resource, resource_2, resource_3])
end
end
describe '.order_by_name_desc' do
subject(:ordered_resources) { described_class.order_by_name_desc }
it 'returns catalog resources sorted by descending name' do
expect(ordered_resources.pluck(:name)).to eq(%w[Z L A])
end
it 'returns catalog resources sorted by descending name with nulls last' do
resource.update!(name: nil)
expect(ordered_resources.pluck(:name)).to eq(['Z', 'L', nil])
end
end
describe '.order_by_name_asc' do
subject(:ordered_resources) { described_class.order_by_name_asc }
it 'returns catalog resources sorted by ascending name' do
expect(ordered_resources.pluck(:name)).to eq(%w[A L Z])
end
it 'returns catalog resources sorted by ascending name with nulls last' do
resource.update!(name: nil)
expect(ordered_resources.pluck(:name)).to eq(['L', 'Z', nil])
end
end
describe '.order_by_latest_released_at_desc' do
it 'returns catalog resources sorted by latest_released_at descending with nulls last' do
ordered_resources = described_class.order_by_latest_released_at_desc
expect(ordered_resources).to eq([resource, resource_2, resource_3])
end
end
describe '.order_by_latest_released_at_asc' do
it 'returns catalog resources sorted by latest_released_at ascending with nulls last' do
ordered_resources = described_class.order_by_latest_released_at_asc
expect(ordered_resources).to eq([resource_2, resource, resource_3])
end
end
describe '#versions' do
it 'returns releases ordered by released date descending' do
expect(resource.versions).to eq([release3, release2, release1])
end
end
describe '#latest_version' do
it 'returns the latest release' do
expect(resource.latest_version).to eq(release3)
end
end
describe '#state' do
it 'defaults to draft' do
expect(resource.state).to eq('draft')
end
end
describe '#publish!' do
context 'when the catalog resource is in draft state' do
it 'updates the state of the catalog resource to published' do
expect(resource.state).to eq('draft')
resource.publish!
expect(resource.reload.state).to eq('published')
end
end
context 'when a catalog resource already has a published state' do
it 'leaves the state as published' do
resource.update!(state: 'published')
resource.publish!
expect(resource.state).to eq('published')
end
end
end
describe '#unpublish!' do
context 'when the catalog resource is in published state' do
it 'updates the state to draft' do
resource.update!(state: :published)
expect(resource.state).to eq('published')
resource.unpublish!
expect(resource.reload.state).to eq('draft')
end
end
context 'when the catalog resource is already in draft state' do
it 'leaves the state as draft' do
expect(resource.state).to eq('draft')
resource.unpublish!
expect(resource.reload.state).to eq('draft')
end
end
end
describe 'sync with project' do
shared_examples 'denormalized columns of the catalog resource match the project' do
it do
expect(resource.name).to eq(project.name)
expect(resource.description).to eq(project.description)
expect(resource.visibility_level).to eq(project.visibility_level)
end
end
context 'when the catalog resource is created' do
it_behaves_like 'denormalized columns of the catalog resource match the project'
end
context 'when the project name is updated' do
before do
project.update!(name: 'My new project name')
end
it_behaves_like 'denormalized columns of the catalog resource match the project'
end
context 'when the project description is updated' do
before do
project.update!(description: 'My new description')
end
it_behaves_like 'denormalized columns of the catalog resource match the project'
end
context 'when the project visibility_level is updated' do
before do
project.update!(visibility_level: 10)
end
it_behaves_like 'denormalized columns of the catalog resource match the project'
end
end
end
|