|
| 1 | +require File.expand_path('../../../test_helper', __FILE__) |
| 2 | +require 'memory_profiler' |
| 3 | + |
| 4 | +class ResourceIdentity < ActiveSupport::TestCase |
| 5 | + |
| 6 | + def test_can_generate_a_consistent_hash_for_comparison |
| 7 | + rid = JSONAPI::ResourceIdentity.new(PostResource, 12) |
| 8 | + assert_equal(rid.hash, [PostResource, 12].hash) |
| 9 | + end |
| 10 | + |
| 11 | + def test_equality |
| 12 | + rid = JSONAPI::ResourceIdentity.new(PostResource, 12) |
| 13 | + rid2 = JSONAPI::ResourceIdentity.new(PostResource, 12) |
| 14 | + assert_equal(rid, rid2) # uses == internally |
| 15 | + assert rid.eql?(rid2) |
| 16 | + end |
| 17 | + |
| 18 | + def test_inequality |
| 19 | + rid = JSONAPI::ResourceIdentity.new(PostResource, 12) |
| 20 | + rid2 = JSONAPI::ResourceIdentity.new(PostResource, 13) |
| 21 | + refute_equal(rid, rid2) |
| 22 | + end |
| 23 | + |
| 24 | + def test_sorting_by_resource_class_name |
| 25 | + rid = JSONAPI::ResourceIdentity.new(CommentResource, 13) |
| 26 | + rid2 = JSONAPI::ResourceIdentity.new(PostResource, 13) |
| 27 | + rid3 = JSONAPI::ResourceIdentity.new(SectionResource, 13) |
| 28 | + assert_equal([rid2, rid3, rid].sort, [rid, rid2, rid3]) |
| 29 | + end |
| 30 | + |
| 31 | + def test_sorting_by_id_secondarily |
| 32 | + rid = JSONAPI::ResourceIdentity.new(PostResource, 12) |
| 33 | + rid2 = JSONAPI::ResourceIdentity.new(PostResource, 13) |
| 34 | + rid3 = JSONAPI::ResourceIdentity.new(PostResource, 14) |
| 35 | + |
| 36 | + assert_equal([rid2, rid3, rid].sort, [rid, rid2, rid3]) |
| 37 | + end |
| 38 | + |
| 39 | + def test_to_s |
| 40 | + rid = JSONAPI::ResourceIdentity.new(PostResource, 12) |
| 41 | + assert_equal(rid.to_s, 'PostResource:12') |
| 42 | + end |
| 43 | + |
| 44 | + def test_comparisons_return_nil_for_non_resource_identity |
| 45 | + rid = JSONAPI::ResourceIdentity.new(PostResource, 13) |
| 46 | + rid2 = "PostResource:13" |
| 47 | + assert_nil(rid <=> rid2) |
| 48 | + end |
| 49 | + |
| 50 | + def test_comparisons_allocate_no_new_memory |
| 51 | + rid = JSONAPI::ResourceIdentity.new(PostResource, 13) |
| 52 | + rid2 = JSONAPI::ResourceIdentity.new(PostResource, 13) |
| 53 | + allocation_report = MemoryProfiler.report do |
| 54 | + rid == rid2 |
| 55 | + end |
| 56 | + assert_equal 0, allocation_report.total_allocated |
| 57 | + end |
| 58 | +end |
0 commit comments