Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JR Test of using a model named `case`
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 'activerecord-jdbcsqlite3-adapter', '52.0', platform: :jruby
if ENV['JSONAPI_RESOURCES_PATH']
gem 'jsonapi-resources', path: ENV['JSONAPI_RESOURCES_PATH'], require: false
else
gem 'jsonapi-resources', git: 'https://github.com/cerebris/jsonapi-resources', require: false
end
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 :entries, force: true do |t|
t.string :name
t.integer :case_id
end
create_table :cases, force: true do |t|
t.string :name
end
end
# create models
class Entry < ActiveRecord::Base
belongs_to :case
end
class Case < ActiveRecord::Base
has_many :entries
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 EntriesController < ApplicationController
include JSONAPI::ActsAsResourceController
end
class CasesController < ApplicationController
include JSONAPI::ActsAsResourceController
end
class EntryResource < JSONAPI::Resource
attribute :name
has_one :case
end
class CaseResource < JSONAPI::Resource
attribute :name
has_many :entries
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.clear
end
# initialize app
TestApp.initialize!
JSONAPI.configure do |config|
config.json_key_format = :underscored_key
config.route_format = :underscored_key
end
# draw routes
TestApp.routes.draw do
jsonapi_resources :cases
jsonapi_resources :entries
end
# prepare tests
require 'minitest/autorun'
require 'rack/test'
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
case1 = Case.create! name: 'case1'
entry1 = Entry.create! name: 'entry1', case_id: case1.id
get '/cases?include=entries', nil, json_api_headers
assert last_response.ok?
json_response = JSON.parse(last_response.body)
puts json_response
puts "------\n"
get '/entries?include=case', nil, json_api_headers
assert last_response.ok?
json_response = JSON.parse(last_response.body)
puts json_response
end
private
def app
TestApp
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.