Created
February 23, 2017 15:58
-
-
Save senid231/4024ffbd54c2bb096837c15b5518e8cf to your computer and use it in GitHub Desktop.
This file contains 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
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', 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 :storages, force: true do |t| | |
t.string :token, null: false | |
t.string :name | |
t.timestamps null: false | |
end | |
create_table :keepers, force: true do |t| | |
t.string :name | |
t.integer :storage_id | |
end | |
end | |
# create models | |
class Storage < ActiveRecord::Base | |
has_one :keeper | |
end | |
class Keeper < ActiveRecord::Base | |
belongs_to :storage | |
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 StoragesController < JsonApiController | |
end | |
class KeepersController < JsonApiController | |
end | |
class StorageResource < JSONAPI::Resource | |
attribute :name | |
end | |
class KeeperResource < JSONAPI::Resource | |
has_one :storage | |
attribute :name | |
def self.updatable_fields(_context) | |
super - [:storage] | |
end | |
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 | |
config.raise_if_parameters_not_allowed = false | |
end | |
# draw routes | |
Rails.application.routes.draw do | |
jsonapi_resources :keepers, only: [:show, :update] | |
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_keeper_update_storage | |
storage = Storage.create! name: 'Warehouse 1', token: 'some-token' | |
keeper = Keeper.create! name: 'John Doe', storage: storage | |
refute_nil keeper.storage | |
update_json = { | |
data: { | |
anything: 'really' | |
} | |
} | |
patch "/keepers/#{keeper.id}/storage", update_json.to_json, json_api_headers | |
puts pretty_response_json | |
assert_equal 404, last_response.status | |
refute_nil keeper.storage | |
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