Skip to content

Fix mt6 assert_nil warnings #2017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions test/action_controller/serialization_scope_name_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def json_key
end
end
class PostTestController < ActionController::Base
attr_accessor :current_user
attr_writer :current_user

def render_post_by_non_admin
self.current_user = User.new(id: 3, name: 'Pete', admin: false)
render json: new_post, serializer: serializer, adapter: :json
Expand All @@ -44,6 +45,10 @@ def render_post_by_admin
render json: new_post, serializer: serializer, adapter: :json
end

def current_user
defined?(@current_user) ? @current_user : :current_user_not_set
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a fun way to do it.

end

private

def new_post
Expand Down Expand Up @@ -75,7 +80,8 @@ def test_default_serialization_scope
end

def test_default_serialization_scope_object
assert_equal @controller.current_user, @controller.serialization_scope
assert_equal :current_user_not_set, @controller.current_user
assert_equal :current_user_not_set, @controller.serialization_scope
end

def test_default_scope_non_admin
Expand Down Expand Up @@ -125,7 +131,7 @@ def test_defined_serialization_scope
end

def test_defined_serialization_scope_object
assert_equal @controller.view_context.class, @controller.serialization_scope.class
assert_equal @controller.view_context.controller, @controller.serialization_scope.controller
end

def test_serialization_scope_non_admin
Expand Down
19 changes: 12 additions & 7 deletions test/active_model_serializers/railtie_test_isolated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
class RailtieTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation

class WithRails < RailtieTest
class WithRailsRequiredFirst < RailtieTest
setup do
require 'rails'
require 'active_model_serializers'
make_basic_app
make_basic_app do |app|
app.config.action_controller.perform_caching = true
end
end

test 'mixes ActionController::Serialization into ActionController::Base' do
Expand All @@ -32,14 +34,17 @@ class WithRails < RailtieTest

test 'it is configured for caching' do
assert_equal ActionController::Base.cache_store, ActiveModelSerializers.config.cache_store
assert_equal Rails.configuration.action_controller.perform_caching, ActiveModelSerializers.config.perform_caching
assert_equal true, Rails.configuration.action_controller.perform_caching
assert_equal true, ActiveModelSerializers.config.perform_caching
end
end

class WithoutRails < RailtieTest
class WithoutRailsRequiredFirst < RailtieTest
setup do
require 'active_model_serializers'
make_basic_app
make_basic_app do |app|
app.config.action_controller.perform_caching = true
end
end

test 'does not mix ActionController::Serialization into ActionController::Base' do
Expand All @@ -56,8 +61,8 @@ class WithoutRails < RailtieTest
test 'it is not configured for caching' do
refute_nil ActionController::Base.cache_store
assert_nil ActiveModelSerializers.config.cache_store
refute Rails.configuration.action_controller.perform_caching
refute ActiveModelSerializers.config.perform_caching
assert_equal true, Rails.configuration.action_controller.perform_caching
assert_nil ActiveModelSerializers.config.perform_caching
end
end
end
13 changes: 9 additions & 4 deletions test/adapter/json_api/include_data_if_sideloaded_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,25 @@ def test_block_relationship
end

def test_node_not_included_when_no_link
expected = nil
assert_relationship(:unlinked_tags, expected)
expected = { meta: {} }
assert_relationship(:unlinked_tags, expected, key_transform: :unaltered)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was returning nil before since the key was unaltered-tags. I'm not sure if meta: {} is the desired behavior, but it's what the current behavior is.

end

private

def assert_relationship(relationship_name, expected, opts = {})
actual = relationship_data(relationship_name, opts)
assert_equal(expected, actual)
end

def result(opts)
opts = { adapter: :json_api }.merge(opts)
serializable(@author, opts).serializable_hash
end

def assert_relationship(relationship_name, expected, opts = {})
def relationship_data(relationship_name, opts = {})
hash = result(opts)
assert_equal(expected, hash[:data][:relationships][relationship_name])
hash[:data][:relationships][relationship_name]
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def test_cache_definition
def test_cache_key_definition
assert_equal('post', @post_serializer.class._cache_key)
assert_equal('writer', @author_serializer.class._cache_key)
assert_equal(nil, @comment_serializer.class._cache_key)
assert_nil(@comment_serializer.class._cache_key)
end

