def test_serialization(mocker): mocker.patch.object(json, 'dumps', return_value=http_options_fixture_json) service_event = HttpOptions.from_dict(data=http_options_fixture) assert service_event.as_json(compact=True) is not None json.dumps.assert_called_with(http_options_fixture, sort_keys=True) assert service_event.as_json() is not None json.dumps.assert_called_with(http_options_fixture, indent=4, sort_keys=True)
def from_dict(cls, data): name = data["name"] action = data["action"] if isinstance(action, str): return cls( name=name, help_=action, args={}, events={}, http_options=None, output=None, data=data, ) args = {} if "arguments" in action: for arg_name, arg in action["arguments"].items(): args[arg_name] = Argument.from_dict( data={"name": arg_name, "argument": arg} ) events = {} if "events" in action: for event_name, event in action["events"].items(): events[event_name] = Event.from_dict( data={"name": event_name, "event": event} ) http_options = action.get("http", None) if http_options is not None: http_options = HttpOptions.from_dict( data={"http_options": http_options} ) output = None if "output" in action: output = ServiceOutput.from_dict(data={"output": action["output"]}) return cls( name=name, help_=action.get("help", "No help available."), args=args, events=events, http_options=http_options, output=output, data=data, )
def test_deserialization(mocker): mocker.patch.object(json, 'loads', return_value=http_options_fixture) http_options = HttpOptions.from_json(jsonstr=http_options_fixture_json) assert http_options is not None json.loads.assert_called_with(http_options_fixture_json) subscribe = HttpOptions.from_dict(data={ "http_options": http_options_fixture["http_options"]["subscribe"] }) unsubscribe= HttpOptions.from_dict(data={ "http_options": http_options_fixture["http_options"]["unsubscribe"] }) assert http_options.subscribe().path() == subscribe.path() assert http_options.subscribe().method() == subscribe.method() assert http_options.subscribe().content_type() == subscribe.content_type() assert http_options.unsubscribe().path() == unsubscribe.path() assert http_options.unsubscribe().method() == unsubscribe.method() assert http_options.unsubscribe().content_type() == unsubscribe.content_type()
def from_dict(cls, data): name = data["name"] action = data["action"] if isinstance(action, str): return cls(name=name, help_=action, args={}, events={}, http_options=None, output=None, data=data) args = {} if 'arguments' in action: for arg_name, arg in action['arguments'].items(): args[arg_name] = Argument.from_dict(data={ "name": arg_name, "argument": arg }) events = {} if 'events' in action: for event_name, event in action['events'].items(): events[event_name] = Event.from_dict(data={ "name": event_name, "event": event }) http_options = action.get('http', None) if http_options is not None: http_options = HttpOptions.from_dict( data={"http_options": http_options}) output = None if 'output' in action: output = Output.from_dict(data={"output": action["output"]}) return cls(name=name, help_=action.get('help_', 'No help_ available'), args=args, events=events, http_options=http_options, output=output, data=data)