Skip to content

Commit af00515

Browse files
committed
test: add polymorphic lookup tests
they pass on v-11-dev I'm going to look into the existing lookup warnings now ``` [POLYMORPHIC TYPE NOT FOUND] No polymorphic types found for fileable [POLYMORPHIC TYPE] No polymorphic types found for FilePropertiesResource fileable [POLYMORPHIC TYPE NOT FOUND] No polymorphic types found for respondent [POLYMORPHIC TYPE] No polymorphic types found for QuestionResource respondent [POLYMORPHIC TYPE NOT FOUND] No polymorphic types found for respondent [POLYMORPHIC TYPE] No polymorphic types found for AnswerResource respondent [POLYMORPHIC TYPE NOT FOUND] No polymorphic types found for keepable [POLYMORPHIC TYPE] No polymorphic types found for KeeperResource keepable ```
1 parent c628274 commit af00515

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

test/fixtures/active_record.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,18 @@
429429
t.integer :version
430430
t.timestamps null: false
431431
end
432+
create_table :contact_media do |t|
433+
t.string :name
434+
t.references :party, polymorphic: true, index: true
435+
end
436+
437+
create_table :individuals do |t|
438+
t.string :name
439+
end
440+
441+
create_table :organizations do |t|
442+
t.string :name
443+
end
432444
end
433445

434446
### MODELS
@@ -624,6 +636,24 @@ class Fact < ActiveRecord::Base
624636
class Like < ActiveRecord::Base
625637
end
626638

639+
class TestApplicationRecord < ActiveRecord::Base
640+
self.abstract_class = true
641+
end
642+
643+
class ContactMedium < TestApplicationRecord
644+
belongs_to :party, polymorphic: true, inverse_of: :contact_media
645+
belongs_to :individual, -> { where( contact_media: { party_type: 'Individual' } ).eager_load( :contact_media ) }, foreign_key: 'party_id'
646+
belongs_to :organization, -> { where( contact_media: { party_type: 'Organization' } ).eager_load( :contact_media ) }, foreign_key: 'party_id'
647+
end
648+
649+
class Individual < TestApplicationRecord
650+
has_many :contact_media, as: :party
651+
end
652+
653+
class Organization < TestApplicationRecord
654+
has_many :contact_media, as: :party
655+
end
656+
627657
class Breed
628658
include ActiveModel::Model
629659

@@ -1227,6 +1257,18 @@ class IndicatorsController < JSONAPI::ResourceController
12271257
class RobotsController < JSONAPI::ResourceController
12281258
end
12291259

1260+
class IndividualsController < BaseController
1261+
end
1262+
1263+
class OrganizationsController < BaseController
1264+
end
1265+
1266+
class ContactMediaController < BaseController
1267+
end
1268+
1269+
class PartiesController < BaseController
1270+
end
1271+
12301272
### RESOURCES
12311273
class BaseResource < JSONAPI::Resource
12321274
abstract
@@ -2685,6 +2727,24 @@ class RobotResource < ::JSONAPI::Resource
26852727
end
26862728
end
26872729

