def test_empty_current_cart(client: TestClient, db_session: Session): owner = OwnerFactory() owner.create(db_session) shop = ShopFactory( longitude=78.0705664, latitude=27.8983082, owner_id=owner.id, radius_metres=500 ) shop.create(db_session) item1 = ItemFactory(shop_id=shop.id, owner_id=owner.id) item1.create(db_session) item2 = ItemFactory(shop_id=shop.id, owner_id=owner.id) item2.create(db_session) user = UserFactory() user.create(db_session) token = user.get_auth_token() cart_item1 = CartFactory(item_id=item1.id, shop_id=shop.id, user_id=user.id) cart_item1.create(db_session) cart_item2 = CartFactory(item_id=item2.id, shop_id=shop.id, user_id=user.id) cart_item2.create(db_session) headers = {'Authorization': f'Bearer {token}'} r = client.delete(f"{config.API_V1_STR}/cart", headers=headers) response_json = r.json() assert cart_item1.get_by_item_id(db_session, item1.id) is None assert cart_item2.get_by_item_id(db_session, item2.id) is None assert r.status_code == 200 assert response_json["message"] == "DELETED: 2 unique items removed from cart"
def test_add_same_item_in_cart_error(client: TestClient, db_session: Session): owner = OwnerFactory() owner.create(db_session) shop = ShopFactory( longitude=78.0705664, latitude=27.8983082, owner_id=owner.id, radius_metres=500 ) shop.create(db_session) item = ItemFactory(shop_id=shop.id, owner_id=owner.id) item.create(db_session) user = UserFactory() user.create(db_session) token = user.get_auth_token() cart = CartFactory(item_id=item.id, shop_id=shop.id, user_id=user.id) cart.create(db_session) post_data = {"item_id": item.id} headers = {'Authorization': f'Bearer {token}'} r = client.post(f"{config.API_V1_STR}/cart", json=post_data, headers=headers) response_json = r.json() assert r.status_code == 400 assert ( response_json['detail'] == 'CONSTRAINT_VIOLATION: Item already exist in your cart or you are trying to add item from different shops' )
def test_minus_action_on_item_in_cart(client: TestClient, db_session: Session): owner = OwnerFactory() owner.create(db_session) shop = ShopFactory( longitude=78.0705664, latitude=27.8983082, owner_id=owner.id, radius_metres=500 ) shop.create(db_session) item = ItemFactory(shop_id=shop.id, owner_id=owner.id) item.create(db_session) user = UserFactory() user.create(db_session) token = user.get_auth_token() cart = CartFactory(item_id=item.id, shop_id=shop.id, user_id=user.id) cart.create(db_session) cart.update(db_session, values={'item_quantity': 2}) patch_data = {"item_id": item.id, "action": "minus"} headers = {'Authorization': f'Bearer {token}'} r = client.patch(f"{config.API_V1_STR}/cart", json=patch_data, headers=headers) response_json = r.json() db_cart_item = cart.get_by_item_id(db_session, item_id=item.id) assert r.status_code == 200 assert response_json['total_items'] == 1 assert response_json['unique_items'] == 1 assert response_json['total_cost'] == item.cost * 1 assert response_json['items'][0]['id'] == item.id assert response_json['items'][0]['name'] == item.name assert response_json['items'][0]['cost'] == item.cost assert response_json['items'][0]['item_quantity'] == 1 assert response_json['items'][0]['item_available'] is True assert db_cart_item.item_quantity == 1
def test_minus_action_on_single_item_in_cart_error( client: TestClient, db_session: Session ): owner = OwnerFactory() owner.create(db_session) shop = ShopFactory( longitude=78.0705664, latitude=27.8983082, owner_id=owner.id, radius_metres=500 ) shop.create(db_session) item = ItemFactory(shop_id=shop.id, owner_id=owner.id) item.create(db_session) user = UserFactory() user.create(db_session) token = user.get_auth_token() cart = CartFactory(item_id=item.id, shop_id=shop.id, user_id=user.id) cart.create(db_session) patch_data = {"item_id": item.id, "action": "minus"} headers = {'Authorization': f'Bearer {token}'} r = client.patch(f"{config.API_V1_STR}/cart", json=patch_data, headers=headers) response_json = r.json() db_cart_item = cart.get_by_item_id(db_session, item_id=item.id) assert r.status_code == 404 assert response_json['detail'] == 'EMPTY_CART' assert db_cart_item is None
def test_add_item_in_cart_from_unavailable_shop_error( client: TestClient, db_session: Session ): owner = OwnerFactory() owner.create(db_session) shop = ShopFactory( longitude=78.0705664, latitude=27.8983082, owner_id=owner.id, radius_metres=500 ) shop.create(db_session) shop.update(db_session, values={'is_available': False}) item = ItemFactory(shop_id=shop.id, owner_id=owner.id) item.create(db_session) user = UserFactory() user.create(db_session) token = user.get_auth_token() cart = CartFactory(item_id=item.id, shop_id=shop.id, user_id=user.id) cart.create(db_session) post_data = {"item_id": item.id} headers = {'Authorization': f'Bearer {token}'} r = client.post(f"{config.API_V1_STR}/cart", json=post_data, headers=headers) response_json = r.json() assert shop.is_available is False assert r.status_code == 400 assert ( response_json['detail'] == 'NOT_AVAILABLE: Shop or item not available currently' )
def test_add_item_in_cart(client: TestClient, db_session: Session): owner = OwnerFactory() owner.create(db_session) shop = ShopFactory( longitude=78.0705664, latitude=27.8983082, owner_id=owner.id, radius_metres=500 ) shop.create(db_session) item = ItemFactory(shop_id=shop.id, owner_id=owner.id) item.create(db_session) user = UserFactory() user.create(db_session) token = user.get_auth_token() post_data = {"item_id": item.id} headers = {'Authorization': f'Bearer {token}'} r = client.post(f"{config.API_V1_STR}/cart", json=post_data, headers=headers) response_json = r.json() assert r.status_code == 201 assert response_json['total_items'] == 1 assert response_json['unique_items'] == 1 assert response_json['total_cost'] == item.cost * 1 assert response_json['items'][0]['id'] == item.id assert response_json['items'][0]['name'] == item.name assert response_json['items'][0]['cost'] == item.cost assert response_json['items'][0]['item_quantity'] == 1 assert response_json['items'][0]['item_available'] is True
def test_shop_details_for_user_address_id_out_of_radius( client: TestClient, db_session: Session): owner = OwnerFactory() owner.create(db_session) shop = ShopFactory( longitude=78.07061944376925, latitude=27.89821838699808, owner_id=owner.id, radius_metres=100, ) shop.create(db_session) user = UserFactory() user.create(db_session) address = AddressFactory(user_id=user.id, longitude=78.06969883180747, latitude=27.89867969055395) address.create(db_session) r = client.get( f"{config.API_V1_STR}/shop-details?shop_id={shop.id}&address_id={address.id}" ) response_json = r.json() assert r.status_code == 200 assert response_json["id"] == shop.id assert response_json["name"] == shop.name assert response_json["address"] == shop.address assert response_json["radius_metres"] == 100 assert response_json["is_available"] is True assert response_json["deliverable"] is False
def test_existing_user_conflict(client: TestClient, db_session: Session) -> None: user = UserFactory() user.create(db_session) data = {"email": user.email, "password": user.password} r = client.post(f"{config.API_V1_STR}/users", json=data) response_json = r.json() assert r.status_code == 409 assert response_json["detail"] == "There was a conflict with an existing user."
def test_add_item_in_cart_when_unauthenticated(client: TestClient, db_session: Session): user = UserFactory() user.create(db_session) post_data = {"item_id": 1} r = client.post(f"{config.API_V1_STR}/cart", json=post_data) response_json = r.json() assert r.status_code == 401 assert response_json['detail'] == 'Not authenticated'
def test_view_cart_when_no_items_added_error(client: TestClient, db_session: Session): user = UserFactory() user.create(db_session) token = user.get_auth_token() headers = {'Authorization': f'Bearer {token}'} r = client.get(f"{config.API_V1_STR}/cart", headers=headers) response_json = r.json() assert r.status_code == 404 assert response_json['detail'] == "EMPTY_CART"
def test_register_user(client: TestClient, db_session: Session) -> None: user = UserFactory() data = {"email": user.email, "password": user.password} r = client.post(f"{config.API_V1_STR}/users", json=data) response_json = r.json() id = response_json["id"] db_user = user.get_by_id(db_session, id) assert r.status_code == 201 assert response_json["email"] == user.email assert response_json["email"] == db_user.email assert verify_password(user.password, db_user.password_hash) is True assert response_json["email_verified"] is False assert (isinstance(id, int) and id >= 0) is True
def test_invalid_action_on_item_update_in_cart(client: TestClient, db_session: Session): user = UserFactory() user.create(db_session) token = user.get_auth_token() patch_data = {"item_id": 1, "action": "invalid"} headers = {'Authorization': f'Bearer {token}'} r = client.patch(f"{config.API_V1_STR}/cart", json=patch_data, headers=headers) response_json = r.json() assert r.status_code == 422 assert response_json['detail'][0]['loc'][0] == 'body' assert response_json['detail'][0]['loc'][1] == 'action' assert ('plus' in response_json['detail'][0]['msg']) is True assert ('minus' in response_json['detail'][0]['msg']) is True
def test_view_cart(client: TestClient, db_session: Session): owner = OwnerFactory() owner.create(db_session) shop = ShopFactory( longitude=78.0705664, latitude=27.8983082, owner_id=owner.id, radius_metres=500 ) shop.create(db_session) item1 = ItemFactory(shop_id=shop.id, owner_id=owner.id) item1.create(db_session) item2 = ItemFactory(shop_id=shop.id, owner_id=owner.id) item2.create(db_session) user = UserFactory() user.create(db_session) token = user.get_auth_token() cart = CartFactory(item_id=item1.id, shop_id=shop.id, user_id=user.id) cart.create(db_session) cart.update(db_session, values={'item_quantity': 4}) cart = CartFactory(item_id=item2.id, shop_id=shop.id, user_id=user.id) cart.create(db_session) cart.update(db_session, values={'item_quantity': 3}) headers = {'Authorization': f'Bearer {token}'} r = client.get(f"{config.API_V1_STR}/cart", headers=headers) response_json = r.json() assert r.status_code == 200 assert response_json['total_items'] == 7 assert response_json['unique_items'] == 2 assert response_json['total_cost'] == item1.cost * 4 + item2.cost * 3 assert len(response_json['items']) == 2 assert response_json['items'][0]['id'] == item1.id assert response_json['items'][0]['name'] == item1.name assert response_json['items'][0]['cost'] == item1.cost assert response_json['items'][0]['item_quantity'] == 4 assert response_json['items'][0]['item_available'] is True assert response_json['items'][1]['id'] == item2.id assert response_json['items'][1]['name'] == item2.name assert response_json['items'][1]['cost'] == item2.cost assert response_json['items'][1]['item_quantity'] == 3 assert response_json['items'][1]['item_available'] is True