Skip to content

Commit 8c1d7dc

Browse files
authored
Merge pull request #980 from senid231/add-bug-report-templates
create bug report templates
2 parents ad12cba + c980031 commit 8c1d7dc

File tree

4 files changed

+294
-0
lines changed

4 files changed

+294
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ Or install it yourself as:
4848
4. Push to the branch (`git push origin my-new-feature`)
4949
5. Create a new Pull Request
5050

51+
## Did you find a bug?
52+
53+
* **Ensure the bug was not already reported** by searching on GitHub under [Issues](https://github.com/cerebris/jsonapi-resources/issues).
54+
55+
* If you're unable to find an open issue addressing the problem, [open a new one](https://github.com/cerebris/jsonapi-resources/issues/new).
56+
Be sure to include a **title and clear description**, as much relevant information as possible,
57+
and a **code sample** or an **executable test case** demonstrating the expected behavior that is not occurring.
58+
59+
* If possible, use the relevant bug report templates to create the issue.
60+
Simply copy the content of the appropriate template into a .rb file, make the necessary changes to demonstrate the issue,
61+
and **paste the content into the issue description**:
62+
* [**Rails 5** issues](https://github.com/cerebris/jsonapi-resources/blob/master/lib/bug_report_templates/rails_5_master.rb)
63+
64+
5165
## License
5266

5367
Copyright 2014-2016 Cerebris Corporation. MIT License (see LICENSE for details).
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
begin
2+
require 'bundler/inline'
3+
rescue LoadError => e
4+
STDERR.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
5+
raise e
6+
end
7+
8+
gemfile(true) do
9+
source 'https://rubygems.org'
10+
11+
gem 'rails', require: false
12+
gem 'sqlite3', platform: :mri
13+
14+
gem 'activerecord-jdbcsqlite3-adapter',
15+
git: 'https://github.com/jruby/activerecord-jdbc-adapter',
16+
branch: 'rails-5',
17+
platform: :jruby
18+
19+
gem 'jsonapi-resources', require: false
20+
end
21+
22+
# prepare active_record database
23+
require 'active_record'
24+
25+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
26+
ActiveRecord::Base.logger = Logger.new(STDOUT)
27+
28+
ActiveRecord::Schema.define do
29+
# Add your schema here
30+
create_table :your_models, force: true do |t|
31+
t.string :name
32+
end
33+
end
34+
35+
# create models
36+
class YourModel < ActiveRecord::Base
37+
end
38+
39+
# prepare rails app
40+
require 'action_controller/railtie'
41+
# require 'action_view/railtie'
42+
require 'jsonapi-resources'
43+
44+
class ApplicationController < ActionController::Base
45+
end
46+
47+
# prepare jsonapi resources and controllers
48+
class YourModelsController < ApplicationController
49+
include JSONAPI::ActsAsResourceController
50+
end
51+
52+
class YourModelResource < JSONAPI::Resource
53+
attribute :name
54+
filter :name
55+
end
56+
57+
class TestApp < Rails::Application
58+
config.root = File.dirname(__FILE__)
59+
config.logger = Logger.new(STDOUT)
60+
Rails.logger = config.logger
61+
62+
secrets.secret_token = 'secret_token'
63+
secrets.secret_key_base = 'secret_key_base'
64+
65+
config.eager_load = false
66+
end
67+
68+
# initialize app
69+
Rails.application.initialize!
70+
71+
JSONAPI.configure do |config|
72+
config.json_key_format = :underscored_key
73+
config.route_format = :underscored_key
74+
end
75+
76+
# draw routes
77+
Rails.application.routes.draw do
78+
jsonapi_resources :your_models, only: [:index, :create]
79+
end
80+
81+
# prepare tests
82+
require 'minitest/autorun'
83+
require 'rack/test'
84+
85+
# Replace this with the code necessary to make your test fail.
86+
class BugTest < Minitest::Test
87+
include Rack::Test::Methods
88+
89+
def json_api_headers
90+
{'Accept' => JSONAPI::MEDIA_TYPE, 'CONTENT_TYPE' => JSONAPI::MEDIA_TYPE}
91+
end
92+
93+
def test_index_your_models
94+
record = YourModel.create! name: 'John Doe'
95+
get '/your_models', nil, json_api_headers
96+
assert last_response.ok?
97+
json_response = JSON.parse(last_response.body)
98+
refute_nil json_response['data']
99+
refute_empty json_response['data']
100+
refute_empty json_response['data'].first
101+
assert record.id.to_s, json_response['data'].first['id']
102+
assert 'your_models', json_response['data'].first['type']
103+
assert({'name' => 'John Doe'}, json_response['data'].first['attributes'])
104+
end
105+
106+
def test_create_your_models
107+
json_request = {
108+
'data' => {
109+
type: 'your_models',
110+
attributes: {
111+
name: 'Jane Doe'
112+
}
113+
}
114+
}
115+
post '/your_models', json_request.to_json, json_api_headers
116+
assert last_response.created?
117+
refute_nil YourModel.find_by(name: 'Jane Doe')
118+
end
119+
120+
private
121+
122+
def app
123+
Rails.application
124+
end
125+
end
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
begin
2+
require 'bundler/inline'
3+
rescue LoadError => e
4+
STDERR.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
5+
raise e
6+
end
7+
8+
gemfile(true) do
9+
source 'https://rubygems.org'
10+
11+
gem 'rails', require: false
12+
gem 'sqlite3', platform: :mri
13+
14+
gem 'activerecord-jdbcsqlite3-adapter',
15+
git: 'https://github.com/jruby/activerecord-jdbc-adapter',
16+
branch: 'rails-5',
17+
platform: :jruby
18+
19+
if ENV['JSONAPI_RESOURCES_PATH']
20+
gem 'jsonapi-resources', path: ENV['JSONAPI_RESOURCES_PATH'], require: false
21+
else
22+
gem 'jsonapi-resources', git: 'https://github.com/cerebris/jsonapi-resources', require: false
23+
end
24+
25+
end
26+
27+
# prepare active_record database
28+
require 'active_record'
29+
30+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
31+
ActiveRecord::Base.logger = Logger.new(STDOUT)
32+
33+
ActiveRecord::Schema.define do
34+
# Add your schema here
35+
create_table :your_models, force: true do |t|
36+
t.string :name
37+
end
38+
end
39+
40+
# create models
41+
class YourModel < ActiveRecord::Base
42+
end
43+
44+
# prepare rails app
45+
require 'action_controller/railtie'
46+
# require 'action_view/railtie'
47+
require 'jsonapi-resources'
48+
49+
class ApplicationController < ActionController::Base
50+
end
51+
52+
# prepare jsonapi resources and controllers
53+
class YourModelsController < ApplicationController
54+
include JSONAPI::ActsAsResourceController
55+
end
56+
57+
class YourModelResource < JSONAPI::Resource
58+
attribute :name
59+
filter :name
60+
end
61+
62+
class TestApp < Rails::Application
63+
config.root = File.dirname(__FILE__)
64+
config.logger = Logger.new(STDOUT)
65+
Rails.logger = config.logger
66+
67+
secrets.secret_token = 'secret_token'
68+
secrets.secret_key_base = 'secret_key_base'
69+
70+
config.eager_load = false
71+
end
72+
73+
# initialize app
74+
Rails.application.initialize!
75+
76+
JSONAPI.configure do |config|
77+
config.json_key_format = :underscored_key
78+
config.route_format = :underscored_key
79+
end
80+
81+
# draw routes
82+
Rails.application.routes.draw do
83+
jsonapi_resources :your_models, only: [:index, :create]
84+
end
85+
86+
# prepare tests
87+
require 'minitest/autorun'
88+
require 'rack/test'
89+
90+
# Replace this with the code necessary to make your test fail.
91+
class BugTest < Minitest::Test
92+
include Rack::Test::Methods
93+
94+
def json_api_headers
95+
{'Accept' => JSONAPI::MEDIA_TYPE, 'CONTENT_TYPE' => JSONAPI::MEDIA_TYPE}
96+
end
97+
98+
def test_index_your_models
99+
record = YourModel.create! name: 'John Doe'
100+
get '/your_models', nil, json_api_headers
101+
assert last_response.ok?
102+
json_response = JSON.parse(last_response.body)
103+
refute_nil json_response['data']
104+
refute_empty json_response['data']
105+
refute_empty json_response['data'].first
106+
assert record.id.to_s, json_response['data'].first['id']
107+
assert 'your_models', json_response['data'].first['type']
108+
assert({'name' => 'John Doe'}, json_response['data'].first['attributes'])
109+
end
110+
111+
def test_create_your_models
112+
json_request = {
113+
'data' => {
114+
type: 'your_models',
115+
attributes: {
116+
name: 'Jane Doe'
117+
}
118+
}
119+
}
120+
post '/your_models', json_request.to_json, json_api_headers
121+
assert last_response.created?
122+
refute_nil YourModel.find_by(name: 'Jane Doe')
123+
end
124+
125+
private
126+
127+
def app
128+
Rails.application
129+
end
130+
end

test/bug_report_templates_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require File.expand_path('../test_helper', __FILE__)
2+
3+
class BugReportTemplatesTest < ActiveSupport::TestCase
4+
5+
def jsonapi_resources_root
6+
File.expand_path('../..', __FILE__)
7+
end
8+
9+
def chdir_path
10+
File.join(jsonapi_resources_root, 'lib', 'bug_report_templates')
11+
end
12+
13+
def assert_bug_report(file_name)
14+
Bundler.with_clean_env do
15+
Dir.chdir(chdir_path) do
16+
assert system({'JSONAPI_RESOURCES_PATH' => jsonapi_resources_root}, Gem.ruby, file_name)
17+
end
18+
end
19+
end
20+
21+
def test_rails_5
22+
assert_bug_report 'rails_5_master.rb'
23+
end
24+
25+
end

0 commit comments

Comments
 (0)