2730+
class ContactMediumResource < JSONAPI::Resource
2731+
attribute :name
2732+
has_one :party, polymorphic: true
2733+
end
2734+
2735+
class IndividualResource < JSONAPI::Resource
2736+
attribute :name
2737+
has_many :contact_media
2738+
end
2739+
2740+
class OrganizationResource < JSONAPI::Resource
2741+
attribute :name
2742+
has_many :contact_media
2743+
end
2744+
2745+
class PartyResource < JSONAPI::Resource
2746+
end
2747+
26882748
### PORO Data - don't do this in a production app
26892749
$breed_data = BreedData.new
26902750
$breed_data.add(Breed.new(0, 'persian'))
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
require File.expand_path('../../../test_helper', __FILE__)
2+
3+
# copied from https://github.com/cerebris/jsonapi-resources/blob/e60dc7dd2c7fdc85834163a7e706a10a8940a62b/test/integration/requests/polymorphic_test.rb
4+
# https://github.com/cerebris/jsonapi-resources/compare/bf4_fix_polymorphic_relations_lookup?expand=1
5+
class PolymorphicTest < ActionDispatch::IntegrationTest
6+
7+
def json_api_headers
8+
{'Accept' => JSONAPI::MEDIA_TYPE, 'CONTENT_TYPE' => JSONAPI::MEDIA_TYPE}
9+
end
10+
11+
def test_find_party_via_contact_medium
12+
individual = Individual.create(name: 'test')
13+
contact_medium = ContactMedium.create(party: individual, name: 'test contact medium')
14+
fetched_party = contact_medium.party
15+
assert_same individual, fetched_party, "Expect an individual to have been found via contact medium model's relationship 'party'"
16+
end
17+
18+
def test_get_individual
19+
individual = Individual.create(name: 'test')
20+
ContactMedium.create(party: individual, name: 'test contact medium')
21+
get "/individuals/#{individual.id}"
22+
assert_jsonapi_response 200
23+
end
24+
25+
def test_get_party_via_contact_medium
26+
individual = Individual.create(name: 'test')
27+
contact_medium = ContactMedium.create(party: individual, name: 'test contact medium')
28+
get "/contact_media/#{contact_medium.id}/party"
29+
assert_jsonapi_response 200, "Expect an individual to have been found via contact medium resource's relationship 'party'"
30+
end
31+
end
32+
33+
# copied from https://github.com/cerebris/jsonapi-resources/pull/1349/files
34+
# require File.expand_path('../test_helper', __FILE__)
35+
#
36+
# Replace this with the code necessary to make your test fail.
37+
# class BugTest < Minitest::Test
38+
# include Rack::Test::Methods
39+
#
40+
# def json_api_headers
41+
# {'Accept' => JSONAPI::MEDIA_TYPE, 'CONTENT_TYPE' => JSONAPI::MEDIA_TYPE}
42+
# end
43+
#
44+
# def teardown
45+
# Individual.delete_all
46+
# ContactMedium.delete_all
47+
# end
48+
#
49+
# def test_find_party_via_contact_medium
50+
# individual = Individual.create(name: 'test')
51+
# contact_medium = ContactMedium.create(party: individual, name: 'test contact medium')
52+
# fetched_party = contact_medium.party
53+
# assert_same individual, fetched_party, "Expect an individual to have been found via contact medium model's relationship 'party'"
54+
# end
55+
#
56+
# def test_get_individual
57+
# individual = Individual.create(name: 'test')
58+
# ContactMedium.create(party: individual, name: 'test contact medium')
59+
# get "/individuals/#{individual.id}"
60+
# assert_last_response_status 200
61+
# end
62+
#
63+
# def test_get_party_via_contact_medium
64+
# individual = Individual.create(name: 'test')
65+
# contact_medium = ContactMedium.create(party: individual, name: 'test contact medium')
66+
# get "/contact_media/#{contact_medium.id}/party"
67+
# assert_last_response_status 200, "Expect an individual to have been found via contact medium resource's relationship 'party'"
68+
# end
69+
#
70+
# private
71+
#
72+
# def assert_last_response_status(status, failure_reason=nil)
73+
# if last_response.status != status
74+
# json_response = JSON.parse(last_response.body) rescue last_response.body
75+
# pp json_response
76+
# end
77+
# assert_equal status, last_response.status, failure_reason
78+
# end
79+
#
80+
# def app
81+
# Rails.application
82+
# end
83+
# end

test/test_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ class CatResource < JSONAPI::Resource
414414
jsonapi_resources :widgets, only: [:index]
415415
jsonapi_resources :indicators, only: [:index]
416416
jsonapi_resources :robots, only: [:index]
417+
jsonapi_resources :contact_media
418+
jsonapi_resources :individuals
419+
jsonapi_resources :organizations
417420

418421
mount MyEngine::Engine => "/boomshaka", as: :my_engine
419422
mount ApiV2Engine::Engine => "/api_v2", as: :api_v2_engine

0 commit comments

Comments
 (0)