def test_dissimilar_item_names(self, rf):
     query = "chair"
     ItemFactory.create(name="blah blah")  # DONATABLE BTW
     target = ItemFactory.create(name=query)
     request = rf.get("item/api/v1/item-autocomplete/", {"q": query})
     response = search_item_autocomplete(request)
     assert response.status_code == 200
     data = json.loads(response.content)
     assert len(data["data"]) == 1, f"Response {data}"
     assert data["data"][0] == [target.id, target.name, target.image]
 def test_show_only_appropriate(self, rf):
     # Remember, "name" is unique.
     ItemFactory.create(name="chai", is_appropriate=False)
     ItemFactory.create(name="hair", is_appropriate=False)  # DONATABLE BTW
     target = ItemFactory.create(name="chair", is_appropriate=True)
     request = rf.get("item/api/v1/item-autocomplete/", {"q": "chair"})
     response = search_item_autocomplete(request)
     assert response.status_code == 200
     data = json.loads(response.content)
     assert len(data["data"]) == 1
     assert data["data"][0] == [target.id, target.name, target.image]
예제 #3
0
 def test_recursion(self, rf, user):
     item1 = ItemFactory.create()
     item2 = ItemFactory.create(parent=item1)
     item1.parent = item2
     item1.save(update_fields=["parent"])
     request = rf.get("blah/")
     request.user = user
     response = item_children(request, item1.id)
     assert response.status_code == 200
     data = json.loads(response.content)
     assert len(data["data"]) == 1
     assert sorted(data["data"], key=lambda x: x[0]) == [[item2.id, item2.name]]
 def test_return_closest_exact_matching(self, rf):
     """Tests closest matching in terms of number of letters
     close to the query. "Three-ring binder shows up before
     binder...
     """
     target1 = ItemFactory.create(name="three-ring binder")
     target2 = ItemFactory.create(name="binder")
     request = rf.get("blah/", {"q": "binder"})
     response = search_item_autocomplete(request)
     assert response.status_code == 200
     data = json.loads(response.content)
     assert len(data["data"]) == 2
     assert data["data"][0] == [target2.id, target2.name, target2.image]
     assert data["data"][1] == [target1.id, target1.name, target1.image]
예제 #5
0
 def test_children_multi_level(self, rf, user):
     item1 = ItemFactory.create()
     item2a = ItemFactory.create(parent=item1)
     item3a = ItemFactory.create_batch(3, parent=item2a)
     item2b = ItemFactory.create(parent=item1)
     item3b = ItemFactory.create_batch(6, parent=item2b)
     request = rf.get("blah/")
     request.user = user
     response = item_children(request, item1.id)
     assert response.status_code == 200
     data = json.loads(response.content)
     assert len(data["data"]) == 11
     assert sorted(data["data"], key=lambda x: x[0]) == [
         [x.id, x.name]
         for x in sorted((item2a, item2b, *item3a, *item3b), key=lambda x: x.id)
     ]
예제 #6
0
 def test_selected_existing_already_in_active_item(self, charity, user):
     """Tests no new WantedItems are created if the item
     is already in WantedItem for the entity.
     """
     items = ItemFactory.create_batch(11)
     items_conditions = [randint(0, 3) for _ in range(len(items))]
     WantedItem.objects.bulk_create(
         [
             WantedItem(charity=charity, item=item, condition=condition)
             for item, condition in zip(items, items_conditions)
         ]
     )
     assert WantedItem.objects.count() == 11
     # Add lingering Item that isn't a part of entity yet
     random_item = ItemFactory.create()
     items.append(random_item)
     items_conditions.append(randint(0, 3))
     # Create proposed item
     proposed = ProposedItem.objects.create(
         entity=charity,
         user=user,
         item=[item.id for item in items],
         item_condition=items_conditions,
     )
     merge(charity, proposed)
     assert WantedItem.objects.count() == 12, "Only one item should've been added"
     assert WantedItem.objects.get(item=random_item)
     assert WantedItem.objects.distinct("item", "charity").count() == 12
     for item, condition in zip(items, items_conditions):
         assert WantedItem.objects.filter(
             item=item, condition=condition, charity=charity
         ).exists()
     assert WantedItem.objects.filter(charity=charity, item=random_item).exists()
