mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-08 04:38:42 +00:00
Add regression test for optional normalisation
Also runs black on the tests file
This commit is contained in:
parent
26fc694189
commit
bb5ed4947d
@ -38,9 +38,7 @@ def test_cached_properties():
|
|||||||
|
|
||||||
# cached_slot_property
|
# cached_slot_property
|
||||||
class TestSlotted:
|
class TestSlotted:
|
||||||
__slots__ = (
|
__slots__ = '_cs_time'
|
||||||
'_cs_time'
|
|
||||||
)
|
|
||||||
|
|
||||||
@utils.cached_slot_property('_cs_time')
|
@utils.cached_slot_property('_cs_time')
|
||||||
def time(self) -> float:
|
def time(self) -> float:
|
||||||
@ -61,7 +59,7 @@ def test_cached_properties():
|
|||||||
(123456789012345678, (2015, 12, 7, 16, 13, 12)),
|
(123456789012345678, (2015, 12, 7, 16, 13, 12)),
|
||||||
(661720302316814366, (2020, 1, 1, 0, 0, 14)),
|
(661720302316814366, (2020, 1, 1, 0, 0, 14)),
|
||||||
(1000000000000000000, (2022, 7, 22, 11, 22, 59)),
|
(1000000000000000000, (2022, 7, 22, 11, 22, 59)),
|
||||||
]
|
],
|
||||||
)
|
)
|
||||||
def test_snowflake_time(snowflake: int, time_tuple: typing.Tuple[int, int, int, int, int, int]):
|
def test_snowflake_time(snowflake: int, time_tuple: typing.Tuple[int, int, int, int, int, int]):
|
||||||
dt = utils.snowflake_time(snowflake)
|
dt = utils.snowflake_time(snowflake)
|
||||||
@ -74,10 +72,7 @@ def test_snowflake_time(snowflake: int, time_tuple: typing.Tuple[int, int, int,
|
|||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_get_find():
|
async def test_get_find():
|
||||||
# Generate a dictionary of random keys to values
|
# Generate a dictionary of random keys to values
|
||||||
mapping = {
|
mapping = {secrets.token_bytes(32): secrets.token_bytes(32) for _ in range(100)}
|
||||||
secrets.token_bytes(32): secrets.token_bytes(32)
|
|
||||||
for _ in range(100)
|
|
||||||
}
|
|
||||||
|
|
||||||
# Turn it into a shuffled iterable of pairs
|
# Turn it into a shuffled iterable of pairs
|
||||||
pair = collections.namedtuple('pair', 'key value')
|
pair = collections.namedtuple('pair', 'key value')
|
||||||
@ -136,7 +131,7 @@ def test_valid_icon_size():
|
|||||||
('https://discordapp.com/invite/dpy', 'dpy'),
|
('https://discordapp.com/invite/dpy', 'dpy'),
|
||||||
('https://discord.com/invite/dpy', 'dpy'),
|
('https://discord.com/invite/dpy', 'dpy'),
|
||||||
('https://discord.gg/dpy', 'dpy'),
|
('https://discord.gg/dpy', 'dpy'),
|
||||||
]
|
],
|
||||||
)
|
)
|
||||||
def test_resolve_invite(url, code):
|
def test_resolve_invite(url, code):
|
||||||
assert utils.resolve_invite(url).code == code
|
assert utils.resolve_invite(url).code == code
|
||||||
@ -148,16 +143,13 @@ def test_resolve_invite(url, code):
|
|||||||
('https://discordapp.com/template/foobar', 'foobar'),
|
('https://discordapp.com/template/foobar', 'foobar'),
|
||||||
('https://discord.com/template/foobar', 'foobar'),
|
('https://discord.com/template/foobar', 'foobar'),
|
||||||
('https://discord.new/foobar', 'foobar'),
|
('https://discord.new/foobar', 'foobar'),
|
||||||
]
|
],
|
||||||
)
|
)
|
||||||
def test_resolve_template(url, code):
|
def test_resolve_template(url, code):
|
||||||
assert utils.resolve_template(url) == code
|
assert utils.resolve_template(url) == code
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize('mention', ['@everyone', '@here'])
|
||||||
'mention',
|
|
||||||
['@everyone', '@here']
|
|
||||||
)
|
|
||||||
def test_escape_mentions(mention):
|
def test_escape_mentions(mention):
|
||||||
assert mention not in utils.escape_mentions(mention)
|
assert mention not in utils.escape_mentions(mention)
|
||||||
assert mention not in utils.escape_mentions(f"one {mention} two")
|
assert mention not in utils.escape_mentions(f"one {mention} two")
|
||||||
@ -171,7 +163,7 @@ def test_escape_mentions(mention):
|
|||||||
([1, 2, 3, 4, 5, 6], 3, [[1, 2, 3], [4, 5, 6]]),
|
([1, 2, 3, 4, 5, 6], 3, [[1, 2, 3], [4, 5, 6]]),
|
||||||
([1, 2, 3, 4, 5, 6], 4, [[1, 2, 3, 4], [5, 6]]),
|
([1, 2, 3, 4, 5, 6], 4, [[1, 2, 3, 4], [5, 6]]),
|
||||||
([1, 2, 3, 4, 5, 6], 5, [[1, 2, 3, 4, 5], [6]]),
|
([1, 2, 3, 4, 5, 6], 5, [[1, 2, 3, 4, 5], [6]]),
|
||||||
]
|
],
|
||||||
)
|
)
|
||||||
async def test_as_chunks(source, chunk_size, chunked):
|
async def test_as_chunks(source, chunk_size, chunked):
|
||||||
assert [x for x in utils.as_chunks(source, chunk_size)] == chunked
|
assert [x for x in utils.as_chunks(source, chunk_size)] == chunked
|
||||||
@ -185,12 +177,17 @@ async def test_as_chunks(source, chunk_size, chunked):
|
|||||||
('datetime.datetime', datetime.datetime),
|
('datetime.datetime', datetime.datetime),
|
||||||
('typing.Union[typing.Literal["a"], typing.Literal["b"]]', typing.Union[typing.Literal["a"], typing.Literal["b"]]),
|
('typing.Union[typing.Literal["a"], typing.Literal["b"]]', typing.Union[typing.Literal["a"], typing.Literal["b"]]),
|
||||||
('typing.Union[typing.Union[int, str], typing.Union[bool, dict]]', typing.Union[int, str, bool, dict]),
|
('typing.Union[typing.Union[int, str], typing.Union[bool, dict]]', typing.Union[int, str, bool, dict]),
|
||||||
]
|
],
|
||||||
)
|
)
|
||||||
def test_resolve_annotation(annotation, resolved):
|
def test_resolve_annotation(annotation, resolved):
|
||||||
assert resolved == utils.resolve_annotation(annotation, globals(), locals(), None)
|
assert resolved == utils.resolve_annotation(annotation, globals(), locals(), None)
|
||||||
|
|
||||||
|
|
||||||
|
def test_resolve_annotation_optional_normalisation():
|
||||||
|
value = utils.resolve_annotation('typing.Union[None, int]', globals(), locals(), None)
|
||||||
|
assert value.__args__ == (int, type(None))
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skipif(sys.version_info < (3, 10), reason="3.10 union syntax")
|
@pytest.mark.skipif(sys.version_info < (3, 10), reason="3.10 union syntax")
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
('annotation', 'resolved'),
|
('annotation', 'resolved'),
|
||||||
@ -198,7 +195,7 @@ def test_resolve_annotation(annotation, resolved):
|
|||||||
('int | None', typing.Optional[int]),
|
('int | None', typing.Optional[int]),
|
||||||
('str | int', typing.Union[str, int]),
|
('str | int', typing.Union[str, int]),
|
||||||
('str | int | None', typing.Optional[typing.Union[str, int]]),
|
('str | int | None', typing.Optional[typing.Union[str, int]]),
|
||||||
]
|
],
|
||||||
)
|
)
|
||||||
def test_resolve_annotation_310(annotation, resolved):
|
def test_resolve_annotation_310(annotation, resolved):
|
||||||
assert resolved == utils.resolve_annotation(annotation, globals(), locals(), None)
|
assert resolved == utils.resolve_annotation(annotation, globals(), locals(), None)
|
||||||
@ -206,27 +203,33 @@ def test_resolve_annotation_310(annotation, resolved):
|
|||||||
|
|
||||||
# is_inside_class tests
|
# is_inside_class tests
|
||||||
|
|
||||||
|
|
||||||
def not_a_class():
|
def not_a_class():
|
||||||
def not_a_class_either():
|
def not_a_class_either():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return not_a_class_either
|
return not_a_class_either
|
||||||
|
|
||||||
|
|
||||||
class ThisIsAClass:
|
class ThisIsAClass:
|
||||||
def in_a_class(self):
|
def in_a_class(self):
|
||||||
def not_directly_in_a_class():
|
def not_directly_in_a_class():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return not_directly_in_a_class
|
return not_directly_in_a_class
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def a_class_method(cls):
|
def a_class_method(cls):
|
||||||
def not_directly_in_a_class():
|
def not_directly_in_a_class():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return not_directly_in_a_class
|
return not_directly_in_a_class
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def a_static_method():
|
def a_static_method():
|
||||||
def not_directly_in_a_class():
|
def not_directly_in_a_class():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return not_directly_in_a_class
|
return not_directly_in_a_class
|
||||||
|
|
||||||
class SubClass:
|
class SubClass:
|
||||||
@ -255,7 +258,7 @@ def test_is_inside_class():
|
|||||||
(datetime.datetime(2020, 1, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc), None, '<t:1577836800>'),
|
(datetime.datetime(2020, 1, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc), None, '<t:1577836800>'),
|
||||||
(datetime.datetime(2020, 1, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc), 'F', '<t:1577836800:F>'),
|
(datetime.datetime(2020, 1, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc), 'F', '<t:1577836800:F>'),
|
||||||
(datetime.datetime(2033, 5, 18, 3, 33, 20, 0, tzinfo=datetime.timezone.utc), 'D', '<t:2000000000:D>'),
|
(datetime.datetime(2033, 5, 18, 3, 33, 20, 0, tzinfo=datetime.timezone.utc), 'D', '<t:2000000000:D>'),
|
||||||
]
|
],
|
||||||
)
|
)
|
||||||
def test_format_dt(dt: datetime.datetime, style: typing.Optional[utils.TimestampStyle], formatted: str):
|
def test_format_dt(dt: datetime.datetime, style: typing.Optional[utils.TimestampStyle], formatted: str):
|
||||||
assert utils.format_dt(dt, style=style) == formatted
|
assert utils.format_dt(dt, style=style) == formatted
|
||||||
|
Loading…
x
Reference in New Issue
Block a user