diff --git a/Guardfile b/Guardfile new file mode 100644 index 000000000..8a58a1cc4 --- /dev/null +++ b/Guardfile @@ -0,0 +1,42 @@ +# A sample Guardfile +# More info at https://github.com/guard/guard#readme + +## Uncomment and set this to only include directories you want to watch +# directories %w(app lib config test spec features) \ +# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")} + +## Note: if you are using the `directories` clause above and you are not +## watching the project directory ('.'), then you will want to move +## the Guardfile to a watched dir and symlink it back, e.g. +# +# $ mkdir config +# $ mv Guardfile config/ +# $ ln -s config/Guardfile . +# +# and, you'll have to watch "config/Guardfile" instead of "Guardfile" + +guard :minitest do + # with Minitest::Unit + watch(%r{^test/(.*)\/?(.*)_test\.rb$}) + watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" } + watch(%r{^test/test_helper\.rb$}) { 'test' } + + # with Minitest::Spec + # watch(%r{^spec/(.*)_spec\.rb$}) + # watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } + # watch(%r{^spec/spec_helper\.rb$}) { 'spec' } + + # Rails 4 + # watch(%r{^app/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" } + # watch(%r{^app/controllers/application_controller\.rb$}) { 'test/controllers' } + # watch(%r{^app/controllers/(.+)_controller\.rb$}) { |m| "test/integration/#{m[1]}_test.rb" } + # watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" } + # watch(%r{^lib/(.+)\.rb$}) { |m| "test/lib/#{m[1]}_test.rb" } + # watch(%r{^test/.+_test\.rb$}) + # watch(%r{^test/test_helper\.rb$}) { 'test' } + + # Rails < 4 + # watch(%r{^app/controllers/(.*)\.rb$}) { |m| "test/functional/#{m[1]}_test.rb" } + # watch(%r{^app/helpers/(.*)\.rb$}) { |m| "test/helpers/#{m[1]}_test.rb" } + # watch(%r{^app/models/(.*)\.rb$}) { |m| "test/unit/#{m[1]}_test.rb" } +end diff --git a/jsonapi-resources.gemspec b/jsonapi-resources.gemspec index 7bc6a8f5d..270ab0c0c 100644 --- a/jsonapi-resources.gemspec +++ b/jsonapi-resources.gemspec @@ -23,8 +23,12 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'rake' spec.add_development_dependency 'minitest' spec.add_development_dependency 'minitest-spec-rails' + spec.add_development_dependency 'minitest-focus' spec.add_development_dependency 'minitest-reporters' + spec.add_development_dependency 'awesome_print' spec.add_development_dependency 'simplecov' spec.add_development_dependency 'pry' + spec.add_development_dependency 'guard' + spec.add_development_dependency 'guard-minitest' spec.add_dependency 'rails', '>= 4.0' end diff --git a/test/fixtures/active_record.rb b/test/fixtures/active_record.rb index abd21e8c9..f94d66402 100644 --- a/test/fixtures/active_record.rb +++ b/test/fixtures/active_record.rb @@ -208,6 +208,15 @@ t.string :serial_number t.integer :person_id end + + create_table :parents, force: true do |t| + t.string :name + end + + create_table :children, force: true do |t| + t.string :name + t.integer :parent_id + end end ### MODELS @@ -440,6 +449,16 @@ class Product < ActiveRecord::Base has_one :picture, as: :imageable end +class Parent < ActiveRecord::Base + has_one :child + + accepts_nested_attributes_for :child +end + +class Child < ActiveRecord::Base + belongs_to :parent +end + ### OperationsProcessor class CountingActiveRecordOperationsProcessor < ActiveRecordOperationsProcessor after_find_operation do @@ -866,6 +885,28 @@ def _save end end +class ParentResource < JSONAPI::Resource + attribute :name + + has_one :child + + private + + def child_attributes=(attributes) + @model.child_attributes = attributes + end + + def self.creatable_fields(context) + super + [:child_attributes] + end +end + +class ChildResource < JSONAPI::Resource + attribute :name + + has_one :parent +end + class PlanetResource < JSONAPI::Resource attribute :name attribute :description diff --git a/test/fixtures/children.yml b/test/fixtures/children.yml new file mode 100644 index 000000000..edba5edea --- /dev/null +++ b/test/fixtures/children.yml @@ -0,0 +1,4 @@ +a: + id: 1 + name: Daniel Jr. + parent_id: 1 diff --git a/test/fixtures/parents.yml b/test/fixtures/parents.yml new file mode 100644 index 000000000..9ffdb8bd7 --- /dev/null +++ b/test/fixtures/parents.yml @@ -0,0 +1,3 @@ +a: + id: 1 + name: Daniel diff --git a/test/test_helper.rb b/test/test_helper.rb index 961b36aee..eca429d92 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -13,8 +13,11 @@ require 'rails/all' require 'rails/test_help' require 'minitest/mock' +require 'minitest/reporters' +require 'minitest/focus' require 'jsonapi-resources' require 'pry' +require 'awesome_print' require File.expand_path('../helpers/value_matchers', __FILE__) require File.expand_path('../helpers/assertions', __FILE__) @@ -256,6 +259,8 @@ class CatResource < JSONAPI::Resource # Ensure backward compatibility with Minitest 4 Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) +Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(color: true)] + class Minitest::Test include Helpers::Assertions include Helpers::ValueMatchers diff --git a/test/unit/operation/operations_processor_test.rb b/test/unit/operation/operations_processor_test.rb index 3e26ba965..a887a49d8 100644 --- a/test/unit/operation/operations_processor_test.rb +++ b/test/unit/operation/operations_processor_test.rb @@ -99,6 +99,36 @@ def test_create_multiple_resources assert_equal(Planet.count, count + 3) end + def test_create_single_resource_with_nested_attributes + op = TestOperationsProcessor.new() + + child_count = Child.count + parent_count = Parent.count + + operations = [ + JSONAPI::CreateResourceOperation.new(ParentResource, + data: { + attributes: { + 'name' => 'earth', + 'child_attributes' => { + 'name' => 'earth jr.' + } + } + }) + ] + + request = JSONAPI::Request.new + request.operations = operations + + operation_results = op.process(request) + + assert_kind_of(JSONAPI::OperationResults, operation_results) + assert_equal(:created, operation_results.results[0].code) + assert_equal(operation_results.results.size, 1) + assert_equal(Child.count, child_count + 1) + assert_equal(Parent.count, parent_count + 1) + end + def test_replace_to_one_relationship op = JSONAPI::OperationsProcessor.new() @@ -520,7 +550,7 @@ def test_safe_run_callback_catch_fail error = StandardError.new callback = ->(error) { nil.explosions} - result = op.send(:safe_run_callback, callback, error) + result = op.send(:safe_run_callback, callback, error) assert_instance_of(JSONAPI::ErrorsOperationResult, result) assert_equal(result.code, 500)