Beispiel #1
0
def test_product_select(db_session):
    assert db_session.query(Product).one_or_none() is None

    ProductFactory.create_batch(5)
    db_session.commit()

    assert len(db_session.query(Product).all()) == 5
def test_session_service_select(service, es_object):
    session_obj = SessionFactory.create()
    session_obj.save(using=es_object.connection)

    prod_list = ProductFactory.create_batch(2, sessionid=session_obj.meta["id"])
    [prod_obj.save(using=es_object.connection) for prod_obj in prod_list]
    Index("store", using=es_object.connection).refresh()

    results = service.select()
    assert len(results) > 0

    session_gender = "I_test_session_service_select"
    session_name = str(uuid4())
    session_obj = SessionFactory.create(gender=session_gender, name=session_name)
    session_obj.save(using=es_object.connection)

    prod_list = ProductFactory.create_batch(2, sessionid=session_obj.meta["id"])
    [prod_obj.save(using=es_object.connection) for prod_obj in prod_list]
    Index("store", using=es_object.connection).refresh()

    results = service.select(gender=session_gender)
    assert len(results) == 1
    assert results[0]["total"] == 2

    results = service.select(name=session_name)
    assert len(results) == 1
    assert results[0]["total"] == 2

    with pytest.raises(NoContentError):
        service.select(gender=str(uuid4()))

    with pytest.raises(NoContentError):
        service.select(name=str(uuid4()))
