def test_product_insert_same_uuid(db_session): test_uuid = uuid4() with pytest.raises(IntegrityError): ProductFactory.create(uuid=test_uuid) db_session.commit() ProductFactory.create(uuid=test_uuid) db_session.commit()
def test_product_insert_es_id(db_session): test_es_id = "asdfasdf" with pytest.raises(IntegrityError): ProductFactory.create(es_id=test_es_id) db_session.commit() ProductFactory.create(es_id=test_es_id) db_session.commit()
def test_search(domain_url, es_object, token_session): query = str(uuid4()) prod_list = [ ProductFactory.create(name=query), ProductFactory.create(gender=query), ProductFactory.create(kind=query), ProductFactory.create(brand=query) ] [prod_obj.save(using=es_object.connection) for prod_obj in prod_list] Index("store", using=es_object.connection).refresh() response = token_session.post( domain_url + "/api/search/%s" % query ) data = response.json() SearchResultsSchema().load(data) assert response.status_code == 200 assert data["total"] == 4 response = token_session.post( domain_url + "/api/search/%s" % query, json={ "pricerange": { "min": 1, "max": 500 } } ) data = response.json() SearchResultsSchema().load(data) assert response.status_code == 200 assert data["total"] == 4 response = token_session.post( domain_url + "/api/search/%s" % query, json={ "pricerange": { "min": 10000, "max": 20000 } } ) with pytest.raises(JSONDecodeError): response.json() assert response.status_code == 204 response = token_session.post( domain_url + "/api/search/%s" % str(uuid4()) ) with pytest.raises(JSONDecodeError): response.json() assert response.status_code == 204
def test_search_products_controller(token_app, es_object): query = str(uuid4()) prod_list = [ ProductFactory.create(name=query), ProductFactory.create(gender=query), ProductFactory.create(kind=query), ProductFactory.create(brand=query) ] [prod_obj.save(using=es_object.connection) for prod_obj in prod_list] Index("store", using=es_object.connection).refresh() with token_app.test_client() as client: response = client.post("api/search/%s/1" % query) data = json.loads(response.data) SearchProductsResultsSchema().load(data) assert response.status_code == 200 assert len(data["products"]) == 4 with token_app.test_client() as client: response = client.post("api/search/%s/1" % query, json={ "pricerange": { "min": 1, "max": 500 }, "pagesize": 1 }) data = json.loads(response.data) SearchProductsResultsSchema().load(data) assert response.status_code == 200 assert len(data["products"]) == 1 with token_app.test_client() as client: response = client.post( "api/search/%s/1" % query, json={"pricerange": { "min": 10000, "max": 20000 }}) with pytest.raises(JSONDecodeError): json.loads(response.data) assert response.status_code == 204 with token_app.test_client() as client: response = client.post("api/search/%s/1" % str(uuid4())) with pytest.raises(JSONDecodeError): json.loads(response.data) assert response.status_code == 204
def test_product_insert(db_session): obj = ProductFactory.create() db_session.commit() assert db_session.query(Product).one() assert db_session.query(Product).filter(Product.id == obj.id).one() assert db_session.query(Product).filter(Product.uuid == obj.uuid).one() assert db_session.query(Product).filter(Product.es_id == obj.es_id).one() test_uuid = uuid4() obj = ProductFactory.create(uuid=test_uuid) db_session.commit() assert obj.uuid == test_uuid
def test_product_service_select_by_item_list(service, es_object): price = {"outlet": 10.0, "retail": 20.0} item_list = [] for i in range(3): obj = ProductFactory.create(price=price) obj.save(using=es_object.connection) item_list.append({"item_id": obj.meta["id"], "amount": i + 1}) Index("store", using=es_object.connection).refresh() results, total = service.select_by_item_list(item_list) assert len(results) == len(item_list) item_id_list = [item["item_id"] for item in item_list] for obj in results: assert type(obj) == Product assert obj.meta["id"] in item_id_list assert total["outlet"] == 60.0 assert total["retail"] == 120.0 fake_item_list = [{"item_id": str(uuid4()), "amount": 2} for x in range(2)] over_item_list = item_list + fake_item_list with pytest.raises(ValidationError): service.select_by_item_list(over_item_list)
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_product_service_super_discounts(service, es_object): prod_list = ProductFactory.create_batch(2) [prod_obj.save(using=es_object.connection) for prod_obj in prod_list] Index("store", using=es_object.connection).refresh() results = service.super_discounts() assert len(results) > 0 assert type(results[0]) == Product test_alt_id = "I_test_product_service_super_discounts" ProductFactory.create(gender=test_alt_id).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.super_discounts(gender=test_alt_id) assert len(results) == 1 assert type(results[0]) == Product with pytest.raises(NoContentError): results = service.super_discounts(gender=str(uuid4()))
def test_product_get(es_object): obj = ProductFactory.create() obj.save(using=es_object.connection) res = Product.get(using=es_object.connection, id=obj.meta["id"]) assert res is not None assert type(res) is Product assert res.meta["id"] == obj.meta["id"] assert res.meta["index"] == "store" assert res.meta["doc_type"] == "products"
def test_product_dict_min(es_object): obj = ProductFactory.create() obj.save(using=es_object.connection) res = Product.get(using=es_object.connection, id=obj.meta["id"]) obj_dict_min = res.get_dict_min() for key in ["id", "name", "image", "price", "discount"]: assert key in obj_dict_min assert len(obj_dict_min.keys()) == 5 for pkey in ["outlet", "retail", "symbol"]: assert pkey in obj_dict_min["price"] assert len(obj_dict_min["price"].keys()) == 3
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_product_service_select_by_id(service, es_object): obj = ProductFactory.create() obj.save(using=es_object.connection) Index("store", using=es_object.connection).refresh() obj_id = obj.meta["id"] results = service.select_by_id(obj_id) assert type(results) == Product assert results.meta["id"] == obj_id fake_id = str(uuid4()) with pytest.raises(NotFoundError): service.select_by_id(fake_id)
def test_product_dict(es_object): obj = ProductFactory.create() obj.save(using=es_object.connection) res = Product.get(using=es_object.connection, id=obj.meta["id"]) obj_dict = res.get_dict() for key in [ "id", "name", "link", "kind", "brand", "details", "care", "about", "images", "sessionid", "sessionname", "gender", "price" ]: assert key in obj_dict assert len(obj_dict.keys()) == 13 for pkey in ["outlet", "retail", "symbol"]: assert pkey in obj_dict["price"] assert len(obj_dict["price"].keys()) == 3
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_product(domain_url, es_object, token_session): prod_obj = ProductFactory.create() prod_obj.save(using=es_object.connection) Index("store", using=es_object.connection).refresh() prod_id = prod_obj.meta["id"] response = token_session.get(domain_url + "/api/product/%s" % prod_id) data = response.json() ProductResultsSchema().load(data) assert response.status_code == 200 response = token_session.get(domain_url + "/api/product/%s" % str(uuid4())) data = response.json() assert data == {} assert response.status_code == 404
def test_product_service_get_total(service, es_object): ProductFactory.create().save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.get_total() assert results > 0 test_alt_id = "I_test_product_service_get_total" test_id = str(uuid4()) ProductFactory.create(gender=test_alt_id).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.get_total(gender=test_alt_id) assert results == 1 results = service.get_total(gender=str(uuid4())) assert results == 0 ProductFactory.create(sessionid=test_id).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.get_total(sessionid=test_id) assert results == 1 results = service.get_total(sessionid=str(uuid4())) assert results == 0 ProductFactory.create(sessionname=test_id).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.get_total(sessionname=test_id) assert results == 1 results = service.get_total(sessionname=str(uuid4())) assert results == 0 ProductFactory.create(brand=test_id).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.get_total(brand=test_id) assert results == 1 results = service.get_total(brand=str(uuid4())) assert results == 0 ProductFactory.create(kind=test_id).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.get_total(kind=test_id) assert results == 1 results = service.get_total(kind=str(uuid4())) assert results == 0 results = service.get_total(pricerange={"min": 1.0, "max": 100.0}) assert results > 0 results = service.get_total(pricerange={"min": 10000.0, "max": 20000.0}) assert results == 0 results = service.get_total(query=test_id) assert results == 2 results = service.get_total(query=test_alt_id) assert results == 1 results = service.get_total(query=str(uuid4())) assert results == 0
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
def test_product_service_select_pricerange(service, es_object): ProductFactory.create(price={ "outlet": 10.0, "retail": 100.0 }).save(using=es_object.connection) ProductFactory.create(price={ "outlet": 20.0, "retail": 120.0 }).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select_pricerange() for key in ["min", "max"]: assert key in results assert len(results.keys()) == 2 assert results["min"] <= results["max"] test_alt_id = "I_test_product_service_select_pricerange" test_id = str(uuid4()) ProductFactory.create(gender=test_alt_id, price={ "outlet": 10.0, "retail": 100.0 }).save(using=es_object.connection) ProductFactory.create(gender=test_alt_id, price={ "outlet": 20.0, "retail": 120.0 }).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select_pricerange(gender=test_alt_id) assert results["min"] == 10.0 assert results["max"] == 20.0 with pytest.raises(NoContentError): service.select_pricerange(gender=str(uuid4())) ProductFactory.create(sessionid=test_id, price={ "outlet": 10.0, "retail": 100.0 }).save(using=es_object.connection) ProductFactory.create(sessionid=test_id, price={ "outlet": 20.0, "retail": 120.0 }).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select_pricerange(sessionid=test_id) assert results["min"] == 10.0 assert results["max"] == 20.0 with pytest.raises(NoContentError): service.select_pricerange(sessionid=str(uuid4())) ProductFactory.create(sessionname=test_id, price={ "outlet": 10.0, "retail": 100.0 }).save(using=es_object.connection) ProductFactory.create(sessionname=test_id, price={ "outlet": 20.0, "retail": 120.0 }).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select_pricerange(sessionname=test_id) assert results["min"] == 10.0 assert results["max"] == 20.0 with pytest.raises(NoContentError): service.select_pricerange(sessionname=str(uuid4())) ProductFactory.create(brand=test_id, price={ "outlet": 10.0, "retail": 100.0 }).save(using=es_object.connection) ProductFactory.create(brand=test_id, price={ "outlet": 20.0, "retail": 120.0 }).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select_pricerange(brand=test_id) assert results["min"] == 10.0 assert results["max"] == 20.0 with pytest.raises(NoContentError): service.select_pricerange(brand=str(uuid4())) ProductFactory.create(kind=test_id, price={ "outlet": 10.0, "retail": 100.0 }).save(using=es_object.connection) ProductFactory.create(kind=test_id, price={ "outlet": 20.0, "retail": 120.0 }).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select_pricerange(kind=test_id) assert results["min"] == 10.0 assert results["max"] == 20.0 with pytest.raises(NoContentError): service.select_pricerange(kind=str(uuid4())) results = service.select_pricerange(query=test_id) assert results["min"] == 10.0 assert results["max"] == 20.0 results = service.select_pricerange(query=test_alt_id) assert results["min"] == 10.0 assert results["max"] == 20.0 with pytest.raises(NoContentError): service.select_pricerange(query=str(uuid4()))
def test_product_service_select_kinds(service, es_object): ProductFactory.create().save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select_kinds() assert len(results) > 0 test_alt_id = "I_test_product_service_select_kinds" test_id = str(uuid4()) ProductFactory.create(gender=test_alt_id).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select_kinds(gender=test_alt_id) assert len(results) == 1 for key in ["kind", "amount"]: assert key in results[0] with pytest.raises(NoContentError): service.select_kinds(gender=str(uuid4())) ProductFactory.create(sessionid=test_id).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select_kinds(sessionid=test_id) assert len(results) == 1 for key in ["kind", "amount"]: assert key in results[0] with pytest.raises(NoContentError): service.select_kinds(sessionid=str(uuid4())) ProductFactory.create(sessionname=test_id).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select_kinds(sessionname=test_id) assert len(results) == 1 for key in ["kind", "amount"]: assert key in results[0] with pytest.raises(NoContentError): service.select_kinds(sessionname=str(uuid4())) ProductFactory.create(brand=test_id).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select_kinds(brand=test_id) assert len(results) == 1 for key in ["kind", "amount"]: assert key in results[0] with pytest.raises(NoContentError): service.select_kinds(brand=str(uuid4())) ProductFactory.create(kind=test_id).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select_kinds(kind=test_id) assert len(results) == 1 for key in ["kind", "amount"]: assert key in results[0] with pytest.raises(NoContentError): service.select_kinds(kind=str(uuid4())) results = service.select_kinds(pricerange={"min": 1.0, "max": 100.0}) assert len(results) > 0 with pytest.raises(NoContentError): service.select_kinds(pricerange={"min": 10000.0, "max": 20000.0}) results = service.select_kinds(query=test_id) assert len(results) == 2 for key in ["kind", "amount"]: assert key in results[0] results = service.select_kinds(query=test_alt_id) assert len(results) == 1 for key in ["kind", "amount"]: assert key in results[0] with pytest.raises(NoContentError): service.select_kinds(query=str(uuid4()))
def test_product_service_select(service, es_object): ProductFactory.create().save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select() assert len(results) > 0 test_alt_id = "I_test_product_service_select" test_id = str(uuid4()) ProductFactory.create(gender=test_alt_id).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select(gender=test_alt_id) assert len(results) == 1 assert type(results[0]) == Product with pytest.raises(NoContentError): service.select(gender=str(uuid4())) ProductFactory.create(sessionid=test_id).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select(sessionid=test_id) assert len(results) == 1 assert type(results[0]) == Product with pytest.raises(NoContentError): service.select(sessionid=str(uuid4())) ProductFactory.create(sessionname=test_id).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select(sessionname=test_id) assert len(results) == 1 assert type(results[0]) == Product with pytest.raises(NoContentError): service.select(sessionname=str(uuid4())) ProductFactory.create(brand=test_id).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select(brand=test_id) assert len(results) == 1 assert type(results[0]) == Product with pytest.raises(NoContentError): service.select(brand=str(uuid4())) ProductFactory.create(kind=test_id).save(using=es_object.connection) Index("store", using=es_object.connection).refresh() results = service.select(kind=test_id) assert len(results) == 1 assert type(results[0]) == Product with pytest.raises(NoContentError): service.select(kind=str(uuid4())) results = service.select(pricerange={"min": 1.0, "max": 100.0}) assert len(results) > 0 assert type(results[0]) == Product with pytest.raises(NoContentError): service.select(pricerange={"min": 10000.0, "max": 20000.0}) results = service.select(query=test_id) assert len(results) == 2 assert type(results[0]) == Product results = service.select(query=test_alt_id) assert len(results) == 1 assert type(results[0]) == Product with pytest.raises(NoContentError): service.select(query=str(uuid4()))