mirror of
https://github.com/Steffo99/async-chain.git
synced 2024-11-25 22:44:22 +00:00
65 lines
1.5 KiB
Python
65 lines
1.5 KiB
Python
import pytest
|
|
import asyncio
|
|
import async_chain
|
|
|
|
pytestmark = pytest.mark.asyncio
|
|
|
|
|
|
class ExampleClass:
|
|
def __init__(self, value):
|
|
self.value = value
|
|
|
|
def __repr__(self):
|
|
return f"Example(value={self.value})"
|
|
|
|
@async_chain.method
|
|
async def plus(self, n):
|
|
await asyncio.sleep(0)
|
|
return self.__class__(self.value + n)
|
|
|
|
@property
|
|
@async_chain.method
|
|
async def incremented(self):
|
|
await asyncio.sleep(0)
|
|
return self.__class__(self.value + 1)
|
|
|
|
|
|
async def test_class_creation():
|
|
example = ExampleClass(0)
|
|
assert example
|
|
|
|
|
|
@pytest.fixture()
|
|
def example():
|
|
return ExampleClass(0)
|
|
|
|
|
|
async def test_method_existence(example):
|
|
assert hasattr(example, "plus")
|
|
assert hasattr(example, "minus")
|
|
assert hasattr(example, "incremented")
|
|
assert hasattr(example, "decremented")
|
|
|
|
|
|
async def test_chain_single_property(example):
|
|
one = await example.incremented
|
|
assert isinstance(one, ExampleClass)
|
|
assert one.value == 1
|
|
|
|
|
|
async def test_chain_multiple_properties(example):
|
|
three = await example.incremented.incremented.incremented
|
|
assert isinstance(three, ExampleClass)
|
|
assert three.value == 3
|
|
|
|
|
|
async def test_chain_single_method(example):
|
|
one = await example.plus(1)
|
|
assert isinstance(one, ExampleClass)
|
|
assert one.value == 1
|
|
|
|
|
|
async def test_chain_multiple_methods(example):
|
|
three = await example.plus(1).plus(1).plus(1)
|
|
assert isinstance(three, ExampleClass)
|
|
assert three.value == 3
|