|
| 1 | +require File.expand_path('../../../test_helper', __FILE__) |
| 2 | +require 'jsonapi-resources' |
| 3 | +require 'json' |
| 4 | + |
| 5 | +class ResponseDocumentTest < ActionDispatch::IntegrationTest |
| 6 | + def setup |
| 7 | + JSONAPI.configuration.json_key_format = :dasherized_key |
| 8 | + JSONAPI.configuration.route_format = :dasherized_route |
| 9 | + end |
| 10 | + |
| 11 | + def create_response_document(operation_results, resource_klass) |
| 12 | + JSONAPI::ResponseDocument.new( |
| 13 | + operation_results, |
| 14 | + { |
| 15 | + primary_resource_klass: resource_klass |
| 16 | + } |
| 17 | + ) |
| 18 | + end |
| 19 | + |
| 20 | + def test_response_document |
| 21 | + operations = [ |
| 22 | + JSONAPI::CreateResourceOperation.new(PlanetResource, data: {attributes: {'name' => 'Earth 2.0'}}), |
| 23 | + JSONAPI::CreateResourceOperation.new(PlanetResource, data: {attributes: {'name' => 'Vulcan'}}) |
| 24 | + ] |
| 25 | + |
| 26 | + request = JSONAPI::Request.new |
| 27 | + request.operations = operations |
| 28 | + |
| 29 | + op = BasicOperationsProcessor.new() |
| 30 | + operation_results = op.process(request) |
| 31 | + |
| 32 | + response_doc = create_response_document(operation_results, PlanetResource) |
| 33 | + |
| 34 | + assert_equal :created, response_doc.status |
| 35 | + contents = response_doc.contents |
| 36 | + assert contents.is_a?(Hash) |
| 37 | + assert contents[:data].is_a?(Array) |
| 38 | + assert_equal 2, contents[:data].size |
| 39 | + end |
| 40 | + |
| 41 | + def test_response_document_multiple_find |
| 42 | + operations = [ |
| 43 | + JSONAPI::FindOperation.new(PostResource, filters: {id: '1'}), |
| 44 | + JSONAPI::FindOperation.new(PostResource, filters: {id: '2'}) |
| 45 | + ] |
| 46 | + |
| 47 | + request = JSONAPI::Request.new |
| 48 | + request.operations = operations |
| 49 | + |
| 50 | + op = ActiveRecordOperationsProcessor.new() |
| 51 | + operation_results = op.process(request) |
| 52 | + |
| 53 | + response_doc = create_response_document(operation_results, PostResource) |
| 54 | + |
| 55 | + assert_equal :ok, response_doc.status |
| 56 | + contents = response_doc.contents |
| 57 | + assert contents.is_a?(Hash) |
| 58 | + assert contents[:data].is_a?(Array) |
| 59 | + assert_equal 2, contents[:data].size |
| 60 | + end |
| 61 | +end |
0 commit comments