from uuid import uuid4

from orchestrator.db import ProductTable, db
from orchestrator.types import SubscriptionLifecycle

from {{ product_types_module }}.{{ product.variable }} import {{ product.type }}, {{ product.type }}Inactive


def test_{{ product.variable }}_new():
    product = ProductTable.query.filter(ProductTable.name == "{{ product.name }}").one()

    diff = {{ product.type }}.diff_product_in_database(product.product_id)
    assert diff == {}

    {{ product.variable }} = {{ product.type }}Inactive.from_product_id(
        product_id=product.product_id, customer_id=uuid4(), status=SubscriptionLifecycle.INITIAL
    )

    assert {{ product.variable }}.subscription_id is not None
    assert {{ product.variable }}.insync is False

    # TODO: Add more product specific asserts

    assert {{ product.variable }}.description == f"Initial subscription of {product.description}"
    {{ product.variable }}.save()

    {{ product.variable }}2 = {{ product.type }}Inactive.from_subscription({{ product.variable }}.subscription_id)
    assert {{ product.variable }} == {{ product.variable }}2


def test_{{ product.variable }}_load_and_save_db({{ product.variable }}_subscription):
    {{ product.variable }} = {{ product.type }}.from_subscription({{ product.variable }}_subscription)

    assert {{ product.variable }}.insync is True

    # TODO: Add more product specific asserts

    {{ product.variable }}.description = "Changed description"

    # TODO: add a product specific change

    {{ product.variable }}.save()

    # Explicit commit here as we are not running in the context of a step
    db.session.commit()

    {{ product.variable }} = {{ product.type }}.from_subscription({{ product.variable }}_subscription)

    # TODO: Add more product specific asserts

    assert {{ product.variable }}.description == "Changed description"
