Skip to content

Commit

Permalink
Add generator testcase for a product with a nested product block
Browse files Browse the repository at this point in the history
Signed-off-by: Mark90 <[email protected]>
  • Loading branch information
Mark90 committed Jun 11, 2024
1 parent 67718d6 commit d837897
Show file tree
Hide file tree
Showing 19 changed files with 573 additions and 6 deletions.
2 changes: 1 addition & 1 deletion test/unit_tests/cli/data/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export PYTHONPATH=../../../../..
python main.py db init

# generate code for the two sample products
for YAML in ../product_config2.yaml ../product_config1.yaml
for YAML in ../product_config2.yaml ../product_config1.yaml ../product_config4.yaml
do
python main.py generate product-blocks --config-file $YAML --no-dryrun --force
python main.py generate product --config-file $YAML --no-dryrun --force
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"""Add example4 product.
Revision ID: 380a5b0c928c
Revises: 44667c4d16cd
Create Date: 2024-06-07 10:23:26.761903
"""

from uuid import uuid4

from alembic import op
from orchestrator.migrations.helpers import create, create_workflow, delete, delete_workflow, ensure_default_workflows
from orchestrator.targets import Target

# revision identifiers, used by Alembic.
revision = "380a5b0c928c"
down_revision = "ea9e6c9de75c"
branch_labels = None
depends_on = None

new_products = {
"products": {
"example4": {
"product_id": uuid4(),
"product_type": "Example4",
"description": "Product example 4",
"tag": "EXAMPLE4",
"status": "active",
"root_product_block": "Example4",
"fixed_inputs": {},
},
},
"product_blocks": {
"Example4Sub": {
"product_block_id": uuid4(),
"description": "example 4 sub product block",
"tag": "EXAMPLE4SUB",
"status": "active",
"resources": {
"str_val": "",
},
"depends_on_block_relations": [],
},
"Example4": {
"product_block_id": uuid4(),
"description": "Example 4 root product block",
"tag": "EXAMPLE4",
"status": "active",
"resources": {
"num_val": "",
},
"depends_on_block_relations": [
"Example4Sub",
],
},
},
"workflows": {},
}

new_workflows = [
{
"name": "create_example4",
"target": Target.CREATE,
"description": "Create example4",
"product_type": "Example4",
},
{
"name": "modify_example4",
"target": Target.MODIFY,
"description": "Modify example4",
"product_type": "Example4",
},
{
"name": "terminate_example4",
"target": Target.TERMINATE,
"description": "Terminate example4",
"product_type": "Example4",
},
{
"name": "validate_example4",
"target": Target.SYSTEM,
"description": "Validate example4",
"product_type": "Example4",
},
]


def upgrade() -> None:
conn = op.get_bind()
create(conn, new_products)
for workflow in new_workflows:
create_workflow(conn, workflow)
ensure_default_workflows(conn)


def downgrade() -> None:
conn = op.get_bind()
for workflow in new_workflows:
delete_workflow(conn, workflow["name"])

delete(conn, new_products)
7 changes: 7 additions & 0 deletions test/unit_tests/cli/data/generate/products/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@
"example1 1000": Example1,
},
) # fmt:skip
from products.product_types.example4 import Example4

SUBSCRIPTION_MODEL_REGISTRY.update(
{
"example4": Example4,
},
) # fmt:skip
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from orchestrator.domain.base import ProductBlockModel
from orchestrator.types import SubscriptionLifecycle
from pydantic import computed_field

from products.product_blocks.example4sub import Example4SubBlock, Example4SubBlockInactive, Example4SubBlockProvisioning


class Example4BlockInactive(ProductBlockModel, product_block_name="Example4"):
num_val: int | None = None
sub_block: Example4SubBlockInactive | None = None


class Example4BlockProvisioning(Example4BlockInactive, lifecycle=[SubscriptionLifecycle.PROVISIONING]):
num_val: int | None = None
sub_block: Example4SubBlockProvisioning

@computed_field
@property
def title(self) -> str:
# TODO: format correct title string
return f"{self.name}"


class Example4Block(Example4BlockProvisioning, lifecycle=[SubscriptionLifecycle.ACTIVE]):
num_val: int | None = None
sub_block: Example4SubBlock
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from orchestrator.domain.base import ProductBlockModel
from orchestrator.types import SubscriptionLifecycle
from pydantic import computed_field


class Example4SubBlockInactive(ProductBlockModel, product_block_name="Example4 Sub"):
str_val: str | None = None


class Example4SubBlockProvisioning(Example4SubBlockInactive, lifecycle=[SubscriptionLifecycle.PROVISIONING]):
str_val: str | None = None

@computed_field
@property
def title(self) -> str:
# TODO: format correct title string
return f"{self.name}"


class Example4SubBlock(Example4SubBlockProvisioning, lifecycle=[SubscriptionLifecycle.ACTIVE]):
str_val: str | None = None
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from orchestrator.domain.base import SubscriptionModel
from orchestrator.types import SubscriptionLifecycle

from products.product_blocks.example4 import Example4Block, Example4BlockInactive, Example4BlockProvisioning


class Example4Inactive(SubscriptionModel, is_base=True):
example4: Example4BlockInactive


