Skip to content

Commit 2650778

Browse files
authored
Merge pull request #164 from Shopify/uuid-locate-many-test
[Tests only] Add `Locator#locate_many` tests for non `id` primary keys
2 parents 1eff550 + 1105a6c commit 2650778

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

test/cases/global_id_test.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
4141
setup do
4242
@uuid = '7ef9b614-353c-43a1-a203-ab2307851990'
4343
@person_gid = GlobalID.create(Person.new(5))
44-
@person_uuid_gid = GlobalID.create(Person.new(@uuid))
44+
@person_uuid_gid = GlobalID.create(PersonUuid.new(@uuid))
4545
@person_namespaced_gid = GlobalID.create(Person::Child.new(4))
4646
@person_model_gid = GlobalID.create(PersonModel.new(id: 1))
4747
@cpk_model_gid = GlobalID.create(CompositePrimaryKeyModel.new(id: ["tenant-key-value", "id-value"]))
@@ -136,7 +136,7 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
136136

137137
test 'as string' do
138138
assert_equal 'gid://bcx/Person/5', @person_gid.to_s
139-
assert_equal "gid://bcx/Person/#{@uuid}", @person_uuid_gid.to_s
139+
assert_equal "gid://bcx/PersonUuid/#{@uuid}", @person_uuid_gid.to_s
140140
assert_equal 'gid://bcx/Person::Child/4', @person_namespaced_gid.to_s
141141
assert_equal 'gid://bcx/PersonModel/1', @person_model_gid.to_s
142142
assert_equal 'gid://bcx/CompositePrimaryKeyModel/tenant-key-value/id-value', @cpk_model_gid.to_s
@@ -146,8 +146,8 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
146146
assert_equal 'Z2lkOi8vYmN4L1BlcnNvbi81', @person_gid.to_param
147147
assert_equal @person_gid, GlobalID.parse('Z2lkOi8vYmN4L1BlcnNvbi81')
148148

149-
assert_equal 'Z2lkOi8vYmN4L1BlcnNvbi83ZWY5YjYxNC0zNTNjLTQzYTEtYTIwMy1hYjIzMDc4NTE5OTA', @person_uuid_gid.to_param
150-
assert_equal @person_uuid_gid, GlobalID.parse('Z2lkOi8vYmN4L1BlcnNvbi83ZWY5YjYxNC0zNTNjLTQzYTEtYTIwMy1hYjIzMDc4NTE5OTA')
149+
assert_equal 'Z2lkOi8vYmN4L1BlcnNvblV1aWQvN2VmOWI2MTQtMzUzYy00M2ExLWEyMDMtYWIyMzA3ODUxOTkw', @person_uuid_gid.to_param
150+
assert_equal @person_uuid_gid, GlobalID.parse('Z2lkOi8vYmN4L1BlcnNvblV1aWQvN2VmOWI2MTQtMzUzYy00M2ExLWEyMDMtYWIyMzA3ODUxOTkw')
151151

152152
assert_equal 'Z2lkOi8vYmN4L1BlcnNvbjo6Q2hpbGQvNA', @person_namespaced_gid.to_param
153153
assert_equal @person_namespaced_gid, GlobalID.parse('Z2lkOi8vYmN4L1BlcnNvbjo6Q2hpbGQvNA')
@@ -162,7 +162,7 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
162162

163163
test 'as URI' do
164164
assert_equal URI('gid://bcx/Person/5'), @person_gid.uri
165-
assert_equal URI("gid://bcx/Person/#{@uuid}"), @person_uuid_gid.uri
165+
assert_equal URI("gid://bcx/PersonUuid/#{@uuid}"), @person_uuid_gid.uri
166166
assert_equal URI('gid://bcx/Person::Child/4'), @person_namespaced_gid.uri
167167
assert_equal URI('gid://bcx/PersonModel/1'), @person_model_gid.uri
168168
assert_equal URI('gid://bcx/CompositePrimaryKeyModel/tenant-key-value/id-value'), @cpk_model_gid.uri
@@ -172,8 +172,8 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
172172
assert_equal 'gid://bcx/Person/5', @person_gid.as_json
173173
assert_equal '"gid://bcx/Person/5"', @person_gid.to_json
174174

