Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
9c623e3e
Commit
9c623e3e
authored
Dec 20, 2016
by
James Edwards-Jones
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added QueryRecorder to test N+1 fix on Milestone#show
parent
a2f57f23
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
0 deletions
+66
-0
spec/features/milestones/show_spec.rb
spec/features/milestones/show_spec.rb
+26
-0
spec/support/query_recorder.rb
spec/support/query_recorder.rb
+40
-0
No files found.
spec/features/milestones/show_spec.rb
0 → 100644
View file @
9c623e3e
require
'rails_helper'
describe
'Milestone show'
,
feature:
true
do
let
(
:user
)
{
create
(
:user
)
}
let
(
:project
)
{
create
(
:empty_project
)
}
let
(
:milestone
)
{
create
(
:milestone
,
project:
project
)
}
let
(
:labels
)
{
create_list
(
:label
,
2
,
project:
project
)
}
let
(
:issue_params
)
{
{
project:
project
,
assignee:
user
,
author:
user
,
milestone:
milestone
,
labels:
labels
}
}
before
do
project
.
add_user
(
user
,
:developer
)
login_as
(
user
)
end
def
visit_milestone
visit
namespace_project_milestone_path
(
project
.
namespace
,
project
,
milestone
)
end
it
'avoids N+1 database queries'
do
create
(
:labeled_issue
,
issue_params
)
control_count
=
ActiveRecord
::
QueryRecorder
.
new
{
visit_milestone
}.
count
create_list
(
:labeled_issue
,
10
,
issue_params
)
expect
{
visit_milestone
}.
not_to
exceed_query_limit
(
control_count
)
end
end
spec/support/query_recorder.rb
0 → 100644
View file @
9c623e3e
module
ActiveRecord
class
QueryRecorder
attr_reader
:log
def
initialize
(
&
block
)
@log
=
[]
ActiveSupport
::
Notifications
.
subscribed
(
method
(
:callback
),
'sql.active_record'
,
&
block
)
end
def
callback
(
name
,
start
,
finish
,
message_id
,
values
)
return
if
%w(CACHE SCHEMA)
.
include?
(
values
[
:name
])
@log
<<
values
[
:sql
]
end
def
count
@log
.
count
end
def
log_message
@log
.
join
(
"
\n\n
"
)
end
end
end
RSpec
::
Matchers
.
define
:exceed_query_limit
do
|
expected
|
supports_block_expectations
match
do
|
block
|
query_count
(
&
block
)
>
expected
end
failure_message_when_negated
do
|
actual
|
"Expected a maximum of
#{
expected
}
queries, got
#{
@recorder
.
count
}
:
\n\n
#{
@recorder
.
log_message
}
"
end
def
query_count
(
&
block
)
@recorder
=
ActiveRecord
::
QueryRecorder
.
new
(
&
block
)
@recorder
.
count
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment