Skip to content

Commit 7789b66

Browse files
committed
Ensure shelflist list view gets multi from Redis
In the new shelflistItemSerializer code that fetches and caches row numbers from Redis, I had misused the RedisObject.get_index function such that a call like `get_index(*item_ids)` returned a singular index position when there is one item_id. I changed this to `get(item_ids, 'values')` to force it to return a list of positions no matter what.
1 parent a78914c commit 7789b66

2 files changed

Lines changed: 35 additions & 10 deletions

File tree

django/sierra/shelflist/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def refresh_row_numbers(self):
132132
row_numbers = self._lookup_cache.get('row_numbers', {})
133133
location_code = self.context['view'].kwargs['code']
134134
r = RedisObject('shelflistitem_manifest', location_code)
135-
fetched = r.get_index(*self.item_ids)
135+
fetched = r.get(self.item_ids, 'values')
136136
if fetched is not None:
137137
row_numbers.update(dict(zip(self.item_ids, fetched)))
138138
self.cache_lookup('row_numbers', row_numbers)

django/sierra/shelflist/tests/test_api.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,31 @@ def test_shelflistitem_row_pagination(api_settings, shelflist_solr_env,
700700
assert row_numbers[-1] == offset + limit - 1
701701

702702

703+
def test_shelflistitem_row_limit_one(api_settings, shelflist_solr_env,
704+
get_shelflist_urls, api_client,
705+
redis_obj, get_found_ids):
706+
"""
707+
When paginating a `shelflistitems` list view, requesting a limit of
708+
one item should work as expected.
709+
"""
710+
recs = shelflist_solr_env.records['shelflistitem']
711+
loc = recs[0]['location_code']
712+
loc_recs = [r for r in recs if r['location_code'] == loc]
713+
using = api_settings.REST_VIEWS_HAYSTACK_CONNECTIONS['ShelflistItems']
714+
index = ShelflistItemIndex(using=using)
715+
manifest = index.get_location_manifest(loc)
716+
redis_key = '{}:{}'.format(REDIS_SHELFLIST_PREFIX, loc)
717+
redis_obj(redis_key).set(manifest)
718+
limit_p = api_settings.REST_FRAMEWORK['PAGINATE_BY_PARAM']
719+
url = get_shelflist_urls(shelflist_solr_env.records['shelflistitem'])[loc]
720+
paginated_url = f"{url}?{limit_p}=1"
721+
response = api_client.get(paginated_url)
722+
found_ids = get_found_ids('id', response)
723+
row_numbers = get_found_ids('rowNumber', response)
724+
assert found_ids == [manifest[0]]
725+
assert row_numbers == [0]
726+
727+
703728
def test_shelflistitem_row_filtering(api_settings, shelflist_solr_env,
704729
get_shelflist_urls, api_client,
705730
assemble_shelflist_test_records,
@@ -781,8 +806,8 @@ def test_shelflistitem_list_row_caching(api_settings, shelflist_solr_env,
781806
cache miss.
782807
"""
783808
mocker.patch.object(
784-
RedisObject, 'get_index', mocker.Mock(
785-
side_effect=lambda *args: list(range(1000, 1000 + len(args)))
809+
RedisObject, 'get', mocker.Mock(
810+
side_effect=lambda l, lt: list(range(1000, 1000 + len(l)))
786811
)
787812
)
788813
ShelflistItemSerializer._lookup_cache['row_numbers'] = {}
@@ -807,28 +832,28 @@ def test_shelflistitem_list_row_caching(api_settings, shelflist_solr_env,
807832
# Redis to get the first item, and we extrapolate to get the rest
808833
# of the items in the list.
809834
assert len(manifest) > 1
810-
call_stack.append(mocker.call(*manifest))
811-
assert RedisObject.get_index.mock_calls == call_stack
835+
call_stack.append(mocker.call(manifest, 'values'))
836+
assert RedisObject.get.mock_calls == call_stack
812837

813838
# Now when we request detail views for individual rows in the list,
814839
# it should still use the cached row number, if it finds it,
815840
# instead of querying Redis.
816841
for ping_row in (0, 2, len(manifest) - 1):
817842
api_client.get(f"{url}{manifest[ping_row]}")
818-
assert RedisObject.get_index.mock_calls == call_stack
843+
assert RedisObject.get.mock_calls == call_stack
819844

820845
# When we hit the list view again it should refresh the cache,
821846
# resulting in an additional call to Redis.
822847
api_client.get(url)
823-
call_stack.append(mocker.call(*manifest))
824-
assert RedisObject.get_index.mock_calls == call_stack
848+
call_stack.append(mocker.call(manifest, 'values'))
849+
assert RedisObject.get.mock_calls == call_stack
825850

826851
# If we clear the serializer lookup_cache, then hitting the detail
827852
# view for an individual row requests the rowNumber from Redis.
828853
ShelflistItemSerializer._lookup_cache['row_numbers'] = {}
829854
api_client.get(f"{url}{manifest[-1]}")
830-
call_stack.append(mocker.call(manifest[-1]))
831-
assert RedisObject.get_index.mock_calls == call_stack
855+
call_stack.append(mocker.call(manifest[-1], 'value'))
856+
assert RedisObject.get.mock_calls == call_stack
832857

833858

834859
def test_shelflistitem_putpatch_requires_auth(api_settings,

0 commit comments

Comments
 (0)