test/controllers/controller_test.rb in jsonapi-resources-0.0.14 vs test/controllers/controller_test.rb in jsonapi-resources-0.0.15

- old
+ new

@@ -180,11 +180,11 @@ def test_sorting_by_multiple_fields get :index, {sort: 'title,body'} assert_response :success - assert_equal 8, json_response['posts'][0]['id'] + assert_equal '8', json_response['posts'][0]['id'] end def test_invalid_sort_param get :index, {sort: 'asdfg'} @@ -209,34 +209,34 @@ get :show, {id: '1'} assert_response :success assert json_response['posts'].is_a?(Hash) assert_equal 'New post', json_response['posts']['title'] assert_equal 'A body!!!', json_response['posts']['body'] - assert_equal [1, 2, 3], json_response['posts']['links']['tags'] - assert_equal [1, 2], json_response['posts']['links']['comments'] + assert_equal ['1', '2', '3'], json_response['posts']['links']['tags'] + assert_equal ['1', '2'], json_response['posts']['links']['comments'] assert_nil json_response['linked'] end def test_show_single_with_includes get :show, {id: '1', include: 'comments'} assert_response :success assert json_response['posts'].is_a?(Hash) assert_equal 'New post', json_response['posts']['title'] assert_equal 'A body!!!', json_response['posts']['body'] - assert_equal [1, 2, 3], json_response['posts']['links']['tags'] - assert_equal [1, 2], json_response['posts']['links']['comments'] + assert_equal ['1', '2', '3'], json_response['posts']['links']['tags'] + assert_equal ['1', '2'], json_response['posts']['links']['comments'] assert_equal 2, json_response['linked']['comments'].size assert_nil json_response['linked']['tags'] end def test_show_single_with_fields get :show, {id: '1', fields: 'author'} assert_response :success assert json_response['posts'].is_a?(Hash) assert_nil json_response['posts']['title'] assert_nil json_response['posts']['body'] - assert_equal 1, json_response['posts']['links']['author'] + assert_equal '1', json_response['posts']['links']['author'] end def test_show_single_invalid_id_format get :show, {id: 'asdfg'} assert_response :bad_request @@ -273,11 +273,11 @@ } } assert_response :created assert json_response['posts'].is_a?(Hash) - assert_equal 3, json_response['posts']['links']['author'] + assert_equal '3', json_response['posts']['links']['author'] assert_equal 'JR is Great', json_response['posts']['title'] assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.', json_response['posts']['body'] end def test_create_link_to_missing_object @@ -359,11 +359,11 @@ } assert_response :created assert json_response['posts'].is_a?(Array) assert_equal json_response['posts'].size, 2 - assert_equal json_response['posts'][0]['links']['author'], 3 + assert_equal json_response['posts'][0]['links']['author'], '3' assert_match /JR is Great/, response.body assert_match /Ember is Great/, response.body end def test_create_multiple_wrong_case @@ -436,14 +436,14 @@ } } assert_response :created assert json_response['posts'].is_a?(Hash) - assert_equal 3, json_response['posts']['links']['author'] + assert_equal '3', json_response['posts']['links']['author'] assert_equal 'JR is Great', json_response['posts']['title'] assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.', json_response['posts']['body'] - assert_equal [3, 4], json_response['posts']['links']['tags'] + assert_equal ['3', '4'], json_response['posts']['links']['tags'] end def test_create_with_links_include_and_fields post :create, { @@ -459,11 +459,11 @@ fields: 'id,title,author' } assert_response :created assert json_response['posts'].is_a?(Hash) - assert_equal 3, json_response['posts']['links']['author'] + assert_equal '3', json_response['posts']['links']['author'] assert_equal 'JR is Great!', json_response['posts']['title'] assert_equal nil, json_response['posts']['body'] assert_equal nil, json_response['posts']['links']['tags'] assert_not_nil json_response['linked']['posts'] assert_not_nil json_response['linked']['people'] @@ -485,15 +485,15 @@ } } assert_response :success assert json_response['posts'].is_a?(Hash) - assert_equal 3, json_response['posts']['links']['author'] - assert_equal javascript.id, json_response['posts']['links']['section'] + assert_equal '3', json_response['posts']['links']['author'] + assert_equal javascript.id.to_s, json_response['posts']['links']['section'] assert_equal 'A great new Post', json_response['posts']['title'] assert_equal 'AAAA', json_response['posts']['body'] - assert matches_array?([3, 4], json_response['posts']['links']['tags']) + assert matches_array?(['3', '4'], json_response['posts']['links']['tags']) end def test_update_remove_links put :update, { @@ -507,11 +507,11 @@ } } assert_response :success assert json_response['posts'].is_a?(Hash) - assert_equal 3, json_response['posts']['links']['author'] + assert_equal '3', json_response['posts']['links']['author'] assert_equal nil, json_response['posts']['links']['section'] assert_equal 'A great new Post', json_response['posts']['title'] assert_equal 'AAAA', json_response['posts']['body'] assert matches_array?([], json_response['posts']['links']['tags']) end @@ -804,21 +804,21 @@ } ]} assert_response :success assert_equal json_response['posts'].size, 2 - assert_equal json_response['posts'][0]['links']['author'], 3 - assert_equal json_response['posts'][0]['links']['section'], javascript.id + assert_equal json_response['posts'][0]['links']['author'], '3' + assert_equal json_response['posts'][0]['links']['section'], javascript.id.to_s assert_equal json_response['posts'][0]['title'], 'A great new Post QWERTY' assert_equal json_response['posts'][0]['body'], 'AAAA' - assert_equal json_response['posts'][0]['links']['tags'], [3, 4] + assert_equal json_response['posts'][0]['links']['tags'], ['3', '4'] - assert_equal json_response['posts'][1]['links']['author'], 3 - assert_equal json_response['posts'][1]['links']['section'], javascript.id + assert_equal json_response['posts'][1]['links']['author'], '3' + assert_equal json_response['posts'][1]['links']['section'], javascript.id.to_s assert_equal json_response['posts'][1]['title'], 'A great new Post ASDFG' assert_equal json_response['posts'][1]['body'], 'AAAA' - assert_equal json_response['posts'][1]['links']['tags'], [3, 4] + assert_equal json_response['posts'][1]['links']['tags'], ['3', '4'] end def test_update_multiple_missing_keys javascript = Section.find_by(name: 'javascript') @@ -1102,11 +1102,11 @@ fields: 'id,transaction_date,iso_currency,cost,employee' } assert_response :created assert json_response['expense_entries'].is_a?(Hash) - assert_equal 3, json_response['expense_entries']['links']['employee'] + assert_equal '3', json_response['expense_entries']['links']['employee'] assert_equal 'USD', json_response['expense_entries']['links']['iso_currency'] assert_equal 50.58, json_response['expense_entries']['cost'] delete :destroy, {id: json_response['expense_entries']['id']} assert_response :no_content @@ -1129,11 +1129,11 @@ fields: 'id,transactionDate,isoCurrency,cost,employee' } assert_response :created assert json_response['expenseEntries'].is_a?(Hash) - assert_equal 3, json_response['expenseEntries']['links']['employee'] + assert_equal '3', json_response['expenseEntries']['links']['employee'] assert_equal 'USD', json_response['expenseEntries']['links']['isoCurrency'] assert_equal 50.58, json_response['expenseEntries']['cost'] delete :destroy, {id: json_response['expenseEntries']['id']} assert_response :no_content @@ -1156,11 +1156,11 @@ fields: 'id,transaction-date,iso-currency,cost,employee' } assert_response :created assert json_response['expense-entries'].is_a?(Hash) - assert_equal 3, json_response['expense-entries']['links']['employee'] + assert_equal '3', json_response['expense-entries']['links']['employee'] assert_equal 'USD', json_response['expense-entries']['links']['iso-currency'] assert_equal 50.58, json_response['expense-entries']['cost'] delete :destroy, {id: json_response['expense-entries']['id']} assert_response :no_content @@ -1344,43 +1344,32 @@ def test_valid_filter_value get :index, {name: 'Joe Author'} assert_response :success assert_equal json_response['people'].size, 1 - assert_equal json_response['people'][0]['id'], 1 + assert_equal json_response['people'][0]['id'], '1' assert_equal json_response['people'][0]['name'], 'Joe Author' end end class AuthorsControllerTest < ActionController::TestCase def test_get_person_as_author get :index, {id: '1'} assert_response :success assert_equal 1, json_response['authors'].size - assert_equal 1, json_response['authors'][0]['id'] + assert_equal '1', json_response['authors'][0]['id'] assert_equal 'Joe Author', json_response['authors'][0]['name'] assert_equal nil, json_response['authors'][0]['email'] assert_equal 1, json_response['authors'][0]['links'].size assert_equal 3, json_response['authors'][0]['links']['posts'].size end - def test_get_person_as_author_variable_email - get :index, {id: '4'} - assert_response :success - assert_equal 1, json_response['authors'].size - assert_equal 4, json_response['authors'][0]['id'] - assert_equal 'Tag Crazy Author', json_response['authors'][0]['name'] - assert_equal 'taggy@xyz.fake', json_response['authors'][0]['email'] - assert_equal 1, json_response['authors'][0]['links'].size - assert_equal 2, json_response['authors'][0]['links']['posts'].size - end - def test_get_person_as_author_by_name_filter get :index, {name: 'thor'} assert_response :success assert_equal 3, json_response['authors'].size - assert_equal 1, json_response['authors'][0]['id'] + assert_equal '1', json_response['authors'][0]['id'] assert_equal 'Joe Author', json_response['authors'][0]['name'] assert_equal 1, json_response['authors'][0]['links'].size assert_equal 3, json_response['authors'][0]['links']['posts'].size end end @@ -1389,30 +1378,30 @@ # Note: Breed names go through the TitleValueFormatter def test_poro_index get :index assert_response :success - assert_equal 0, json_response['breeds'][0]['id'] + assert_equal '0', json_response['breeds'][0]['id'] assert_equal 'Persian', json_response['breeds'][0]['name'] end def test_poro_show get :show, {id: '0'} assert_response :success assert json_response['breeds'].is_a?(Hash) - assert_equal 0, json_response['breeds']['id'] + assert_equal '0', json_response['breeds']['id'] assert_equal 'Persian', json_response['breeds']['name'] end def test_poro_show_multiple get :show, {id: '0,2'} assert_response :success assert json_response['breeds'].is_a?(Array) assert_equal 2, json_response['breeds'].size - assert_equal 0, json_response['breeds'][0]['id'] + assert_equal '0', json_response['breeds'][0]['id'] assert_equal 'Persian', json_response['breeds'][0]['name'] - assert_equal 2, json_response['breeds'][1]['id'] + assert_equal '2', json_response['breeds'][1]['id'] assert_equal 'Sphinx', json_response['breeds'][1]['name'] end def test_poro_create_simple post :create, @@ -1465,10 +1454,74 @@ get :show assert_response :success end end +class Api::V1::PostsControllerTest < ActionController::TestCase + def test_show_post_namespaced + get :show, {id: '1'} + assert_response :success + assert_hash_equals( + { + posts: { + id: '1', + title: 'New post', + body: 'A body!!!', + subject: 'New post', + links: { + section: nil, + writer: '1', + comments: ['1', '2'] + } + } + }, json_response + ) + end + + def test_show_post_namespaced_include + get :show, {id: '1', include: 'writer'} + assert_response :success + assert_equal '1', json_response['posts']['links']['writer'] + assert_nil json_response['posts']['links']['tags'] + assert_equal '1', json_response['linked']['writers'][0]['id'] + assert_equal 'joe@xyz.fake', json_response['linked']['writers'][0]['email'] + end + + def test_index_filter_on_association_namespaced + get :index, {writer: '1'} + assert_response :success + assert_equal 3, json_response['posts'].size + end + + def test_sorting_desc_namespaced + get :index, {sort: '-title'} + + assert_response :success + assert_equal "Update This Later - Multiple", json_response['posts'][0]['title'] + end + + def test_create_simple_namespaced + post :create, + { + posts: { + title: 'JR - now with Namespacing', + body: 'JSONAPIResources is the greatest thing since unsliced bread now that it has namespaced resources.', + links: { + writer: '3' + } + } + } + + assert_response :created + assert json_response['posts'].is_a?(Hash) + assert_equal '3', json_response['posts']['links']['writer'] + assert_equal 'JR - now with Namespacing', json_response['posts']['title'] + assert_equal 'JSONAPIResources is the greatest thing since unsliced bread now that it has namespaced resources.', json_response['posts']['body'] + end + +end + class FactsControllerTest < ActionController::TestCase def test_type_formatting get :show, {id: '1'} assert_response :success assert json_response['facts'].is_a?(Hash) @@ -1481,6 +1534,5 @@ assert_equal '2000-01-01T20:00:00Z', json_response['facts']['bedtime'] assert_equal 'abc', json_response['facts']['photo'] assert_equal false, json_response['facts']['cool'] end end -