Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions state-manager/app/controller/upsert_graph_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from app.tasks.verify_graph import verify_graph

from fastapi import BackgroundTasks, HTTPException
from beanie.operators import Set

logger = LogsManager().get_logger()

Expand All @@ -23,15 +22,13 @@ async def upsert_graph_template(namespace_name: str, graph_name: str, body: Upse
namespace_name=namespace_name,
x_exosphere_request_id=x_exosphere_request_id)

await graph_template.set_secrets(body.secrets).update(
Set({
GraphTemplate.nodes: body.nodes, # type: ignore
GraphTemplate.validation_status: GraphTemplateValidationStatus.PENDING, # type: ignore
GraphTemplate.validation_errors: [], # type: ignore
GraphTemplate.retry_policy: body.retry_policy, # type: ignore
GraphTemplate.store_config: body.store_config # type: ignore
})
)
graph_template.set_secrets(body.secrets)
graph_template.validation_status = GraphTemplateValidationStatus.PENDING
graph_template.validation_errors = []
graph_template.retry_policy = body.retry_policy
graph_template.store_config = body.store_config
graph_template.nodes = body.nodes
await graph_template.save()
Comment thread
NiveditJain marked this conversation as resolved.
Comment thread
NiveditJain marked this conversation as resolved.

else:
logger.info(
Expand Down Expand Up @@ -63,6 +60,7 @@ async def upsert_graph_template(namespace_name: str, graph_name: str, body: Upse
validation_errors=graph_template.validation_errors,
secrets={secret_name: True for secret_name in graph_template.get_secrets().keys()},
retry_policy=graph_template.retry_policy,
store_config=graph_template.store_config,
Comment thread
NiveditJain marked this conversation as resolved.
created_at=graph_template.created_at,
updated_at=graph_template.updated_at
)
Expand Down
1 change: 1 addition & 0 deletions state-manager/app/models/db/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async def insert_many(cls, documents: list["State"]) -> InsertManyResult:
return await super().insert_many(documents) # type: ignore

class Settings:
validate_on_save = True
indexes = [
IndexModel(
[
Expand Down
31 changes: 22 additions & 9 deletions state-manager/tests/unit/controller/test_upsert_graph_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from app.models.graph_template_validation_status import GraphTemplateValidationStatus
from app.models.node_template_model import NodeTemplate
from app.models.retry_policy_model import RetryPolicyModel
from app.models.store_config_model import StoreConfig
Comment thread
NiveditJain marked this conversation as resolved.


class TestUpsertGraphTemplate:
Expand Down Expand Up @@ -83,6 +84,9 @@ def mock_existing_template(self, mock_nodes, mock_secrets):
max_delay=30000
)

# Add store_config
template.store_config = StoreConfig()

return template

@patch('app.controller.upsert_graph_template.GraphTemplate')
Expand All @@ -101,7 +105,7 @@ async def test_upsert_graph_template_update_existing(
"""Test successful update of existing graph template"""
# Arrange

mock_existing_template.update = AsyncMock()
mock_existing_template.save = AsyncMock()
mock_existing_template.set_secrets = MagicMock(return_value=mock_existing_template)
mock_graph_template_class.find_one = AsyncMock(return_value=mock_existing_template)

Expand All @@ -118,15 +122,15 @@ async def test_upsert_graph_template_update_existing(

# Assert
assert result.nodes == mock_upsert_request.nodes
assert result.validation_status == GraphTemplateValidationStatus.VALID
assert result.validation_status == GraphTemplateValidationStatus.PENDING
assert result.validation_errors == []
assert result.secrets == {"api_key": True, "database_url": True}
assert result.created_at == mock_existing_template.created_at
assert result.updated_at == mock_existing_template.updated_at

# Verify template was updated
mock_existing_template.set_secrets.assert_called_once_with(mock_upsert_request.secrets)
mock_existing_template.update.assert_called_once()
mock_existing_template.save.assert_called_once()

# Verify background task was added
mock_background_tasks.add_task.assert_called_once_with(mock_verify_graph, mock_existing_template)
Expand Down Expand Up @@ -165,6 +169,9 @@ async def test_upsert_graph_template_create_new(
)
mock_new_template.retry_policy = mock_retry_policy

# Add store_config
mock_new_template.store_config = StoreConfig()

mock_graph_template_class.insert = AsyncMock(return_value=mock_new_template)

# Act
Expand Down Expand Up @@ -242,15 +249,18 @@ async def test_upsert_graph_template_with_empty_nodes(
mock_existing_template.get_secrets.return_value = {}
mock_existing_template.set_secrets.return_value = mock_existing_template

# Add proper retry_policy mock
# Add proper retry_policy mock
mock_retry_policy = RetryPolicyModel(
max_retries=3,
backoff_factor=1000,
max_delay=30000
)
mock_existing_template.retry_policy = mock_retry_policy

mock_existing_template.update = AsyncMock()
# Add store_config
mock_existing_template.store_config = StoreConfig()

mock_existing_template.save = AsyncMock()

Comment thread
NiveditJain marked this conversation as resolved.
mock_graph_template_class.find_one = AsyncMock(return_value=mock_existing_template)

Expand All @@ -266,7 +276,7 @@ async def test_upsert_graph_template_with_empty_nodes(
sleep(1) # wait for the background task to complete
# Assert
assert result.nodes == []
assert result.validation_status == GraphTemplateValidationStatus.VALID
assert result.validation_status == GraphTemplateValidationStatus.PENDING
assert result.validation_errors == []
assert result.secrets == {}

Expand Down Expand Up @@ -301,8 +311,11 @@ async def test_upsert_graph_template_with_validation_errors(
max_delay=30000
)
mock_existing_template.retry_policy = mock_retry_policy

# Add store_config
mock_existing_template.store_config = StoreConfig()

mock_existing_template.update = AsyncMock()
mock_existing_template.save = AsyncMock()

mock_graph_template_class.find_one = AsyncMock(return_value=mock_existing_template)

Expand All @@ -318,8 +331,8 @@ async def test_upsert_graph_template_with_validation_errors(
sleep(1) # wait for the background task to complete

# Assert
assert result.validation_status == GraphTemplateValidationStatus.INVALID
assert result.validation_errors == ["Previous error 1", "Previous error 2"] # Should be reset to empty
assert result.validation_status == GraphTemplateValidationStatus.PENDING
assert result.validation_errors == [] # Should be reset to empty

@patch('app.controller.upsert_graph_template.GraphTemplate')
async def test_upsert_graph_template_validation_error(
Expand Down
Loading