class Example4Provisioning(Example4Inactive, lifecycle=[SubscriptionLifecycle.PROVISIONING]):
example4: Example4BlockProvisioning


class Example4(Example4Provisioning, lifecycle=[SubscriptionLifecycle.ACTIVE]):
example4: Example4Block
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from uuid import uuid4

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

from products.product_types.example4 import Example4, Example4Inactive


def test_example4_new():
product = ProductTable.query.filter(ProductTable.name == "example4").one()

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

example4 = Example4Inactive.from_product_id(
product_id=product.product_id,
customer_id=uuid4(),
status=SubscriptionLifecycle.INITIAL,
)

assert example4.subscription_id is not None
assert example4.insync is False

# TODO: Add more product specific asserts

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

example42 = Example4Inactive.from_subscription(example4.subscription_id)
assert example4 == example42


def test_example4_load_and_save_db(example4_subscription):
example4 = Example4.from_subscription(example4_subscription)

assert example4.insync is True

# TODO: Add more product specific asserts

example4.description = "Changed description"

# TODO: add a product specific change

example4.save()

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

example4 = Example4.from_subscription(example4_subscription)

# TODO: Add more product specific asserts

assert example4.description == "Changed description"
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest
from orchestrator.db import ProductTable

from products.product_types.example4 import Example4
from test.unit_tests.workflows import assert_complete, extract_state, run_workflow


@pytest.mark.workflow()
def test_happy_flow(responses):
# given

# TODO insert additional mocks, if needed (ImsMocks)

product = db.session.scalars(select(ProductTable).where(ProductTable.name == "example4")).one()

# when

init_state = {
"customer_id": customer_id,
# TODO add initial state
}

result, process, step_log = run_workflow("create_example4", [{"product": product.product_id}, init_state])

# then

assert_complete(result)
state = extract_state(result)

subscription = Example4.from_subscription(state["subscription_id"])
assert subscription.status == "active"
assert subscription.description == "TODO add correct description"
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest
from orchestrator.types import SubscriptionLifecycle

from products.product_types.example4 import Example4
from test.unit_tests.workflows import assert_complete, extract_state, run_workflow


@pytest.mark.workflow()
def test_happy_flow(responses, example4_subscription):
# given

customer_id = "3f4fc287-0911-e511-80d0-005056956c1a"
crm = CrmMocks(responses)
crm.get_customer_by_uuid(customer_id)

# TODO insert additional mocks, if needed (ImsMocks)

# when

init_state = {}

result, process, step_log = run_workflow(
"modify_example4",
[{"subscription_id": example4_subscription}, init_state, {}],
)

# then

assert_complete(result)
state = extract_state(result)

example4 = Example4.from_subscription(state["subscription_id"])
assert example4.status == SubscriptionLifecycle.ACTIVE
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest
from orchestrator.types import SubscriptionLifecycle

from products.product_types.example4 import Example4
from test.unit_tests.workflows import assert_complete, extract_state, run_workflow


@pytest.mark.workflow()
def test_happy_flow(responses, example4_subscription):
# when

# TODO: insert mocks here if needed

result, _, _ = run_workflow("terminate_example4", [{"subscription_id": example4_subscription}, {}])

# then

assert_complete(result)
state = extract_state(result)
assert "subscription" in state

# Check subscription in DB

example4 = Example4.from_subscription(example4_subscription)
assert example4.end_date is not None
assert example4.status == SubscriptionLifecycle.TERMINATED
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest

from test.unit_tests.workflows import assert_complete, extract_state, run_workflow


@pytest.mark.workflow()
def test_happy_flow(responses, example4_subscription):
# when

result, _, _ = run_workflow("validate_example4", {"subscription_id": example4_subscription})

# then

assert_complete(result)
state = extract_state(result)
assert state["check_core_db"] is True
6 changes: 5 additions & 1 deletion test/unit_tests/cli/data/generate/translations/en-GB.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
"workflow": {
"create_example1": "Create example1",
"create_example2": "Create example2",
"create_example4": "Create example4",
"modify_example1": "Modify example1",
"modify_example2": "Modify example2",
"modify_example4": "Modify example4",
"terminate_example1": "Terminate example1",
"terminate_example2": "Terminate example2",
"terminate_example4": "Terminate example4",
"validate_example1": "Validate example1",
"validate_example2": "Validate example2"
"validate_example2": "Validate example2",
"validate_example4": "Validate example4"
}
}
4 changes: 4 additions & 0 deletions test/unit_tests/cli/data/generate/workflows/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@
LazyWorkflowInstance("workflows.example1.modify_example1", "modify_example1")
LazyWorkflowInstance("workflows.example1.terminate_example1", "terminate_example1")
LazyWorkflowInstance("workflows.example1.validate_example1", "validate_example1")
LazyWorkflowInstance("workflows.example4.create_example4", "create_example4")
LazyWorkflowInstance("workflows.example4.modify_example4", "modify_example4")
LazyWorkflowInstance("workflows.example4.terminate_example4", "terminate_example4")
LazyWorkflowInstance("workflows.example4.validate_example4", "validate_example4")
Loading

0 comments on commit d837897

Please sign in to comment.