Ejemplo n.º 1
0
 def test_list_active_entity_items(self, rf, charity, user,
                                   django_assert_max_num_queries):
     # Create test data
     item_a = WantedItemFactory.create(item=ItemFactory.create(name="a"),
                                       charity=charity,
                                       condition=randint(0, 3))
     item_k = WantedItemFactory.create(item=ItemFactory.create(name="k"),
                                       charity=charity,
                                       condition=randint(0, 3))
     item_z = WantedItemFactory.create(item=ItemFactory.create(name="z"),
                                       charity=charity,
                                       condition=randint(0, 3))
     request = rf.get("blah/")
     with django_assert_max_num_queries(3):
         response = views.list_active_entity_items(request,
                                                   charity_id=charity.id)
     assert response.status_code == 200
     data = json.loads(response.content)["data"]
     assert data[0][0] == item_a.item.name
     assert data[0][1] == item_a.condition
     assert data[1][0] == item_k.item.name
     assert data[1][1] == item_k.condition
     assert data[2][0] == item_z.item.name
     assert data[2][1] == item_z.condition
Ejemplo n.º 2
0
    def test_search_multiple_items(self, client, charity):
        # Create the items
        chair = ItemFactory.create(name="chair")
        pan = ItemFactory.create(name="pan")
        jeans = ItemFactory.create(name="jeans")
        # Create items organizations wants
        # All items are created with condition 3: Brand New
        WantedItemFactory.create_batch(3, item=chair)  # 3
        WantedItemFactory.create(item=chair, charity=charity)  # 4
        WantedItemFactory.create(item=pan, charity=charity)  # 4 since same charity
        WantedItemFactory.create_batch(2, item=pan)  # 6
        WantedItemFactory.create(item=jeans)  # 7

        WantedItemFactory.create_batch(25, item=jeans)

        response = client.get(
            f"/item/multi-lookup/?"
            f"q={item_encode_b64(chair.id, 3)}&"
            f"q={item_encode_b64(pan.id, 3)}&"
            f"q={item_encode_b64(jeans.id, 3)}"
        )
        assert response.status_code == 200
        data: dict = response.context["data"]
        assert len(data) == 25, f"Supposed to show 25 max pagination.\n{data}"
        assert response.context["page_obj"].has_next() is True
        assert response.context["page_obj"].has_previous() is False
        first_key = list(data.keys())[0]
        assert first_key == charity.id, (
            "Target charity, which supports the most items "
            "out of the searched, should be the first result."
        )
        assert (
            len(data[first_key][:-2])
            == WantedItem.objects.filter(
                item_id__in=[chair.id, pan.id, jeans.id],
                charity=charity,
            ).count()
        ), (
            "Assuming the additional information is only name and description"
            "of the organization, hence -2 in the test, then you're missing items."
        )

        charities_seen = []
        for k in data.keys():
            # Check the rest isn't the exact same charity
            assert k not in charities_seen, "Charities shown must be distinct"
            charities_seen.append(k)