Skip to content

Commit

Permalink
Support reverse relation filter by in comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
karally authored and stphivos committed Nov 14, 2021
1 parent 0bab367 commit 49d7ca3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions django_mock_queries/comparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def lte_comparison(first, second):


def in_comparison(first, second):
if isinstance(first, list):
return bool(set(first).intersection(set(second)))

return first in second if first is not None else False


Expand Down
12 changes: 12 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ def test_query_filters_model_objects_by_bad_field(self):
r"Choices are 'id', 'make', 'make_id', 'model', 'passengers', 'sedan', 'speed', 'variations'\."):
self.mock_set.filter(bad_field='bogus')

def test_query_filters_reverse_relationship_by_in_comparison(self):
with mocked_relations(Manufacturer):
cars = [Car(speed=1)]

make = Manufacturer()
make.car_set = MockSet(*cars)

self.mock_set.add(make)

result = self.mock_set.filter(car__speed__in=[1, 2])
assert result.count() == 1

def test_query_exclude(self):
item_1 = MockModel(foo=1, bar='a')
item_2 = MockModel(foo=1, bar='b')
Expand Down
6 changes: 6 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ def test_is_match_in_value_check(self):
result = utils.is_match(1, [1, 3], constants.COMPARISON_IN)
assert result is True

result = utils.is_match([3], [1, 2], constants.COMPARISON_IN)
assert result is False

result = utils.is_match([1, 3], [1, 2], constants.COMPARISON_IN)
assert result is True

@patch('django_mock_queries.utils.get_attribute')
@patch('django_mock_queries.utils.is_match', MagicMock(return_value=True))
def test_matches_includes_object_in_results_when_match(self, get_attr_mock):
Expand Down

0 comments on commit 49d7ca3

Please sign in to comment.