@@ -750,3 +750,186 @@ def test_add_items_to_collection_success(client, default_collection, example_ite
750
750
child_refcodes = [item ["refcode" ] for item in collection_data ["child_items" ]]
751
751
752
752
assert all (refcode in child_refcodes for refcode in refcodes )
753
+
754
+
755
+ @pytest .mark .dependency ()
756
+ def test_remove_items_from_collection_success (
757
+ client , database , default_sample_dict , default_collection
758
+ ):
759
+ """Test successfully removing items from a collection."""
760
+ sample_1_dict = default_sample_dict .copy ()
761
+ sample_1_dict ["item_id" ] = "test_sample_remove_1"
762
+ sample_1_dict ["collections" ] = []
763
+
764
+ sample_2_dict = default_sample_dict .copy ()
765
+ sample_2_dict ["item_id" ] = "test_sample_remove_2"
766
+ sample_2_dict ["collections" ] = []
767
+
768
+ for sample_dict in [sample_1_dict , sample_2_dict ]:
769
+ response = client .post ("/new-sample/" , json = sample_dict )
770
+ assert response .status_code == 201
771
+
772
+ collection_dict = default_collection .dict ()
773
+ collection_dict ["collection_id" ] = "test_collection_remove"
774
+ response = client .put ("/collections" , json = {"data" : collection_dict })
775
+ assert response .status_code == 201
776
+
777
+ collection_from_db = database .collections .find_one ({"collection_id" : "test_collection_remove" })
778
+ collection_object_id = collection_from_db ["_id" ]
779
+
780
+ item_ids = [sample_1_dict ["item_id" ], sample_2_dict ["item_id" ]]
781
+
782
+ for item_id in item_ids :
783
+ database .items .update_one (
784
+ {"item_id" : item_id },
785
+ {
786
+ "$push" : {
787
+ "relationships" : {
788
+ "type" : "collections" ,
789
+ "immutable_id" : collection_object_id ,
790
+ }
791
+ }
792
+ },
793
+ )
794
+
795
+ for item_id in item_ids :
796
+ item = database .items .find_one ({"item_id" : item_id })
797
+ assert item is not None
798
+ collection_relationships = [
799
+ rel for rel in item .get ("relationships" , []) if rel .get ("type" ) == "collections"
800
+ ]
801
+ assert len (collection_relationships ) == 1
802
+
803
+ item_1_refcode = client .get (f"/get-item-data/{ sample_1_dict ['item_id' ]} " ).json ["item_data" ][
804
+ "refcode"
805
+ ]
806
+ item_2_refcode = client .get (f"/get-item-data/{ sample_2_dict ['item_id' ]} " ).json ["item_data" ][
807
+ "refcode"
808
+ ]
809
+ refcodes = [item_1_refcode , item_2_refcode ]
810
+
811
+ response = client .delete (
812
+ "/collections/test_collection_remove/items" , json = {"refcodes" : refcodes }
813
+ )
814
+
815
+ assert response .status_code == 200
816
+ data = response .get_json ()
817
+ assert data ["status" ] == "success"
818
+ assert data ["removed_count" ] == 2
819
+
820
+ for item_id in item_ids :
821
+ item = database .items .find_one ({"item_id" : item_id })
822
+ assert item is not None
823
+ collection_relationships = [
824
+ rel for rel in item .get ("relationships" , []) if rel .get ("type" ) == "collections"
825
+ ]
826
+ assert len (collection_relationships ) == 0
827
+
828
+
829
+ @pytest .mark .dependency ()
830
+ def test_remove_items_from_collection_not_found (client ):
831
+ """Test removing items from non-existent collection."""
832
+ response = client .delete (
833
+ "/collections/nonexistent_collection/items" , json = {"refcodes" : ["refcode1" , "refcode2" ]}
834
+ )
835
+
836
+ assert response .status_code == 404
837
+ data = response .get_json ()
838
+ assert data ["error" ] == "Collection not found"
839
+
840
+
841
+ @pytest .mark .dependency ()
842
+ def test_remove_items_from_collection_no_items_provided (client , default_collection ):
843
+ """Test removing with no item IDs provided."""
844
+ collection_dict = default_collection .dict ()
845
+ collection_dict ["collection_id" ] = "test_collection_empty_items"
846
+ response = client .put ("/collections" , json = {"data" : collection_dict })
847
+ assert response .status_code == 201
848
+
849
+ collection_id = collection_dict ["collection_id" ]
850
+ response = client .delete (f"/collections/{ collection_id } /items" , json = {"refcodes" : []})
851
+
852
+ assert response .status_code == 400
853
+ data = response .get_json ()
854
+ assert data ["error" ] == "No refcodes provided"
855
+
856
+
857
+ @pytest .mark .dependency ()
858
+ def test_remove_items_from_collection_no_matching_items (client , default_collection ):
859
+ """Test removing items that don't exist."""
860
+ collection_dict = default_collection .dict ()
861
+ collection_dict ["collection_id" ] = "test_collection_no_match"
862
+ response = client .put ("/collections" , json = {"data" : collection_dict })
863
+ assert response .status_code == 201
864
+
865
+ collection_id = collection_dict ["collection_id" ]
866
+ response = client .delete (
867
+ f"/collections/{ collection_id } /items" ,
868
+ json = {"refcodes" : ["nonexistent_refcode_1" , "nonexistent_refcode_2" ]},
869
+ )
870
+
871
+ assert response .status_code == 404
872
+ data = response .get_json ()
873
+ assert data ["status" ] == "error"
874
+ assert data ["message" ] == "No matching items found."
875
+
876
+
877
+ @pytest .mark .dependency ()
878
+ def test_remove_items_from_collection_partial_success (
879
+ client , database , default_sample_dict , default_collection
880
+ ):
881
+ """Test removing items where some exist in collection and some don't."""
882
+ sample_dict = default_sample_dict .copy ()
883
+ sample_dict ["item_id" ] = "test_sample_partial"
884
+ sample_dict ["collections" ] = []
885
+
886
+ response = client .post ("/new-sample/" , json = sample_dict )
887
+ assert response .status_code == 201
888
+
889
+ collection_dict = default_collection .dict ()
890
+ collection_dict ["collection_id" ] = "test_collection_partial"
891
+ response = client .put ("/collections" , json = {"data" : collection_dict })
892
+ assert response .status_code == 201
893
+
894
+ collection_from_db = database .collections .find_one ({"collection_id" : "test_collection_partial" })
895
+ collection_object_id = collection_from_db ["_id" ]
896
+
897
+ item_id = sample_dict ["item_id" ]
898
+
899
+ database .items .update_one (
900
+ {"item_id" : item_id },
901
+ {
902
+ "$push" : {
903
+ "relationships" : {
904
+ "type" : "collections" ,
905
+ "immutable_id" : collection_object_id ,
906
+ }
907
+ }
908
+ },
909
+ )
910
+
911
+ item = database .items .find_one ({"item_id" : item_id })
912
+ collection_relationships = [
913
+ rel for rel in item .get ("relationships" , []) if rel .get ("type" ) == "collections"
914
+ ]
915
+ assert len (collection_relationships ) == 1
916
+
917
+ item_refcode = client .get (f"/get-item-data/{ sample_dict ['item_id' ]} " ).json ["item_data" ][
918
+ "refcode"
919
+ ]
920
+
921
+ response = client .delete (
922
+ "/collections/test_collection_partial/items" ,
923
+ json = {"refcodes" : [item_refcode , "nonexistent_refcode" ]},
924
+ )
925
+
926
+ assert response .status_code == 207
927
+ data = response .get_json ()
928
+ assert data ["status" ] == "partial-success"
929
+ assert "Only 1 items updated" in data ["message" ]
930
+
931
+ item = database .items .find_one ({"item_id" : item_id })
932
+ collection_relationships = [
933
+ rel for rel in item .get ("relationships" , []) if rel .get ("type" ) == "collections"
934
+ ]
935
+ assert len (collection_relationships ) == 0
0 commit comments