Coverage for src/prosemark/adapters/fake_storage.py: 100%

12 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-09-24 18:08 +0000

1# Copyright (c) 2024 Prosemark Contributors 

2# This software is licensed under the MIT License 

3 

4"""Fake storage adapter for testing binder persistence.""" 

5 

6from typing import TYPE_CHECKING 

7 

8from prosemark.exceptions import BinderNotFoundError 

9from prosemark.ports.binder_repo import BinderRepo 

10 

11if TYPE_CHECKING: # pragma: no cover 

12 from prosemark.domain.models import Binder 

13 

14 

15class FakeBinderRepo(BinderRepo): 

16 """In-memory fake implementation of BinderRepo for testing. 

17 

18 Provides minimal binder storage functionality using memory storage 

19 instead of filesystem. Maintains the same interface contract as 

20 production implementations but without actual file I/O. 

21 

22 This fake stores a single binder in memory and tracks whether 

23 a binder has been saved. The load operation returns the saved 

24 binder or raises BinderNotFoundError if nothing has been saved. 

25 

26 Examples: 

27 >>> from prosemark.domain.models import Binder 

28 >>> repo = FakeBinderRepo() 

29 >>> binder = Binder(roots=[]) 

30 >>> repo.save(binder) 

31 >>> loaded = repo.load() 

32 >>> loaded.roots == [] 

33 True 

34 

35 """ 

36 

37 def __init__(self) -> None: 

38 """Initialize empty fake repository.""" 

39 self._binder: Binder | None = None 

40 

41 def load(self) -> 'Binder': 

42 """Load binder from memory storage. 

43 

44 Returns: 

45 The previously saved Binder instance. 

46 

47 Raises: 

48 BinderNotFoundError: If no binder has been saved yet. 

49 

50 """ 

51 if self._binder is None: # pragma: no cover 

52 msg = 'No binder has been saved' 

53 raise BinderNotFoundError(msg) # pragma: no cover 

54 return self._binder 

55 

56 def save(self, binder: 'Binder') -> None: 

57 """Save binder to memory storage. 

58 

59 Args: 

60 binder: The Binder instance to store in memory. 

61 

62 """ 

63 self._binder = binder 

64 

65 def set_binder(self, binder: 'Binder') -> None: 

66 """Set binder directly for testing convenience. 

67 

68 This is a test-specific method that allows direct setup 

69 of the binder state without going through the save() method. 

70 

71 Args: 

72 binder: The Binder instance to store in memory. 

73 

74 """ 

75 self._binder = binder