Coverage for opt/mealie/lib/python3.12/site-packages/mealie/db/models/_model_utils/guid.py: 92%

36 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-11-25 15:32 +0000

1import uuid 1a

2from typing import Any 1a

3 

4from sqlalchemy import Dialect 1a

5from sqlalchemy.dialects.postgresql import UUID 1a

6from sqlalchemy.types import CHAR, TypeDecorator 1a

7 

8 

9class GUID(TypeDecorator): 1a

10 """Platform-independent GUID type. 

11 Uses PostgreSQL's UUID type, otherwise uses 

12 CHAR(32), storing as stringified hex values. 

13 """ 

14 

15 impl = CHAR 1a

16 cache_ok = True 1a

17 

18 @staticmethod 1a

19 def generate(): 1a

20 return uuid.uuid4() 1ahbicdjkeflg

21 

22 @staticmethod 1a

23 def convert_value_to_guid(value: Any, dialect: Dialect) -> str | None: 1a

24 if value is None: 1ahmnbicdjkeflg

25 return value 1ahbicdjkeflg

26 elif dialect.name == "postgresql": 26 ↛ 27line 26 didn't jump to line 27 because the condition on line 26 was never true1ahmnbicdjkeflg

27 return str(value) 

28 else: 

29 if not isinstance(value, uuid.UUID): 1ahmnbicdjkeflg

30 return f"{uuid.UUID(value).int:032x}" 1mnbicdjeflg

31 else: 

32 # hexstring 

33 return f"{value.int:032x}" 1ahmnbicdjkeflg

34 

35 def load_dialect_impl(self, dialect): 1a

36 if dialect.name == "postgresql": 36 ↛ 37line 36 didn't jump to line 37 because the condition on line 36 was never true1ahmbicdjkeflg

37 return dialect.type_descriptor(UUID()) 

38 else: 

39 return dialect.type_descriptor(CHAR(32)) 1ahmbicdjkeflg

40 

41 def process_bind_param(self, value, dialect): 1a

42 return self.convert_value_to_guid(value, dialect) 1ahmnbicdjkeflg

43 

44 def _uuid_value(self, value): 1a

45 if value is None: 1ahmnbicdjkeflg

46 return value 1ahmnbicdjkeflg

47 else: 

48 if not isinstance(value, uuid.UUID): 1ahmnbicdjkeflg

49 value = uuid.UUID(value) 1ahmnbicdjkeflg

50 return value 1ahmnbicdjkeflg

51 

52 def process_result_value(self, value, dialect): 1a

53 return self._uuid_value(value) 1ahmnbicdjkeflg

54 

55 def sort_key_function(self, value): 1a

56 return self._uuid_value(value) 1ahmbcdkefg