def test_search_item(item): assert ( reverse("item:lookup-item", kwargs={"pk": item_encode_b64(item.id)}) == f"/item/lookup/{item_encode_b64(item.id)}/" ) assert ( resolve(f"/item/lookup/{item_encode_b64(item.id)}/").view_name == "item:lookup-item" )
def test_search_item_dont_show_if_not_condition_well_enough(self, rf, item): """Searching an item of brand new will show up for all organizations. If you have a poor condition item, then only show WantedItem with 0 and below (which is none since 0 is the lowest). """ condition = 2 WantedItemFactory.create_batch(3, item=item, condition=condition) encoded = item_encode_b64(item.id, condition - 1) request = rf.get(f"item/lookup/{encoded}") response = search_item(request, encoded) assert response.status_code == 200 data = json.loads(response.content) assert len(data["data"]) == 0
def search_item_autocomplete(request): """Simple autocomplete for all existing items. """ # TODO Migrate to elasticsearch once there's enough items # TODO Use websockets instead since elasticsearch-dsl supports asyncio query = request.GET.get("q", None) if query is None: raise Http404(_("You must specify a query")) queryset = (Item.objects.defer("is_appropriate").filter( name__icontains=str(query), is_appropriate=True).values_list("id", "name", "image")[:15]) data = {"data": list(queryset)} try: # Give the item ID with condition appended condition = int(request.GET.get("condition")) for item in data["data"]: item[0] = item_encode_b64(item[0], condition) except (KeyError, ValueError, TypeError): pass return JsonResponse(data=data)
def test_search_multiple_items_with_page(self, rf): request = rf.get(f"item/multi-lookup/", {"q": item_encode_b64(1), "page": "1"}) response = search_multiple_items(request) assert response.status_code == 200
def test_search_item_show_if_condition_is_better_than_wanted_item(self, rf, item): condition = 2 wanted_items = WantedItemFactory.create_batch(3, item=item, condition=condition) encoded = item_encode_b64(item.id, condition + 1) _item_test_request(rf, encoded, wanted_items)
def test_search_item(self, rf, item): wanted_items = WantedItemFactory.create_batch(3, item=item) encoded = item_encode_b64(item.id) _item_test_request(rf, encoded, wanted_items)
def test_decode(self): b64_to_wanted_item(item_encode_b64(_big_serial_max))
def test_raise_if_item_id_is_less_than_0(self, item_id): with pytest.raises(AssertionError): item_encode_b64(item_id)
def test_encode(self, item_id): """Checks the length actually decreased""" condition = 2 assert len(item_encode_b64(item_id, condition)) < len( str(item_id) + str(condition) )