from asynctest import TestCase as AsyncTestCase
from asynctest import mock as async_mock
from ..stats import Collector
[docs]class TestStats(AsyncTestCase):
[docs] async def test_fn_decorator(self):
stats = Collector()
@stats
def test_fn():
pass
test_fn()
assert set(stats.results["avg"]) == {
"TestStats.test_fn_decorator.<locals>.test_fn"
}
with self.assertRaises(ValueError):
stats(None)
[docs] async def test_method_decorator(self):
stats = Collector()
class TestClass:
@stats
def test(self):
pass
@stats
async def test_async(self):
pass
@stats.mark("marked", "marked2")
def test_mark(self):
pass
def test_wrap(self):
pass
instance = TestClass()
stats.wrap(instance, "test_wrap")
instance.test()
await instance.test_async()
instance.test_mark()
instance.test_wrap()
assert set(stats.results["avg"]) == {
"TestStats.test_method_decorator.<locals>.TestClass.test",
"TestStats.test_method_decorator.<locals>.TestClass.test_async",
"TestStats.test_method_decorator.<locals>.TestClass.test_mark",
"marked",
"marked2",
"TestStats.test_method_decorator.<locals>.TestClass.test_wrap",
}
[docs] async def test_disable(self):
stats = Collector()
stats.enabled = False
stats.log("test", 1.0)
assert not set(stats.results["avg"])
stats.enabled = True
stats.log("test", 1.0)
assert stats.results["avg"] == {"test": 1.0}