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_shop_db_model_constraints(db_session: Session): owner = OwnerFactory() owner.create(db_session) shop = Shop( name="My Shop Name", address="My shop address", owner_id=owner.id, location="POINT(45 32)", radius_metres=500, ) db_session.add(shop) db_session.commit() assert_non_nullable(shop, "id") assert_max_length(shop, "id", 100) assert_non_nullable(shop, "name") assert_max_length(shop, "name", 50) assert_non_nullable(shop, "address") assert_max_length(shop, "address", 50) assert_non_nullable(shop, "owner_id") assert_non_nullable(shop, "location") assert_non_nullable(shop, "radius_metres") assert_non_nullable(shop, "is_available") assert_nullable(shop, "phone_number")
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_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_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_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_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_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_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
def test_shop_details_for_user_shop_id_capitalized(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) r = client.get( f"{config.API_V1_STR}/shop-details?shop_id={shop.id.capitalize()}&longitude=78.0698410774254&latitude=27.89865957642155" ) response_json = r.json() assert r.status_code == 200 assert response_json["id"] == shop.id
def test_shop_details_for_user_invalid_address_id_param( 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) r = client.get( f"{config.API_V1_STR}/shop-details?shop_id={shop.id}&address_id=1") response_json = r.json() assert r.status_code == 422 assert response_json["detail"] == "INVALID_LOCATION: addess_id is invalid"
def test_get_shops_list_for_user(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=1000) shop.create(db_session) r = client.get( f"{config.API_V1_STR}/shops-list?longitude=78.06374&latitude=27.90493") response_json = r.json() assert r.status_code == 200 assert len(response_json) == 1 response_shop = response_json[0] assert response_shop['id'] == shop.id assert response_shop['name'] == shop.name assert response_shop['address'] == shop.address
def test_shop_details_for_user_without_location_param(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) r = client.get(f"{config.API_V1_STR}/shop-details?shop_id={shop.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 None
def test_shop_details_for_user_shop_id_slug_change_on_name_change( 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) data = {"name": "my new shop name"} headers = {'Authorization': f'Bearer {owner.get_auth_token()}'} r = client.patch(f"{config.API_V1_STR}/shops/{shop.id}", json=data, headers=headers) response_json = r.json() assert "my-new-shop-name-" in response_json["id"]
def test_shop_details_for_user_shop_id_slug(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) r = client.get(f"{config.API_V1_STR}/shop-details?shop_id={shop.id}") response_json = r.json() assert r.status_code == 200 assert response_json["id"] == shop.id assert shop.name in response_json["id"] random_slug_number_str: str = response_json["id"].split("-", 1)[1] assert len(random_slug_number_str) == 6 assert random_slug_number_str.isdigit() is True