Skip to content

Commit

Permalink
Query count clean up for the different strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
lgebhardt committed Jan 13, 2024
1 parent 276438f commit 348eb32
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 17 deletions.
124 changes: 107 additions & 17 deletions test/controllers/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,37 @@ def test_index_filter_not_allowed
end

def test_index_include_one_level_query_count
assert_query_count(through_primary? ? 4 : 2) do
expected_count = case
when testing_v09?
2
when testing_v10?
4
when through_primary?
3
else
2
end

assert_query_count(expected_count) do
assert_cacheable_get :index, params: {include: 'author'}
end

assert_response :success
end

def test_index_include_two_levels_query_count
assert_query_count(through_primary? ? 6 : 3) do
expected_count = case
when testing_v09?
3
when testing_v10?
6
when through_primary?
5
else
3
end

assert_query_count(expected_count) do
assert_cacheable_get :index, params: { include: 'author,author.comments' }
end
assert_response :success
Expand Down Expand Up @@ -355,7 +377,7 @@ def test_index_filter_by_ids_and_fields_2
end

def test_filter_relationship_single
assert_query_count(through_primary? ? 2 : 1) do
assert_query_count(testing_v10? ? 2 : 1) do
assert_cacheable_get :index, params: {filter: {tags: '505,501'}}
end
assert_response :success
Expand All @@ -366,7 +388,7 @@ def test_filter_relationship_single
end

def test_filter_relationships_multiple
assert_query_count(through_primary? ? 2 : 1) do
assert_query_count(testing_v10? ? 2 : 1) do
assert_cacheable_get :index, params: { filter: { tags: '505,501', comments: '3' } }
end
assert_response :success
Expand Down Expand Up @@ -3303,7 +3325,18 @@ def test_books_offset_pagination_no_params_includes_query_count_one_level

with_jsonapi_config_changes do
JSONAPI.configuration.json_key_format = :dasherized_key
assert_query_count(through_primary? ? 5 : 3) do
expected_count = case
when testing_v09?
3
when testing_v10?
5
when through_primary?
4
else
3
end

assert_query_count(expected_count) do
assert_cacheable_get :index, params: { include: 'book-comments' }
end
assert_response :success
Expand All @@ -3317,7 +3350,18 @@ def test_books_offset_pagination_no_params_includes_query_count_two_levels
with_jsonapi_config_changes do
JSONAPI.configuration.json_key_format = :dasherized_key

assert_query_count(through_primary? ? 7 : 4) do
expected_count = case
when testing_v09?
4
when testing_v10?
7
when through_primary?
6
else
4
end

assert_query_count(expected_count) do
assert_cacheable_get :index, params: { include: 'book-comments,book-comments.author' }
end
assert_response :success
Expand Down Expand Up @@ -3451,7 +3495,18 @@ def test_books_included_paged

with_jsonapi_config_changes do
JSONAPI.configuration.json_key_format = :dasherized_key
assert_query_count(through_primary? ? 5 : 3) do
expected_count = case
when testing_v09?
3
when testing_v10?
5
when through_primary?
4
else
3
end

assert_query_count(expected_count) do
assert_cacheable_get :index, params: { filter: { id: '0' }, include: 'book-comments' }
assert_response :success
assert_equal 1, json_response['data'].size
Expand All @@ -3468,7 +3523,7 @@ def test_books_banned_non_book_admin
JSONAPI.configuration.top_level_meta_include_record_count = true
JSONAPI.configuration.json_key_format = :dasherized_key

assert_query_count(through_primary? ? 3 : 2) do
assert_query_count(testing_v10? ? 3 : 2) do
assert_cacheable_get :index, params: { page: { offset: 50, limit: 12 } }
assert_response :success
assert_equal 12, json_response['data'].size
Expand All @@ -3485,7 +3540,19 @@ def test_books_banned_non_book_admin_includes_switched

Api::V2::BookResource.paginator :offset
JSONAPI.configuration.top_level_meta_include_record_count = true
assert_query_count(through_primary? ? 5 : 3) do

expected_count = case
when testing_v09?
3
when testing_v10?
5
when through_primary?
4
else
3
end

