Ejemplo n.º 1
0
    def test_put_with_conversion_uses_return_value(self):
        def convert(_):
            return "yeah, i got called"

        mapping = data_binding.StringMapBuilder()
        mapping.put("blep", "meow", conversion=convert)
        assert dict(mapping) == {"blep": "yeah, i got called"}
Ejemplo n.º 2
0
    def compile(self, **kwargs: typing.Any) -> CompiledRoute:
        """Generate a formatted `CompiledRoute` for this route.

        This takes into account any URL parameters that have been passed.

        Parameters
        ----------
        **kwargs : typing.Any
            Any parameters to interpolate into the route path.

        Returns
        -------
        CompiledRoute
            The compiled route.
        """
        data = data_binding.StringMapBuilder()
        for k, v in kwargs.items():
            data.put(k, v)

        return CompiledRoute(
            route=self,
            compiled_path=self.path_template.format_map(data),
            major_param_hash=MAJOR_PARAM_COMBOS[self.major_params](data)
            if self.major_params else "-",
        )
Ejemplo n.º 3
0
    def test_put_with_conversion_passes_raw_input_to_converter(self):
        mapping = data_binding.StringMapBuilder()
        convert = mock.Mock()

        expect = object()
        mapping.put("yaskjgakljglak", expect, conversion=convert)
        convert.assert_called_once_with(expect)
Ejemplo n.º 4
0
    def test_put_py_singleton_conversion_runs_before_check(self):
        def convert(_):
            return True

        mapping = data_binding.StringMapBuilder()
        mapping.put("im hungry", "yo", conversion=convert)
        assert dict(mapping) == {"im hungry": "true"}
Ejemplo n.º 5
0
 def test_duplicate_puts_stores_values_as_sequence(self):
     m1 = mock.Mock()
     m2 = mock.Mock()
     mapping = data_binding.StringMapBuilder()
     mapping.put("foo", m1)
     mapping.put("foo", m2)
     assert mapping.getall("foo") == [m1.__str__(), m2.__str__()]
     assert list(mapping.keys()) == ["foo", "foo"]
Ejemplo n.º 6
0
    async def _next_chunk(
        self
    ) -> typing.Optional[typing.Generator[users.User, typing.Any, None]]:
        query = data_binding.StringMapBuilder()
        query.put("after", self._first_id)
        query.put("limit", 100)

        raw_chunk = await self._request_call(compiled_route=self._route,
                                             query=query)
        chunk = typing.cast(data_binding.JSONArray, raw_chunk)

        if not chunk:
            return None

        self._first_id = chunk[-1]["id"]
        return (self._entity_factory.deserialize_user(u) for u in chunk)
Ejemplo n.º 7
0
    async def __anext__(self) -> audit_logs.AuditLog:
        query = data_binding.StringMapBuilder()
        query.put("limit", 100)
        query.put("user_id", self._user)
        query.put("event_type", self._action_type)
        query.put("before", self._first_id)

        raw_response = await self._request_call(compiled_route=self._route,
                                                query=query)
        response = typing.cast(data_binding.JSONObject, raw_response)

        if not response["audit_log_entries"]:
            raise StopAsyncIteration

        log = self._entity_factory.deserialize_audit_log(response)
        self._first_id = str(min(log.entries.keys()))
        return log
Ejemplo n.º 8
0
    async def _next_chunk(
        self
    ) -> typing.Optional[typing.Generator[applications.OwnGuild, typing.Any,
                                          None]]:
        query = data_binding.StringMapBuilder()
        query.put("before" if self._newest_first else "after", self._first_id)
        query.put("limit", 100)

        raw_chunk = await self._request_call(compiled_route=self._route,
                                             query=query)
        chunk = typing.cast(data_binding.JSONArray, raw_chunk)

        if not chunk:
            return None

        self._first_id = chunk[-1]["id"]
        return (self._entity_factory.deserialize_own_guild(g) for g in chunk)
