Coverage for src/counter_os/bootstrap/application_configuration.py: 81%

20 statements  

« prev     ^ index     » next       coverage.py v7.17.5, created at 2026-11-29 10:32 -0600

1import os 

2from dataclasses import dataclass 

3 

4from dotenv import load_dotenv 

5 

6from counter_os.bootstrap.application_environment_type import ApplicationEnvironmentType 

7from counter_os.infrastructure.frontend_type import FrontendType 

8from counter_os.infrastructure.persistence.counter_repository_type import ( 

9 CounterRepositoryType, 

10) 

11 

12 

13@dataclass(frozen=True) 

14class ApplicationConfiguration: 

15 counter_repository_type: str 

16 counter_repository_location_sqlite_local: str 

17 counter_repository_location_postgres_local: str 

18 frontend_type: str 

19 app_env: str 

20 flask_port: int 

21 flask_secret_key: str 

22 

23 @classmethod 

24 def from_environment( 

25 cls, 

26 *, 

27 frontend_override: str | None = None, 

28 counter_repository_type_override: str | None = None, 

29 ) -> ApplicationConfiguration: 

30 

31 # Only load .env file if in development mode 

32 app_env = os.getenv("APP_ENV", ApplicationEnvironmentType.DEVELOPMENT) 

33 

34 if app_env != ApplicationEnvironmentType.PRODUCTION: 34 ↛ 38line 34 didn't jump to line 38 because the condition on line 34 was always true

35 load_dotenv() 

36 

37 counter_repository_type: str 

38 if counter_repository_type_override is not None: 38 ↛ 39line 38 didn't jump to line 39 because the condition on line 38 was never true

39 counter_repository_type = counter_repository_type_override 

40 else: 

41 counter_repository_type = os.getenv( 

42 "COUNTER_REPOSITORY_TYPE", CounterRepositoryType.MEMORY 

43 ) 

44 

45 frontend: str 

46 if frontend_override is not None: 46 ↛ 47line 46 didn't jump to line 47 because the condition on line 46 was never true

47 frontend = frontend_override 

48 else: 

49 frontend = os.getenv("FRONTEND_TYPE", FrontendType.CLI) 

50 

51 return cls( 

52 counter_repository_type=counter_repository_type, 

53 counter_repository_location_sqlite_local=os.getenv( 

54 "COUNTER_REPOSITORY_LOCATION_SQLITE_LOCAL", "data.sqlite3" 

55 ), 

56 counter_repository_location_postgres_local=os.getenv( 

57 "COUNTER_REPOSITORY_LOCATION_POSTGRES_LOCAL", 

58 "user:password@localhost:5432/counter_os", 

59 ), 

60 frontend_type=frontend, 

61 app_env=app_env, 

62 flask_port=int(os.getenv("FLASK_PORT", "5001")), 

63 flask_secret_key=os.getenv("FLASK_SECRET_KEY", ""), 

64 )