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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe UserGroupNotificationSettingsFinder do
let_it_be(:user) { create(:user) }
subject { described_class.new(user, Group.where(id: groups.map(&:id))).execute }
def attributes(&proc)
subject.map(&proc).uniq
end
context 'when the groups have no existing notification settings' do
context 'when the groups have no ancestors' do
let_it_be(:groups) { create_list(:group, 3) }
it 'will be a default Global notification setting', :aggregate_failures do
expect(subject.count).to eq(3)
expect(attributes(&:notification_email)).to eq([nil])
expect(attributes(&:level)).to eq(['global'])
end
end
context 'when the groups have ancestors' do
context 'when an ancestor has a level other than Global' do
let_it_be(:ancestor_a) { create(:group) }
let_it_be(:group_a) { create(:group, parent: ancestor_a) }
let_it_be(:ancestor_b) { create(:group) }
let_it_be(:group_b) { create(:group, parent: ancestor_b) }
let_it_be(:email) { create(:email, :confirmed, email: 'ancestor@example.com', user: user) }
let_it_be(:groups) { [group_a, group_b] }
before do
create(:notification_setting, user: user, source: ancestor_a, level: 'participating', notification_email: email.email)
create(:notification_setting, user: user, source: ancestor_b, level: 'participating', notification_email: email.email)
end
it 'has the same level set' do
expect(attributes(&:level)).to eq(['participating'])
end
it 'has the same email set' do
expect(attributes(&:notification_email)).to eq(['ancestor@example.com'])
end
it 'only returns the two queried groups' do
expect(subject.count).to eq(2)
end
end
context 'when an ancestor has a Global level but has an email set' do
let_it_be(:grand_ancestor) { create(:group) }
let_it_be(:ancestor) { create(:group, parent: grand_ancestor) }
let_it_be(:group) { create(:group, parent: ancestor) }
let_it_be(:ancestor_email) { create(:email, :confirmed, email: 'ancestor@example.com', user: user) }
let_it_be(:grand_email) { create(:email, :confirmed, email: 'grand@example.com', user: user) }
let_it_be(:groups) { [group] }
before do
create(:notification_setting, user: user, source: grand_ancestor, level: 'participating', notification_email: grand_email.email)
create(:notification_setting, user: user, source: ancestor, level: 'global', notification_email: ancestor_email.email)
end
it 'has the same email and level set', :aggregate_failures do
expect(subject.count).to eq(1)
expect(attributes(&:level)).to eq(['global'])
expect(attributes(&:notification_email)).to eq(['ancestor@example.com'])
end
end
it 'does not cause an N+1', :aggregate_failures do
parent = create(:group)
child = create(:group, parent: parent)
control = ActiveRecord::QueryRecorder.new do
described_class.new(user, Group.where(id: child.id)).execute
end
other_parent = create(:group)
other_children = create_list(:group, 2, parent: other_parent)
result = nil
expect do
result = described_class.new(user, Group.where(id: other_children.append(child).map(&:id))).execute
end.not_to exceed_query_limit(control)
expect(result.count).to eq(3)
end
end
end
end
|