Skip to content

Commit

Permalink
test: failing request posting sti with polymorphic has one
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Jan 22, 2024
1 parent 76ef777 commit a45b85d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
11 changes: 9 additions & 2 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@
t.string :drive_layout
t.string :serial_number
t.integer :person_id
t.references :imageable, polymorphic: true, index: true
t.timestamps null: false
end

Expand Down Expand Up @@ -734,6 +735,9 @@ class Picture < ActiveRecord::Base

class Vehicle < ActiveRecord::Base
belongs_to :person
belongs_to :imageable, polymorphic: true
# belongs_to :document, -> { where( pictures: { imageable_type: 'Document' } ) }, foreign_key: 'imageable_id'
# belongs_to :product, -> { where( pictures: { imageable_type: 'Product' } ) }, foreign_key: 'imageable_id'
end

class Car < Vehicle
Expand All @@ -743,13 +747,13 @@ class Boat < Vehicle
end

class Document < ActiveRecord::Base
has_many :pictures, as: :imageable
has_many :pictures, as: :imageable # polymorphic
belongs_to :author, class_name: 'Person', foreign_key: 'author_id'
has_one :file_properties, as: :fileable
end

class Product < ActiveRecord::Base
has_many :pictures, as: :imageable
has_many :pictures, as: :imageable # polymorphic
belongs_to :designer, class_name: 'Person', foreign_key: 'designer_id'
has_one :file_properties, as: :fileable
end
Expand Down Expand Up @@ -1336,6 +1340,7 @@ class VehicleResource < JSONAPI::Resource
immutable

has_one :person
has_one :imageable, polymorphic: true
attributes :make, :model, :serial_number
end

Expand Down Expand Up @@ -1915,6 +1920,8 @@ class PreferencesResource < PreferencesResource; end
class SectionResource < SectionResource; end
class TagResource < TagResource; end
class CommentResource < CommentResource; end
class DocumentResource < DocumentResource; end
class ProductResource < ProductResource; end
class VehicleResource < VehicleResource; end
class CarResource < CarResource; end
class BoatResource < BoatResource; end
Expand Down
41 changes: 41 additions & 0 deletions test/integration/requests/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,47 @@ def test_post_polymorphic_with_has_many_relationship
assert_equal Car, person.vehicles.fourth.class
end

def test_post_sti_polymorphic_with_has_one_relationship
post '/vehicles', params:
{
'data' => {
'type' => 'cars',
'attributes' => {
'make' => 'Mazda',
'model' => 'Miata MX5',
'drive_layout' => 'Front Engine RWD',
'serial_number' => '32432adfsfdysua',
},
'relationships' => {
'person' => {
'data' => {
'type' => 'people', 'id' => '1001',
}
},
'picture' => {
'data' => {
'type' => 'products', 'id' => '1',
}
},
}
}
}.to_json,
headers: {
'CONTENT_TYPE' => JSONAPI::MEDIA_TYPE,
'Accept' => JSONAPI::MEDIA_TYPE
}

assert_jsonapi_response 201

body = JSON.parse(response.body)
car = Vehicle.find(body.dig("data", "id"))

assert_equal "Card", car.type
assert_equal "Mazda", car.make
assert_equal Product, car.picture.class
assert_equal Person, car.person.class
end

def test_post_polymorphic_invalid_with_wrong_type
post '/people', params:
{
Expand Down

0 comments on commit a45b85d

Please sign in to comment.