def test_no_change(self, co3_args): client = self._connect(co3_args) inc = self._create_incident(client, {"name": "test"}) uri = "/incidents/%d" % inc['id'] # Create a conflict inc["name"] = "the wrong value" inc["vers"] -= 1 # Force it to check old_value patch = resilient.Patch(inc) patch.add_value("name", "test updated") def mycb(response, patch_status, patch): raise resilient.NoChange response = client.patch_with_callback(uri, patch, mycb) assert response assert response.status_code == 200 patch_status = resilient.PatchStatus(response.json()) assert not patch_status.is_success() assert patch_status.get_conflict_fields() == ["name"]
def test_exchange_conflicting_value(self): # Given a base object with a value of "test1". base = dict(mytest1="test1") # And a patch that is attempting to modify that base object to have a value of "test2". patch = resilient.Patch(base) patch.add_value("mytest1", "test2") # Confirm that it does indeed have an "old value" of "test1" (this is taken from the base object). assert patch.get_old_value("mytest1") == "test1" # When we create a patch status that simulates a conflict error from the server (where the # value of base changed from "test1" to "blah"). patch_status = resilient.PatchStatus({ "success": False, "field_failures": [{ "field": "mytest1", "your_original_value": "test2", "actual_current_value": "blah" }], "message": "Some message" }) # When I exchange the conflicting value... patch.exchange_conflicting_value(patch_status, "mytest1", "test2") # The patch's "old value" will be the current server's value. assert patch.get_old_value("mytest1") == "blah" assert patch.get_new_value("mytest1") == "test2"
def test_values(self): status = resilient.PatchStatus(TestPatchStatus._make_test_data()) assert status.get_your_original_value("mytest1") == "original1" assert status.get_actual_current_value("mytest1") == "current1" assert status.get_your_original_value("mytest2") == "original2" assert status.get_actual_current_value("mytest2") == "current2"
def test_has_failures(self): status = resilient.PatchStatus(TestPatchStatus._make_test_data()) assert not status.is_success() assert status.has_field_failures() assert status.get_conflict_fields() == ["mytest1", "mytest2"] assert status.is_conflict_field("mytest1") assert status.is_conflict_field("mytest2") assert not status.is_conflict_field("blah")
def test_patch_no_conflict(self, co3_args): """ do incident_patch with no conflict """ client = self._connect(co3_args) inc = self._create_incident(client, {"name": "test"}) uri = "/incidents/%d" % inc['id'] patch = resilient.Patch(inc) patch.add_value("name", "test updated") response = client.patch(uri, patch, overwrite_conflict=False) assert resilient.PatchStatus(response.json()).is_success() inc = client.get("/incidents/%d" % inc['id']) assert inc['name'] == "test updated"
def test_field_name_found(self): status = resilient.PatchStatus(TestPatchStatus._make_test_data()) with pytest.raises(ValueError) as exception_info: status.get_your_original_value("blah") assert "No conflict found for field blah" in str(exception_info.value) with pytest.raises(ValueError) as exception_info: status.get_your_original_value("blah") assert "No conflict found for field blah" in str(exception_info.value)
def test_patch_conflict(self, co3_args, overwrite_conflict): """ do incident patch that results in conflict """ client = self._connect(co3_args) inc = self._create_incident(client, {"name": "test"}) uri = "/incidents/%d" % inc['id'] # Create a conflict inc["name"] = "the wrong value" inc["vers"] -= 1 # Force it to check old_value patch = resilient.Patch(inc) patch.add_value("name", "test updated") if overwrite_conflict: # If overwrite_conflict is specified then patch will return. response = client.patch(uri, patch, overwrite_conflict=overwrite_conflict) assert resilient.PatchStatus( response.json()).is_success() is overwrite_conflict else: # Not overwriting conflict, so an exception will be thrown. with pytest.raises( resilient.PatchConflictException) as exception_info: client.patch(uri, patch, overwrite_conflict=overwrite_conflict) # Gather the patch_status value from the exception for additional verification. patch_status = exception_info.value.patch_status fail_msg = "could not be applied due to a conflicting edit by another user. The following field(s) were in conflict: name." assert fail_msg in patch_status.get_message() assert patch_status.get_conflict_fields() == ["name"] assert patch_status.get_your_original_value( "name") == "the wrong value" assert patch_status.get_actual_current_value("name") == "test" inc = client.get("/incidents/%d" % inc['id']) if overwrite_conflict: assert inc['name'] == "test updated" else: assert inc['name'] == "test"
def test_message(self): status = resilient.PatchStatus(TestPatchStatus._make_test_data()) assert status.get_message() == "Some message"
def test_success(self, co3_args, success): test_data = {"success": success} status = resilient.PatchStatus(test_data) assert status.is_success() == success