test/controllers/controller_test.rb in jsonapi-resources-0.6.1 vs test/controllers/controller_test.rb in jsonapi-resources-0.6.2
- old
+ new
@@ -35,14 +35,14 @@
original_config = JSONAPI.configuration.dup
JSONAPI.configuration.operations_processor = :error_raising
JSONAPI.configuration.exception_class_whitelist = []
@controller.class.instance_variable_set(:@callback_message, "none")
- @controller.class.on_server_error do
+ BaseController.on_server_error do
@controller.class.instance_variable_set(:@callback_message, "Sent from block")
end
-
+
get :index
assert_equal @controller.class.instance_variable_get(:@callback_message), "Sent from block"
# test that it renders the default server error response
assert_equal "Internal Server Error", json_response['errors'][0]['title']
@@ -52,11 +52,11 @@
end
def test_on_server_error_method_callback_with_exception
original_config = JSONAPI.configuration.dup
JSONAPI.configuration.operations_processor = :error_raising
- JSONAPI.configuration.exception_class_whitelist = []
+ JSONAPI.configuration.exception_class_whitelist = []
#ignores methods that don't exist
@controller.class.on_server_error :set_callback_message, :a_bogus_method
@controller.class.instance_variable_set(:@callback_message, "none")
@@ -68,11 +68,11 @@
ensure
JSONAPI.configuration = original_config
end
def test_on_server_error_callback_without_exception
-
+
callback = Proc.new { @controller.class.instance_variable_set(:@callback_message, "Sent from block") }
@controller.class.on_server_error callback
@controller.class.instance_variable_set(:@callback_message, "none")
get :index
@@ -1220,11 +1220,11 @@
PostResource.has_many :special_tags, relation_name: :special_tags, class_name: "Tag"
post :create_relationship, {post_id: 14, relationship: 'special_tags', data: [{type: 'tags', id: 2}]}
#check the relationship was created successfully
assert_equal 1, Post.find(14).special_tags.count
- before_tags = Post.find(14).tags.count
+ before_tags = Post.find(14).tags.count
delete :destroy_relationship, {post_id: 14, relationship: 'special_tags', data: [{type: 'tags', id: 2}]}
assert_equal 0, Post.find(14).special_tags.count, "Relationship that matches URL relationship not destroyed"
#check that the tag association is not affected
@@ -2262,17 +2262,94 @@
assert_equal 'authors', json_response['data'][0]['type']
assert_equal 'Joe Author', json_response['data'][0]['attributes']['name']
assert_equal nil, json_response['data'][0]['attributes']['email']
end
+ def test_show_person_as_author
+ get :show, {id: '1'}
+ assert_response :success
+ assert_equal '1', json_response['data']['id']
+ assert_equal 'authors', json_response['data']['type']
+ assert_equal 'Joe Author', json_response['data']['attributes']['name']
+ assert_equal nil, json_response['data']['attributes']['email']
+ end
+
def test_get_person_as_author_by_name_filter
get :index, {filter: {name: 'thor'}}
assert_response :success
assert_equal 3, json_response['data'].size
assert_equal '1', json_response['data'][0]['id']
assert_equal 'Joe Author', json_response['data'][0]['attributes']['name']
end
+
+ def test_meta_serializer_options
+ JSONAPI.configuration.json_key_format = :camelized_key
+
+ Api::V5::AuthorResource.class_eval do
+ def meta(options)
+ {
+ fixed: 'Hardcoded value',
+ computed: "#{self.class._type.to_s}: #{options[:serializer].url_generator.self_link(self)}",
+ computed_foo: options[:serialization_options][:foo],
+ options[:serializer].format_key('test_key') => 'test value'
+ }
+ end
+ end
+
+ get :show, {id: '1'}
+ assert_response :success
+ assert_equal '1', json_response['data']['id']
+ assert_equal 'Hardcoded value', json_response['data']['meta']['fixed']
+ assert_equal 'authors: http://test.host/api/v5/authors/1', json_response['data']['meta']['computed']
+ assert_equal 'bar', json_response['data']['meta']['computed_foo']
+ assert_equal 'test value', json_response['data']['meta']['testKey']
+
+ ensure
+ JSONAPI.configuration.json_key_format = :dasherized_key
+ Api::V5::AuthorResource.class_eval do
+ def meta(options)
+ # :nocov:
+ { }
+ # :nocov:
+ end
+ end
+ end
+
+ def test_meta_serializer_hash_data
+ JSONAPI.configuration.json_key_format = :camelized_key
+
+ Api::V5::AuthorResource.class_eval do
+ def meta(options)
+ {
+ custom_hash: {
+ fixed: 'Hardcoded value',
+ computed: "#{self.class._type.to_s}: #{options[:serializer].url_generator.self_link(self)}",
+ computed_foo: options[:serialization_options][:foo],
+ options[:serializer].format_key('test_key') => 'test value'
+ }
+ }
+ end
+ end
+
+ get :show, {id: '1'}
+ assert_response :success
+ assert_equal '1', json_response['data']['id']
+ assert_equal 'Hardcoded value', json_response['data']['meta']['custom_hash']['fixed']
+ assert_equal 'authors: http://test.host/api/v5/authors/1', json_response['data']['meta']['custom_hash']['computed']
+ assert_equal 'bar', json_response['data']['meta']['custom_hash']['computed_foo']
+ assert_equal 'test value', json_response['data']['meta']['custom_hash']['testKey']
+
+ ensure
+ JSONAPI.configuration.json_key_format = :dasherized_key
+ Api::V5::AuthorResource.class_eval do
+ def meta(options)
+ # :nocov:
+ { }
+ # :nocov:
+ end
+ end
+ end
end
class BreedsControllerTest < ActionController::TestCase
# Note: Breed names go through the TitleValueFormatter
@@ -3156,6 +3233,6 @@
make: 'Toyota',
}
}
end
end
-end
\ No newline at end of file
+end