-
Notifications
You must be signed in to change notification settings - Fork 532
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
resource identity comparable #1430
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require File.expand_path('../../../test_helper', __FILE__) | ||
require 'memory_profiler' | ||
|
||
class ResourceIdentity < ActiveSupport::TestCase | ||
|
||
def test_can_generate_a_consistent_hash_for_comparison | ||
rid = JSONAPI::ResourceIdentity.new(PostResource, 12) | ||
assert_equal(rid.hash, [PostResource, 12].hash) | ||
end | ||
|
||
def test_equality | ||
rid = JSONAPI::ResourceIdentity.new(PostResource, 12) | ||
rid2 = JSONAPI::ResourceIdentity.new(PostResource, 12) | ||
assert_equal(rid, rid2) # uses == internally | ||
assert rid.eql?(rid2) | ||
end | ||
|
||
def test_inequality | ||
rid = JSONAPI::ResourceIdentity.new(PostResource, 12) | ||
rid2 = JSONAPI::ResourceIdentity.new(PostResource, 13) | ||
refute_equal(rid, rid2) | ||
end | ||
|
||
def test_sorting_by_resource_class_name | ||
rid = JSONAPI::ResourceIdentity.new(CommentResource, 13) | ||
rid2 = JSONAPI::ResourceIdentity.new(PostResource, 13) | ||
rid3 = JSONAPI::ResourceIdentity.new(SectionResource, 13) | ||
assert_equal([rid2, rid3, rid].sort, [rid, rid2, rid3]) | ||
end | ||
|
||
def test_sorting_by_id_secondarily | ||
rid = JSONAPI::ResourceIdentity.new(PostResource, 12) | ||
rid2 = JSONAPI::ResourceIdentity.new(PostResource, 13) | ||
rid3 = JSONAPI::ResourceIdentity.new(PostResource, 14) | ||
|
||
assert_equal([rid2, rid3, rid].sort, [rid, rid2, rid3]) | ||
end | ||
|
||
def test_to_s | ||
rid = JSONAPI::ResourceIdentity.new(PostResource, 12) | ||
assert_equal(rid.to_s, 'PostResource:12') | ||
end | ||
|
||
def test_comparisons_return_nil_for_non_resource_identity | ||
rid = JSONAPI::ResourceIdentity.new(PostResource, 13) | ||
rid2 = "PostResource:13" | ||
assert_nil(rid <=> rid2) | ||
end | ||
|
||
def test_comparisons_allocate_no_new_memory | ||
rid = JSONAPI::ResourceIdentity.new(PostResource, 13) | ||
rid2 = JSONAPI::ResourceIdentity.new(PostResource, 13) | ||
allocation_report = MemoryProfiler.report do | ||
rid == rid2 | ||
end | ||
assert_equal 0, allocation_report.total_allocated | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comparsble includes some of the above methods. Is the override here intended?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the idea is we have a composite key and need to ensure we sort consistently even when given a polymorphic list of ResourceIdentities. This logic places grouping by type as a higher priority than by id. I chose that logic since it will seem more natural when ids are either UUIDs or integers.
I could remove the override of
==
as well, which might be more performant than thehash
call. I'm going to test this and see what makes the most sense. I wish I had focused on including Comparable earlier.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My gut was wrong and it like I should keep the override of
==
that useshash
as it's about twice as fast in the most common case where the resource klasses are the same:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also tested removing the check on
other_identity.is_a?(ResourceIdentity)
and it only saved a small amount of time:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job testing!