def from_state(cls: Type[T], state: State, max_size: int, encoder: Encoder) -> T: state_dict = state.as_dict() for key in ["args", "kwargs", "options"]: if key in state_dict: encoded_value = encoder.encode(state_dict[key]) state_dict[key] = encoded_value if sys.getsizeof(encoded_value) <= max_size else None return cls(**state_dict)
def test_filter_messages(self, stub_broker, api_client): state = State("some_message_id") state_backend = stub_broker.get_state_backend() state_backend.set_state(state, ttl=1000) data = {"sort_column": "message_id", "selected_message_ids": ["some_message_id"]} res = api_client.post("/messages/states", data=json.dumps(data), content_type="application/json") assert res.json == {"count": 1, "data": [state.as_dict()]}
def test_get_by_message_id(self, stub_broker, state_middleware, api_client): message_id = "1" state = State(message_id, StateNamesEnum.Pending) state_middleware.backend.set_state(state, ttl=1000) res = api_client.get("/messages/state/{}".format(message_id)) assert res.json == state.as_dict()
def test_get_state_by_name(self, name_state, stub_broker, state_middleware, api_client): state = State("1", name_state) state_middleware.backend.set_state(state, ttl=1000) args = {"search_value": name_state.value} res = api_client.get("/messages/states", data=args) assert res.json == {"count": 1, "data": [state.as_dict()]}
def test_filter_messages(self, stub_broker, api_client, state_middleware): state = State("some_message_id", ) state_middleware.backend.set_state(state, ttl=1000) data = { "sort_column": "message_id", "search_value": "some_mes", } res = api_client.get("/messages/states", query_string=data) assert res.json == {"count": 1, "data": [state.as_dict()]}
def test_get_state_by_name(self, status, stub_broker, state_middleware, api_client): state = State("1", status) state_middleware.backend.set_state(state, ttl=1000) args = {"selected_statuses": [status.value]} res = api_client.post("/messages/states", data=json.dumps(args), content_type="application/json") assert res.json == {"count": 1, "data": [state.as_dict()]}
def test_get_states_by_name(self, n, stub_broker, state_middleware, api_client): # generate random test random_states = [] for i in range(n): random_state = State(f"id{i}", choice(list(StateStatusesEnum))) # random state random_states.append(random_state.as_dict()) state_middleware.backend.set_state(random_state, ttl=1000) # check storage res = api_client.post("/messages/states", data=json.dumps({"size": 100}), content_type="application/json") states = res.json["data"] assert len(states) == n # check integrity for state in states: assert state in random_states
def test_get_states_by_name(self, n, stub_broker, state_middleware, api_client): # generate random test random_states = [] for i in range(n): random_state = State( "id{}".format(i), choice(list(StateNamesEnum)), ) # random state random_states.append(random_state.as_dict()) state_middleware.backend.set_state(random_state, ttl=1000) # check storage res = api_client.get("/messages/states?size=100") states = res.json["data"] assert len(states) == n # check integrity for state in states: assert state in random_states