def test_get_front_products(admin_user):
    shop1 = get_default_shop()
    shop2 = Shop.objects.create(status=ShopStatus.ENABLED)
    supplier = get_default_supplier()

    p1 = create_product("product1", shop=shop1, supplier=supplier)
    p2 = create_product("product2", shop=shop1, supplier=supplier)

    sp1 = ShopProduct.objects.get(shop=shop1, product=p1)
    sp2 = ShopProduct.objects.get(shop=shop1, product=p2)

    # add products to other shop
    sp3 = ShopProduct.objects.create(shop=shop2, product=p1)
    sp4 = ShopProduct.objects.create(shop=shop2, product=p2)

    request = get_request("/api/shuup/front/products/", admin_user)
    response = FrontProductViewSet.as_view({"get": "list"})(request)
    response.render()
    assert response.status_code == status.HTTP_200_OK
    products_data = json.loads(response.content.decode("utf-8"))
    # should return only 2 procuts with 2 shops each
    assert len(products_data) == 2
    products = sorted(products_data, key=lambda p: p["id"])
    assert products[0]["id"] == p1.id
    assert products[0]["shop_products"] == [sp1.id, sp3.id]
    assert products[1]["id"] == p2.id
    assert products[1]["shop_products"] == [sp2.id, sp4.id]
示例#2
0
def test_get_front_products(admin_user):
    shop1 = get_default_shop()
    shop2 = Shop.objects.create(status=ShopStatus.ENABLED)
    supplier = get_default_supplier()

    p1 = create_product("product1", shop=shop1, supplier=supplier)
    p2 = create_product("product2", shop=shop1, supplier=supplier)

    sp1 = ShopProduct.objects.get(shop=shop1, product=p1)
    sp2 = ShopProduct.objects.get(shop=shop1, product=p2)

    # add products to other shop
    sp3 = ShopProduct.objects.create(shop=shop2, product=p1)
    sp4 = ShopProduct.objects.create(shop=shop2, product=p2)

    request = get_request("/api/shuup/front/products/", admin_user)
    response = FrontProductViewSet.as_view({"get": "list"})(request)
    response.render()
    assert response.status_code == status.HTTP_200_OK
    products_data = json.loads(response.content.decode("utf-8"))
    # should return only 2 procuts with 2 shops each
    assert len(products_data) == 2
    products = sorted(products_data, key=lambda p: p["id"])
    assert products[0]["id"] == p1.id
    assert products[0]["shop_products"] == [sp1.id, sp3.id]
    assert products[1]["id"] == p2.id
    assert products[1]["shop_products"] == [sp2.id, sp4.id]
def test_nearby_products(admin_user):
    get_default_shop()
    supplier = create_simple_supplier("supplier1")

    # create Apple and its products
    shop1 = Shop.objects.create(status=ShopStatus.ENABLED)
    shop1.contact_address = MutableAddress.objects.create(
        name="Apple Infinite Loop",
        street="1 Infinite Loop",
        country="US",
        city="Cupertino",
        latitude=37.331667,
        longitude=-122.030146)
    shop1.save()
    product1 = create_product("macbook", shop1, supplier=supplier)
    product2 = create_product("imac", shop1, supplier=supplier)

    # create Google and its products
    shop2 = Shop.objects.create(status=ShopStatus.ENABLED)
    shop2.contact_address = MutableAddress.objects.create(
        name="Google",
        street="1600 Amphitheatre Pkwy",
        country="US",
        city="Mountain View",
        latitude=37.422000,
        longitude=-122.084024)
    shop2.save()
    product3 = create_product("nexus 1", shop2, supplier=supplier)
    product4 = create_product("nexux 7", shop2, supplier=supplier)

    my_position_to_apple = 2.982
    my_position_to_google = 10.57
    # YMCA
    my_position = (37.328330, -122.063612)

    # fetch products and their closest shops
    params = {
        "lat": my_position[0],
        "lng": my_position[1],
        "ordering": "-distance"
    }
    request = get_request("/api/shuup/front/products/",
                          admin_user,
                          data=params)
    response = FrontProductViewSet.as_view({"get": "list"})(request)
    response.render()
    assert response.status_code == status.HTTP_200_OK
    products = json.loads(response.content.decode("utf-8"))
    assert len(products) == 4

    assert products[0]["id"] == product3.id
    assert products[0]["name"] == product3.name
    assert products[0][
        "closest_shop_distance"] - my_position_to_google < 0.05  # 5 meters of error margin

    assert products[1]["id"] == product4.id
    assert products[1]["name"] == product4.name
    assert products[1][
        "closest_shop_distance"] - my_position_to_google < 0.05  # 5 meters of error margin

    assert products[2]["id"] == product1.id
    assert products[2]["name"] == product1.name
    assert products[2][
        "closest_shop_distance"] - my_position_to_apple < 0.05  # 5 meters of error margin

    assert products[3]["id"] == product2.id
    assert products[3]["name"] == product2.name
    assert products[3][
        "closest_shop_distance"] - my_position_to_apple < 0.05  # 5 meters of error margin
示例#4
0
def test_nearby_products(admin_user):
    get_default_shop()
    supplier = create_simple_supplier("supplier1")

    # create Apple and its products
    shop1 = Shop.objects.create(status=ShopStatus.ENABLED)
    shop1.contact_address = MutableAddress.objects.create(
        name="Apple Infinite Loop",
        street="1 Infinite Loop",
        country="US",
        city="Cupertino",
        latitude=37.331667,
        longitude=-122.030146
    )
    shop1.save()
    product1 = create_product("macbook", shop1, supplier=supplier)
    product2 = create_product("imac", shop1, supplier=supplier)

    # create Google and its products
    shop2 = Shop.objects.create(status=ShopStatus.ENABLED)
    shop2.contact_address = MutableAddress.objects.create(
        name="Google",
        street="1600 Amphitheatre Pkwy",
        country="US",
        city="Mountain View",
        latitude=37.422000,
        longitude=-122.084024
    )
    shop2.save()
    product3 = create_product("nexus 1", shop2, supplier=supplier)
    product4 = create_product("nexux 7", shop2, supplier=supplier)

    my_position_to_apple = 2.982
    my_position_to_google = 10.57
    # YMCA
    my_position = (37.328330, -122.063612)

    # fetch products and their closest shops
    params = {"lat": my_position[0], "lng": my_position[1], "ordering": "-distance"}
    request = get_request("/api/shuup/front/products/", admin_user, data=params)
    response = FrontProductViewSet.as_view({"get": "list"})(request)
    response.render()
    assert response.status_code == status.HTTP_200_OK
    products = json.loads(response.content.decode("utf-8"))
    assert len(products) == 4

    assert products[0]["id"] == product3.id
    assert products[0]["name"] == product3.name
    assert products[0]["closest_shop_distance"] - my_position_to_google < 0.05    # 5 meters of error margin

    assert products[1]["id"] == product4.id
    assert products[1]["name"] == product4.name
    assert products[1]["closest_shop_distance"] - my_position_to_google < 0.05    # 5 meters of error margin

    assert products[2]["id"] == product1.id
    assert products[2]["name"] == product1.name
    assert products[2]["closest_shop_distance"] - my_position_to_apple < 0.05    # 5 meters of error margin

    assert products[3]["id"] == product2.id
    assert products[3]["name"] == product2.name
    assert products[3]["closest_shop_distance"] - my_position_to_apple < 0.05    # 5 meters of error margin