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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe DesignManagement::Action do
describe 'relations' do
it { is_expected.to belong_to(:design) }
it { is_expected.to belong_to(:version) }
end
describe 'scopes' do
let_it_be(:issue) { create(:issue) }
let_it_be(:design_a) { create(:design, issue: issue) }
let_it_be(:design_b) { create(:design, issue: issue) }
context 'with 3 designs' do
let_it_be(:design_c) { create(:design, issue: issue) }
let_it_be(:action_a_1) { create(:design_action, design: design_a) }
let_it_be(:action_a_2) { create(:design_action, design: design_a, event: :deletion) }
let_it_be(:action_b) { create(:design_action, design: design_b) }
let_it_be(:action_c) { create(:design_action, design: design_c, event: :deletion) }
describe '.most_recent' do
let(:designs) { [design_a, design_b, design_c] }
before_all do
create(:design_version, designs: [design_a, design_b, design_c])
create(:design_version, designs: [design_a, design_b])
create(:design_version, designs: [design_a])
end
it 'finds the correct version for each design' do
dvs = described_class.where(design: designs)
expected = designs
.map(&:id)
.zip(dvs.order("version_id DESC").pluck(:version_id).uniq)
actual = dvs.most_recent.map { |dv| [dv.design_id, dv.version_id] }
expect(actual).to eq(expected)
end
end
describe '.by_design' do
it 'returns the actions by design_id' do
expect(described_class.by_design([design_a.id, design_b.id]))
.to match_array([action_a_1, action_a_2, action_b])
end
end
describe '.with_version' do
it 'preloads the version' do
actions = described_class.with_version
expect { actions.map(&:version) }.not_to exceed_query_limit(2)
expect(actions.count).to be > 2
end
end
describe '.by_event' do
it 'returns the actions by event type' do
expect(described_class.by_event(:deletion)).to match_array([action_a_2, action_c])
end
end
end
describe '.up_to_version' do
# let bindings are not available in before(:all) contexts,
# so we need to redefine the array on each construction.
let_it_be(:oldest) { create(:design_version, designs: [design_a, design_b]) }
let_it_be(:middle) { create(:design_version, designs: [design_a, design_b]) }
let_it_be(:newest) { create(:design_version, designs: [design_a, design_b]) }
subject { described_class.where(design: issue.designs).up_to_version(version) }
context 'the version is nil' do
let(:version) { nil }
it 'returns all design_versions' do
is_expected.to have_attributes(size: 6)
end
end
context 'when given a Version instance' do
context 'the version is the most current' do
let(:version) { newest }
it { is_expected.to have_attributes(size: 6) }
end
context 'the version is the oldest' do
let(:version) { oldest }
it { is_expected.to have_attributes(size: 2) }
end
context 'the version is the middle one' do
let(:version) { middle }
it { is_expected.to have_attributes(size: 4) }
end
end
context 'when given a commit SHA' do
context 'the version is the most current' do
let(:version) { newest.sha }
it { is_expected.to have_attributes(size: 6) }
end
context 'the version is the oldest' do
let(:version) { oldest.sha }
it { is_expected.to have_attributes(size: 2) }
end
context 'the version is the middle one' do
let(:version) { middle.sha }
it { is_expected.to have_attributes(size: 4) }
end
end
context 'when given a String that is not a commit SHA' do
let(:version) { 'foo' }
it { expect { subject }.to raise_error(ArgumentError) }
end
end
end
end
|