-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
transaction_exit_statement_spec.rb
115 lines (103 loc) · 3.28 KB
/
transaction_exit_statement_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::TransactionExitStatement, :config do
it 'registers an offense when `return` is used in transactions' do
expect_offense(<<~RUBY)
ApplicationRecord.transaction do
return if user.active?
^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit).
end
RUBY
end
it 'registers an offense when `break` is used in transactions' do
expect_offense(<<~RUBY)
ApplicationRecord.transaction do
break if user.active?
^^^^^ Exit statement `break` is not allowed. Use `raise` (rollback) or `next` (commit).
end
RUBY
end
it 'registers an offense when `throw` is used in transactions' do
expect_offense(<<~RUBY)
ApplicationRecord.transaction do
throw if user.active?
^^^^^ Exit statement `throw` is not allowed. Use `raise` (rollback) or `next` (commit).
end
RUBY
end
it 'registers an offense when `return` is used in `with_lock` transactions' do
expect_offense(<<~RUBY)
user.with_lock do
return if user.active?
^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit).
end
RUBY
end
it 'does not register an offense when `next` is used in transactions' do
expect_no_offenses(<<~RUBY)
ApplicationRecord.transaction do
next if user.active?
end
RUBY
end
it 'does not register an offense when `raise` is used in transactions' do
expect_no_offenses(<<~RUBY)
ApplicationRecord.transaction do
raise if user.active?
end
RUBY
end
it 'registers an offense when `return` is used in `loop` in transactions' do
expect_offense(<<~RUBY)
ApplicationRecord.transaction do
loop do
return if condition
^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit).
end
end
RUBY
end
it 'registers an offense when `throw` is used in `loop` in transactions' do
expect_offense(<<~RUBY)
ApplicationRecord.transaction do
loop do
throw if condition
^^^^^ Exit statement `throw` is not allowed. Use `raise` (rollback) or `next` (commit).
end
end
RUBY
end
it 'does not register an offense when `break` is used in `loop` in transactions' do
expect_no_offenses(<<~RUBY)
ApplicationRecord.transaction do
loop do
break if condition
end
end
RUBY
end
it 'registers an offense when `return` is used in `rescue`' do
expect_offense(<<~RUBY)
ApplicationRecord.transaction do
rescue
return do_something
^^^^^^^^^^^^^^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit).
end
RUBY
end
it 'registers an offense when `return` is used outside of a `rescue`' do
expect_offense(<<~RUBY)
ApplicationRecord.transaction do
return if user.active?
^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit).
rescue
pass
end
RUBY
end
it 'does not register an offense when transaction block is empty' do
expect_no_offenses(<<~RUBY)
ApplicationRecord.transaction do
end
RUBY
end
end