Coverage for src/typedal/for_py4web.py: 100%

1"""

2 ONLY USE IN COMBINATION WITH PY4WEB!

3"""

4

5 from typing import Any

6

7 import threadsafevariable

8 from py4web.core import ICECUBE, Fixture

9

10 from .core import TypeDAL

11

12

13 class DAL(TypeDAL, Fixture): # pragma: no cover

14 """

15 Fixture similar to the py4web pydal fixture, but for typedal.

16 """

17

18 def on_request(self, _: dict[str, Any]) -> None:

19 """

20 Make sure there is a database connection when a request comes in.

21 """

22 self.get_connection_from_pool_or_new()

23 threadsafevariable.ThreadSafeVariable.restore(ICECUBE)

24

25 def on_error(self, _: dict[str, Any]) -> None:

26 """

27 Rollback db on error.

28 """

29 self.recycle_connection_in_pool_or_close("rollback")

30

31 def on_success(self, _: dict[str, Any]) -> None:

32 """

33 Commit db on success.

34 """

35 self.recycle_connection_in_pool_or_close("commit")