def test_product_caching_object():
    shop_product = get_default_shop_product()
    product = shop_product.product
    another_product = create_product("PCOTestProduct")

    pco = ProductCachingObject()
    pco.product = product
    assert pco.product is product
    assert pco.product_id == product.pk
    assert ProductCachingObject(
    ).product != pco.product  # Assert PCOs are separate
    assert pco._product_cache == pco.product  # This private property is courtesy of ModelCachingDescriptor

    pco = ProductCachingObject()
    pco.product_id = product.pk
    assert pco.product == product
    assert pco.product_id == product.pk

    # Not creating a new PCO here
    pco.product = another_product
    assert pco.product == another_product
    assert pco.product_id == another_product.pk

    # Nor here
    pco.product_id = product.pk
    assert pco.product == product
    assert pco.product_id == product.pk
def test_product_caching_object_type_validation():
    with pytest.raises(TypeError):
        pco = ProductCachingObject()
        pco.product_id = "yeah"

    with pytest.raises(TypeError):
        pco = ProductCachingObject()
        pco.product = "yeahhh"

    with pytest.raises(ValueError):
        pco = ProductCachingObject()
        pco.product = Product()
def test_product_caching_object_nulling():
    pco = ProductCachingObject()
    pco.product = None
    assert not pco.product
    assert not pco.product_id

    pco = ProductCachingObject()
    pco.product = None
    assert not pco.product_id

    pco = ProductCachingObject()
    pco.product_id = None
    assert not pco.product
def test_product_caching_object():
    shop_product = get_default_shop_product()
    product = shop_product.product
    another_product = create_product("PCOTestProduct")

    pco = ProductCachingObject()
    pco.product = product
    assert pco.product is product
    assert pco.product_id == product.pk
    assert ProductCachingObject().product != pco.product  # Assert PCOs are separate
    assert pco._product_cache == pco.product  # This private property is courtesy of ModelCachingDescriptor

    pco = ProductCachingObject()
    pco.product_id = product.pk
    assert pco.product == product
    assert pco.product_id == product.pk

    # Not creating a new PCO here
    pco.product = another_product
    assert pco.product == another_product
    assert pco.product_id == another_product.pk

    # Nor here
    pco.product_id = product.pk
    assert pco.product == product
    assert pco.product_id == product.pk
def test_product_caching_object_type_validation():
    with pytest.raises(TypeError):
        pco = ProductCachingObject()
        pco.product_id = "yeah"

    with pytest.raises(TypeError):
        pco = ProductCachingObject()
        pco.product = "yeahhh"

    with pytest.raises(ValueError):
        pco = ProductCachingObject()
        pco.product = Product()
def test_product_caching_object_nulling():
    pco = ProductCachingObject()
    pco.product = None
    assert not pco.product
    assert not pco.product_id

    pco = ProductCachingObject()
    pco.product = None
    assert not pco.product_id

    pco = ProductCachingObject()
    pco.product_id = None
    assert not pco.product