175-
assert_equal "gid://bcx/Person/#{@uuid}", @person_uuid_gid.as_json
176-
assert_equal "\"gid://bcx/Person/#{@uuid}\"", @person_uuid_gid.to_json
175+
assert_equal "gid://bcx/PersonUuid/#{@uuid}", @person_uuid_gid.as_json
176+
assert_equal "\"gid://bcx/PersonUuid/#{@uuid}\"", @person_uuid_gid.to_json
177177

178178
assert_equal 'gid://bcx/Person::Child/4', @person_namespaced_gid.as_json
179179
assert_equal '"gid://bcx/Person::Child/4"', @person_namespaced_gid.to_json
@@ -195,15 +195,15 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
195195

196196
test 'model name' do
197197
assert_equal 'Person', @person_gid.model_name
198-
assert_equal 'Person', @person_uuid_gid.model_name
198+
assert_equal 'PersonUuid', @person_uuid_gid.model_name
199199
assert_equal 'Person::Child', @person_namespaced_gid.model_name
200200
assert_equal 'PersonModel', @person_model_gid.model_name
201201
assert_equal 'CompositePrimaryKeyModel', @cpk_model_gid.model_name
202202
end
203203

204204
test 'model class' do
205205
assert_equal Person, @person_gid.model_class
206-
assert_equal Person, @person_uuid_gid.model_class
206+
assert_equal PersonUuid, @person_uuid_gid.model_class
207207
assert_equal Person::Child, @person_namespaced_gid.model_class
208208
assert_equal PersonModel, @person_model_gid.model_class
209209
assert_equal CompositePrimaryKeyModel, @cpk_model_gid.model_class

test/cases/global_locator_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class GlobalLocatorTest < ActiveSupport::TestCase
66
@gid = model.to_gid
77
@sgid = model.to_sgid
88
@cpk_model = CompositePrimaryKeyModel.new(id: ["tenant-key-value", "id-value"])
9+
@uuid_pk_model = PersonUuid.new('7ef9b614-353c-43a1-a203-ab2307851990')
910
@cpk_gid = @cpk_model.to_gid
1011
@cpk_sgid = @cpk_model.to_sgid
1112
end
@@ -86,6 +87,17 @@ class GlobalLocatorTest < ActiveSupport::TestCase
8687
GlobalID::Locator.locate_many([ Person.new('1').to_gid, Person.new('2').to_gid ])
8788
end
8889

90+
test 'by many GIDs of a UUID pk class' do
91+
expected = [ @uuid_pk_model, @uuid_pk_model ]
92+
assert_equal expected, GlobalID::Locator.locate_many(expected.map(&:to_gid))
93+
end
94+
95+
test 'by many GIDs of a UUID pk class with ignore missing' do
96+
gids_to_locate = [ @uuid_pk_model, PersonUuid.new(Person::HARDCODED_ID_FOR_MISSING_PERSON), @uuid_pk_model ]
97+
expected = [ @uuid_pk_model, @uuid_pk_model ]
98+
assert_equal expected, GlobalID::Locator.locate_many(gids_to_locate.map(&:to_gid), ignore_missing: true)
99+
end
100+
89101
test '#locate_many by composite primary key GIDs of the same class' do
90102
records = [ @cpk_model, CompositePrimaryKeyModel.new(id: ["tenant-key-value2", "id-value2"]) ]
91103
located = GlobalID::Locator.locate_many(records.map(&:to_gid))

test/models/person.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def self.find(id_or_ids)
2525
end
2626

2727
def self.where(conditions)
28-
(conditions[:id] - [HARDCODED_ID_FOR_MISSING_PERSON]).collect { |id| new(id) }
28+
(conditions[primary_key] - [HARDCODED_ID_FOR_MISSING_PERSON]).collect { |id| new(id) }
2929
end
3030

3131
def initialize(id = 1)
@@ -37,6 +37,12 @@ def ==(other)
3737
end
3838
end
3939

40+
class PersonUuid < Person
41+
def self.primary_key
42+
:uuid
43+
end
44+
end
45+
4046
class Person::Scoped < Person
4147
def initialize(*)
4248
super

0 commit comments

Comments
 (0)