From 338b7cb785d0f69baf3ccc915eebc329d3998921 Mon Sep 17 00:00:00 2001 From: Tiago Guedes Date: Sun, 17 Dec 2017 16:52:53 -0200 Subject: [PATCH 1/9] Improve counting records from database --- lib/jsonapi/utils/support/pagination.rb | 36 ++++++++++++++++++++----- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/lib/jsonapi/utils/support/pagination.rb b/lib/jsonapi/utils/support/pagination.rb index 49ebc27..57ee057 100644 --- a/lib/jsonapi/utils/support/pagination.rb +++ b/lib/jsonapi/utils/support/pagination.rb @@ -2,6 +2,8 @@ module JSONAPI module Utils module Support module Pagination + RecordCountError = Class.new(ArgumentError) + # Apply proper pagination to the records. # # @param records [ActiveRecord::Relation, Array] collection of records @@ -130,15 +132,35 @@ def pagination_range # # @api private def count_records(records, options) - if options[:count].present? - options[:count] - elsif records.is_a?(Array) - records.length - else - records = apply_filter(records, options) if params[:filter].present? - records.except(:group, :order).count("DISTINCT #{records.table.name}.id") + return options[:count].to_i if options[:count].is_a?(Numeric) + + case records + when ActiveRecord::Relation then count_records_from_database(records, options) + when Array then records.length + else raise RecordCountError, "Can't count records with the given options" end end + + # Count records from the datatase applying the given request filters + # and skipping things like eager loading, grouping and sorting. + # + # @param records [ActiveRecord::Relation, Array] collection of records + # e.g.: User.all or [{ id: 1, name: 'Tiago' }, { id: 2, name: 'Doug' }] + # + # @param options [Hash] JU's options + # e.g.: { resource: V2::UserResource, count: 100 } + # + # @return [Integer] + # e.g.: 42 + # + # @api private + def count_records_from_database(records, options) + records = apply_filter(records, options) if params[:filter].present? + count = -> (records, except: []) { records.except(*except).distinct.count } + count.(records, except: %i(includes group order)) + rescue ActiveRecord::StatementInvalid + count.(records, except: %i(group order)) + end end end end From 6886f5ebb7b3e7109fa3ce564d60e27824c9ff7a Mon Sep 17 00:00:00 2001 From: Tiago Guedes Date: Sun, 17 Dec 2017 18:53:10 -0200 Subject: [PATCH 2/9] Avoid duplicate record counts --- lib/jsonapi/utils/response/formatters.rb | 4 ++-- lib/jsonapi/utils/support/pagination.rb | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/jsonapi/utils/response/formatters.rb b/lib/jsonapi/utils/response/formatters.rb index 460e3a0..899026a 100644 --- a/lib/jsonapi/utils/response/formatters.rb +++ b/lib/jsonapi/utils/response/formatters.rb @@ -171,7 +171,7 @@ def turn_into_resource(record, options) end end - # Apply some result options like pagination params and count to a collection response. + # Apply some result options like pagination params and record count to collection responses. # # @param records [ActiveRecord::Relation, Hash, Array] # Object to be formatted into JSON @@ -192,7 +192,7 @@ def result_options(records, options) end if JSONAPI.configuration.top_level_meta_include_record_count - data[:record_count] = count_records(records, options) + data[:record_count] = record_count_for(records, options) end end end diff --git a/lib/jsonapi/utils/support/pagination.rb b/lib/jsonapi/utils/support/pagination.rb index 57ee057..ab775a7 100644 --- a/lib/jsonapi/utils/support/pagination.rb +++ b/lib/jsonapi/utils/support/pagination.rb @@ -35,7 +35,23 @@ def apply_pagination(records, options = {}) # @api public def pagination_params(records, options) return {} unless JSONAPI.configuration.top_level_links_include_pagination - paginator.links_page_params(record_count: count_records(records, options)) + paginator.links_page_params(record_count: record_count_for(records, options)) + end + + # Apply memoization to the record count result avoiding then duplicate counts. + # + # @param records [ActiveRecord::Relation, Array] collection of records + # e.g.: User.all or [{ id: 1, name: 'Tiago' }, { id: 2, name: 'Doug' }] + # + # @param options [Hash] JU's options + # e.g.: { resource: V2::UserResource, count: 100 } + # + # @return [Integer] + # e.g.: 42 + # + # @api public + def record_count_for(records, options) + @record_count ||= count_records(records, options) end private From a7c918fd1f7b0b8625fc1a8345b3cff1f976d515 Mon Sep 17 00:00:00 2001 From: Tiago Guedes Date: Sat, 6 Jan 2018 00:29:44 -0200 Subject: [PATCH 3/9] Refactor count lambda --- lib/jsonapi/utils/support/pagination.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/jsonapi/utils/support/pagination.rb b/lib/jsonapi/utils/support/pagination.rb index ab775a7..6935b61 100644 --- a/lib/jsonapi/utils/support/pagination.rb +++ b/lib/jsonapi/utils/support/pagination.rb @@ -172,11 +172,17 @@ def count_records(records, options) # @api private def count_records_from_database(records, options) records = apply_filter(records, options) if params[:filter].present? - count = -> (records, except: []) { records.except(*except).distinct.count } + count = -> (records, except: []) do + records.except(*except).count(distinct_count_sql(records)) + end count.(records, except: %i(includes group order)) rescue ActiveRecord::StatementInvalid count.(records, except: %i(group order)) end + + def distinct_count_sql(records) + "DISTINCT #{records.table_name}.#{records.primary_key}" + end end end end From 696fbc7c005185fb25da691c9b2f3aea8ba1fc5f Mon Sep 17 00:00:00 2001 From: Tiago Guedes Date: Sat, 13 Jan 2018 04:50:39 -0200 Subject: [PATCH 4/9] Write more specs for the record count feature --- lib/jsonapi/utils/support/pagination.rb | 13 +++- spec/features/record_count_spec.rb | 93 +++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 spec/features/record_count_spec.rb diff --git a/lib/jsonapi/utils/support/pagination.rb b/lib/jsonapi/utils/support/pagination.rb index 6935b61..3cff5da 100644 --- a/lib/jsonapi/utils/support/pagination.rb +++ b/lib/jsonapi/utils/support/pagination.rb @@ -38,7 +38,7 @@ def pagination_params(records, options) paginator.links_page_params(record_count: record_count_for(records, options)) end - # Apply memoization to the record count result avoiding then duplicate counts. + # Apply memoization to the record count result avoiding duplicate counts. # # @param records [ActiveRecord::Relation, Array] collection of records # e.g.: User.all or [{ id: 1, name: 'Tiago' }, { id: 2, name: 'Doug' }] @@ -172,7 +172,7 @@ def count_records(records, options) # @api private def count_records_from_database(records, options) records = apply_filter(records, options) if params[:filter].present? - count = -> (records, except: []) do + count = -> (records, except:) do records.except(*except).count(distinct_count_sql(records)) end count.(records, except: %i(includes group order)) @@ -180,6 +180,15 @@ def count_records_from_database(records, options) count.(records, except: %i(group order)) end + # Build the SQL distinct count with some reflection on the "records" object. + # + # @param records [ActiveRecord::Relation] collection of records + # e.g.: User.all + # + # @return [String] + # e.g.: "DISTINCT users.id" + # + # @api private def distinct_count_sql(records) "DISTINCT #{records.table_name}.#{records.primary_key}" end diff --git a/spec/features/record_count_spec.rb b/spec/features/record_count_spec.rb new file mode 100644 index 0000000..0d0f8ca --- /dev/null +++ b/spec/features/record_count_spec.rb @@ -0,0 +1,93 @@ +require 'spec_helper' + +## +# Configs +## + +# Resource +class RecordCountTestResource < JSONAPI::Resource; end + +# Controller +class RecordCountTestController < BaseController + def explicit_count + jsonapi_render json: User.all, options: { count: 42, resource: UserResource } + end + + def array_count + jsonapi_render json: User.all.to_a, options: { resource: UserResource } + end + + def active_record_count + jsonapi_render json: User.all, options: { resource: UserResource } + end + + def active_record_count_with_eager_load + users = User.all.includes(:posts) + jsonapi_render json: users, options: { resource: UserResource } + end + + def active_record_count_with_eager_load_and_where_clause + users = User.all.includes(:posts).where(posts: { id: Post.first.id }) + jsonapi_render json: users, options: { resource: UserResource } + end +end + +# Routes +TestApp.routes.draw do + controller :record_count_test do + get :explicit_count + get :array_count + get :active_record_count + get :active_record_count_with_eager_load + get :active_record_count_with_eager_load_and_where_clause + end +end + +## +# Feature tests +## + +describe RecordCountTestController, type: :controller do + include_context 'JSON API headers' + + before(:all) do + JSONAPI.configuration.json_key_format = :underscored_key + FactoryGirl.create_list(:user, 3, :with_posts) + end + + describe 'explicit count' do + it 'returns the count based on the passed "options"' do + get :explicit_count + expect(response).to have_meta_record_count(42) + end + end + + describe 'array count' do + it 'returns the count based on the array length' do + get :array_count + expect(response).to have_meta_record_count(User.count) + end + end + + describe 'active record count' do + it 'returns the count based on the AR\'s query result' do + get :active_record_count + expect(response).to have_meta_record_count(User.count) + end + end + + describe 'active record count with eager load' do + it 'returns the count based on the AR\'s query result' do + get :active_record_count_with_eager_load + expect(response).to have_meta_record_count(User.count) + end + end + + describe 'active record count with eager load and where clause' do + it 'returns the count based on the AR\'s query result' do + get :active_record_count_with_eager_load_and_where_clause + count = User.joins(:posts).where(posts: { id: Post.first.id }).count + expect(response).to have_meta_record_count(count) + end + end +end From c74c2f122313f0b5323fb8ac7a8c19e197acae86 Mon Sep 17 00:00:00 2001 From: Tiago Guedes Date: Sun, 14 Jan 2018 00:31:06 -0200 Subject: [PATCH 5/9] Refactor test helpers --- spec/controllers/posts_controller_spec.rb | 2 +- spec/controllers/profile_controller_spec.rb | 2 +- spec/controllers/users_controller_spec.rb | 2 +- spec/features/record_count_spec.rb | 2 +- spec/rails_helper.rb | 18 ++++ spec/spec_helper.rb | 92 --------------------- spec/support/application.rb | 79 ++++++++++++++++++ spec/support/serializers.rb | 0 8 files changed, 101 insertions(+), 96 deletions(-) create mode 100644 spec/rails_helper.rb create mode 100644 spec/support/application.rb delete mode 100644 spec/support/serializers.rb diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb index aeef1f4..bdf6c42 100644 --- a/spec/controllers/posts_controller_spec.rb +++ b/spec/controllers/posts_controller_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' describe PostsController, type: :controller do include_context 'JSON API headers' diff --git a/spec/controllers/profile_controller_spec.rb b/spec/controllers/profile_controller_spec.rb index b32cba1..9127013 100644 --- a/spec/controllers/profile_controller_spec.rb +++ b/spec/controllers/profile_controller_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' describe ProfileController, type: :controller do include_context 'JSON API headers' diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 3b0248b..e7971f8 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' describe UsersController, type: :controller do include_context 'JSON API headers' diff --git a/spec/features/record_count_spec.rb b/spec/features/record_count_spec.rb index 0d0f8ca..e17d78e 100644 --- a/spec/features/record_count_spec.rb +++ b/spec/features/record_count_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'rails_helper' ## # Configs diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000..c05f0ef --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,18 @@ +require 'spec_helper' + +require 'rails/all' +require 'rails/test_help' +require 'rspec/rails' + +require 'jsonapi-resources' +require 'jsonapi/utils' + +require 'support/models' +require 'support/factories' +require 'support/resources' +require 'support/controllers' +require 'support/paginators' +require 'support/application' + +require 'support/shared/jsonapi_errors' +require 'support/shared/jsonapi_request' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index dac5c6a..d6c565f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,18 +1,7 @@ -require 'rails/all' -require 'rails/test_help' -require 'rspec/rails' require 'smart_rspec' require 'factory_girl' - -require 'jsonapi-resources' -require 'jsonapi/utils' - require 'support/helpers' -## -# General configs -## - RSpec.configure do |config| config.include FactoryGirl::Syntax::Methods config.include Helpers::ResponseParser @@ -24,85 +13,4 @@ config.mock_with :rspec do |mocks| mocks.verify_partial_doubles = true end - - config.before(:all) do - %w(posts categories profiles users).each do |table_name| - ActiveRecord::Base.connection.execute("DELETE FROM #{table_name}; VACUUM;") - end - end -end - -Rails.env = 'test' - -JSONAPI.configure do |config| - config.json_key_format = :underscored_key - - config.allow_include = true - config.allow_sort = true - config.allow_filter = true - - config.default_page_size = 10 - config.maximum_page_size = 10 - config.default_paginator = :paged - config.top_level_links_include_pagination = true - - config.top_level_meta_include_record_count = true - config.top_level_meta_record_count_key = :record_count -end - -## -# Rails application -## - -puts "RAILS VERSION: #{Rails.version}" - -class TestApp < Rails::Application - config.eager_load = false - config.root = File.dirname(__FILE__) - config.session_store :cookie_store, key: 'session' - config.secret_key_base = 'secret' - - # Raise errors on unsupported parameters - config.action_controller.action_on_unpermitted_parameters = :log - - ActiveRecord::Schema.verbose = false - config.active_record.schema_format = :none - config.active_support.test_order = :random - - # Turn off millisecond precision to maintain Rails 4.0 and 4.1 compatibility in test results - Rails::VERSION::MAJOR >= 4 && Rails::VERSION::MINOR >= 1 && - ActiveSupport::JSON::Encoding.time_precision = 0 - - I18n.enforce_available_locales = false - I18n.available_locales = [:en, :ru] - I18n.default_locale = :en - I18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] -end - -Dir[Rails.root.join('support/shared/**/*.rb')].each { |f| require f } - -require 'support/models' -require 'support/factories' -require 'support/resources' -require 'support/controllers' -require 'support/paginators' - -## -# Routes -## - -JSONAPI.configuration.route_format = :dasherized_route - -TestApp.routes.draw do - jsonapi_resources :users do - jsonapi_links :profile - jsonapi_resources :posts, shallow: true - end - - jsonapi_resource :profile - - patch :update_with_error_on_base, to: 'posts#update_with_error_on_base' - - get :index_with_hash, to: 'posts#index_with_hash' - get :show_with_hash, to: 'posts#show_with_hash' end diff --git a/spec/support/application.rb b/spec/support/application.rb new file mode 100644 index 0000000..7908bb8 --- /dev/null +++ b/spec/support/application.rb @@ -0,0 +1,79 @@ +## +# General configs +## + +RSpec.configure do |config| + config.before(:all) do + %w[posts categories profiles users].each do |table_name| + ActiveRecord::Base.connection.execute("DELETE FROM #{table_name}; VACUUM;") + end + end +end + +JSONAPI.configure do |config| + config.json_key_format = :underscored_key + + config.allow_include = true + config.allow_sort = true + config.allow_filter = true + + config.default_page_size = 10 + config.maximum_page_size = 10 + config.default_paginator = :paged + config.top_level_links_include_pagination = true + + config.top_level_meta_include_record_count = true + config.top_level_meta_record_count_key = :record_count +end + +## +# Rails application +## + +Rails.env = 'test' +puts "Rails version: #{Rails.version}" + +class TestApp < Rails::Application + config.eager_load = false + config.root = File.dirname(__FILE__) + config.session_store :cookie_store, key: 'session' + config.secret_key_base = 'secret' + + # Raise errors on unsupported parameters + config.action_controller.action_on_unpermitted_parameters = :log + + ActiveRecord::Schema.verbose = false + config.active_record.schema_format = :none + config.active_support.test_order = :random + + # Turn off millisecond precision to maintain Rails 4.0 and 4.1 compatibility in test results + Rails::VERSION::MAJOR >= 4 && Rails::VERSION::MINOR >= 1 && + ActiveSupport::JSON::Encoding.time_precision = 0 + + I18n.enforce_available_locales = false + I18n.available_locales = [:en, :ru] + I18n.default_locale = :en + I18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] +end + +Dir[Rails.root.join('support/shared/**/*.rb')].each { |f| require f } + +## +# Routes +## + +JSONAPI.configuration.route_format = :dasherized_route + +TestApp.routes.draw do + jsonapi_resources :users do + jsonapi_links :profile + jsonapi_resources :posts, shallow: true + end + + jsonapi_resource :profile + + patch :update_with_error_on_base, to: 'posts#update_with_error_on_base' + + get :index_with_hash, to: 'posts#index_with_hash' + get :show_with_hash, to: 'posts#show_with_hash' +end diff --git a/spec/support/serializers.rb b/spec/support/serializers.rb deleted file mode 100644 index e69de29..0000000 From a7f52a80e07478de0936f1c5be93581442685078 Mon Sep 17 00:00:00 2001 From: Tiago Guedes Date: Sun, 14 Jan 2018 00:41:56 -0200 Subject: [PATCH 6/9] Fix TestApp path --- spec/rails_helper.rb | 3 ++- spec/{support/application.rb => test_app.rb} | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) rename spec/{support/application.rb => test_app.rb} (96%) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index c05f0ef..4e9d3c4 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -12,7 +12,8 @@ require 'support/resources' require 'support/controllers' require 'support/paginators' -require 'support/application' require 'support/shared/jsonapi_errors' require 'support/shared/jsonapi_request' + +require 'test_app' diff --git a/spec/support/application.rb b/spec/test_app.rb similarity index 96% rename from spec/support/application.rb rename to spec/test_app.rb index 7908bb8..f0332c0 100644 --- a/spec/support/application.rb +++ b/spec/test_app.rb @@ -56,8 +56,6 @@ class TestApp < Rails::Application I18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] end -Dir[Rails.root.join('support/shared/**/*.rb')].each { |f| require f } - ## # Routes ## From 09afb1e2d18edf525fd80102b35e37ddb22628ae Mon Sep 17 00:00:00 2001 From: Tiago Guedes Date: Sun, 14 Jan 2018 23:26:45 -0200 Subject: [PATCH 7/9] Write specs for JSONAPI::Utils::Support::Pagination --- spec/jsonapi/utils/support/pagination_spec.rb | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 spec/jsonapi/utils/support/pagination_spec.rb diff --git a/spec/jsonapi/utils/support/pagination_spec.rb b/spec/jsonapi/utils/support/pagination_spec.rb new file mode 100644 index 0000000..576b053 --- /dev/null +++ b/spec/jsonapi/utils/support/pagination_spec.rb @@ -0,0 +1,130 @@ +require 'rails_helper' + +describe JSONAPI::Utils::Support::Pagination do + subject do + OpenStruct.new(params: {}).extend(JSONAPI::Utils::Support::Pagination) + end + + before(:all) do + FactoryGirl.create_list(:user, 2) + end + + let(:options) { {} } + + ## + # Public API + ## + + describe '#record_count_for' do + context 'with array' do + let(:records) { User.all.to_a } + + it 'applies memoization on the record count' do + expect(records).to receive(:length).and_return(records.length).once + 2.times { subject.record_count_for(records, options) } + end + end + + context 'with ActiveRecord object' do + let(:records) { User.all } + + it 'applies memoization on the record count' do + expect(records).to receive(:except).and_return(records).once + 2.times { subject.record_count_for(records, options) } + end + end + end + + ## + # Private API + ## + + describe '#count_records' do + shared_examples_for 'counting records' do + it 'counts records' do + expect(subject.send(:count_records, records, options)).to eq(count) + end + end + + context 'with count present within the options' do + let(:records) { User.all } + let(:options) { { count: 999 } } + let(:count) { 999 } + it_behaves_like 'counting records' + end + + context 'with array' do + let(:records) { User.all.to_a } + let(:count) { records.length } + it_behaves_like 'counting records' + end + + context 'with ActiveRecord object' do + let(:records) { User.all } + let(:count) { records.count } + it_behaves_like 'counting records' + end + + context 'when no strategy can be applied' do + let(:records) { Object.new } + let(:count) { } + + it 'raises an error' do + expect { + subject.send(:count_records, records, options) + }.to raise_error(JSONAPI::Utils::Support::Pagination::RecordCountError) + end + end + end + + describe '#count_records_from_database' do + shared_examples_for 'skipping eager load SQL when counting records' do + it 'skips any eager load for the SQL count query (default)' do + expect(records).to receive(:except) + .with(:includes, :group, :order) + .and_return(User.all) + .once + expect(records).to receive(:except) + .with(:group, :order) + .and_return(User.all) + .exactly(0) + .times + subject.send(:count_records_from_database, records, options) + end + end + + context 'when not eager loading records' do + let(:records) { User.all } + it_behaves_like 'skipping eager load SQL when counting records' + end + + context 'when eager loading records' do + let(:records) { User.includes(:posts) } + it_behaves_like 'skipping eager load SQL when counting records' + end + + context 'when eager loading records and using where clause on associations' do + let(:records) { User.includes(:posts).where(posts: { id: 1 }) } + + it 'fallbacks to the SQL count query with eager load' do + expect(records).to receive(:except) + .with(:includes, :group, :order) + .and_raise(ActiveRecord::StatementInvalid) + .once + expect(records).to receive(:except) + .with(:group, :order) + .and_return(User.all) + .once + subject.send(:count_records_from_database, records, options) + end + end + end + + describe '#distinct_count_sql' do + let(:records) { OpenStruct.new(table_name: 'foos', primary_key: 'id') } + + it 'builds the distinct count SQL query' do + expect(subject.send(:distinct_count_sql, records)).to eq('DISTINCT foos.id') + end + end +end From 3cf618c4eddf08c64ca735b9f9981fe4515dd927 Mon Sep 17 00:00:00 2001 From: Tiago Guedes Date: Sun, 14 Jan 2018 23:51:13 -0200 Subject: [PATCH 8/9] Fix issue with drawing Rails routes on tests --- .travis.yml | 2 +- spec/features/record_count_spec.rb | 20 ++++++++++-------- spec/rails_helper.rb | 11 ++++++++++ spec/test_app.rb | 33 +++++++++++++----------------- 4 files changed, 38 insertions(+), 28 deletions(-) diff --git a/.travis.yml b/.travis.yml index f574b04..8bf1951 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,5 +6,5 @@ rvm: - 2.3.3 matrix: before_install: gem install bundler -v 1.13.6 -script: bundle exec rspec spec/controllers +script: bundle exec rspec spec diff --git a/spec/features/record_count_spec.rb b/spec/features/record_count_spec.rb index e17d78e..b90a982 100644 --- a/spec/features/record_count_spec.rb +++ b/spec/features/record_count_spec.rb @@ -33,13 +33,17 @@ def active_record_count_with_eager_load_and_where_clause end # Routes -TestApp.routes.draw do - controller :record_count_test do - get :explicit_count - get :array_count - get :active_record_count - get :active_record_count_with_eager_load - get :active_record_count_with_eager_load_and_where_clause +def TestApp.draw_record_count_test_routes + JSONAPI.configuration.json_key_format = :underscored_key + + TestApp.routes.draw do + controller :record_count_test do + get :explicit_count + get :array_count + get :active_record_count + get :active_record_count_with_eager_load + get :active_record_count_with_eager_load_and_where_clause + end end end @@ -51,7 +55,7 @@ def active_record_count_with_eager_load_and_where_clause include_context 'JSON API headers' before(:all) do - JSONAPI.configuration.json_key_format = :underscored_key + TestApp.draw_record_count_test_routes FactoryGirl.create_list(:user, 3, :with_posts) end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 4e9d3c4..4413845 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -17,3 +17,14 @@ require 'support/shared/jsonapi_request' require 'test_app' + +RSpec.configure do |config| + config.before(:all) do + TestApp.draw_app_routes + + %w[posts categories profiles users].each do |table_name| + ActiveRecord::Base.connection.execute("DELETE FROM #{table_name}; VACUUM;") + end + end +end + diff --git a/spec/test_app.rb b/spec/test_app.rb index f0332c0..7c00004 100644 --- a/spec/test_app.rb +++ b/spec/test_app.rb @@ -2,14 +2,6 @@ # General configs ## -RSpec.configure do |config| - config.before(:all) do - %w[posts categories profiles users].each do |table_name| - ActiveRecord::Base.connection.execute("DELETE FROM #{table_name}; VACUUM;") - end - end -end - JSONAPI.configure do |config| config.json_key_format = :underscored_key @@ -31,7 +23,6 @@ ## Rails.env = 'test' -puts "Rails version: #{Rails.version}" class TestApp < Rails::Application config.eager_load = false @@ -60,18 +51,22 @@ class TestApp < Rails::Application # Routes ## -JSONAPI.configuration.route_format = :dasherized_route +def TestApp.draw_app_routes + JSONAPI.configuration.route_format = :dasherized_route -TestApp.routes.draw do - jsonapi_resources :users do - jsonapi_links :profile - jsonapi_resources :posts, shallow: true - end + TestApp.routes.draw do + jsonapi_resources :users do + jsonapi_links :profile + jsonapi_resources :posts, shallow: true + end - jsonapi_resource :profile + jsonapi_resource :profile - patch :update_with_error_on_base, to: 'posts#update_with_error_on_base' + patch :update_with_error_on_base, to: 'posts#update_with_error_on_base' - get :index_with_hash, to: 'posts#index_with_hash' - get :show_with_hash, to: 'posts#show_with_hash' + get :index_with_hash, to: 'posts#index_with_hash' + get :show_with_hash, to: 'posts#show_with_hash' + end end + +puts "\nRunning Rails #{Rails.version} on #{Rails.env} environment" From 567cedaaf0f30f79f8b2aa867a847e17010edf0b Mon Sep 17 00:00:00 2001 From: Tiago Guedes Date: Mon, 15 Jan 2018 00:01:00 -0200 Subject: [PATCH 9/9] Bump up the gem's version --- lib/jsonapi/utils/version.rb | 2 +- spec/rails_helper.rb | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/jsonapi/utils/version.rb b/lib/jsonapi/utils/version.rb index 436b181..565b844 100644 --- a/lib/jsonapi/utils/version.rb +++ b/lib/jsonapi/utils/version.rb @@ -1,5 +1,5 @@ module JSONAPI module Utils - VERSION = '0.7.0'.freeze + VERSION = '0.7.1'.freeze end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 4413845..7f9fb77 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -27,4 +27,3 @@ end end end -