Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/cloud/firestore_v1/transforms.py: 59%

41 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-09 06:27 +0000

1# Copyright 2017 Google LLC All rights reserved. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15"""Helpful constants to use for Google Cloud Firestore.""" 

16 

17 

18class Sentinel(object): 

19 """Sentinel objects used to signal special handling.""" 

20 

21 __slots__ = ("description",) 

22 

23 def __init__(self, description) -> None: 

24 self.description = description 

25 

26 def __repr__(self): 

27 return "Sentinel: {}".format(self.description) 

28 

29 

30DELETE_FIELD = Sentinel("Value used to delete a field in a document.") 

31 

32 

33SERVER_TIMESTAMP = Sentinel( 

34 "Value used to set a document field to the server timestamp." 

35) 

36 

37 

38class _ValueList(object): 

39 """Read-only list of values. 

40 

41 Args: 

42 values (List | Tuple): values held in the helper. 

43 """ 

44 

45 slots = ("_values",) 

46 

47 def __init__(self, values) -> None: 

48 if not isinstance(values, (list, tuple)): 

49 raise ValueError("'values' must be a list or tuple.") 

50 

51 if len(values) == 0: 

52 raise ValueError("'values' must be non-empty.") 

53 

54 self._values = list(values) 

55 

56 def __eq__(self, other): 

57 if not isinstance(other, self.__class__): 

58 return NotImplemented 

59 return self._values == other._values 

60 

61 @property 

62 def values(self): 

63 """Values to append. 

64 

65 Returns (List): 

66 values to be appended by the transform. 

67 """ 

68 return self._values 

69 

70 

71class ArrayUnion(_ValueList): 

72 """Field transform: appends missing values to an array field. 

73 

74 See: 

75 https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1#google.firestore.v1.DocumentTransform.FieldTransform.FIELDS.google.firestore.v1.ArrayValue.google.firestore.v1.DocumentTransform.FieldTransform.append_missing_elements 

76 

77 Args: 

78 values (List | Tuple): values to append. 

79 """ 

80 

81 

82class ArrayRemove(_ValueList): 

83 """Field transform: remove values from an array field. 

84 

85 See: 

86 https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1#google.firestore.v1.DocumentTransform.FieldTransform.FIELDS.google.firestore.v1.ArrayValue.google.firestore.v1.DocumentTransform.FieldTransform.remove_all_from_array 

87 

88 Args: 

89 values (List | Tuple): values to remove. 

90 """ 

91 

92 

93class _NumericValue(object): 

94 """Hold a single integer / float value. 

95 

96 Args: 

97 value (int | float): value held in the helper. 

98 """ 

99 

100 def __init__(self, value) -> None: 

101 if not isinstance(value, (int, float)): 

102 raise ValueError("Pass an integer / float value.") 

103 

104 self._value = value 

105 

106 @property 

107 def value(self): 

108 """Value used by the transform. 

109 

110 Returns: 

111 (Integer | Float) value passed in the constructor. 

112 """ 

113 return self._value 

114 

115 def __eq__(self, other): 

116 if not isinstance(other, self.__class__): 

117 return NotImplemented 

118 return self._value == other._value 

119 

120 

121class Increment(_NumericValue): 

122 """Field transform: increment a numeric field with specified value. 

123 

124 See: 

125 https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1#google.firestore.v1.DocumentTransform.FieldTransform.FIELDS.google.firestore.v1.ArrayValue.google.firestore.v1.DocumentTransform.FieldTransform.increment 

126 

127 Args: 

128 value (int | float): value used to increment the field. 

129 """ 

130 

131 

132class Maximum(_NumericValue): 

133 """Field transform: bound numeric field with specified value. 

134 

135 See: 

136 https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1#google.firestore.v1.DocumentTransform.FieldTransform.FIELDS.google.firestore.v1.ArrayValue.google.firestore.v1.DocumentTransform.FieldTransform.maximum 

137 

138 Args: 

139 value (int | float): value used to bound the field. 

140 """ 

141 

142 

143class Minimum(_NumericValue): 

144 """Field transform: bound numeric field with specified value. 

145 

146 See: 

147 https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1#google.firestore.v1.DocumentTransform.FieldTransform.FIELDS.google.firestore.v1.ArrayValue.google.firestore.v1.DocumentTransform.FieldTransform.minimum 

148 

149 Args: 

150 value (int | float): value used to bound the field. 

151 """