-
Notifications
You must be signed in to change notification settings - Fork 539
Expand file tree
/
Copy pathtest_helper.rb
More file actions
704 lines (587 loc) · 19.2 KB
/
test_helper.rb
File metadata and controls
704 lines (587 loc) · 19.2 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
require 'simplecov'
require 'database_cleaner'
# To run tests with coverage:
# COVERAGE=true bundle exec rake test
# To test on a specific rails version use this:
# export RAILS_VERSION=5.2.4.4; bundle update; bundle exec rake test
# export RAILS_VERSION=6.0.3.4; bundle update; bundle exec rake test
# export RAILS_VERSION=6.1.1; bundle update; bundle exec rake test
# We are no longer having Travis test Rails 4.2.11., but you can try it with:
# export RAILS_VERSION=4.2.11; bundle update rails; bundle exec rake test
# To Switch rails versions and run a particular test order:
# export RAILS_VERSION=6.1.1; bundle update; bundle exec rake TESTOPTS="--seed=39333" test
if ENV['COVERAGE']
SimpleCov.start do
end
end
ENV['DATABASE_URL'] ||= "sqlite3:test_db"
require 'active_record/railtie'
require 'rails/test_help'
require 'minitest/mock'
require 'jsonapi-resources'
require 'pry'
require File.expand_path('../helpers/value_matchers', __FILE__)
require File.expand_path('../helpers/assertions', __FILE__)
require File.expand_path('../helpers/functional_helpers', __FILE__)
require File.expand_path('../helpers/configuration_helpers', __FILE__)
Rails.env = 'test'
I18n.load_path += Dir[File.expand_path("../../locales/*.yml", __FILE__)]
I18n.enforce_available_locales = false
JSONAPI.configure do |config|
config.json_key_format = :camelized_key
end
ActiveSupport::Deprecation.silenced = true
puts "Testing With 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 = :raise
ActiveRecord::Schema.verbose = false
config.active_record.schema_format = :none
config.active_support.test_order = :random
config.active_support.halt_callback_chains_on_return_false = false
config.active_record.time_zone_aware_types = [:time, :datetime]
config.active_record.belongs_to_required_by_default = false
unless Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR < 2 ||
Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR >= 1 ||
Rails::VERSION::MAJOR > 6
config.active_record.sqlite3.represent_boolean_as_integer = true
end
end
DatabaseCleaner.allow_remote_database_url = true
DatabaseCleaner.strategy = :transaction
module MyEngine
class Engine < ::Rails::Engine
isolate_namespace MyEngine
end
end
module ApiV2Engine
class Engine < ::Rails::Engine
isolate_namespace ApiV2Engine
end
end
# Monkeypatch ActionController::TestCase to delete the RAW_POST_DATA on subsequent calls in the same test.
module ClearRawPostHeader
def process(action, **args)
@request.delete_header 'RAW_POST_DATA'
super action, **args
end
end
class ActionController::TestCase
prepend ClearRawPostHeader
end
# Patch to allow :api_json mime type to be treated as JSON
# Otherwise it is run through `to_query` and empty arrays are dropped.
module ActionController
class TestRequest < ActionDispatch::TestRequest
def assign_parameters(routes, controller_path, action, parameters, generated_path, query_string_keys)
non_path_parameters = {}
path_parameters = {}
parameters.each do |key, value|
if query_string_keys.include?(key)
non_path_parameters[key] = value
else
if value.is_a?(Array)
value = value.map(&:to_param)
else
value = value.to_param
end
path_parameters[key] = value
end
end
if get?
if self.query_string.blank?
self.query_string = non_path_parameters.to_query
end
else
if ENCODER.should_multipart?(non_path_parameters)
self.content_type = ENCODER.content_type
data = ENCODER.build_multipart non_path_parameters
else
fetch_header('CONTENT_TYPE') do |k|
set_header k, 'application/x-www-form-urlencoded'
end
# parser = ActionDispatch::Http::Parameters::DEFAULT_PARSERS[Mime::Type.lookup(fetch_header('CONTENT_TYPE'))]
case content_mime_type.to_sym
when nil
raise "Unknown Content-Type: #{content_type}"
when :json, :api_json
data = ActiveSupport::JSON.encode(non_path_parameters)
when :xml
data = non_path_parameters.to_xml
when :url_encoded_form
data = non_path_parameters.to_query
else
@custom_param_parsers[content_mime_type] = ->(_) { non_path_parameters }
data = non_path_parameters.to_query
end
end
set_header 'CONTENT_LENGTH', data.length.to_s
set_header 'rack.input', StringIO.new(data)
end
fetch_header("PATH_INFO") do |k|
set_header k, generated_path
end
path_parameters[:controller] = controller_path
path_parameters[:action] = action
self.path_parameters = path_parameters
end
end
end
def assert_query_count(expected, msg = nil, &block)
@queries = []
callback = lambda {|_, _, _, _, payload|
@queries.push payload[:sql]
}
ActiveSupport::Notifications.subscribed(callback, 'sql.active_record', &block)
show_queries unless expected == @queries.size
assert expected == @queries.size, "Expected #{expected} queries, ran #{@queries.size} queries"
@queries = nil
end
def track_queries(&block)
@queries = []
callback = lambda {|_, _, _, _, payload|
@queries.push payload[:sql]
}
ActiveSupport::Notifications.subscribed(callback, 'sql.active_record', &block)
show_queries
@queries = nil
end
def show_queries
@queries.each_with_index do |query, index|
puts "sql[#{index}]: #{query}"
end
end
TestApp.initialize!
require File.expand_path('../fixtures/active_record', __FILE__)
module Pets
module V1
class CatsController < JSONAPI::ResourceController
end
class CatResource < JSONAPI::Resource
attribute :name
attribute :breed
key_type :uuid
end
end
end
JSONAPI.configuration.route_format = :underscored_route
TestApp.routes.draw do
jsonapi_resources :sessions
jsonapi_resources :people
jsonapi_resources :special_people
jsonapi_resources :comments
jsonapi_resources :firms
jsonapi_resources :tags
jsonapi_resources :hair_cuts
jsonapi_resources :posts do
jsonapi_relationships
jsonapi_links :special_tags
jsonapi_links :super_tags, only: [:create]
end
jsonapi_resources :sections
jsonapi_resources :iso_currencies
jsonapi_resources :expense_entries
jsonapi_resources :breeds
jsonapi_resources :planets
jsonapi_resources :planet_types
jsonapi_resources :moons
jsonapi_resources :craters
jsonapi_resource :preferences
jsonapi_resources :facts
jsonapi_resources :categories
jsonapi_resources :pictures
jsonapi_resources :documents
jsonapi_resources :products
jsonapi_resources :file_properties
jsonapi_resources :vehicles
jsonapi_resources :cars
jsonapi_resources :boats
jsonapi_resources :flat_posts
jsonapi_resources :blog_posts
jsonapi_resources :books
jsonapi_resources :authors
jsonapi_resources :questions
jsonapi_resources :answers
jsonapi_resources :doctors
jsonapi_resources :patients
jsonapi_resources :access_cards
jsonapi_resources :response
jsonapi_resources :paragraph
jsonapi_resources :employees
jsonapi_resources :robots
jsonapi_resources :lists
jsonapi_resources :list_items
namespace :api do
jsonapi_resources :boxes
jsonapi_resources :things
jsonapi_resources :users
namespace :v1 do
jsonapi_resources :people
jsonapi_resources :comments
jsonapi_resources :tags
jsonapi_resources :posts
jsonapi_resources :sections
jsonapi_resources :iso_currencies
jsonapi_resources :expense_entries
jsonapi_resources :breeds
jsonapi_resources :planets
jsonapi_resources :planet_types
jsonapi_resources :moons
jsonapi_resources :craters
jsonapi_resource :preferences
jsonapi_resources :likes
jsonapi_resources :writers
end
JSONAPI.configuration.route_format = :underscored_route
namespace :v2 do
jsonapi_resources :posts
jsonapi_resource :preferences, except: [:create, :destroy]
jsonapi_resources :authors
jsonapi_resources :books
jsonapi_resources :book_comments
#
jsonapi_resources :sections
jsonapi_resources :comments
jsonapi_resources :vehicles
jsonapi_resources :cars
jsonapi_resources :boats
jsonapi_resources :hair_cuts
jsonapi_resources :people
end
namespace :v3 do
jsonapi_resource :preferences do
# Intentionally empty block to skip relationship urls
end
jsonapi_resources :posts, except: [:destroy] do
jsonapi_link :author, except: [:destroy]
jsonapi_links :tags, only: [:show, :create]
end
end
JSONAPI.configuration.route_format = :camelized_route
namespace :v4 do
jsonapi_resources :posts do
end
jsonapi_resources :expense_entries do
jsonapi_link :iso_currency
jsonapi_related_resource :iso_currency
end
jsonapi_resources :iso_currencies do
end
jsonapi_resources :books
end
JSONAPI.configuration.route_format = :dasherized_route
namespace :v5 do
jsonapi_resources :people
jsonapi_resources :posts do
end
jsonapi_resources :painters
jsonapi_resources :paintings
jsonapi_resources :collectors
jsonapi_resources :authors
jsonapi_resources :author_details
jsonapi_resources :expense_entries
jsonapi_resources :iso_currencies
jsonapi_resources :tags
jsonapi_resources :comments
jsonapi_resources :employees
end
JSONAPI.configuration.route_format = :underscored_route
JSONAPI.configuration.route_format = :dasherized_route
namespace :v6 do
jsonapi_resources :authors
jsonapi_resources :author_details
jsonapi_resources :posts
jsonapi_resources :sections
jsonapi_resources :customers
jsonapi_resources :purchase_orders
jsonapi_resources :line_items
jsonapi_resources :order_flags
end
JSONAPI.configuration.route_format = :underscored_route
namespace :v7 do
jsonapi_resources :customers
jsonapi_resources :purchase_orders
jsonapi_resources :line_items
jsonapi_resources :categories
jsonapi_resources :clients
end
namespace :v8 do
jsonapi_resources :numeros_telefone
end
namespace :v9 do
jsonapi_resources :people
jsonapi_resource :preferences
end
end
namespace :admin_api do
namespace :v1 do
jsonapi_resources :people
end
end
namespace :dasherized_namespace, path: 'dasherized-namespace' do
namespace :v1 do
jsonapi_resources :people
end
end
namespace :pets do
namespace :v1 do
jsonapi_resources :cats
end
end
jsonapi_resources :keepers, only: [:show]
jsonapi_resources :storages
jsonapi_resources :workers, only: [:show]
jsonapi_resources :widgets, only: [:index]
jsonapi_resources :indicators, only: [:index]
jsonapi_resources :robots, only: [:index]
mount MyEngine::Engine => "/boomshaka", as: :my_engine
mount ApiV2Engine::Engine => "/api_v2", as: :api_v2_engine
end
MyEngine::Engine.routes.draw do
namespace :api do
namespace :v1 do
jsonapi_resources :people
end
end
namespace :admin_api do
namespace :v1 do
jsonapi_resources :people
end
end
namespace :dasherized_namespace, path: 'dasherized-namespace' do
namespace :v1 do
jsonapi_resources :people
end
end
namespace :optional_namespace, path: 'optional_namespace' do
namespace :v1, path: '' do
jsonapi_resources :people
end
end
end
ApiV2Engine::Engine.routes.draw do
jsonapi_resources :people
end
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
class Minitest::Test
include Helpers::Assertions
include Helpers::ValueMatchers
include Helpers::FunctionalHelpers
include Helpers::ConfigurationHelpers
include ActiveRecord::TestFixtures
def run_in_transaction?
true
end
self.fixture_path = "#{Rails.root}/fixtures"
fixtures :all
end
class ActiveSupport::TestCase
self.fixture_path = "#{Rails.root}/fixtures"
fixtures :all
setup do
@routes = TestApp.routes
end
end
class ActionDispatch::IntegrationTest
self.fixture_path = "#{Rails.root}/fixtures"
fixtures :all
def assert_jsonapi_response(expected_status, msg = nil)
assert_equal JSONAPI::MEDIA_TYPE, response.content_type
if status != expected_status && status >= 400
pp json_response rescue nil
end
assert_equal expected_status, status, msg
end
def assert_jsonapi_get(url, msg = "GET response must be 200")
get url, headers: { 'Accept' => JSONAPI::MEDIA_TYPE }
assert_jsonapi_response 200, msg
end
# Perform a GET request, make sure it returns 200, then try it again with caching enabled
# to make sure that doesn't affect the output.
def assert_cacheable_jsonapi_get(url, cached_classes = :all)
assert_nil JSONAPI.configuration.resource_cache
assert_jsonapi_get url
non_caching_response = json_response.dup
cache = ActiveSupport::Cache::MemoryStore.new
warmup = with_resource_caching(cache, cached_classes) do
assert_jsonapi_get url, "Cache warmup GET response must be 200"
end
assert_equal(
non_caching_response.pretty_inspect,
json_response.pretty_inspect,
"Cache warmup response must match normal response"
)
cached = with_resource_caching(cache, cached_classes) do
assert_jsonapi_get url, "Cached GET response must be 200"
end
assert_equal(
non_caching_response.pretty_inspect,
json_response.pretty_inspect,
"Cached response must match normal response"
)
assert_equal 0, cached[:total][:misses], "Cached response must not cause any cache misses"
assert_equal warmup[:total][:misses], cached[:total][:hits], "Cached response must use cache"
end
end
class ActionController::TestCase
def assert_cacheable_get(action, **args)
assert_nil JSONAPI.configuration.resource_cache
normal_queries = []
normal_query_callback = lambda {|_, _, _, _, payload| normal_queries.push payload[:sql] }
ActiveSupport::Notifications.subscribed(normal_query_callback, 'sql.active_record') do
get action, **args
end
non_caching_response = json_response_sans_all_backtraces
non_caching_status = response.status
# Don't let all the cache-testing requests mess with assert_query_count
orig_queries = @queries.try(:dup)
orig_request_headers = @request.headers.dup
ar_resource_klass = nil
modes = {none: [], all: :all}
if @controller.class.included_modules.include?(JSONAPI::ActsAsResourceController)
ar_resource_klass = @controller.send(:resource_klass)
if ar_resource_klass._model_class.respond_to?(:arel_table)
modes[:root_only] = [ar_resource_klass]
modes[:all_but_root] = {except: [ar_resource_klass]}
else
ar_resource_klass = nil
end
end
modes.each do |mode, cached_resources|
cache = ActiveSupport::Cache::MemoryStore.new
cache_activity = {}
[:warmup, :lookup].each do |phase|
begin
cache_queries = []
cache_query_callback = lambda { |_, _, _, _, payload|
cache_queries.push payload[:sql]
}
cache_activity[phase] = with_resource_caching(cache, cached_resources) do
ActiveSupport::Notifications.subscribed(cache_query_callback, 'sql.active_record') do
@controller = nil
setup_controller_request_and_response
@request.headers.merge!(orig_request_headers.dup)
get action, **args
end
end
rescue Exception
puts "Exception raised during cache (mode: #{mode}) #{phase}"
raise
end
if response.status != non_caching_status
pp json_response rescue nil
end
assert_equal(
non_caching_status,
response.status,
"Cache (mode: #{mode}) #{phase} response status must match normal response"
)
assert_equal(
non_caching_response.pretty_inspect,
json_response_sans_all_backtraces.pretty_inspect,
"Cache (mode: #{mode}) #{phase} response body must match normal response"
)
assert_operator(
cache_queries.size,
:<=,
normal_queries.size,
"Cache (mode: #{mode}) #{phase} action made too many queries:\n#{cache_queries.pretty_inspect}"
)
end
if mode == :all
if [:index, :show, :show_related_resource, :show_related_resources].include?(action)
if ar_resource_klass && response.status == 200 && json_response["data"].try(:size).try(:>, 0)
assert_operator(
cache_activity[:warmup][:total][:misses],
:>,
0,
"Cache (mode: #{mode}) warmup response with non-empty data must cause cache misses"
)
end
end
assert_equal 0, cache_activity[:lookup][:total][:misses],
"Cache (mode: #{mode}) lookup response must not cause any cache misses"
assert_operator(
cache_activity[:lookup][:total][:hits],
:>=,
cache_activity[:warmup][:total][:misses],
"Cache (mode: #{mode}) lookup response must use cache entries created by warmup"
)
end
end
@queries = orig_queries
end
private
def json_response_sans_all_backtraces
return nil if response.body.to_s.strip.empty?
r = json_response.dup
(r["errors"] || []).each do |err|
err["meta"].delete("backtrace") if err.has_key?("meta")
err["meta"].delete("application_backtrace") if err.has_key?("meta")
end
return r
end
end
class IntegrationBenchmark < ActionDispatch::IntegrationTest
def self.runnable_methods
methods_matching(/^bench_/)
end
def self.run_one_method(klass, method_name, reporter)
Benchmark.bmbm(method_name.length) do |job|
job.report(method_name) do
super(klass, method_name, reporter)
end
end
puts
end
end
class UpperCamelizedKeyFormatter < JSONAPI::KeyFormatter
class << self
def format(key)
super.camelize(:upper)
end
def unformat(formatted_key)
formatted_key.to_s.underscore
end
end
end
class DateWithTimezoneValueFormatter < JSONAPI::ValueFormatter
class << self
def format(raw_value)
raw_value.in_time_zone('Eastern Time (US & Canada)').to_s
end
end
end
class DateValueFormatter < JSONAPI::ValueFormatter
class << self
def format(raw_value)
raw_value.strftime('%m/%d/%Y')
end
end
end
class TitleValueFormatter < JSONAPI::ValueFormatter
class << self
def format(raw_value)
super(raw_value).titlecase
end
def unformat(value)
value.to_s.downcase
end
end
end
class OptionalRouteFormatter < JSONAPI::RouteFormatter
class << self
def format(route)
return if route == 'v1'
super
end
def unformat(formatted_route)
super
end
end
end