Repository navigation
Expand file tree
/
Copy pathtest_validation.py
More file actions
97 lines (70 loc) · 2.74 KB
/
Copy pathtest_validation.py
File metadata and controls
97 lines (70 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import pytest
import os
os.environ["TELEGRAM_API_ID"] = "12345"
os.environ["TELEGRAM_API_HASH"] = "dummy_hash"
from main import validate_id, ValidationError, log_and_format_error
from functools import wraps
import asyncio
from typing import Union, List, Optional
# A simple async function to be decorated for testing
@validate_id("user_id", "chat_id", "user_ids")
async def dummy_function(**kwargs):
return "success", kwargs
@pytest.mark.asyncio
async def test_valid_integer_id():
result, kwargs = await dummy_function(user_id=12345)
assert result == "success"
assert kwargs["user_id"] == 12345
@pytest.mark.asyncio
async def test_valid_negative_integer_id():
result, kwargs = await dummy_function(chat_id=-100123456)
assert result == "success"
assert kwargs["chat_id"] == -100123456
@pytest.mark.asyncio
async def test_valid_string_integer_id():
result, kwargs = await dummy_function(user_id="12345")
assert result == "success"
assert kwargs["user_id"] == 12345
@pytest.mark.asyncio
async def test_valid_username():
result, kwargs = await dummy_function(user_id="@test_user")
assert result == "success"
assert kwargs["user_id"] == "@test_user"
@pytest.mark.asyncio
async def test_valid_username_without_at():
result, kwargs = await dummy_function(user_id="test_user_long_enough")
assert result == "success"
assert kwargs["user_id"] == "test_user_long_enough"
@pytest.mark.asyncio
async def test_valid_list_of_ids():
result, kwargs = await dummy_function(user_ids=[123, "456", "@test_user"])
assert result == "success"
assert kwargs["user_ids"] == [123, 456, "@test_user"]
@pytest.mark.asyncio
async def test_invalid_float_id():
result = await dummy_function(user_id=123.45)
assert "Invalid user_id" in result
assert "Type must be an integer or a string" in result
@pytest.mark.asyncio
async def test_invalid_string_id():
result = await dummy_function(user_id="inv") # too short
assert "Invalid user_id" in result
assert "Must be a valid integer ID, or a username string" in result
@pytest.mark.asyncio
async def test_integer_out_of_range():
result = await dummy_function(user_id=2**64)
assert "Invalid user_id" in result
assert "out of the valid integer range" in result
@pytest.mark.asyncio
async def test_invalid_item_in_list():
result = await dummy_function(user_ids=[123, "456", 123.45])
assert "Invalid user_ids" in result
assert "Type must be an integer or a string" in result
@pytest.mark.asyncio
async def test_no_id_provided():
result, kwargs = await dummy_function()
assert result == "success"
@pytest.mark.asyncio
async def test_none_id_provided():
result, kwargs = await dummy_function(user_id=None)
assert result == "success"