예제 #1
0
def test_token_property(telegraph: Telegraph):
    telegraph.token = 'abcdef01234567890'
    assert telegraph.token == 'abcdef01234567890'

    with pytest.raises(TypeError):
        telegraph.token = 42

    account = types.Account(short_name='test', author_name='Test',
                            access_token='abcdef01234567890')
    telegraph.token = account
    assert telegraph.token == account.access_token

    with pytest.raises(TypeError):
        account = types.Account(short_name='test', author_name='Test')
        telegraph.token = account

    del telegraph.token

    assert telegraph.token is None
예제 #2
0
def test_context_token(telegraph: Telegraph):
    original_token = telegraph.token

    with telegraph.with_token('foo'):
        assert telegraph.token == 'foo'

        with telegraph.with_token('bar'):
            assert telegraph.token == 'bar'
            telegraph.token = 'baz'
            assert telegraph.token == 'bar'

        assert telegraph.token == 'foo'

    assert telegraph.token != original_token
    assert telegraph.token == 'baz'
예제 #3
0
import asyncio

from aiograph import Telegraph

telegraph = Telegraph('token')

print(f"1. Root: '{telegraph.token}'")
with telegraph.with_token('foo'):  # Will change token in current context
    print(f"2. Inside context manager: '{telegraph.token}'")

    with telegraph.with_token('bar'):  # Will change token in current context
        print(f"3. Inside child context manager: '{telegraph.token}'")
        telegraph.token = 'baz'  # Doesn't affect token inside current context
        print(
            f"4. After changing: '{telegraph.token}' (is not changed inside context manager)"
        )

    print(f"5. Inside context manager: '{telegraph.token}'")

print(f"6. Root: '{telegraph.token}'")  # Shows changed token

asyncio.run(telegraph.close())