Skip to content

Commit daf077b

Browse files
authored
feat: adds None as possible chainable target (#20)
* Adds ability to paginate using the pages API (#19) * feat: adds ability to paginate using the pages API which was originally hidden to the user * chore: adds testcase for new paginator * feat: adds None as possible chainable target
1 parent 318d75c commit daf077b

2 files changed

Lines changed: 59 additions & 4 deletions

File tree

hostingde/model/filter.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def contains(self, value: str) -> 'FilterCondition':
157157
self.relation = FilterConditionRelation.EQUAL
158158
return self
159159

160-
def __and__(self, other: FilterElement) -> 'FilterChain':
160+
def __and__(self, other: Optional[FilterElement]) -> 'FilterChain':
161161
"""
162162
Dynamically construct a 'and' filter chain object using binary operations.
163163
:param other: The other filter element to chain to this element.
@@ -172,10 +172,12 @@ def __and__(self, other: FilterElement) -> 'FilterChain':
172172
return FilterChain(FilterChainConnective.AND).add_filter(self).add_filter(other)
173173
else:
174174
raise FilterCompilationException('Unknown filter element connective operation')
175+
elif other is None:
176+
return self
175177
else:
176178
raise FilterCompilationException('Unknown filter element type')
177179

178-
def __or__(self, other: FilterElement) -> 'FilterChain':
180+
def __or__(self, other: Optional[FilterElement]) -> 'FilterChain':
179181
"""
180182
Dynamically construct a 'or' filter chain object using binary operations.
181183
:param other: The other filter element to chain to this element.
@@ -190,6 +192,8 @@ def __or__(self, other: FilterElement) -> 'FilterChain':
190192
return FilterChain(FilterChainConnective.OR).add_filter(self).add_filter(other)
191193
else:
192194
raise FilterCompilationException('Unknown filter element connective operation')
195+
elif other is None:
196+
return self
193197
else:
194198
raise FilterCompilationException('Unknown filter element type')
195199

@@ -242,7 +246,7 @@ def add_filter(self, filter_element: FilterElement, front: bool = False) -> 'Fil
242246

243247
return self
244248

245-
def __and__(self, other: FilterElement) -> 'FilterChain':
249+
def __and__(self, other: Optional[FilterElement]) -> 'FilterChain':
246250
"""
247251
Dynamically construct a 'and' filter chain object using binary operations.
248252
:param other: The other filter element to chain to this element.
@@ -269,10 +273,12 @@ def __and__(self, other: FilterElement) -> 'FilterChain':
269273
return FilterChain(FilterChainConnective.AND).add_filter(self).add_filter(other)
270274
else:
271275
raise FilterCompilationException('Unknown filter element connective operation')
276+
elif other is None:
277+
return self
272278
else:
273279
raise FilterCompilationException('Unknown filter element type')
274280

275-
def __or__(self, other: FilterElement) -> 'FilterChain':
281+
def __or__(self, other: Optional[FilterElement]) -> 'FilterChain':
276282
"""
277283
Dynamically construct a 'and' filter chain object using binary operations.
278284
:param other: The other filter element to chain to this element.
@@ -299,5 +305,7 @@ def __or__(self, other: FilterElement) -> 'FilterChain':
299305
return FilterChain(FilterChainConnective.OR).add_filter(self).add_filter(other)
300306
else:
301307
raise FilterCompilationException('Unknown filter element connective operation')
308+
elif other is None:
309+
return self
302310
else:
303311
raise FilterCompilationException('Unknown filter element type')

tests/unit/model/test_filter.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ def test_condition_needs_value(self):
6666

6767

6868
class TestSimpleFilterChain:
69+
70+
def test_filter_bitand_none(self):
71+
f1 = FilterCondition('field').eq('value')
72+
f2 = None
73+
74+
chain = f1 & f2
75+
76+
filter_object = chain.to_filter_object()
77+
78+
assert filter_object is not None
79+
assert filter_object.get('field') == 'field'
80+
assert filter_object.get('value') == 'value'
81+
assert filter_object.get('relation') == 'equal'
82+
6983
def test_filter_bitand_chain(self):
7084
f1 = FilterCondition('field').eq('value')
7185
f2 = FilterCondition('field2').ne('value')
@@ -78,6 +92,19 @@ def test_filter_bitand_chain(self):
7892
assert filter_object.get('subFilterConnective') == 'and'
7993
assert len(filter_object.get('subFilter', [])) == 2
8094

95+
def test_filter_bitor_none(self):
96+
f1 = FilterCondition('field').eq('value')
97+
f2 = None
98+
99+
chain = f1 | f2
100+
101+
filter_object = chain.to_filter_object()
102+
103+
assert filter_object is not None
104+
assert filter_object.get('field') == 'field'
105+
assert filter_object.get('value') == 'value'
106+
assert filter_object.get('relation') == 'equal'
107+
81108
def test_filter_bitor_chain(self):
82109
f1 = FilterCondition('field').eq('value')
83110
f2 = FilterCondition('field2').ne('value')
@@ -96,6 +123,26 @@ def test_empty_chain_should_throw(self):
96123

97124

98125
class TestFilterIntoChainInsertion:
126+
def test_none_into_and_chain(self):
127+
c1 = FilterCondition('field2').eq('radnom') & FilterCondition('field3').eq('something')
128+
129+
chain = c1 & None
130+
131+
filter_object = chain.to_filter_object()
132+
assert filter_object is not None
133+
assert filter_object.get('subFilterConnective') == 'and'
134+
assert len(filter_object.get('subFilter', [])) == 2
135+
136+
def test_none_into_or_chain(self):
137+
c1 = FilterCondition('field2').eq('radnom') | FilterCondition('field3').eq('something')
138+
139+
chain = c1 | None
140+
141+
filter_object = chain.to_filter_object()
142+
assert filter_object is not None
143+
assert filter_object.get('subFilterConnective') == 'or'
144+
assert len(filter_object.get('subFilter', [])) == 2
145+
99146
def test_filter_ele_and_into_and(self):
100147
f1 = FilterCondition('field').eq('value')
101148
c1 = FilterCondition('field2').eq('radnom') & FilterCondition('field3').eq('something')

0 commit comments

Comments
 (0)