Coverage for pyodmongo/models/db_field_info.py: 100%

37 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-16 15:08 +0000

1from typing import Any 

2from dataclasses import dataclass 

3from .query_operators import ComparisonOperator 

4from .id_model import Id 

5from bson import ObjectId 

6 

7 

8@dataclass 

9class DbField: 

10 """ 

11 Represents a field within a database model, containing metadata necessary for 

12 managing the serialization and deserialization of model fields, especially in 

13 contexts involving references to other documents or complex nested structures. 

14 

15 Attributes: 

16 field_name (str | None): The name of the field as defined in the database model. 

17 field_alias (str | None): The alias used for the field in database operations, 

18 which might differ from the field name. 

19 path_str (str | None): The string representation of the path to the field within 

20 nested structures or related models. 

21 field_type (Any | None): The data type of the field, which can be any valid Python 

22 type or a more complex custom type. 

23 by_reference (bool | None): Indicates whether the field is linked by reference 

24 to another document or model, rather than embedded. 

25 is_list (bool | None): Specifies if the field is expected to be a list of items, 

26 typically used for handling multiple relationships or 

27 collections of values. 

28 has_model_fields (bool | None): Indicates if the field itself contains sub-fields 

29 that are also modeled, suggesting a nested data 

30 structure that requires special handling. 

31 """ 

32 

33 field_name: str = None 

34 field_alias: str = None 

35 path_str: str = None 

36 field_type: Any = None 

37 by_reference: bool = None 

38 is_list: bool = None 

39 has_model_fields: bool = None 

40 

41 def comparison_operator(self, operator: str, value: Any) -> ComparisonOperator: 

42 if self.by_reference or self.field_type == Id: 

43 if type(value) != list and value is not None: 

44 value = ObjectId(value) 

45 if type(value) == list: 

46 value = [ObjectId(v) for v in value] 

47 return ComparisonOperator( 

48 path_str=self.path_str, operator=operator, value=value 

49 ) 

50 

51 def __lt__(self, value: Any) -> ComparisonOperator: 

52 return self.comparison_operator(operator="$lt", value=value) 

53 

54 def __le__(self, value: Any) -> ComparisonOperator: 

55 return self.comparison_operator(operator="$lte", value=value) 

56 

57 def __eq__(self, value: Any) -> ComparisonOperator: 

58 if isinstance(value, DbField): 

59 return super().__eq__(value) 

60 return self.comparison_operator(operator="$eq", value=value) 

61 

62 def __ne__(self, value: Any) -> ComparisonOperator: 

63 if isinstance(value, DbField): 

64 return super().__ne__(value) 

65 return self.comparison_operator(operator="$ne", value=value) 

66 

67 def __gt__(self, value: Any) -> ComparisonOperator: 

68 return self.comparison_operator(operator="$gt", value=value) 

69 

70 def __ge__(self, value: Any) -> ComparisonOperator: 

71 return self.comparison_operator(operator="$gte", value=value)