From 3753f52c72429401af730a909f3fa2a3e565fcd1 Mon Sep 17 00:00:00 2001 From: Steph Herbers Date: Wed, 18 Dec 2024 13:25:57 -0500 Subject: [PATCH 01/10] prefactor: add the deletions into a test class --- apps/assistants/tests/test_delete.py | 67 ++++++++++++++-------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/apps/assistants/tests/test_delete.py b/apps/assistants/tests/test_delete.py index 2c8485116..7e43b7935 100644 --- a/apps/assistants/tests/test_delete.py +++ b/apps/assistants/tests/test_delete.py @@ -27,37 +27,36 @@ def code_resource(assistant): return tool_resource -@pytest.mark.django_db() -def test_files_to_delete_when_only_referenced_by_one_resource(code_resource): - files_to_delete = list(_get_files_to_delete(code_resource.assistant.team, code_resource.id)) - assert len(files_to_delete) == 2 - assert {f.id for f in files_to_delete} == {f.id for f in code_resource.files.all()} - - -@pytest.mark.django_db() -def test_files_not_to_delete_when_referenced_by_multiple_resources(code_resource): - all_files = list(code_resource.files.all()) - tool_resource = ToolResources.objects.create(tool_type="file_search", assistant=code_resource.assistant) - tool_resource.files.set([all_files[0]]) - - # only the second file should be deleted - files_to_delete = list(_get_files_to_delete(code_resource.assistant.team, code_resource.id)) - assert len(files_to_delete) == 1 - assert files_to_delete[0].id == all_files[1].id - - files_to_delete = list(_get_files_to_delete(tool_resource.assistant.team, tool_resource.id)) - assert len(files_to_delete) == 0 - - -@pytest.mark.django_db() -def test_delete_openai_files_for_resource(code_resource): - all_files = list(code_resource.files.all()) - assert all(f.external_id for f in all_files) - assert all(f.external_source for f in all_files) - client = Mock() - delete_openai_files_for_resource(client, code_resource.assistant.team, code_resource) - - assert client.files.delete.call_count == 2 - all_files = list(code_resource.files.all()) - assert not any(f.external_id for f in all_files) - assert not any(f.external_source for f in all_files) +class TestAssistantDeletion: + @pytest.mark.django_db() + def test_files_to_delete_when_only_referenced_by_one_resource(self, code_resource): + files_to_delete = list(_get_files_to_delete(code_resource.assistant.team, code_resource.id)) + assert len(files_to_delete) == 2 + assert {f.id for f in files_to_delete} == {f.id for f in code_resource.files.all()} + + @pytest.mark.django_db() + def test_files_not_to_delete_when_referenced_by_multiple_resources(self, code_resource): + all_files = list(code_resource.files.all()) + tool_resource = ToolResources.objects.create(tool_type="file_search", assistant=code_resource.assistant) + tool_resource.files.set([all_files[0]]) + + # only the second file should be deleted + files_to_delete = list(_get_files_to_delete(code_resource.assistant.team, code_resource.id)) + assert len(files_to_delete) == 1 + assert files_to_delete[0].id == all_files[1].id + + files_to_delete = list(_get_files_to_delete(tool_resource.assistant.team, tool_resource.id)) + assert len(files_to_delete) == 0 + + @pytest.mark.django_db() + def test_delete_openai_files_for_resource(self, code_resource): + all_files = list(code_resource.files.all()) + assert all(f.external_id for f in all_files) + assert all(f.external_source for f in all_files) + client = Mock() + delete_openai_files_for_resource(client, code_resource.assistant.team, code_resource) + + assert client.files.delete.call_count == 2 + all_files = list(code_resource.files.all()) + assert not any(f.external_id for f in all_files) + assert not any(f.external_source for f in all_files) From 70a78bf877dbdec14f5568c872861a18b296bc02 Mon Sep 17 00:00:00 2001 From: Steph Herbers Date: Wed, 18 Dec 2024 15:37:59 -0500 Subject: [PATCH 02/10] modify archive for assistants such that it checks for references for all assitatns before archiving and deleteling those references as well --- apps/assistants/models.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/apps/assistants/models.py b/apps/assistants/models.py index 391b61bc1..d3209c74e 100644 --- a/apps/assistants/models.py +++ b/apps/assistants/models.py @@ -176,20 +176,23 @@ def get_custom_action_operations(self) -> models.QuerySet: def archive(self): from apps.assistants.tasks import delete_openai_assistant_task - # don't archive assistant if it's still referenced by an active experiment or pipeline - if self.get_related_experiments_queryset().exists(): + if self.get_related_experiments_queryset().exists() or self.get_related_pipeline_node_queryset().exists(): return False - - if self.get_related_pipeline_node_queryset().exists(): - return False - - super().archive() if self.is_working_version: - # TODO: should this delete the assistant from OpenAI? + for ( + version + ) in self.versions.all(): # first perform all checks so assistants are not archived prior to return False + if ( + version.get_related_experiments_queryset().exists() + or version.get_related_pipeline_node_queryset().exists() + ): + return False + for version in self.versions.all(): + delete_openai_assistant_task.delay(version.id) self.versions.update(is_archived=True, audit_action=AuditAction.AUDIT) - else: - delete_openai_assistant_task.delay(self.id) + super().archive() + delete_openai_assistant_task.delay(self.id) return True def get_related_experiments_queryset(self): From 35ce4f08613e5c2f14f12af41c9e056747119ffc Mon Sep 17 00:00:00 2001 From: Steph Herbers Date: Wed, 18 Dec 2024 15:39:39 -0500 Subject: [PATCH 03/10] add tests --- apps/assistants/tests/test_delete.py | 54 +++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/apps/assistants/tests/test_delete.py b/apps/assistants/tests/test_delete.py index 7e43b7935..52cb34c82 100644 --- a/apps/assistants/tests/test_delete.py +++ b/apps/assistants/tests/test_delete.py @@ -1,12 +1,14 @@ import uuid -from unittest.mock import Mock +from unittest.mock import Mock, patch import pytest from apps.assistants.models import ToolResources from apps.assistants.sync import _get_files_to_delete, delete_openai_files_for_resource from apps.utils.factories.assistants import OpenAiAssistantFactory +from apps.utils.factories.experiment import ExperimentFactory from apps.utils.factories.files import FileFactory +from apps.utils.factories.pipelines import PipelineFactory @pytest.fixture() @@ -27,14 +29,13 @@ def code_resource(assistant): return tool_resource +@pytest.mark.django_db() class TestAssistantDeletion: - @pytest.mark.django_db() def test_files_to_delete_when_only_referenced_by_one_resource(self, code_resource): files_to_delete = list(_get_files_to_delete(code_resource.assistant.team, code_resource.id)) assert len(files_to_delete) == 2 assert {f.id for f in files_to_delete} == {f.id for f in code_resource.files.all()} - @pytest.mark.django_db() def test_files_not_to_delete_when_referenced_by_multiple_resources(self, code_resource): all_files = list(code_resource.files.all()) tool_resource = ToolResources.objects.create(tool_type="file_search", assistant=code_resource.assistant) @@ -48,7 +49,6 @@ def test_files_not_to_delete_when_referenced_by_multiple_resources(self, code_re files_to_delete = list(_get_files_to_delete(tool_resource.assistant.team, tool_resource.id)) assert len(files_to_delete) == 0 - @pytest.mark.django_db() def test_delete_openai_files_for_resource(self, code_resource): all_files = list(code_resource.files.all()) assert all(f.external_id for f in all_files) @@ -60,3 +60,49 @@ def test_delete_openai_files_for_resource(self, code_resource): all_files = list(code_resource.files.all()) assert not any(f.external_id for f in all_files) assert not any(f.external_source for f in all_files) + + +# assistant.refresh_from_db() + + +@pytest.mark.django_db() +class TestAssistantArchival: + def test_archive_assistant(self): + assistant = OpenAiAssistantFactory() + assert assistant.is_archived is False + assistant.archive() + assert assistant.is_archived is True + + def test_archive_assistant_with_still_exisiting_experiment_and_pipeline(self): + experiment = ExperimentFactory() + assistant = OpenAiAssistantFactory() + experiment.pipeline = PipelineFactory(team=experiment.team) + experiment.assistant = assistant + experiment.save() + + assistant.archive() + assert assistant.is_archived is False # archiving failed + + experiment.archive() + assistant.archive() + assert assistant.is_archived is True # archiving successful + + @patch("apps.assistants.sync.push_assistant_to_openai", Mock()) + def test_archive_versioned_assistant_with_still_exisiting_experiment_and_pipeline(self): + assistant = OpenAiAssistantFactory() + v2_assistant = assistant.create_new_version() + experiment = ExperimentFactory() + experiment.pipeline = PipelineFactory(team=experiment.team) + experiment.assistant = v2_assistant + experiment.save() + + assistant.archive() + assert assistant.is_archived is False # archiving failed + assert v2_assistant.is_archived is False + + experiment.archive() + assistant.archive() + v2_assistant.refresh_from_db() + + assert assistant.is_archived is True # archiving successful + assert v2_assistant.is_archived is True From b5404ef02781e8626ba02b21f5f03dc172196159 Mon Sep 17 00:00:00 2001 From: Steph Herbers Date: Thu, 19 Dec 2024 11:05:34 -0500 Subject: [PATCH 04/10] modify the reference local objects to include those of the versioned assistants when the referenced assistant is going to be archived --- apps/assistants/models.py | 15 ++++++++++++--- apps/assistants/views.py | 15 +++++++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/apps/assistants/models.py b/apps/assistants/models.py index d3209c74e..87f0c0941 100644 --- a/apps/assistants/models.py +++ b/apps/assistants/models.py @@ -9,7 +9,7 @@ from field_audit.models import AuditAction, AuditingManager from apps.chat.agent.tools import get_assistant_tools -from apps.experiments.models import VersionsMixin, VersionsObjectManagerMixin +from apps.experiments.models import Experiment, VersionsMixin, VersionsObjectManagerMixin from apps.experiments.versioning import VersionField from apps.teams.models import BaseTeamModel from apps.utils.models import BaseModel @@ -195,12 +195,21 @@ def archive(self): delete_openai_assistant_task.delay(self.id) return True - def get_related_experiments_queryset(self): + def get_related_experiments_queryset(self, query=None): + if query: + return Experiment.objects.filter(assistant_id__in=query, is_archived=False) + return self.experiment_set.filter(is_archived=False) - def get_related_pipeline_node_queryset(self): + def get_related_pipeline_node_queryset(self, query=None): from apps.pipelines.models import Node + if query: + return Node.objects.filter(type="AssistantNode").filter( + params__assistant_id__in=query, + pipeline__is_archived=False, + ) + return Node.objects.filter(type="AssistantNode").filter( params__assistant_id=str(self.id), pipeline__is_archived=False, diff --git a/apps/assistants/views.py b/apps/assistants/views.py index 82d5a0d03..5a95fc862 100644 --- a/apps/assistants/views.py +++ b/apps/assistants/views.py @@ -1,6 +1,7 @@ from django.contrib import messages from django.contrib.auth.mixins import PermissionRequiredMixin from django.db import transaction +from django.db.models import Q from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import get_object_or_404, render from django.template.loader import render_to_string @@ -212,13 +213,23 @@ def delete(self, request, team_slug: str, pk: int): messages.success(request, "Assistant Archived") return HttpResponse() else: + version_query = None + if assistant.is_working_version: + version_query = list( + map( + str, + OpenAiAssistant.objects.filter( + Q(id=assistant.id) | Q(working_version__id=assistant.id) + ).values_list("id", flat=True), + ) + ) experiments = [ Chip(label=experiment.name, url=experiment.get_absolute_url()) - for experiment in assistant.get_related_experiments_queryset() + for experiment in assistant.get_related_experiments_queryset(query=version_query) ] pipeline_nodes = [ Chip(label=node.pipeline.name, url=node.pipeline.get_absolute_url()) - for node in assistant.get_related_pipeline_node_queryset().select_related("pipeline") + for node in assistant.get_related_pipeline_node_queryset(query=version_query).select_related("pipeline") ] response = render_to_string( "assistants/partials/referenced_objects.html", From 527e24e244d3e46613f723e4758de73088eedea4 Mon Sep 17 00:00:00 2001 From: Steph Herbers Date: Thu, 19 Dec 2024 11:08:37 -0500 Subject: [PATCH 05/10] add tests for queryset functions --- apps/assistants/tests/test_delete.py | 71 +++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 6 deletions(-) diff --git a/apps/assistants/tests/test_delete.py b/apps/assistants/tests/test_delete.py index 52cb34c82..ce85a48b0 100644 --- a/apps/assistants/tests/test_delete.py +++ b/apps/assistants/tests/test_delete.py @@ -2,13 +2,14 @@ from unittest.mock import Mock, patch import pytest +from django.db.models import Q -from apps.assistants.models import ToolResources +from apps.assistants.models import OpenAiAssistant, ToolResources from apps.assistants.sync import _get_files_to_delete, delete_openai_files_for_resource from apps.utils.factories.assistants import OpenAiAssistantFactory from apps.utils.factories.experiment import ExperimentFactory from apps.utils.factories.files import FileFactory -from apps.utils.factories.pipelines import PipelineFactory +from apps.utils.factories.pipelines import NodeFactory, PipelineFactory @pytest.fixture() @@ -73,10 +74,9 @@ def test_archive_assistant(self): assistant.archive() assert assistant.is_archived is True - def test_archive_assistant_with_still_exisiting_experiment_and_pipeline(self): + def test_archive_assistant_with_still_exisiting_experiment(self): experiment = ExperimentFactory() assistant = OpenAiAssistantFactory() - experiment.pipeline = PipelineFactory(team=experiment.team) experiment.assistant = assistant experiment.save() @@ -91,8 +91,8 @@ def test_archive_assistant_with_still_exisiting_experiment_and_pipeline(self): def test_archive_versioned_assistant_with_still_exisiting_experiment_and_pipeline(self): assistant = OpenAiAssistantFactory() v2_assistant = assistant.create_new_version() - experiment = ExperimentFactory() - experiment.pipeline = PipelineFactory(team=experiment.team) + pipeline = PipelineFactory() + experiment = ExperimentFactory(pipeline=pipeline) experiment.assistant = v2_assistant experiment.save() @@ -106,3 +106,62 @@ def test_archive_versioned_assistant_with_still_exisiting_experiment_and_pipelin assert assistant.is_archived is True # archiving successful assert v2_assistant.is_archived is True + + @patch("apps.assistants.sync.push_assistant_to_openai", Mock()) + def test_get_related_pipeline_node_queryset_with_versions(self): + assistant = OpenAiAssistantFactory() + v2_assistant = assistant.create_new_version() + pipeline = PipelineFactory() + NodeFactory(type="AssistantNode", pipeline=pipeline, params={"assistant_id": str(assistant.id)}) + exp = ExperimentFactory(pipeline=pipeline) + exp.assistant = assistant + exp.save() + v2_exp = exp.create_new_version() + NodeFactory(type="AssistantNode", pipeline=v2_exp.pipeline, params={"assistant_id": str(v2_assistant.id)}) + NodeFactory(type="AssistantNode", pipeline=v2_exp.pipeline, params={"assistant_id": str(v2_assistant.id)}) + v2_exp.assistant = v2_assistant + v2_exp.save() + assistant.refresh_from_db() + v2_assistant.refresh_from_db() + + version_query = list( + map( + str, + OpenAiAssistant.objects.filter(Q(id=assistant.id) | Q(working_version__id=assistant.id)).values_list( + "id", flat=True + ), + ) + ) + + assert len(assistant.get_related_pipeline_node_queryset()) == 1 + assert len(v2_assistant.get_related_pipeline_node_queryset()) == 2 + assert len(assistant.get_related_pipeline_node_queryset(query=version_query)) == 3 + + @patch("apps.assistants.sync.push_assistant_to_openai", Mock()) + def test_get_related_experiments_queryset_with_versions(self): + assistant = OpenAiAssistantFactory() + v2_assistant = assistant.create_new_version() + exp = ExperimentFactory() + exp.assistant = assistant + exp.save() + exp2 = ExperimentFactory() + exp2.assistant = assistant + exp2.save() + v2_exp = exp.create_new_version() + v2_exp.assistant = v2_assistant + v2_exp.save() + assistant.refresh_from_db() + v2_assistant.refresh_from_db() + + version_query = list( + map( + str, + OpenAiAssistant.objects.filter(Q(id=assistant.id) | Q(working_version__id=assistant.id)).values_list( + "id", flat=True + ), + ) + ) + + assert len(assistant.get_related_experiments_queryset()) == 2 + assert len(v2_assistant.get_related_experiments_queryset()) == 1 + assert len(assistant.get_related_experiments_queryset(query=version_query)) == 3 From 63b5e3c8b029d76e6eeae7ed80772bac88069948 Mon Sep 17 00:00:00 2001 From: Steph Herbers Date: Tue, 14 Jan 2025 13:11:14 -0500 Subject: [PATCH 06/10] add AND to filter to to check if the assistant is also working_version OR defualt version to display, else we don't need to block --- apps/assistants/views.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/assistants/views.py b/apps/assistants/views.py index 0007c1a34..0030c649d 100644 --- a/apps/assistants/views.py +++ b/apps/assistants/views.py @@ -219,7 +219,9 @@ def delete(self, request, team_slug: str, pk: int): map( str, OpenAiAssistant.objects.filter( - Q(id=assistant.id) | Q(working_version__id=assistant.id) + Q(id=assistant.id) + | Q(working_version__id=assistant.id) + & (Q(is_working_version=True) | Q(is_default_version=True)) ).values_list("id", flat=True), ) ) From ea2fac1333268b93f712f8ea17f07ccbb28ff154 Mon Sep 17 00:00:00 2001 From: Steph Herbers Date: Tue, 14 Jan 2025 13:11:57 -0500 Subject: [PATCH 07/10] rename param and add typing for get related queryset functions for clarity --- apps/assistants/models.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/assistants/models.py b/apps/assistants/models.py index 6e366c49f..be929cb5d 100644 --- a/apps/assistants/models.py +++ b/apps/assistants/models.py @@ -190,18 +190,18 @@ def archive(self): delete_openai_assistant_task.delay(self.id) return True - def get_related_experiments_queryset(self, query=None): - if query: - return Experiment.objects.filter(assistant_id__in=query, is_archived=False) + def get_related_experiments_queryset(self, assistant_ids: list = None): + if assistant_ids: + return Experiment.objects.filter(assistant_id__in=assistant_ids, is_archived=False) return self.experiment_set.filter(is_archived=False) - def get_related_pipeline_node_queryset(self, query=None): + def get_related_pipeline_node_queryset(self, assistant_ids: list = None): from apps.pipelines.models import Node - if query: + if assistant_ids: return Node.objects.filter(type="AssistantNode").filter( - params__assistant_id__in=query, + params__assistant_id__in=assistant_ids, pipeline__is_archived=False, ) From 5fb1bade4c3363a34b2da6b0c698d12c061e2bcf Mon Sep 17 00:00:00 2001 From: Steph Herbers Date: Tue, 14 Jan 2025 13:37:52 -0500 Subject: [PATCH 08/10] fix logic- checks for working version and default version on the experiments and pipelines --- apps/assistants/models.py | 11 +++++++++-- apps/assistants/views.py | 4 +--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/apps/assistants/models.py b/apps/assistants/models.py index be929cb5d..e2150507a 100644 --- a/apps/assistants/models.py +++ b/apps/assistants/models.py @@ -3,6 +3,7 @@ from django.contrib.postgres.fields import ArrayField from django.core.validators import MaxValueValidator, MinValueValidator from django.db import models, transaction +from django.db.models import Q from django.urls import reverse from field_audit import audit_fields from field_audit.models import AuditAction, AuditingManager @@ -192,20 +193,26 @@ def archive(self): def get_related_experiments_queryset(self, assistant_ids: list = None): if assistant_ids: - return Experiment.objects.filter(assistant_id__in=assistant_ids, is_archived=False) + return Experiment.objects.filter( + Q(working_version_id=None) | Q(is_default_version=True), + assistant_id__in=assistant_ids, + is_archived=False, + ) - return self.experiment_set.filter(is_archived=False) + return self.experiment_set.filter(Q(working_version_id=None) | Q(is_default_version=True), is_archived=False) def get_related_pipeline_node_queryset(self, assistant_ids: list = None): from apps.pipelines.models import Node if assistant_ids: return Node.objects.filter(type="AssistantNode").filter( + Q(pipeline__working_version_id=None), params__assistant_id__in=assistant_ids, pipeline__is_archived=False, ) return Node.objects.filter(type="AssistantNode").filter( + Q(pipeline__working_version_id=None), params__assistant_id=str(self.id), pipeline__is_archived=False, ) diff --git a/apps/assistants/views.py b/apps/assistants/views.py index 0030c649d..0007c1a34 100644 --- a/apps/assistants/views.py +++ b/apps/assistants/views.py @@ -219,9 +219,7 @@ def delete(self, request, team_slug: str, pk: int): map( str, OpenAiAssistant.objects.filter( - Q(id=assistant.id) - | Q(working_version__id=assistant.id) - & (Q(is_working_version=True) | Q(is_default_version=True)) + Q(id=assistant.id) | Q(working_version__id=assistant.id) ).values_list("id", flat=True), ) ) From bff8ccf0cdd11d907041df3b0c38d199791c6de3 Mon Sep 17 00:00:00 2001 From: Steph Herbers Date: Tue, 14 Jan 2025 14:40:04 -0500 Subject: [PATCH 09/10] tests --- apps/assistants/tests/test_delete.py | 101 +++++++-------------------- 1 file changed, 27 insertions(+), 74 deletions(-) diff --git a/apps/assistants/tests/test_delete.py b/apps/assistants/tests/test_delete.py index ce85a48b0..91e93d7c6 100644 --- a/apps/assistants/tests/test_delete.py +++ b/apps/assistants/tests/test_delete.py @@ -2,14 +2,12 @@ from unittest.mock import Mock, patch import pytest -from django.db.models import Q -from apps.assistants.models import OpenAiAssistant, ToolResources +from apps.assistants.models import ToolResources from apps.assistants.sync import _get_files_to_delete, delete_openai_files_for_resource from apps.utils.factories.assistants import OpenAiAssistantFactory from apps.utils.factories.experiment import ExperimentFactory from apps.utils.factories.files import FileFactory -from apps.utils.factories.pipelines import NodeFactory, PipelineFactory @pytest.fixture() @@ -74,94 +72,49 @@ def test_archive_assistant(self): assistant.archive() assert assistant.is_archived is True - def test_archive_assistant_with_still_exisiting_experiment(self): + @patch("apps.assistants.sync.push_assistant_to_openai", Mock()) + def test_archive_assistant_succeeds_with_released_experiment_experiment(self): experiment = ExperimentFactory() - assistant = OpenAiAssistantFactory() - experiment.assistant = assistant + exp_v2 = experiment.create_new_version() + experiment.is_default = True experiment.save() - + assistant = OpenAiAssistantFactory() + exp_v2 = experiment.create_new_version() + exp_v2.assistant = assistant + exp_v2.save() + assert exp_v2.is_default_version is False + assert exp_v2.is_working_version is False assistant.archive() - assert assistant.is_archived is False # archiving failed + assistant.refresh_from_db() + + assert assistant.is_archived is True # archiving succeeded - experiment.archive() + @patch("apps.assistants.sync.push_assistant_to_openai", Mock()) + def test_asistant_archive_blocked_by_working_related_experiment(self): + assistant = OpenAiAssistantFactory() + experiment = ExperimentFactory(assistant=assistant) + experiment.save() + + assert experiment.is_working_version is True assistant.archive() - assert assistant.is_archived is True # archiving successful + assistant.refresh_from_db() + assert assistant.is_archived is False # archiving blocked @patch("apps.assistants.sync.push_assistant_to_openai", Mock()) - def test_archive_versioned_assistant_with_still_exisiting_experiment_and_pipeline(self): + def test_asistant_archive_blocked_by_published_related_experiment(self): assistant = OpenAiAssistantFactory() v2_assistant = assistant.create_new_version() - pipeline = PipelineFactory() - experiment = ExperimentFactory(pipeline=pipeline) - experiment.assistant = v2_assistant + experiment = ExperimentFactory(assistant=v2_assistant) + experiment.is_default = True experiment.save() assistant.archive() assert assistant.is_archived is False # archiving failed assert v2_assistant.is_archived is False - experiment.archive() + experiment.archive() # first archive related experiment through v2_assistant assistant.archive() v2_assistant.refresh_from_db() assert assistant.is_archived is True # archiving successful assert v2_assistant.is_archived is True - - @patch("apps.assistants.sync.push_assistant_to_openai", Mock()) - def test_get_related_pipeline_node_queryset_with_versions(self): - assistant = OpenAiAssistantFactory() - v2_assistant = assistant.create_new_version() - pipeline = PipelineFactory() - NodeFactory(type="AssistantNode", pipeline=pipeline, params={"assistant_id": str(assistant.id)}) - exp = ExperimentFactory(pipeline=pipeline) - exp.assistant = assistant - exp.save() - v2_exp = exp.create_new_version() - NodeFactory(type="AssistantNode", pipeline=v2_exp.pipeline, params={"assistant_id": str(v2_assistant.id)}) - NodeFactory(type="AssistantNode", pipeline=v2_exp.pipeline, params={"assistant_id": str(v2_assistant.id)}) - v2_exp.assistant = v2_assistant - v2_exp.save() - assistant.refresh_from_db() - v2_assistant.refresh_from_db() - - version_query = list( - map( - str, - OpenAiAssistant.objects.filter(Q(id=assistant.id) | Q(working_version__id=assistant.id)).values_list( - "id", flat=True - ), - ) - ) - - assert len(assistant.get_related_pipeline_node_queryset()) == 1 - assert len(v2_assistant.get_related_pipeline_node_queryset()) == 2 - assert len(assistant.get_related_pipeline_node_queryset(query=version_query)) == 3 - - @patch("apps.assistants.sync.push_assistant_to_openai", Mock()) - def test_get_related_experiments_queryset_with_versions(self): - assistant = OpenAiAssistantFactory() - v2_assistant = assistant.create_new_version() - exp = ExperimentFactory() - exp.assistant = assistant - exp.save() - exp2 = ExperimentFactory() - exp2.assistant = assistant - exp2.save() - v2_exp = exp.create_new_version() - v2_exp.assistant = v2_assistant - v2_exp.save() - assistant.refresh_from_db() - v2_assistant.refresh_from_db() - - version_query = list( - map( - str, - OpenAiAssistant.objects.filter(Q(id=assistant.id) | Q(working_version__id=assistant.id)).values_list( - "id", flat=True - ), - ) - ) - - assert len(assistant.get_related_experiments_queryset()) == 2 - assert len(v2_assistant.get_related_experiments_queryset()) == 1 - assert len(assistant.get_related_experiments_queryset(query=version_query)) == 3 From 03020f88088b3fff8e4cab23c432b7a44a22c04c Mon Sep 17 00:00:00 2001 From: Steph Herbers Date: Fri, 17 Jan 2025 16:22:30 -0500 Subject: [PATCH 10/10] add pipeline tests --- apps/assistants/tests/test_delete.py | 47 ++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/apps/assistants/tests/test_delete.py b/apps/assistants/tests/test_delete.py index 91e93d7c6..1ac176567 100644 --- a/apps/assistants/tests/test_delete.py +++ b/apps/assistants/tests/test_delete.py @@ -8,6 +8,7 @@ from apps.utils.factories.assistants import OpenAiAssistantFactory from apps.utils.factories.experiment import ExperimentFactory from apps.utils.factories.files import FileFactory +from apps.utils.factories.pipelines import NodeFactory, PipelineFactory @pytest.fixture() @@ -73,13 +74,12 @@ def test_archive_assistant(self): assert assistant.is_archived is True @patch("apps.assistants.sync.push_assistant_to_openai", Mock()) - def test_archive_assistant_succeeds_with_released_experiment_experiment(self): - experiment = ExperimentFactory() - exp_v2 = experiment.create_new_version() - experiment.is_default = True - experiment.save() + def test_archive_assistant_succeeds_with_released_related_experiment(self): + exp_v1 = ExperimentFactory() + exp_v2 = exp_v1.create_new_version() + exp_v1.save() assistant = OpenAiAssistantFactory() - exp_v2 = experiment.create_new_version() + exp_v2 = exp_v1.create_new_version() exp_v2.assistant = assistant exp_v2.save() assert exp_v2.is_default_version is False @@ -118,3 +118,38 @@ def test_asistant_archive_blocked_by_published_related_experiment(self): assert assistant.is_archived is True # archiving successful assert v2_assistant.is_archived is True + + @patch("apps.assistants.sync.push_assistant_to_openai", Mock()) + def test_archive_assistant_succeeds_with_released_related_pipeline(self): + pipeline = PipelineFactory() + exp_v1 = ExperimentFactory(pipeline=pipeline) + exp_v2 = exp_v1.create_new_version() + assistant = OpenAiAssistantFactory() + NodeFactory(pipeline=exp_v2.pipeline, type="AssistantNode", params={"assistant_id": assistant.id}) + exp_v2.is_default_version = False + exp_v2.save() + + assert exp_v2.pipeline.is_working_version is False + assistant.archive() + assistant.refresh_from_db() + + assert assistant.is_archived is True # archiving successful + + @patch("apps.assistants.sync.push_assistant_to_openai", Mock()) + def test_archive_assistant_fails_with_working_related_pipeline(self): + pipeline = PipelineFactory() + assistant = OpenAiAssistantFactory() + NodeFactory(pipeline=pipeline, type="AssistantNode", params={"assistant_id": assistant.id}) + exp_v1 = ExperimentFactory(pipeline=pipeline) + exp_v1.save() + + assert pipeline.is_working_version is True + assistant.archive() + assert assistant.is_archived is False # archiving failed + + exp_v1.archive() + pipeline.archive() + assistant.archive() + + assert pipeline.is_archived is True + assert assistant.is_archived is True # archiving successful