@@ -587,7 +587,7 @@ async def test_import_index_with_overwrite_fallback_to_create(
587587 async def test_import_pipeline_with_overwrite_true (
588588 self , pipeline_service : PipelineService , index_pipeline : Pipeline , mock_api : AsyncMock
589589 ) -> None :
590- """Test importing a pipeline with overwrite=True creates a new version via POST endpoint ."""
590+ """Test importing a pipeline with overwrite=True patches latest draft version."""
591591 config = PipelineConfig (
592592 name = "test_pipeline_overwrite" ,
593593 inputs = PipelineInputs (query = ["retriever.query" ]),
@@ -600,39 +600,99 @@ async def test_import_pipeline_with_overwrite_true(
600600 validation_response = Mock (spec = Response )
601601 validation_response .status_code = HTTPStatus .NO_CONTENT .value
602602
603- # Mock successful "create new version" response
603+ # Mock successful versions response, latest version is a draft
604+ versions_response = Mock (status_code = HTTPStatus .OK .value )
605+ versions_response .json .return_value = {
606+ "data" : [{"version_id" : "42abcd" , "is_draft" : True }],
607+ }
608+
609+ # Mock successful overwrite (PATCH) response
604610 overwrite_response = Mock (spec = Response )
605- overwrite_response .status_code = HTTPStatus .CREATED .value
611+ overwrite_response .status_code = HTTPStatus .OK .value
612+
613+ mock_api .post .return_value = validation_response
614+ mock_api .get .return_value = versions_response
615+ mock_api .patch .return_value = overwrite_response
616+
617+ await pipeline_service .import_async (index_pipeline , config )
618+
619+ # validation + GET versions + PATCH draft version
620+ assert mock_api .post .call_count == 1
621+ assert mock_api .get .call_count == 1
622+ assert mock_api .patch .call_count == 1
623+
624+ # Check validation call
625+ validation_call = mock_api .post .call_args_list [0 ]
626+ assert validation_call .kwargs ["endpoint" ] == "pipeline_validations"
627+ assert "query_yaml" in validation_call .kwargs ["json" ]
628+ # When overwrite=True, name should be excluded from validation payload
629+ assert "name" not in validation_call .kwargs ["json" ]
630+
631+ # Check PATCH call
632+ overwrite_call = mock_api .patch .call_args_list [0 ]
633+ assert (
634+ overwrite_call .kwargs ["endpoint" ]
635+ == "pipelines/test_pipeline_overwrite/versions/42abcd"
636+ )
637+ assert "config_yaml" in overwrite_call .kwargs ["json" ]
638+
639+ @pytest .mark .asyncio
640+ async def test_import_pipeline_with_overwrite_true_creates_new_version_when_not_draft (
641+ self , pipeline_service : PipelineService , index_pipeline : Pipeline , mock_api : AsyncMock
642+ ) -> None :
643+ """Test importing a pipeline with overwrite=True creates a new version when latest version is not draft."""
644+ config = PipelineConfig (
645+ name = "test_pipeline_overwrite" ,
646+ inputs = PipelineInputs (query = ["retriever.query" ]),
647+ outputs = PipelineOutputs (documents = "meta_ranker.documents" ),
648+ strict_validation = False ,
649+ overwrite = True ,
650+ )
651+
652+ # Mock successful validation response
653+ validation_response = Mock (spec = Response )
654+ validation_response .status_code = HTTPStatus .NO_CONTENT .value
655+
656+ # Mock versions response, latest version is NOT a draft
657+ versions_response = Mock (status_code = HTTPStatus .OK .value )
658+ versions_response .json .return_value = {
659+ "data" : [{"version_id" : "42abcd" , "is_draft" : False }],
660+ }
661+
662+ # Mock successful "create new version" response
663+ new_version_response = Mock (spec = Response )
664+ new_version_response .status_code = HTTPStatus .CREATED .value
606665
607666 # First POST is validation, second POST is "create new version"
608- mock_api .post .side_effect = [validation_response , overwrite_response ]
667+ mock_api .post .side_effect = [validation_response , new_version_response ]
668+ mock_api .get .return_value = versions_response
609669
610670 await pipeline_service .import_async (index_pipeline , config )
611671
612- # Should call validation endpoint first, then create-version endpoint
672+ # validation + GET versions + POST versions (new version)
613673 assert mock_api .post .call_count == 2
614- # No GET/PATCH/PUT calls in the overwrite path anymore
615- assert mock_api .get .call_count == 0
674+ assert mock_api .get .call_count == 1
616675 assert mock_api .patch .call_count == 0
617- assert mock_api .put .call_count == 0
618676
619677 # Check validation call
620678 validation_call = mock_api .post .call_args_list [0 ]
621679 assert validation_call .kwargs ["endpoint" ] == "pipeline_validations"
622680 assert "query_yaml" in validation_call .kwargs ["json" ]
623- # When overwrite=True, name should be excluded from validation payload
624681 assert "name" not in validation_call .kwargs ["json" ]
625682
626- # Check create-version call
627- overwrite_call = mock_api .post .call_args_list [1 ]
628- assert overwrite_call .kwargs ["endpoint" ] == "pipelines/test_pipeline_overwrite/versions"
629- assert "config_yaml" in overwrite_call .kwargs ["json" ]
683+ # Check create-version POST call
684+ create_version_call = mock_api .post .call_args_list [1 ]
685+ assert (
686+ create_version_call .kwargs ["endpoint" ]
687+ == "pipelines/test_pipeline_overwrite/versions"
688+ )
689+ assert "config_yaml" in create_version_call .kwargs ["json" ]
630690
631691 @pytest .mark .asyncio
632692 async def test_import_pipeline_with_overwrite_fallback_to_create (
633693 self , pipeline_service : PipelineService , index_pipeline : Pipeline , mock_api : AsyncMock
634694 ) -> None :
635- """Test importing a pipeline with overwrite=True that falls back to create when version creation fails ."""
695+ """Test importing a pipeline with overwrite=True that falls back to create when resource doesn't exist ."""
636696
637697 config = PipelineConfig (
638698 name = "test_pipeline_fallback" ,
@@ -646,41 +706,35 @@ async def test_import_pipeline_with_overwrite_fallback_to_create(
646706 validation_response = Mock (spec = Response )
647707 validation_response .status_code = HTTPStatus .NO_CONTENT .value
648708
649- # Mock non-201 response for POST /pipelines/{name}/versions (version creation fails )
650- version_fail_response = Mock (spec = Response )
651- version_fail_response .status_code = HTTPStatus .BAD_REQUEST .value
709+ # Mock 404 response for GET (resource not found )
710+ not_found_response = Mock (spec = Response )
711+ not_found_response .status_code = HTTPStatus .NOT_FOUND .value
652712
653- # Mock successful creation response for POST /pipelines
713+ # Mock successful creation response
654714 create_response = Mock (spec = Response )
655715 create_response .status_code = HTTPStatus .CREATED .value
656716
657- # POST calls: validation, create-version (fails), create-pipeline (fallback)
658- mock_api .post . side_effect = [ validation_response , version_fail_response , create_response ]
717+ mock_api . post . side_effect = [ validation_response , create_response ]
718+ mock_api .get . return_value = not_found_response
659719
660720 await pipeline_service .import_async (index_pipeline , config )
661721
662- # Should call validation endpoint, then POST to create new version (fails),
663- # then POST to create the pipeline
664- assert mock_api .post .call_count == 3
665- # No GET anymore; overwrite logic doesn't fetch versions
666- assert mock_api .get .call_count == 0
667- assert mock_api .patch .call_count == 0
668- assert mock_api .put .call_count == 0
722+ # validation + GET (404) + POST create
723+ assert mock_api .post .call_count == 2
724+ assert mock_api .get .call_count == 1
669725
670726 # Check validation call
671727 validation_call = mock_api .post .call_args_list [0 ]
672728 assert validation_call .kwargs ["endpoint" ] == "pipeline_validations"
673729 assert "query_yaml" in validation_call .kwargs ["json" ]
674- # When overwrite=True, name should be excluded from validation payload
675730 assert "name" not in validation_call .kwargs ["json" ]
676731
677- # Check attempted version creation call
678- version_call = mock_api .post .call_args_list [1 ]
679- assert version_call .kwargs ["endpoint" ] == "pipelines/test_pipeline_fallback/versions"
680- assert "config_yaml" in version_call .kwargs ["json" ]
732+ # Check GET versions attempt
733+ get_call = mock_api .get .call_args_list [0 ]
734+ assert get_call .kwargs ["endpoint" ] == "pipelines/test_pipeline_fallback/versions"
681735
682- # Check fallback create-pipeline call
683- create_call = mock_api .post .call_args_list [2 ]
736+ # Check fallback POST call
737+ create_call = mock_api .post .call_args_list [1 ]
684738 assert create_call .kwargs ["endpoint" ] == "pipelines"
685739 assert create_call .kwargs ["json" ]["name" ] == "test_pipeline_fallback"
686740 assert "query_yaml" in create_call .kwargs ["json" ]
0 commit comments