Beispiel #3
0
def test_gender_controller(domain_url, es_object, token_session):
    session_obj = SessionFactory.create(gender="Women")
    session_obj.save(using=es_object.connection)
    prod_list = ProductFactory.create_batch(2,
                                            gender="Women",
                                            sessionid=session_obj.meta["id"])
    [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/gender/women")

    data = response.json()
    GenderResultsSchema().load(data)
    assert response.status_code == 200
    assert len(data["discounts"]) >= 2

    response = token_session.post(domain_url + "/api/gender/women",
                                  json={"amount": 1})

    data = response.json()
    GenderResultsSchema().load(data)
    assert response.status_code == 200
    assert len(data["discounts"]) == 1

    response = token_session.post(domain_url + "/api/gender/%s" % str(uuid4()))

    with pytest.raises(JSONDecodeError):
        response.json()

    assert response.status_code == 204
Beispiel #4
0
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
Beispiel #5
0
def test_product_list(domain_url, es_object, token_session):
    price = {"outlet": 10.0, "retail": 20.0}
    prod_list = ProductFactory.create_batch(2, price=price)
    [prod_obj.save(using=es_object.connection) for prod_obj in prod_list]
    Index("store", using=es_object.connection).refresh()

    prod_item_list = [{
        "item_id": p.meta["id"],
        "amount": 3
    } for p in prod_list]

    response = token_session.post(domain_url + "/api/product/list",
                                  json={
                                      "item_list": prod_item_list,
                                  })

    data = response.json()
    ProductsListSchema().load(data)
    assert response.status_code == 200
    assert len(data["products"]) == 2
    assert data["total"]["outlet"] == 60.0
    assert data["total"]["retail"] == 120.0

    fake_item_list = [{"item_id": str(uuid4()), "amount": 1} for p in range(2)]

    response = token_session.post(domain_url + "/api/product/list",
                                  json={
                                      "item_list":
                                      prod_item_list + fake_item_list,
                                  })

    data = response.json()
    ErrorSchema().load(data)
    assert response.status_code == 400
Beispiel #6
0
def test_product_service_products_count(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()

    result = service.products_count()
    assert result > 0
Beispiel #7
0
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
Beispiel #8
0
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_product_list_controller(token_app, es_object):
    price = {"outlet": 10.0, "retail": 20.0}
    prod_list = ProductFactory.create_batch(2, price=price)
    [prod_obj.save(using=es_object.connection) for prod_obj in prod_list]
    Index("store", using=es_object.connection).refresh()

    prod_item_list = [{
        "item_id": p.meta["id"],
        "amount": 3
    } for p in prod_list]

    with token_app.test_client() as client:
        response = client.post("api/product/list",
                               json={
                                   "item_list": prod_item_list,
                               })

    data = json.loads(response.data)
    ProductsListSchema().load(data)
    assert response.status_code == 200
    assert len(data["products"]) == 2
    assert data["total"]["outlet"] == 60.0
    assert data["total"]["retail"] == 120.0

    fake_item_list = [{"item_id": str(uuid4()), "amount": 1} for p in range(2)]

    with token_app.test_client() as client:
        response = client.post("api/product/list",
                               json={
                                   "id_list": prod_item_list + fake_item_list,
                               })

    data = json.loads(response.data)
    ErrorSchema().load(data)
    assert response.status_code == 400
def test_count_products(service, es_object):
    session_id = str(uuid4())
    prod_list = ProductFactory.create_batch(2, sessionid=session_id)
    [prod_obj.save(using=es_object.connection) for prod_obj in prod_list]
    Index("store", using=es_object.connection).refresh()

    result = service._SessionService__count_products(session_id)
    assert result == 2
def test_brand_products_controller(token_app, es_object):
    brand = str(uuid4())
    prod_list = ProductFactory.create_batch(2, brand=brand)
    [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/brand/%s/1" % brand
        )

    data = json.loads(response.data)
    SearchProductsResultsSchema().load(data)
    assert response.status_code == 200
    assert len(data["products"]) == 2

    with token_app.test_client() as client:
        response = client.post(
            "api/brand/%s/1" % brand,
            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/brand/%s/1" % brand,
            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/brand/%s/1" % str(uuid4())
        )

    with pytest.raises(JSONDecodeError):
        json.loads(response.data)

    assert response.status_code == 204
def test_session_products(domain_url, es_object, token_session):
    session_obj = SessionFactory.create(gender="Women")
    session_obj.save(using=es_object.connection)
    session_id = session_obj.meta["id"]
    prod_list = ProductFactory.create_batch(2, gender="Women", sessionid=session_id)
    [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/session/%s/1" % session_id
    )

    data = response.json()
    SearchProductsResultsSchema().load(data)
    assert response.status_code == 200
    assert len(data["products"]) == 2

    response = token_session.post(
        domain_url + "/api/session/%s/1" % session_id,
        json={
            "pricerange": {
                "min": 1,
                "max": 500
            },
            "pagesize": 1
        }
    )

    data = response.json()
    SearchProductsResultsSchema().load(data)
    assert response.status_code == 200
    assert len(data["products"]) == 1

    response = token_session.post(
        domain_url + "/api/session/%s/1" % session_id,
        json={
            "pricerange": {
                "min": 10000,
                "max": 20000
            }
        }
    )

    with pytest.raises(JSONDecodeError):
        response.json()

    assert response.status_code == 204

    response = token_session.post(
        domain_url + "/api/session/%s/1" % str(uuid4())
    )

    with pytest.raises(JSONDecodeError):
        response.json()

    assert response.status_code == 204
def test_start_controller(token_app, 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()

    with token_app.test_client() as client:
        response = client.get("api/start")

    data = json.loads(response.data)
    ProductsCountSchema().load(data)
    assert response.status_code == 200
    assert data["count"] > 0
Beispiel #14
0
def test_start(domain_url, es_object, token_session):
    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()

    response = token_session.get(
        domain_url + "/api/start"
    )

    data = response.json()
    ProductsCountSchema().load(data)
    assert response.status_code == 200
    assert data["count"] > 0
Beispiel #15
0
def test_session_controller(token_app, es_object):
    session_obj = SessionFactory.create(gender="Women")
    session_obj.save(using=es_object.connection)
    session_id = session_obj.meta["id"]
    prod_list = ProductFactory.create_batch(2,
                                            gender="Women",
                                            sessionid=session_id)
    [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/session/%s" % session_id)

    data = json.loads(response.data)
    SessionResultsSchema().load(data)
    assert response.status_code == 200
    assert data["sessions"][0]["gender"] == session_obj.gender
    assert data["total"] == 2

    with token_app.test_client() as client:
        response = client.post("api/session/%s" % session_id,
                               json={"pricerange": {
                                   "min": 1,
                                   "max": 500
                               }})

    data = json.loads(response.data)
    SessionResultsSchema().load(data)
    assert response.status_code == 200
    assert data["sessions"][0]["gender"] == session_obj.gender
    assert data["total"] == 2

    with token_app.test_client() as client:
        response = client.post(
            "api/session/%s" % session_id,
            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/session/%s" % str(uuid4()))

    data = json.loads(response.data)
    assert data == {}
    assert response.status_code == 404
def test_order_service_insert(service, db_perm_session):
    user_slug = uuid_to_slug(uuid4())

    with pytest.raises(NoContentError):
        service.select_by_user_slug(user_slug=user_slug)

    item_list = [{"item_id": str(uuid_to_slug(uuid4())), "amount": 2} for i in range(2)]
    ins = service.insert(user_slug=user_slug, item_list=item_list)

    assert ins is True
    assert len(db_perm_session.query(Order).all()) == 1
    assert len(db_perm_session.query(Product).all()) == 2
    assert len(db_perm_session.query(OrderProduct).all()) == 2

    result = service.select_by_user_slug(user_slug=user_slug)

    assert len(result["orders"]) == 1
    order_info = result["orders"][0].to_dict()
    assert order_info["product_types"] == 2
    assert order_info["items_amount"] == 4

    product_list = ProductFactory.create_batch(5)
    db_perm_session.commit()

    item_list = [{"item_id": p.es_id, "amount": 3} for p in product_list]
    ins = service.insert(user_slug=user_slug, item_list=item_list)

    assert ins is True
    assert len(db_perm_session.query(Order).all()) == 2
    assert len(db_perm_session.query(Product).all()) == 7
    assert len(db_perm_session.query(OrderProduct).all()) == 7

    result = service.select_by_user_slug(user_slug=user_slug)

    assert len(result["orders"]) == 2
    order_info = result["orders"][0].to_dict()
    assert order_info["product_types"] == 5
    assert order_info["items_amount"] == 15

    user_slug = uuid_to_slug(uuid4())
    ins = service.insert(user_slug=user_slug, item_list=item_list)

    assert ins is True
    assert len(db_perm_session.query(Order).all()) == 3
    assert len(db_perm_session.query(Product).all()) == 7
    assert len(db_perm_session.query(OrderProduct).all()) == 12

    result = service.select_by_user_slug(user_slug=user_slug)

    assert len(result["orders"]) == 1
Beispiel #17
0
def test_kind(domain_url, es_object, token_session):
    kind = str(uuid4())
    prod_list = ProductFactory.create_batch(2, kind=kind)
    [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/kind/%s" % kind)

    data = response.json()
    SearchResultsSchema().load(data)
    assert response.status_code == 200
    assert data["total"] == 2

    response = token_session.post(domain_url + "/api/kind/%s" % kind,
                                  json={"pricerange": {
                                      "min": 1,
                                      "max": 500
                                  }})

    data = response.json()
    SearchResultsSchema().load(data)
    assert response.status_code == 200
    assert data["total"] == 2

    response = token_session.post(
        domain_url + "/api/kind/%s" % kind,
        json={"pricerange": {
            "min": 10000,
            "max": 20000
        }})

    with pytest.raises(JSONDecodeError):
        response.json()

    assert response.status_code == 204

    response = token_session.post(domain_url + "/api/kind/%s" % str(uuid4()))

    with pytest.raises(JSONDecodeError):
        response.json()

    assert response.status_code == 204
Beispiel #18
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()))
Beispiel #19
0
def test_gender_controller(token_app, es_object):
    session_obj = SessionFactory.create(gender="Women")
    session_obj.save(using=es_object.connection)
    prod_list = ProductFactory.create_batch(2, gender="Women", sessionid=session_obj.meta["id"])
    [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/gender/women"
        )

    data = json.loads(response.data)
    GenderResultsSchema().load(data)
    assert response.status_code == 200
    assert len(data["discounts"]) >= 2

    with token_app.test_client() as client:
        response = client.post(
            "api/gender/women",
            json={
                "amount": 1
            }
        )

    data = json.loads(response.data)
    GenderResultsSchema().load(data)
    assert response.status_code == 200
    assert len(data["discounts"]) == 1

    with token_app.test_client() as client:
        response = client.post(
            "api/gender/%s" % str(uuid4())
        )

    with pytest.raises(JSONDecodeError):
        json.loads(response.data)

    assert response.status_code == 204
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