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

33 statements  

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

1from pydantic import BaseModel 

2from typing import Any 

3 

4 

5class QueryOperator(BaseModel): 

6 """ 

7 Base model for operators used in PyODMongo queries. Provides the foundational 

8 structure for combining query conditions using logical operators. 

9 

10 Methods: 

11 __and__(self, value): Combines the current operator with another using 

12 the logical AND ('$and') operator. 

13 __or__(self, value): Combines the current operator with another using 

14 the logical OR ('$or') operator. 

15 """ 

16 

17 def __and__(self, value): 

18 return LogicalOperator(operator="$and", operators=(self, value)) 

19 

20 def __or__(self, value): 

21 return LogicalOperator(operator="$or", operators=(self, value)) 

22 

23 def to_dict(self): ... 

24 

25 

26class ComparisonOperator(QueryOperator): 

27 """ 

28 Represents a single comparison operation in a PyODMongo query, containing a field 

29 path, an operator, and a value. This model is used to construct query conditions 

30 that specify how individual fields should be compared to given values. 

31 

32 Attributes: 

33 path_str (str): The string representation of the path to the field in the database 

34 document, used to identify the field to which the operation applies. 

35 operator (str): The comparison operator (e.g., '$eq', '$gt', '$lt', etc.) that 

36 defines the type of comparison to be performed. 

37 value (Any): The value to compare against the field specified by path_str. The type 

38 of this value can vary depending on the field type and the specific 

39 operation being performed. 

40 """ 

41 

42 path_str: str 

43 operator: str 

44 value: Any 

45 

46 def to_dict(self): 

47 return {self.path_str: {self.operator: self.value}} 

48 

49 

50class _LogicalOperator(QueryOperator): 

51 """ 

52 A base model for logical operators used in PyODMongo queries. This class is not 

53 intended to be used directly but is extended to support complex logical structures 

54 combining multiple comparison operations. 

55 

56 Attributes: 

57 operator (str): The logical operator (e.g., '$and', '$or') that specifies how 

58 the included operators are combined. 

59 operators (tuple[ComparisonOperator, ...]): A tuple of comparison operators 

60 that are to be logically combined according to the `operator`. 

61 """ 

62 

63 operator: str 

64 operators: tuple[Any, ...] 

65 

66 def to_dict(self): 

67 acu_list = [] 

68 for op in self.operators: 

69 acu_list.append(op.to_dict()) 

70 return {self.operator: acu_list} 

71 

72 

73class LogicalOperator(_LogicalOperator): 

74 """ 

75 Extends _LogicalOperator to allow nested logical operations in PyODMongo queries. 

76 This class supports including both basic comparison operators and other nested 

77 logical operators, providing flexibility in defining complex query conditions. 

78 

79 Attributes: 

80 operators (tuple[ComparisonOperator | _LogicalOperator, ...]): A tuple containing 

81 either comparison operators or other logical operators, allowing 

82 for nested logical structures. This enables the construction of 

83 intricate query logic necessary for advanced database operations. 

84 """ 

85 

86 operators: tuple[Any, ...] 

87 

88 

89class ElemMatchOperator(QueryOperator): 

90 """ 

91 Represents an $elemMatch operation in a PyODMongo query, used to match documents 

92 containing an array field with at least one element that matches the specified 

93 criteria. 

94 

95 Attributes: 

96 field (Any): The field on which the $elemMatch operation is applied. This 

97 typically represents the path to an array field in the database 

98 document. 

99 operators (tuple[ComparisonOperator | _LogicalOperator, ...]): A tuple of comparison 

100 operators and/or logical operators that define the matching criteria 

101 for elements within the array. 

102 """ 

103 

104 field: Any 

105 operators: tuple[Any, ...] 

106 

107 def to_dict(self): 

108 elem_match = {} 

109 for op in self.operators: 

110 for key, value in op.to_dict().items(): 

111 elem_match[key] = value 

112 return {self.field.path_str: {"$elemMatch": elem_match}}