@@ -714,6 +714,120 @@ def test_create_segments_with_description_condition(project, client): # type: i
714
714
assert segment_condition_description_value == "test-description"
715
715
716
716
717
+ def test_update_segment_add_new_root_rule (
718
+ project : Project , admin_client_new : APIClient , segment : Segment
719
+ ) -> None :
720
+ # Given
721
+ url = reverse (
722
+ "api-v1:projects:project-segments-detail" , args = [project .id , segment .id ]
723
+ )
724
+ data = {
725
+ "name" : segment .name ,
726
+ "project" : project .id ,
727
+ "rules" : [
728
+ {
729
+ "type" : "ANY" ,
730
+ "rules" : [
731
+ {
732
+ "type" : "ALL" ,
733
+ "rules" : [],
734
+ "conditions" : [
735
+ {"property" : "foo" , "operator" : "EQUAL" , "value" : "bar" }
736
+ ],
737
+ }
738
+ ],
739
+ }
740
+ ],
741
+ }
742
+
743
+ # When
744
+ response = admin_client_new .put (
745
+ url , data = json .dumps (data ), content_type = "application/json"
746
+ )
747
+ # Then
748
+ assert response .status_code == status .HTTP_200_OK
749
+ assert response .json ()["rules" ][0 ]["type" ] == "ANY"
750
+ assert response .json ()["rules" ][0 ]["rules" ][0 ]["type" ] == "ALL"
751
+ assert response .json ()["rules" ][0 ]["rules" ][0 ]["conditions" ][0 ]["property" ] == "foo"
752
+ assert (
753
+ response .json ()["rules" ][0 ]["rules" ][0 ]["conditions" ][0 ]["operator" ] == "EQUAL"
754
+ )
755
+ assert response .json ()["rules" ][0 ]["rules" ][0 ]["conditions" ][0 ]["value" ] == "bar"
756
+
757
+
758
+ def test_update_segment_add_new_rule (
759
+ project : Project ,
760
+ admin_client_new : APIClient ,
761
+ segment : Segment ,
762
+ segment_rule : SegmentRule ,
763
+ settings : SettingsWrapper ,
764
+ ) -> None :
765
+ # Given
766
+ settings .SEGMENT_RULES_CONDITIONS_EXPLICIT_ORDERING_ENABLED = True
767
+
768
+ url = reverse (
769
+ "api-v1:projects:project-segments-detail" , args = [project .id , segment .id ]
770
+ )
771
+ nested_rule = SegmentRule .objects .create (
772
+ rule = segment_rule , type = SegmentRule .ANY_RULE
773
+ )
774
+ existing_condition = Condition .objects .create (
775
+ rule = nested_rule , property = "foo" , operator = EQUAL , value = "bar"
776
+ )
777
+
778
+ data = {
779
+ "name" : segment .name ,
780
+ "project" : project .id ,
781
+ "rules" : [
782
+ {
783
+ "id" : segment_rule .id ,
784
+ "type" : segment_rule .type ,
785
+ "rules" : [
786
+ {
787
+ "id" : nested_rule .id ,
788
+ "type" : nested_rule .type ,
789
+ "rules" : [],
790
+ "conditions" : [
791
+ {
792
+ "id" : existing_condition .id ,
793
+ "property" : existing_condition .property ,
794
+ "operator" : existing_condition .operator ,
795
+ "value" : existing_condition .value ,
796
+ },
797
+ ],
798
+ },
799
+ { # New rule
800
+ "type" : SegmentRule .ANY_RULE ,
801
+ "rules" : [],
802
+ "conditions" : [
803
+ {
804
+ "property" : "foo" ,
805
+ "operator" : EQUAL ,
806
+ "value" : "bar" ,
807
+ },
808
+ ],
809
+ },
810
+ ],
811
+ "conditions" : [],
812
+ }
813
+ ],
814
+ }
815
+
816
+ # When
817
+ response = admin_client_new .put (
818
+ url , data = json .dumps (data ), content_type = "application/json"
819
+ )
820
+
821
+ # Then
822
+ assert response .status_code == status .HTTP_200_OK
823
+ assert response .json ()["rules" ][0 ]["rules" ][1 ]["type" ] == SegmentRule .ANY_RULE
824
+ assert response .json ()["rules" ][0 ]["rules" ][1 ]["conditions" ][0 ]["property" ] == "foo"
825
+ assert response .json ()["rules" ][0 ]["rules" ][1 ]["conditions" ][0 ]["operator" ] == EQUAL
826
+ assert response .json ()["rules" ][0 ]["rules" ][1 ]["conditions" ][0 ]["value" ] == "bar"
827
+
828
+ assert segment_rule .rules .count () == 2
829
+
830
+
717
831
def test_update_segment_add_new_condition (
718
832
project : Project ,
719
833
admin_client_new : APIClient ,
@@ -784,6 +898,79 @@ def test_update_segment_add_new_condition(
784
898
assert expected_new_condition .value == new_condition_value
785
899
786
900
901
+ def test_update_segment_delete_and_update_existing_condition (
902
+ project : Project ,
903
+ admin_client_new : APIClient ,
904
+ segment : Segment ,
905
+ segment_rule : SegmentRule ,
906
+ settings : SettingsWrapper ,
907
+ ) -> None :
908
+ # Given
909
+ settings .SEGMENT_RULES_CONDITIONS_EXPLICIT_ORDERING_ENABLED = True
910
+
911
+ url = reverse (
912
+ "api-v1:projects:project-segments-detail" , args = [project .id , segment .id ]
913
+ )
914
+ nested_rule = SegmentRule .objects .create (
915
+ rule = segment_rule , type = SegmentRule .ANY_RULE
916
+ )
917
+ existing_condition = Condition .objects .create (
918
+ rule = nested_rule , property = "foo" , operator = EQUAL , value = "bar"
919
+ )
920
+ new_condition = Condition .objects .create (
921
+ rule = nested_rule , property = "foo2" , operator = EQUAL , value = "bar2"
922
+ )
923
+
924
+ new_condition_updated_property = "foo3"
925
+ new_condition_updated_value = "bar3"
926
+ data = {
927
+ "name" : segment .name ,
928
+ "project" : project .id ,
929
+ "rules" : [
930
+ {
931
+ "id" : segment_rule .id ,
932
+ "type" : segment_rule .type ,
933
+ "rules" : [
934
+ {
935
+ "id" : nested_rule .id ,
936
+ "type" : nested_rule .type ,
937
+ "rules" : [],
938
+ "conditions" : [
939
+ {
940
+ "id" : existing_condition .id ,
941
+ "delete" : True ,
942
+ "property" : existing_condition .property ,
943
+ "operator" : existing_condition .operator ,
944
+ "value" : existing_condition .value ,
945
+ },
946
+ {
947
+ "id" : new_condition .id ,
948
+ "property" : new_condition_updated_property ,
949
+ "operator" : new_condition .operator ,
950
+ "value" : new_condition_updated_value ,
951
+ },
952
+ ],
953
+ }
954
+ ],
955
+ "conditions" : [],
956
+ }
957
+ ],
958
+ }
959
+
960
+ # When
961
+ response = admin_client_new .put (
962
+ url , data = json .dumps (data ), content_type = "application/json"
963
+ )
964
+
965
+ # Then
966
+ assert response .status_code == status .HTTP_200_OK
967
+
968
+ assert nested_rule .conditions .count () == 1
969
+ assert (expected_new_condition := nested_rule .conditions .first ())
970
+ assert expected_new_condition .property == new_condition_updated_property
971
+ assert expected_new_condition .value == new_condition_updated_value
972
+
973
+
787
974
def test_can_not_update_system_segment (
788
975
project : Project ,
789
976
admin_client_new : APIClient ,
@@ -1011,7 +1198,6 @@ def test_update_segment_delete_existing_condition( # type: ignore[no-untyped-de
1011
1198
1012
1199
# Then
1013
1200
assert response .status_code == status .HTTP_200_OK
1014
-
1015
1201
assert nested_rule .conditions .count () == 0
1016
1202
1017
1203
@@ -1041,21 +1227,20 @@ def test_update_segment_delete_existing_rule(project, client, segment, segment_r
1041
1227
"type" : nested_rule .type ,
1042
1228
"rules" : [],
1043
1229
"conditions" : [],
1230
+ "delete" : True ,
1044
1231
}
1045
1232
],
1046
1233
"conditions" : [],
1047
- "delete" : True ,
1048
1234
}
1049
1235
],
1050
1236
}
1051
1237
1052
1238
# When
1053
1239
response = client .put (url , data = json .dumps (data ), content_type = "application/json" )
1054
-
1055
1240
# Then
1056
1241
assert response .status_code == status .HTTP_200_OK
1057
1242
1058
- assert segment_rule .conditions .count () == 0
1243
+ assert nested_rule .conditions .count () == 0
1059
1244
1060
1245
1061
1246
@pytest .mark .parametrize (
0 commit comments