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 json_fix # noqa: F401

8 import threadsafevariable

9 from py4web.core import ICECUBE, Fixture

10

11 from .core import TypeDAL

12

13

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

15 """

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

17 """

18

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

20 """

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

22 """

23 self.get_connection_from_pool_or_new()

24 threadsafevariable.ThreadSafeVariable.restore(ICECUBE)

25

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

27 """

28 Rollback db on error.

29 """

30 self.recycle_connection_in_pool_or_close("rollback")

31

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

33 """

34 Commit db on success.

35 """

36 self.recycle_connection_in_pool_or_close("commit")