예제 #7
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)
예제 #8
0
def test_create_thread_proposed_item(charity, user):
    item = ItemFactory.create()
    ProposedItem.objects.create(entity=charity, user=user, item=[item.id])
    assert Thread.objects.count() == 1
    assert Message.objects.count() == 1
    thread = Thread.objects.first()
    assert thread.type == 4
    message = Message.objects.first()
    assert message.thread == thread
 def test_item_missing_condition(self, client, user, charity):
     item = ItemFactory.create(is_appropriate=False)
     client.force_login(user)
     response = client.post(
         self.view_url,
         data={"entity": charity.id, "item": str(item.id)},
     )
     assert response.status_code == 400
     errors = json.loads(response.content)["errors"]
     assert "item" in errors
예제 #10
0
 def test_no_children(self, rf, user):
     parent = ItemFactory.create()
     progeny = ItemFactory.create_batch(9, parent=parent)
     request = rf.get("blah/")
     request.user = user
     response = item_children(request, progeny[0].id)
     assert response.status_code == 200
     data = json.loads(response.content)
     assert len(data["data"]) == 0
     assert data["data"] == []
예제 #11
0
 def test_remove_duplicate_items(self, charity, user):
     item = ItemFactory.create()
     # What happens when a duplicate like this appears? First or second? First :)
     proposed = ProposedItem.objects.create(
         entity=charity, user=user, item=[item.id, item.id], item_condition=[2, 1]
     )
     merge(charity, proposed)
     assert Item.objects.count() == 1
     assert WantedItem.objects.count() == 1
     assert WantedItem.objects.first().condition == 2
예제 #12
0
 def test_children_level_1(self, rf, user):
     parent = ItemFactory.create()
     progeny = ItemFactory.create_batch(9, parent=parent)
     request = rf.get("blah/")
     request.user = user
     response = item_children(request, parent.id)
     assert response.status_code == 200
     data = json.loads(response.content)
     assert len(data["data"]) == 9
     assert sorted(data["data"], key=lambda x: x[0]) == [
         [x.id, x.name] for x in progeny
     ]
예제 #13
0
 def test_item_condition_incorrect_length(self, client, user, charity, neg):
     item = ItemFactory.create(is_appropriate=False)
     client.force_login(user)
     response = client.post(
         self.view_url,
         data={
             "entity": charity.id,
             "item": str(item.id),
             "item_condition": [1] * (1 - neg),
         },
     )
     assert response.status_code == 400
     errors = json.loads(response.content)["errors"]
     assert "item" in errors
예제 #14
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
 def test_typeahead(self, rf):
     query = "an"
     ItemFactory.create(name="pan")
     ItemFactory.create(name="canned food")
     ItemFactory.create(name="blah")
     request = rf.get("item/api/v1/item-autocomplete/", {"q": query})
     response = search_item_autocomplete(request)
     assert response.status_code == 200
     data = json.loads(response.content)
     assert len(data["data"]) == 2, f"Response {data}"
     assert query in data["data"][0][1]
     assert query in data["data"][1][1]
예제 #16
0
 def test_duplicate_name_and_item(self, charity, user):
     """Tests that a duplicate listed name and listed item
     don't create two WantedItems.
     """
     item = ItemFactory.create()
     proposed = ProposedItem.objects.create(
         entity=charity,
         user=user,
         item=[item.id],
         item_condition=[1],
         names=[item.name.lower()],
         names_condition=[2],
     )
     merge(charity, proposed)
     assert Item.objects.get(name=item.name)
     assert WantedItem.objects.count() == 1
     wanted_item = WantedItem.objects.first()
     assert wanted_item.item == item
     assert wanted_item.condition == 1