@@ -558,7 +558,7 @@ def test_create_activation_with_bad_rulebook_hash(
558
558
assert response .status_code == status .HTTP_400_BAD_REQUEST
559
559
assert str (response .data [SOURCE_MAPPING_ERROR_KEY ][0 ]) == (
560
560
"Rulebook has changed since the sources were mapped."
561
- " Please reattach Event streams again "
561
+ " Please reattach event streams"
562
562
)
563
563
564
564
@@ -811,25 +811,20 @@ def test_update_activation_with_everything(
811
811
"source_name" : f"{ DEFAULT_SOURCE_NAME_PREFIX } 1" ,
812
812
}
813
813
)
814
- # to be enabled in another PR
815
- """
816
814
test_activation2 ["source_mappings" ] = yaml .dump (source_mappings2 )
817
- """
815
+
818
816
response = admin_client .patch (
819
817
f"{ api_url_v1 } /activations/{ activation_id } /" , data = test_activation2
820
818
)
821
- assert response .status_code == status .HTTP_200_OK
822
- data = response .data
823
- activation = models .Activation .objects .filter (id = data ["id" ]).first ()
824
- assert activation .rulebook_name == f"{ TEST_RULEBOOK ['name' ]} 2"
825
- """
826
- swapped_ruleset = yaml.safe_load(activation.rulebook_rulesets)
827
- assert sorted(swapped_ruleset[0]["sources"][0].keys()) == [
828
- "ansible.eda.pg_listener",
829
- "name",
830
- ]
831
- assert data["event_streams"][0]["name"] == "demo-2"
832
- """
819
+ assert_updated_event_stream_mapping (
820
+ response ,
821
+ ["ansible.eda.pg_listener" , "name" ],
822
+ test_activation2 ["source_mappings" ],
823
+ "demo-2" ,
824
+ )
825
+ activation = models .Activation .objects .filter (
826
+ id = response .data ["id" ]
827
+ ).first ()
833
828
for key , val in test_activation2 .items ():
834
829
if key == "extra_var" :
835
830
assert activation .extra_var == (
@@ -842,6 +837,63 @@ def test_update_activation_with_everything(
842
837
assert activation .eda_credentials .all ()[0 ].id == val [0 ]
843
838
continue
844
839
if key == "source_mappings" :
845
- assert activation .source_mappings .strip () == val .strip ()
846
840
continue
847
841
assert getattr (activation , key ) == val
842
+
843
+ # test unchanged event stream mapping
844
+ test_activation3 = {"name" : "name3" }
845
+ response = admin_client .patch (
846
+ f"{ api_url_v1 } /activations/{ activation_id } /" , data = test_activation3
847
+ )
848
+ assert_updated_event_stream_mapping (
849
+ response ,
850
+ ["ansible.eda.pg_listener" , "name" ],
851
+ test_activation2 ["source_mappings" ],
852
+ "demo-2" ,
853
+ )
854
+
855
+ # test only changing event stream mapping
856
+ source_mappings = []
857
+ for event_stream in fks ["event_streams" ]:
858
+ source_mappings .append (
859
+ {
860
+ "event_stream_name" : event_stream .name ,
861
+ "event_stream_id" : event_stream .id ,
862
+ "rulebook_hash" : get_rulebook_hash (LEGACY_TEST_RULESETS ),
863
+ "source_name" : f"{ DEFAULT_SOURCE_NAME_PREFIX } 1" ,
864
+ }
865
+ )
866
+ test_activation4 = {"source_mappings" : yaml .dump (source_mappings )}
867
+ response = admin_client .patch (
868
+ f"{ api_url_v1 } /activations/{ activation_id } /" , data = test_activation4
869
+ )
870
+ assert_updated_event_stream_mapping (
871
+ response ,
872
+ ["ansible.eda.pg_listener" , "name" ],
873
+ test_activation4 ["source_mappings" ],
874
+ "demo" ,
875
+ )
876
+
877
+ # test clearing event stream mapping
878
+ test_activation5 = {"rulebook_id" : fks2 ["rulebook_id" ]}
879
+ response = admin_client .patch (
880
+ f"{ api_url_v1 } /activations/{ activation_id } /" , data = test_activation5
881
+ )
882
+ assert_updated_event_stream_mapping (
883
+ response , ["ansible.eda.range" ], "" , ""
884
+ )
885
+
886
+
887
+ def assert_updated_event_stream_mapping (
888
+ response , sources : list , source_mappings : str , event_stream_name : str
889
+ ):
890
+ assert response .status_code == status .HTTP_200_OK
891
+ data = response .data
892
+ activation = models .Activation .objects .filter (id = data ["id" ]).first ()
893
+ swapped_ruleset = yaml .safe_load (activation .rulebook_rulesets )
894
+ assert sorted (swapped_ruleset [0 ]["sources" ][0 ].keys ()) == sources
895
+ assert data ["source_mappings" ].strip () == source_mappings .strip ()
896
+ if event_stream_name :
897
+ assert data ["event_streams" ][0 ]["name" ] == "demo-2"
898
+ else :
899
+ assert data ["event_streams" ] == []
0 commit comments