Skip to content

Instantly share code, notes, and snippets.

@senid231
Created April 20, 2017 14:07
Show Gist options
  • Save senid231/4face22d00f342b22fcddbaf6e59466e to your computer and use it in GitHub Desktop.
Save senid231/4face22d00f342b22fcddbaf6e59466e to your computer and use it in GitHub Desktop.
bug with linkage to_one belongs_to relationship with overridden primary_key
begin
require 'bundler/inline'
rescue LoadError => e
STDERR.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', '4.2.7', require: false
gem 'sqlite3', platform: :mri
gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
# gem 'jsonapi-resources', '0.8.0', require: false
# gem 'jsonapi-resources', require: false
gem 'jsonapi-resources', path: '../..', require: false
# gem 'jsonapi-resources', git: 'https://github.com/cerebris/jsonapi-resources', require: false
end
# prepare active_record database
require 'active_record'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
# Add your schema here
create_table :access_cards, force: true do |t|
t.string :token, null: false
t.string :security_level
t.timestamps null: false
end
create_table :workers, force: true do |t|
t.string :name
t.integer :access_card_id, null: false
t.timestamps null: false
end
end
# create models
class AccessCard < ActiveRecord::Base
has_one :worker, class_name: 'Worker'
end
class Worker < ActiveRecord::Base
belongs_to :access_card
end
# prepare rails app
require 'action_controller/railtie'
# require 'action_view/railtie'
require 'jsonapi-resources'
class ApplicationController < ActionController::Base
end
# prepare jsonapi resources and controllers
class JsonApiController < ApplicationController
include JSONAPI::ActsAsResourceController
end
class AccessCardsController < JsonApiController
end
class WorkersController < JsonApiController
end
class AccessCardResource < JSONAPI::Resource
model_name 'AccessCard'
key_type :string
primary_key :token
attribute :security_level
end
class WorkerResource < JSONAPI::Resource
model_name 'Worker'
has_one :access_card
attribute :name
end
class TestApp < Rails::Application
config.root = File.dirname(__FILE__)
config.logger = Logger.new(STDOUT)
Rails.logger = config.logger
secrets.secret_token = 'secret_token'
secrets.secret_key_base = 'secret_key_base'
config.eager_load = false
end
# initialize app
Rails.application.initialize!
JSONAPI.configure do |config|
config.json_key_format = :underscored_key
config.route_format = :underscored_key
end
# draw routes
Rails.application.routes.draw do
jsonapi_resources :workers, only: [:show]
end
# prepare tests
require 'minitest/autorun'
require 'rack/test'
# Replace this with the code necessary to make your test fail.
class BugTest < Minitest::Test
include Rack::Test::Methods
def response_json
JSON.parse(last_response.body) rescue nil
end
def pretty_response_json
JSON.pretty_generate(response_json) rescue nil
end
def json_api_headers
{'Accept' => JSONAPI::MEDIA_TYPE, 'CONTENT_TYPE' => JSONAPI::MEDIA_TYPE}
end
def test_show_worker_include_access_card
access_card = AccessCard.create! security_level: 'admin', token: 'some-token'
worker = Worker.create! name: 'John Doe', access_card: access_card
get "/workers/#{worker.id}", {include: 'access_card'}, json_api_headers
puts pretty_response_json
assert last_response.ok?
data = response_json['data']
refute_nil data
assert_equal worker.id.to_s, data['id']
refute_nil data['relationships']
refute_nil data['relationships']['access_card']
refute_nil data['relationships']['access_card']['data']
assert_equal 'access_cards', data['relationships']['access_card']['data']['type']
assert_equal access_card.token, data['relationships']['access_card']['data']['id']
included = response_json['included']
refute_nil included
assert_equal 'access_cards', included.first['type']
assert_equal access_card.token, included.first['id']
end
private
def app
Rails.application
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment