def test_order_insert_same_uuid(db_session): test_uuid = uuid4() with pytest.raises(IntegrityError): OrderFactory.create(uuid=test_uuid) db_session.commit() OrderFactory.create(uuid=test_uuid) db_session.commit()
def test_order_service_select_by_user_slug(service, db_perm_session): user_slug = uuid_to_slug(uuid4()) with pytest.raises(NoContentError): service.select_by_user_slug(user_slug=user_slug) OrderFactory.create_batch(5, user_slug=user_slug) db_perm_session.commit() result = service.select_by_user_slug(user_slug=user_slug) assert len(result["orders"]) == 5 assert type(result["orders"][0]) is Order assert result["total"] == 5 assert result["pages"] == 1 datenow = date.today() spanstart = datenow - timedelta(days=1) spanend = datenow + timedelta(days=1) result = service.select_by_user_slug(user_slug=user_slug, datespan={"start": spanstart, "end": spanend}) assert len(result["orders"]) == 5 assert type(result["orders"][0]) is Order assert result["total"] == 5 assert result["pages"] == 1 with pytest.raises(NoContentError): result = service.select_by_user_slug(user_slug=user_slug, datespan={"start": spanend, "end": spanstart}) with pytest.raises(NoContentError): spanstart = datenow - timedelta(days=5) spanend = datenow - timedelta(days=4) result = service.select_by_user_slug(user_slug=user_slug, datespan={"start": spanstart, "end": spanend})
def test_order_select(db_session): assert db_session.query(Order).one_or_none() is None OrderFactory.create_batch(5) db_session.commit() assert len(db_session.query(Order).all()) == 5
def test_order_update_set_items(db_session): obj = OrderFactory.create() db_session.commit() assert not obj.items assert db_session.query(Product).one_or_none() is None assert db_session.query(OrderProduct).one_or_none() is None prod_list = ProductFactory.create_batch(5) for p in prod_list: OrderProduct(order=obj, product=p, amount=2) db_session.commit() assert len(obj.items) == 5 assert type(obj.items[0]) == OrderProduct assert len(db_session.query(Product).all()) == 5 assert len(db_session.query(OrderProduct).all()) == 5 obj = OrderFactory.create() for p in prod_list[0:3]: OrderProduct(order=obj, product=p, amount=1) db_session.commit() assert len(obj.items) == 3 assert type(obj.items[0]) == OrderProduct assert len(db_session.query(Product).all()) == 5 assert len(db_session.query(OrderProduct).all()) == 8
def test_order_service_select_by_user_slug_pages(service, db_perm_session): user_slug = uuid_to_slug(uuid4()) OrderFactory.create_batch(22, user_slug=user_slug) db_perm_session.commit() result = service.select_by_user_slug(user_slug=user_slug, page=1, page_size=10) assert result is not None assert len(result["orders"]) == 10 assert type(result["orders"][0]) is Order assert result["total"] == 22 assert result["pages"] == 3 result = service.select_by_user_slug(user_slug=user_slug, page=5, page_size=5) assert result is not None assert len(result["orders"]) == 2 assert type(result["orders"][0]) is Order assert result["total"] == 22 assert result["pages"] == 5 with pytest.raises(NoContentError): service.select_by_user_slug(user_slug=user_slug, page=6, page_size=5) with pytest.raises(ZeroDivisionError): service.select_by_user_slug(user_slug=user_slug, page=1, page_size=0) with pytest.raises(DataError): service.select_by_user_slug(user_slug=user_slug, page=0, page_size=5)
def test_order_insert(db_session): obj = OrderFactory.create() db_session.commit() assert db_session.query(Order).one() assert db_session.query(Order).filter(Order.id == obj.id).one() assert db_session.query(Order).filter(Order.uuid == obj.uuid).one() assert db_session.query(Order).filter( Order.user_uuid == obj.user_uuid).one() user_slug = uuid_to_slug(uuid4()) obj = OrderFactory.create(user_slug=user_slug) db_session.commit() assert uuid_to_slug(obj.user_uuid) == user_slug
def test_order_dict(db_session): obj = OrderFactory.create() db_session.commit() obj_dict = obj.to_dict() for key in [ "slug", "created_at", "updated_at", "product_types", "items_amount" ]: assert key in obj_dict assert len(obj_dict.keys()) == 5 assert obj_dict["product_types"] == 0 assert obj_dict["items_amount"] == 0 prod_list = ProductFactory.create_batch(5) for p in prod_list: OrderProduct(order=obj, product=p, amount=2) db_session.commit() obj_dict = obj.to_dict() assert obj_dict["product_types"] == 5 assert obj_dict["items_amount"] == 10 item = obj.items[0] item_dict = item.to_dict() for key in ["item_id", "amount"]: assert key in item_dict assert item_dict["item_id"] == item.product.es_id assert item_dict["amount"] == 2
def test_order_delete(db_session): obj = OrderFactory.create() db_session.commit() assert not obj.items assert db_session.query(Product).one_or_none() is None assert db_session.query(OrderProduct).one_or_none() is None prod_list = ProductFactory.create_batch(5) for p in prod_list: OrderProduct(order=obj, product=p, amount=2) db_session.commit() assert len(obj.items) == 5 assert type(obj.items[0]) == OrderProduct assert len(db_session.query(Product).all()) == 5 assert len(db_session.query(OrderProduct).all()) == 5 result = db_session.query(Order).filter(Order.id == obj.id).delete() assert result == 1 db_session.commit() assert db_session.query(Order).one_or_none() is None assert len(db_session.query(Product).all()) == 5 assert db_session.query(OrderProduct).one_or_none() is None
def test_delete_controller(token_app, db_perm_session, prod_list): user_slug = uuid_to_slug(uuid4()) obj = OrderFactory.create(user_slug=user_slug) db_perm_session.commit() order_slug = obj.uuid_slug prod_id_list = [p.meta["id"] for p in prod_list] for es_id in prod_id_list: product = ProductFactory.create(es_id=es_id) OrderProductFactory.create(order=obj, product=product, amount=2) db_perm_session.commit() assert len(db_perm_session.query(Order).all()) == 1 assert len(db_perm_session.query(Product).all()) == 5 assert len(db_perm_session.query(OrderProduct).all()) == 5 fake_user_slug = uuid_to_slug(uuid4()) with token_app.test_client() as client: response = client.delete( "api/order/delete/%s/%s" % (fake_user_slug, order_slug) ) data = json.loads(response.data) assert data == {} assert response.status_code == 404 assert len(db_perm_session.query(Order).all()) == 1 assert len(db_perm_session.query(Product).all()) == 5 assert len(db_perm_session.query(OrderProduct).all()) == 5 with token_app.test_client() as client: response = client.delete( "api/order/delete/%s/%s" % (user_slug, order_slug) ) data = json.loads(response.data) assert data == {} assert response.status_code == 200 assert len(db_perm_session.query(Order).all()) == 0 assert len(db_perm_session.query(Product).all()) == 5 assert len(db_perm_session.query(OrderProduct).all()) == 0 with token_app.test_client() as client: response = client.delete( "api/order/delete/%s/%s" % (user_slug, order_slug) ) data = json.loads(response.data) assert data == {} assert response.status_code == 404 assert len(db_perm_session.query(Order).all()) == 0 assert len(db_perm_session.query(Product).all()) == 5 assert len(db_perm_session.query(OrderProduct).all()) == 0
def test_select_by_slug_controller(token_app, db_perm_session, prod_list): user_slug = uuid_to_slug(uuid4()) obj = OrderFactory.create(user_slug=user_slug) db_perm_session.commit() order_slug = obj.uuid_slug prod_id_list = [p.meta["id"] for p in prod_list] amount = 1 for es_id in prod_id_list: product = ProductFactory.create(es_id=es_id) OrderProductFactory.create(order=obj, product=product, amount=amount) amount += 1 db_perm_session.commit() with token_app.test_client() as client: response = client.get( "api/order/%s/%s" % (user_slug, order_slug) ) data = json.loads(response.data) OrderSchema().load(data) assert response.status_code == 200 assert data["slug"] == order_slug assert data["product_types"] == len(prod_list) assert data["items_amount"] == ((1 + len(prod_list)) * len(prod_list)) / 2 assert len(data["products"]) == len(prod_list) for item in [item.to_dict() for item in obj.items]: product = next(p for p in data["products"] if p["id"] == item["item_id"]) assert product["amount"] == item["amount"] with token_app.test_client() as client: response = client.get( "api/order/WILLrogerPEREIRAslugBR/WILLrogerPEREIRAslugBR" ) data = json.loads(response.data) assert data == {} assert response.status_code == 404 with token_app.test_client() as client: response = client.get( "api/order/WILLrogerPEREIRAslugBR/%s" % order_slug ) data = json.loads(response.data) assert data == {} assert response.status_code == 404
def test_delete(domain_url, db_perm_session, token_session, prod_list): user_slug = uuid_to_slug(uuid4()) obj = OrderFactory.create(user_slug=user_slug) db_perm_session.commit() order_slug = obj.uuid_slug prod_id_list = [p.meta["id"] for p in prod_list] for es_id in prod_id_list: product = ProductFactory.create(es_id=es_id) OrderProductFactory.create(order=obj, product=product, amount=2) db_perm_session.commit() assert len(db_perm_session.query(Order).all()) == 1 assert len(db_perm_session.query(Product).all()) == 5 assert len(db_perm_session.query(OrderProduct).all()) == 5 fake_user_slug = uuid_to_slug(uuid4()) response = token_session.delete(domain_url + "/api/order/delete/%s/%s" % (fake_user_slug, order_slug)) data = response.json() assert data == {} assert response.status_code == 404 assert len(db_perm_session.query(Order).all()) == 1 assert len(db_perm_session.query(Product).all()) == 5 assert len(db_perm_session.query(OrderProduct).all()) == 5 response = token_session.delete(domain_url + "/api/order/delete/%s/%s" % (user_slug, order_slug)) data = response.json() assert data == {} assert response.status_code == 200 assert len(db_perm_session.query(Order).all()) == 0 assert len(db_perm_session.query(Product).all()) == 5 assert len(db_perm_session.query(OrderProduct).all()) == 0 response = token_session.delete(domain_url + "/api/order/delete/%s/%s" % (user_slug, order_slug)) data = response.json() assert data == {} assert response.status_code == 404 assert len(db_perm_session.query(Order).all()) == 0 assert len(db_perm_session.query(Product).all()) == 5 assert len(db_perm_session.query(OrderProduct).all()) == 0
def test_select_by_user_controller_not_registered(token_app, db_perm_session): user_slug = uuid_to_slug(uuid4()) bad_obj_list = OrderFactory.create_batch(4, user_slug=user_slug) bad_product = ProductFactory.create() for order in bad_obj_list: OrderProductFactory.create(order=order, product=bad_product, amount=5) db_perm_session.commit() with token_app.test_client() as client: response = client.post("api/order/user/%s" % user_slug) data = json.loads(response.data) ErrorSchema().load(data) assert response.status_code == 400 assert data["error"].find("not registered") != -1
def test_select_by_user_controller_not_registered(domain_url, db_perm_session, token_session): user_slug = uuid_to_slug(uuid4()) bad_obj_list = OrderFactory.create_batch(4, user_slug=user_slug) bad_product = ProductFactory.create() for order in bad_obj_list: OrderProductFactory.create(order=order, product=bad_product, amount=5) db_perm_session.commit() response = token_session.post(domain_url + "/api/order/user/%s" % user_slug) data = response.json() ErrorSchema().load(data) assert response.status_code == 400 assert data["error"].find("not registered") != -1
def test_order_service_select_by_slug(service, db_perm_session): user_slug = uuid_to_slug(uuid4()) order_slug = uuid_to_slug(uuid4()) with pytest.raises(NotFoundError): service.select_by_slug(user_slug=user_slug, order_slug=order_slug) obj = OrderFactory.create() db_perm_session.commit() order_slug = obj.uuid_slug user_slug = obj.user_slug result = service.select_by_slug(user_slug=user_slug, order_slug=order_slug) assert type(result) is Order user_slug = uuid_to_slug(uuid4()) with pytest.raises(NotFoundError): service.select_by_slug(user_slug=user_slug, order_slug=order_slug)
def test_order_service_delete(service, db_perm_session): assert len(db_perm_session.query(Order).all()) == 0 with pytest.raises(NotFoundError): service.delete(user_slug="WILLrogerPEREIRAslugBR", order_slug="WILLrogerPEREIRAslugBR") user_slug = uuid_to_slug(uuid4()) obj = OrderFactory.create(user_slug=user_slug) db_perm_session.commit() prod_list = ProductFactory.create_batch(5) for p in prod_list: OrderProduct(order=obj, product=p, amount=2) db_perm_session.commit() assert len(db_perm_session.query(Order).all()) == 1 assert len(db_perm_session.query(Product).all()) == 5 assert len(db_perm_session.query(OrderProduct).all()) == 5 fake_user_slug = uuid_to_slug(uuid4()) with pytest.raises(NotFoundError): service.delete(user_slug=fake_user_slug, order_slug=obj.uuid_slug) assert len(db_perm_session.query(Order).all()) == 1 assert len(db_perm_session.query(Product).all()) == 5 assert len(db_perm_session.query(OrderProduct).all()) == 5 delete = service.delete(user_slug=user_slug, order_slug=obj.uuid_slug) assert delete is True assert len(db_perm_session.query(Order).all()) == 0 assert len(db_perm_session.query(Product).all()) == 5 assert len(db_perm_session.query(OrderProduct).all()) == 0
def test_order_insert_invalid_user_slug(db_session): with pytest.raises(SlugDecodeError): OrderFactory.create(user_slug="churros")
def test_select_by_user_controller(domain_url, db_perm_session, token_session, prod_list): user_slug = uuid_to_slug(uuid4()) prod_id_list = [p.meta["id"] for p in prod_list] product_list = [ ProductFactory.create(es_id=es_id) for es_id in prod_id_list ] db_perm_session.commit() obj_list = OrderFactory.create_batch(2, user_slug=user_slug) for product in product_list: OrderProductFactory.create(order=obj_list[0], product=product, amount=2) for product in product_list[0:3]: OrderProductFactory.create(order=obj_list[1], product=product, amount=5) db_perm_session.commit() response = token_session.post(domain_url + "/api/order/user/%s" % user_slug) data = response.json() UserOrdersSchema().load(data) assert response.status_code == 200 assert len(data["orders"]) == 2 assert data["total"] == 2 assert data["pages"] == 1 order_slug_list = [order["slug"] for order in data["orders"]] for slug in order_slug_list: assert slug in [obj.uuid_slug for obj in obj_list] for order in data["orders"]: if order["slug"] == obj_list[0].uuid_slug: assert order["product_types"] == 5 assert order["items_amount"] == 10 else: assert order["product_types"] == 3 assert order["items_amount"] == 15 response = token_session.post(domain_url + "/api/order/user/%s" % user_slug, json={ "page": "1", "page_size": "1" }) data = response.json() UserOrdersSchema().load(data) assert response.status_code == 200 assert len(data["orders"]) == 1 assert data["total"] == 2 assert data["pages"] == 2 response = token_session.post( domain_url + "/api/order/user/%s" % user_slug, json={ "datespan": { "start": str(date.today() - timedelta(days=1)), "end": str(date.today() + timedelta(days=1)) } }) data = response.json() UserOrdersSchema().load(data) assert response.status_code == 200 assert len(data["orders"]) == 2 assert data["total"] == 2 assert data["pages"] == 1 response = token_session.post(domain_url + "/api/order/user/WILLrogerPEREIRAslugBR") with pytest.raises(JSONDecodeError): response.json() assert response.status_code == 204