def test_patch_commands( self, http_client, base_url, mongo_system, system_id, bg_command, field, value, dev, succeed, ): if dev: mongo_system.version += ".dev" mongo_system.deep_save() # Make changes to the new command if field: if field == "parameters": value = [value] setattr(bg_command, field, value) # Also delete the id, otherwise mongo gets really confused delattr(bg_command, "id") body = PatchOperation( operation="replace", path="/commands", value=SchemaParser.serialize_command( [bg_command], to_string=False, many=True ), ) request = HTTPRequest( base_url + "/api/v1/systems/" + system_id, method="PATCH", headers={"content-type": "application/json"}, body=SchemaParser.serialize_patch(body), ) response = yield http_client.fetch(request, raise_error=False) if succeed: assert response.code == 200 updated = SchemaParser.parse_system( response.body.decode("utf-8"), from_string=True ) assert_command_equal(bg_command, updated.commands[0]) else: assert response.code == 400
def update_system(self, system_id, new_commands=None, **kwargs): """Update a System Args: system_id (str): The System ID new_commands (Optional[List[Command]]): New System commands Keyword Args: add_instance (Instance): An Instance to append metadata (dict): New System metadata description (str): New System description display_name (str): New System display name icon_name (str): New System icon name template (str): New System template Returns: System: The updated system """ operations = [] if new_commands is not None: commands = SchemaParser.serialize_command(new_commands, to_string=False, many=True) operations.append(PatchOperation("replace", "/commands", commands)) add_instance = kwargs.pop("add_instance", None) if add_instance: instance = SchemaParser.serialize_instance(add_instance, to_string=False) operations.append(PatchOperation("add", "/instance", instance)) metadata = kwargs.pop("metadata", {}) if metadata: operations.append(PatchOperation("update", "/metadata", metadata)) # The remaining kwargs are all strings # Sending an empty string (instead of None) ensures they're actually cleared for key, value in kwargs.items(): operations.append( PatchOperation("replace", "/%s" % key, value or "")) return self.client.patch_system( system_id, SchemaParser.serialize_patch(operations, many=True))