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 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 handle(self, *args, **options): if not settings.DEBUG: raise Exception("You cannot use this command in production.") print("Creating Item app data.") org_1 = Charity.objects.create( name="Org 1", link="https://google.com/", description="Desc.", how_to_donate="Address", ) org_2 = Charity.objects.create( name="Org 2", link="https://google.com/", description="Desc.", how_to_donate="Address", ) org_3 = Charity.objects.create( name="Org 3", link="https://google.com/", description="Desc.", how_to_donate="Address", ) item_1 = Item.objects.create(name="pan") item_2 = Item.objects.create(name="canned food") Item.objects.create(name="chair") item_3 = Item.objects.create(name="fork") item_4 = Item.objects.create(name="blood") WantedItem.objects.create(item=item_1, charity=org_1) # 3 orgs WantedItem.objects.create(item=item_2, charity=org_1) # 2 orgs WantedItem.objects.create(item=item_3, charity=org_1) # 2 orgs WantedItem.objects.create(item=item_4, charity=org_1) # 1 org WantedItem.objects.create(item=item_1, charity=org_2) WantedItem.objects.create(item=item_2, charity=org_2) WantedItem.objects.create(item=item_1, charity=org_3) WantedItem.objects.create(item=item_3, charity=org_3) ProposedItem.objects.create( entity=org_1, user=UserFactory.create(), item=[item_1.id] + [x.id for x in ItemFactory.create_batch(10)], names=[Faker().bs() for _ in range(50)], ) ProposedItem.objects.create( entity=org_2, user=UserFactory.create(), item=[item_3.id] + [x.id for x in ItemFactory.create_batch(10)], names=[Faker().bs() for _ in range(50)], ) # For testing pagination - change to 1 in paginating WantedItemFactory.create_batch(25, item=item_1) # For testing proposed item, if user selects a generic # item, then all of its children are added as well. # If no parent, then select that only. GH #3 parent = Item.objects.create(name="parent1") [ Item.objects.create(parent=parent, name=f"child{x}") for x in range(7) ] # Also, during searching, if a generic item appears # we warn the user that they should check the list beforehand. print("Finished creating Item app data.")
def wanted_item() -> WantedItem: return WantedItemFactory()
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)
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)