test/controllers/controller_test.rb in jsonapi-resources-0.5.0 vs test/controllers/controller_test.rb in jsonapi-resources-0.5.1

- old
+ new

@@ -1425,17 +1425,10 @@ delete :destroy, {id: '5,6,99999'} assert_response :not_found assert_equal initial_count, Post.count end - def test_delete_extra_param - initial_count = Post.count - delete :destroy, {id: '4', asdfg: 'aaaa'} - assert_response :bad_request - assert_equal initial_count, Post.count - end - def test_show_to_one_relationship get :show_relationship, {post_id: '1', relationship: 'author'} assert_response :success assert_hash_equals json_response, {data: { @@ -2157,15 +2150,12 @@ end end class FactsControllerTest < ActionController::TestCase - def setup - JSONAPI.configuration.json_key_format = :camelized_key - end - def test_type_formatting + JSONAPI.configuration.json_key_format = :camelized_key get :show, {id: '1'} assert_response :success assert json_response['data'].is_a?(Hash) assert_equal 'Jane Author', json_response['data']['attributes']['spouseName'] assert_equal 'First man to run across Antartica.', json_response['data']['attributes']['bio'] @@ -2175,10 +2165,44 @@ assert_equal '1965-06-30', json_response['data']['attributes']['birthday'] assert_equal '2000-01-01T20:00:00Z', json_response['data']['attributes']['bedtime'] assert_equal 'abc', json_response['data']['attributes']['photo'] assert_equal false, json_response['data']['attributes']['cool'] end + + def test_create_with_invalid_data + JSONAPI.configuration.json_key_format = :dasherized_key + set_content_type_header! + post :create, + { + data: { + type: 'facts', + attributes: { + bio: '', + :"quality-rating" => '', + :"spouse-name" => '', + salary: 100000, + :"date-time-joined" => '', + birthday: '', + bedtime: '', + photo: 'abc', + cool: false + }, + relationships: { + } + } + } + + assert_response :unprocessable_entity + + assert_equal "/data/attributes/spouse-name", json_response['errors'][0]['source']['pointer'] + assert_equal "can't be blank", json_response['errors'][0]['detail'] + assert_equal "spouse-name - can't be blank", json_response['errors'][0]['title'] + + assert_equal "/data/attributes/bio", json_response['errors'][1]['source']['pointer'] + assert_equal "can't be blank", json_response['errors'][1]['detail'] + assert_equal "bio - can't be blank", json_response['errors'][1]['title'] + end end class Api::V2::BooksControllerTest < ActionController::TestCase def setup JSONAPI.configuration.json_key_format = :dasherized_key @@ -2630,7 +2654,68 @@ } } assert_response :unprocessable_entity assert_match /Save failed or was cancelled/, json_response['errors'][0]['detail'] + end +end + +class Api::V1::MoonsControllerTest < ActionController::TestCase + def test_get_related_resource + get :get_related_resource, {crater_id: 'S56D', relationship: 'moon', source: "api/v1/craters"} + assert_response :success + assert_hash_equals json_response, + { + data: { + id: "1", + type: "moons", + links: {self: "http://test.host/moons/1"}, + attributes: {name: "Titan", description: "Best known of the Saturn moons."}, + relationships: { + planet: {links: {self: "http://test.host/moons/1/relationships/planet", related: "http://test.host/moons/1/planet"}}, + craters: {links: {self: "http://test.host/moons/1/relationships/craters", related: "http://test.host/moons/1/craters"}}} + } + } + + end +end + +class Api::V1::CratersControllerTest < ActionController::TestCase + def test_show_single + get :show, {id: 'S56D'} + assert_response :success + assert json_response['data'].is_a?(Hash) + assert_equal 'S56D', json_response['data']['attributes']['code'] + assert_equal 'Very large crater', json_response['data']['attributes']['description'] + assert_nil json_response['included'] + end + + def test_get_related_resources + get :get_related_resources, {moon_id: '1', relationship: 'craters', source: "api/v1/moons"} + assert_response :success + assert_hash_equals json_response, + { + data: [ + {id:"A4D3", + type:"craters", + links:{self: "http://test.host/craters/A4D3"}, + attributes:{code: "A4D3", description: "Small crater"}, + relationships:{moon: {links: {self: "http://test.host/craters/A4D3/relationships/moon", related: "http://test.host/craters/A4D3/moon"}}} + }, + {id: "S56D", + type: "craters", + links:{self: "http://test.host/craters/S56D"}, + attributes:{code: "S56D", description: "Very large crater"}, + relationships:{moon: {links: {self: "http://test.host/craters/S56D/relationships/moon", related: "http://test.host/craters/S56D/moon"}}} + } + ] + } + end + + def test_show_relationship + get :show_relationship, {crater_id: 'S56D', relationship: 'moon'} + + assert_response :success + assert_equal "moons", json_response['data']['type'] + assert_equal "1", json_response['data']['id'] end end