assert_query_count(expected_count) do
assert_cacheable_get :index, params: { page: { offset: 0, limit: 12 }, include: 'book-comments' }
assert_response :success
assert_equal 12, json_response['data'].size
Expand All @@ -3504,7 +3571,19 @@ def test_books_banned_non_book_admin_includes_nested_includes
JSONAPI.configuration.json_key_format = :dasherized_key
JSONAPI.configuration.top_level_meta_include_record_count = true
Api::V2::BookResource.paginator :offset
assert_query_count(through_primary? ? 7 : 4) do

expected_count = case
when testing_v09?
4
when testing_v10?
7
when through_primary?
6
else
4
end

assert_query_count(expected_count) do
assert_cacheable_get :index, params: { page: { offset: 0, limit: 12 }, include: 'book-comments.author' }
assert_response :success
assert_equal 12, json_response['data'].size
Expand All @@ -3522,7 +3601,7 @@ def test_books_banned_admin
Api::V2::BookResource.paginator :offset
JSONAPI.configuration.json_key_format = :dasherized_key
JSONAPI.configuration.top_level_meta_include_record_count = true
assert_query_count(through_primary? ? 3 : 2) do
assert_query_count(testing_v10? ? 3 : 2) do
assert_cacheable_get :index, params: { page: { offset: 50, limit: 12 }, filter: { banned: 'true' } }
end
assert_response :success
Expand All @@ -3539,7 +3618,7 @@ def test_books_not_banned_admin
Api::V2::BookResource.paginator :offset
JSONAPI.configuration.json_key_format = :dasherized_key
JSONAPI.configuration.top_level_meta_include_record_count = true
assert_query_count(through_primary? ? 3 : 2) do
assert_query_count(testing_v10? ? 3 : 2) do
assert_cacheable_get :index, params: { page: { offset: 50, limit: 12 }, filter: { banned: 'false' }, fields: { books: 'id,title' } }
end
assert_response :success
Expand All @@ -3557,7 +3636,7 @@ def test_books_banned_non_book_admin_overlapped

Api::V2::BookResource.paginator :offset
JSONAPI.configuration.top_level_meta_include_record_count = true
assert_query_count(through_primary? ? 3 : 2) do
assert_query_count(testing_v10? ? 3 : 2) do
assert_cacheable_get :index, params: { page: { offset: 590, limit: 20 } }
end
assert_response :success
Expand All @@ -3574,7 +3653,18 @@ def test_books_included_exclude_unapproved
with_jsonapi_config_changes do
JSONAPI.configuration.json_key_format = :dasherized_key

assert_query_count(through_primary? ? 4 : 2) do
expected_count = case
when testing_v09?
2
when testing_v10?
4
when through_primary?
3
else
2
end

assert_query_count(expected_count) do
assert_cacheable_get :index, params: { filter: { id: '0,1,2,3,4' }, include: 'book-comments' }
end
assert_response :success
Expand Down Expand Up @@ -3695,7 +3785,7 @@ def setup

def test_book_comments_all_for_admin
$test_user = Person.find(1005)
assert_query_count(through_primary? ? 2 : 1) do
assert_query_count(testing_v10? ? 2 : 1) do
assert_cacheable_get :index
end
assert_response :success
Expand All @@ -3704,7 +3794,7 @@ def test_book_comments_all_for_admin

def test_book_comments_unapproved_context_based
$test_user = Person.find(1005)
assert_query_count(through_primary? ? 2 : 1) do
assert_query_count(testing_v10? ? 2 : 1) do
assert_cacheable_get :index, params: { filter: { approved: 'false' } }
end
assert_response :success
Expand All @@ -3713,7 +3803,7 @@ def test_book_comments_unapproved_context_based

def test_book_comments_exclude_unapproved_context_based
$test_user = Person.find(1001)
assert_query_count(through_primary? ? 2 : 1) do
assert_query_count(testing_v10? ? 2 : 1) do
assert_cacheable_get :index
end
assert_response :success
Expand Down
4 changes: 4 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,10 @@ def through_primary?
JSONAPI.configuration.default_find_related_through == :primary
end

def testing_v10?
JSONAPI.configuration.default_resource_retrieval_strategy == 'JSONAPI::ActiveRelationRetrievalV10'
end

def testing_v09?
JSONAPI.configuration.default_resource_retrieval_strategy == 'JSONAPI::ActiveRelationRetrievalV09'
end
Expand Down

0 comments on commit 348eb32

Please sign in to comment.