def test_cache_key_interpolation_with_updated_at_when_cache_key_is_not_defined_on_object
Expand Down Expand Up @@ -171,7 +171,7 @@ def test_error_is_raised_if_cache_key_is_not_defined_on_object_or_passed_as_cach

def test_cache_options_definition
assert_equal({ expires_in: 0.1, skip_digest: true }, @post_serializer.class._cache_options)
assert_equal(nil, @blog_serializer.class._cache_options)
assert_nil(@blog_serializer.class._cache_options)
assert_equal({ expires_in: 1.day, skip_digest: true }, @comment_serializer.class._cache_options)
end

Expand All @@ -182,8 +182,8 @@ def test_fragment_cache_definition

def test_associations_separately_cache
cache_store.clear
assert_equal(nil, cache_store.fetch(@post.cache_key))
assert_equal(nil, cache_store.fetch(@comment.cache_key))
assert_nil(cache_store.fetch(@post.cache_key))
assert_nil(cache_store.fetch(@comment.cache_key))

Timecop.freeze(Time.current) do
render_object_with_cache(@post)
Expand Down
12 changes: 6 additions & 6 deletions test/serializers/caching_configuration_test_isolated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ class PerformCachingTrue < CachingConfigurationTest
end

test 'the non-cached serializer cache_store is nil' do
assert_equal nil, @non_cached_serializer._cache
assert_equal nil, @non_cached_serializer.cache_store
assert_equal nil, @non_cached_serializer._cache
assert_nil @non_cached_serializer._cache
assert_nil @non_cached_serializer.cache_store
assert_nil @non_cached_serializer._cache
end

test 'the non-cached serializer does not have cache_enabled?' do
Expand Down Expand Up @@ -136,9 +136,9 @@ class PerformCachingFalse < CachingConfigurationTest
end

test 'the non-cached serializer cache_store is nil' do
assert_equal nil, @non_cached_serializer._cache
assert_equal nil, @non_cached_serializer.cache_store
assert_equal nil, @non_cached_serializer._cache
assert_nil @non_cached_serializer._cache
assert_nil @non_cached_serializer.cache_store
assert_nil @non_cached_serializer._cache
end

test 'the non-cached serializer does not have cache_enabled?' do
Expand Down
12 changes: 6 additions & 6 deletions test/serializers/serializer_for_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def setup

def test_serializer_for_non_ams_serializer
serializer = ActiveModel::Serializer.serializer_for(@tweet)
assert_equal nil, serializer
assert_nil serializer
end

def test_serializer_for_existing_serializer
Expand All @@ -71,12 +71,12 @@ def test_serializer_for_existing_serializer_with_lookup_disabled
serializer = with_serializer_lookup_disabled do
ActiveModel::Serializer.serializer_for(@profile)
end
assert_equal nil, serializer
assert_nil serializer
end

def test_serializer_for_not_existing_serializer
serializer = ActiveModel::Serializer.serializer_for(@model)
assert_equal nil, serializer
assert_nil serializer
end

def test_serializer_inherited_serializer
Expand All @@ -88,7 +88,7 @@ def test_serializer_inherited_serializer_with_lookup_disabled
serializer = with_serializer_lookup_disabled do
ActiveModel::Serializer.serializer_for(@my_profile)
end
assert_equal nil, serializer
assert_nil serializer
end

def test_serializer_custom_serializer
Expand All @@ -114,7 +114,7 @@ def test_serializer_for_namespaced_resource_with_lookup_disabled
serializer = with_serializer_lookup_disabled do
ActiveModel::Serializer.serializer_for(post)
end
assert_equal nil, serializer
assert_nil serializer
end

def test_serializer_for_nested_resource
Expand All @@ -128,7 +128,7 @@ def test_serializer_for_nested_resource_with_lookup_disabled
serializer = with_serializer_lookup_disabled do
ResourceNamespace::PostSerializer.serializer_for(comment)
end
assert_equal nil, serializer
assert_nil serializer
end
end
end
Expand Down