Ejemplo n.º 9
0
    async def _next_chunk(
        self
    ) -> typing.Optional[typing.Generator[messages.Message, typing.Any, None]]:
        query = data_binding.StringMapBuilder()
        query.put(self._direction, self._first_id)
        query.put("limit", 100)

        raw_chunk = await self._request_call(compiled_route=self._route,
                                             query=query)
        chunk = typing.cast(data_binding.JSONArray, raw_chunk)

        if not chunk:
            return None
        if self._direction == "after":
            chunk.reverse()

        self._first_id = chunk[-1]["id"]
        return (self._entity_factory.deserialize_message(m) for m in chunk)
Ejemplo n.º 10
0
    async def _next_chunk(
        self
    ) -> typing.Optional[typing.Generator[guilds.Member, typing.Any, None]]:
        query = data_binding.StringMapBuilder()
        query.put("after", self._first_id)
        query.put("limit", 1000)

        chunk = await self._request_call(compiled_route=self._route,
                                         query=query)
        assert isinstance(chunk, list)

        if not chunk:
            return None

        self._first_id = chunk[-1]["user"]["id"]

        return (self._entity_factory.deserialize_member(
            m, guild_id=self._guild_id) for m in chunk)
Ejemplo n.º 11
0
    async def _next_chunk(
        self
    ) -> typing.Optional[typing.Generator[applications.OwnGuild, typing.Any,
                                          None]]:
        query = data_binding.StringMapBuilder()
        query.put("before" if self._newest_first else "after", self._first_id)
        # We rely on Discord's default for the limit here since for this endpoint this has always scaled
        # along side the maximum page size limit to match the maximum amount of guilds a user can be in.

        chunk = await self._request_call(compiled_route=self._route,
                                         query=query)
        assert isinstance(chunk, list)

        if not chunk:
            return None

        self._first_id = chunk[-1]["id"]
        return (self._entity_factory.deserialize_own_guild(g) for g in chunk)
Ejemplo n.º 12
0
    async def __anext__(self) -> audit_logs.AuditLog:
        query = data_binding.StringMapBuilder()
        query.put("limit", 100)
        query.put("user_id", self._user)
        query.put("action_type", self._action_type, conversion=int)
        query.put("before", self._first_id)

        response = await self._request_call(compiled_route=self._route,
                                            query=query)
        assert isinstance(response, dict)

        audit_log_entries = response["audit_log_entries"]
        if not audit_log_entries:
            raise StopAsyncIteration

        log = self._entity_factory.deserialize_audit_log(response)
        # Since deserialize_audit_log may skip entries it doesn't recognise,
        # first_id has to be calculated based on the raw payload as log.entries
        # may be missing entries.
        self._first_id = str(min(entry["id"] for entry in audit_log_entries))
        return log
Ejemplo n.º 13
0
 def test_is_multidict(self):
     assert isinstance(data_binding.StringMapBuilder(), multidict.MultiDict)
Ejemplo n.º 14
0
 def test_is_mapping(self):
     assert isinstance(data_binding.StringMapBuilder(), typing.Mapping)
Ejemplo n.º 15
0
 def test_put_general_value_casts_to_str(self):
     m = mock.Mock()
     mapping = data_binding.StringMapBuilder()
     mapping.put("foo", m)
     assert dict(mapping) == {"foo": m.__str__()}
Ejemplo n.º 16
0
 def test_starts_empty(self):
     mapping = data_binding.StringMapBuilder()
     assert mapping == {}
Ejemplo n.º 17
0
 def test_put_py_singleton(self, name, input_val, expect):
     mapping = data_binding.StringMapBuilder()
     mapping.put(name, input_val)
     assert dict(mapping) == {name: expect}
Ejemplo n.º 18
0
 def test_put_int(self):
     mapping = data_binding.StringMapBuilder()
     mapping.put("yeet", 420_69)
     assert dict(mapping) == {"yeet": "42069"}
Ejemplo n.º 19
0
 def test_put_undefined(self):
     mapping = data_binding.StringMapBuilder()
     mapping.put("foo", undefined.UNDEFINED)
     assert dict(mapping) == {}
Ejemplo n.º 20
0
    def test_put_Unique(self):
        mapping = data_binding.StringMapBuilder()

        mapping.put("myunique", MyUnique(123))
        assert dict(mapping) == {"myunique": "123"}