Skip to content

Commit

Permalink
test: add polymorphic lookup tests
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
bf4 committed Jan 19, 2024
1 parent c628274 commit af00515
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
60 changes: 60 additions & 0 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,18 @@
t.integer :version
t.timestamps null: false
end
create_table :contact_media do |t|
t.string :name
t.references :party, polymorphic: true, index: true
end

create_table :individuals do |t|
t.string :name
end

create_table :organizations do |t|
t.string :name
end
end

### MODELS
Expand Down Expand Up @@ -624,6 +636,24 @@ class Fact < ActiveRecord::Base
class Like < ActiveRecord::Base
end

class TestApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end

class ContactMedium < TestApplicationRecord
belongs_to :party, polymorphic: true, inverse_of: :contact_media
belongs_to :individual, -> { where( contact_media: { party_type: 'Individual' } ).eager_load( :contact_media ) }, foreign_key: 'party_id'
belongs_to :organization, -> { where( contact_media: { party_type: 'Organization' } ).eager_load( :contact_media ) }, foreign_key: 'party_id'
end

class Individual < TestApplicationRecord
has_many :contact_media, as: :party
end

class Organization < TestApplicationRecord
has_many :contact_media, as: :party
end

class Breed
include ActiveModel::Model

Expand Down Expand Up @@ -1227,6 +1257,18 @@ class IndicatorsController < JSONAPI::ResourceController
class RobotsController < JSONAPI::ResourceController
end

class IndividualsController < BaseController
end

class OrganizationsController < BaseController
end

class ContactMediaController < BaseController
end

class PartiesController < BaseController
end

### RESOURCES
class BaseResource < JSONAPI::Resource
abstract
Expand Down Expand Up @@ -2685,6 +2727,24 @@ class RobotResource < ::JSONAPI::Resource
end
end

class ContactMediumResource < JSONAPI::Resource
attribute :name
has_one :party, polymorphic: true
end

class IndividualResource < JSONAPI::Resource
attribute :name
has_many :contact_media
end

class OrganizationResource < JSONAPI::Resource
attribute :name
has_many :contact_media
end

class PartyResource < JSONAPI::Resource
end

### PORO Data - don't do this in a production app
$breed_data = BreedData.new
$breed_data.add(Breed.new(0, 'persian'))
Expand Down
83 changes: 83 additions & 0 deletions test/integration/requests/polymorphic_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
require File.expand_path('../../../test_helper', __FILE__)

# copied from https://github.com/cerebris/jsonapi-resources/blob/e60dc7dd2c7fdc85834163a7e706a10a8940a62b/test/integration/requests/polymorphic_test.rb
# https://github.com/cerebris/jsonapi-resources/compare/bf4_fix_polymorphic_relations_lookup?expand=1
class PolymorphicTest < ActionDispatch::IntegrationTest

def json_api_headers
{'Accept' => JSONAPI::MEDIA_TYPE, 'CONTENT_TYPE' => JSONAPI::MEDIA_TYPE}
end

def test_find_party_via_contact_medium
individual = Individual.create(name: 'test')
contact_medium = ContactMedium.create(party: individual, name: 'test contact medium')
fetched_party = contact_medium.party
assert_same individual, fetched_party, "Expect an individual to have been found via contact medium model's relationship 'party'"
end

def test_get_individual
individual = Individual.create(name: 'test')
ContactMedium.create(party: individual, name: 'test contact medium')
get "/individuals/#{individual.id}"
assert_jsonapi_response 200
end

def test_get_party_via_contact_medium
individual = Individual.create(name: 'test')
contact_medium = ContactMedium.create(party: individual, name: 'test contact medium')
get "/contact_media/#{contact_medium.id}/party"
assert_jsonapi_response 200, "Expect an individual to have been found via contact medium resource's relationship 'party'"
end
end

# copied from https://github.com/cerebris/jsonapi-resources/pull/1349/files
# require File.expand_path('../test_helper', __FILE__)
#
# Replace this with the code necessary to make your test fail.
# class BugTest < Minitest::Test
# include Rack::Test::Methods
#
# def json_api_headers
# {'Accept' => JSONAPI::MEDIA_TYPE, 'CONTENT_TYPE' => JSONAPI::MEDIA_TYPE}
# end
#
# def teardown
# Individual.delete_all
# ContactMedium.delete_all
# end
#
# def test_find_party_via_contact_medium
# individual = Individual.create(name: 'test')
# contact_medium = ContactMedium.create(party: individual, name: 'test contact medium')
# fetched_party = contact_medium.party
# assert_same individual, fetched_party, "Expect an individual to have been found via contact medium model's relationship 'party'"
# end
#
# def test_get_individual
# individual = Individual.create(name: 'test')
# ContactMedium.create(party: individual, name: 'test contact medium')
# get "/individuals/#{individual.id}"
# assert_last_response_status 200
# end
#
# def test_get_party_via_contact_medium
# individual = Individual.create(name: 'test')
# contact_medium = ContactMedium.create(party: individual, name: 'test contact medium')
# get "/contact_media/#{contact_medium.id}/party"
# assert_last_response_status 200, "Expect an individual to have been found via contact medium resource's relationship 'party'"
# end
#
# private
#
# def assert_last_response_status(status, failure_reason=nil)
# if last_response.status != status
# json_response = JSON.parse(last_response.body) rescue last_response.body
# pp json_response
# end
# assert_equal status, last_response.status, failure_reason
# end
#
# def app
# Rails.application
# end
# end
3 changes: 3 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,9 @@ class CatResource < JSONAPI::Resource
jsonapi_resources :widgets, only: [:index]
jsonapi_resources :indicators, only: [:index]
jsonapi_resources :robots, only: [:index]
jsonapi_resources :contact_media
jsonapi_resources :individuals
jsonapi_resources :organizations

mount MyEngine::Engine => "/boomshaka", as: :my_engine
mount ApiV2Engine::Engine => "/api_v2", as: :api_v2_engine
Expand Down

0 comments on commit af00515

Please sign in to comment.