Created
November 11, 2020 12:53
-
-
Save larskanis/16af0f9e37eac17f27800468a5a6acab to your computer and use it in GitHub Desktop.
Trigger error on hash collision in jsonapi-resources
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# run this some hundred times per: | |
# while ruby test-error-hash.rb ; do echo; done | |
# | |
begin | |
require 'bundler/inline' | |
require 'bundler' | |
rescue LoadError => e | |
STDERR.puts 'Bundler version 1.10 or later is required. Please update your Bundler' | |
raise e | |
end | |
gemfile(true, ui: ENV['SILENT'] ? Bundler::UI::Silent.new : Bundler::UI::Shell.new) do | |
source 'https://rubygems.org' | |
gem 'rails', require: false | |
gem 'sqlite3', platform: :mri | |
gem 'jsonapi-resources', require: false | |
end | |
# prepare active_record database | |
require 'active_record' | |
class NullLogger < Logger | |
def initialize(*_args) | |
end | |
def add(*_args, &_block) | |
end | |
end | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Base.logger = ENV['SILENT'] ? NullLogger.new : Logger.new(STDOUT) | |
ActiveRecord::Migration.verbose = !ENV['SILENT'] | |
ActiveRecord::Schema.define do | |
# Add your schema here | |
create_table :posts, force: true do |t| | |
t.string :text | |
end | |
create_table :comments, force: true do |t| | |
t.string :text | |
t.references :post | |
end | |
end | |
# create models | |
class Post < ActiveRecord::Base | |
has_many :comments | |
end | |
class Comment < ActiveRecord::Base | |
belongs_to :posts | |
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 PostsController < ApplicationController | |
include JSONAPI::ActsAsResourceController | |
end | |
class CommentsController < ApplicationController | |
include JSONAPI::ActsAsResourceController | |
end | |
class PostResource < JSONAPI::Resource | |
attribute :text | |
filter :text | |
has_many :comments | |
end | |
class CommentResource < JSONAPI::Resource | |
has_one :post | |
attribute :text | |
filter :text | |
end | |
class TestApp < Rails::Application | |
config.root = File.dirname(__FILE__) | |
config.logger = ENV['SILENT'] ? NullLogger.new : Logger.new(STDOUT) | |
Rails.logger = config.logger | |
secrets.secret_token = 'secret_token' | |
secrets.secret_key_base = 'secret_key_base' | |
config.eager_load = false | |
config.hosts << "example.org" | |
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 :posts, only: [:index, :create] | |
jsonapi_resources :comments, only: [:index, :create] | |
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 json_api_headers | |
{'Accept' => JSONAPI::MEDIA_TYPE, 'CONTENT_TYPE' => JSONAPI::MEDIA_TYPE} | |
end | |
def test_index_your_models | |
1.times do | |
record = Post.new text: 'My post' | |
record.comments.build(text: 'My comment') | |
record.save! | |
get "/posts/#{record.id}/comments", nil, json_api_headers | |
assert last_response.ok? | |
end | |
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