Skip to content

Commit

Permalink
Add string based filtering
Browse files Browse the repository at this point in the history
Related-To: #854
  • Loading branch information
rixvet committed Aug 24, 2023
1 parent b316d03 commit 8448321
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 21 deletions.
19 changes: 1 addition & 18 deletions ci/apiv2/hashtopolis.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,7 @@ def filter(self, expand, max_results, ordering, filter):
uri = self._api_endpoint + self._model_uri
headers = self._headers

filter_list = []
cast = {
'__gt': '>',
'__gte': '>=',
'__lt': '<',
'__lte': '<=',
}
for k, v in filter.items():
filter_item = None
for k2, v2 in cast.items():
if k.endswith(k2):
filter_item = f'{k[:-len(k2)]}{v2}{v}'
break
# Default to equal assignment
if filter_item is None:
filter_item = f'{k}={v}'
filter_list.append(filter_item)

filter_list = [f'{k}={v}' for k, v in filter.items()]
payload = {
'filter': filter_list,
'maxResults': max_results if max_results is not None else 999,
Expand Down
104 changes: 101 additions & 3 deletions ci/apiv2/test_filter_and_ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,116 @@ def create_test_objects(self, **kwargs):
def test_filter(self):
model_objs = self.create_test_objects()
objs = HashType.objects.filter(hashTypeId__gte=90000, hashTypeId__lte=91000)
self.assertEqual([x.id for x in model_objs], [x.id for x in objs])
self.assertEqual(
[x.id for x in model_objs],
[x.id for x in objs])

# Broken due to bug https://github.com/hashtopolis/server/issues/968
def test_filter__contains(self):
search_token = "SHA"
objs = HashType.objects.filter(description__contains=search_token)
all_objs = HashType.objects.all()
self.assertEqual(
[x.id for x in all_objs if search_token in x.description],
[x.id for x in objs])

# Broken due to bug https://github.com/hashtopolis/server/issues/968
def test_filter__endswith(self):
search_token = 'sha512'
objs = HashType.objects.filter(description__endswith=search_token)
all_objs = HashType.objects.all()
self.assertEqual(
[x.id for x in all_objs if x.description.endswith(search_token)],
[x.id for x in objs])

def test_filter__eq(self):
objs = HashType.objects.filter(hashTypeId__eq=100)
all_objs = HashType.objects.all()
self.assertEqual(
[x.id for x in all_objs if x.hashTypeId == 100],
[x.id for x in objs])

def test_filter__gt(self):
objs = HashType.objects.filter(hashTypeId__gt=8000)
all_objs = HashType.objects.all()
self.assertEqual(
[x.id for x in all_objs if x.hashTypeId > 8000],
[x.id for x in objs])

def test_filter__gte(self):
objs = HashType.objects.filter(hashTypeId__gte=8000)
all_objs = HashType.objects.all()
self.assertEqual(
[x.id for x in all_objs if x.hashTypeId >= 8000],
[x.id for x in objs])

def test_filter__icontains(self):
search_token = 'ShA'
objs = HashType.objects.filter(description__icontains=search_token)
all_objs = HashType.objects.all()
self.assertEqual(
[x.id for x in all_objs if search_token.lower() in x.description.lower()],
[x.id for x in objs])

def test_filter__iendswith(self):
search_token = 'sHa512'
objs = HashType.objects.filter(description__iendswith=search_token)
all_objs = HashType.objects.all()
self.assertEqual(
[x.id for x in all_objs if x.description.lower().endswith(search_token.lower())],
[x.id for x in objs])

def test_filter__istartswith(self):
search_token = 'NeTnTLM'
objs = HashType.objects.filter(description__istartswith=search_token)
all_objs = HashType.objects.all()
self.assertEqual(
[x.id for x in all_objs if x.description.lower().startswith(search_token.lower())],
[x.id for x in objs])

def test_filter__lt(self):
objs = HashType.objects.filter(hashTypeId__lt=100)
all_objs = HashType.objects.all()
self.assertEqual(
[x.id for x in all_objs if x.hashTypeId < 100],
[x.id for x in objs])

def test_filter__lte(self):
objs = HashType.objects.filter(hashTypeId__lte=100)
all_objs = HashType.objects.all()
self.assertEqual(
[x.id for x in all_objs if x.hashTypeId <= 100],
[x.id for x in objs])

def test_filter__ne(self):
objs = HashType.objects.filter(hashTypeId__ne=100)
all_objs = HashType.objects.all()
self.assertEqual(
[x.id for x in all_objs if x.hashTypeId != 100],
[x.id for x in objs])

# Broken due to bug https://github.com/hashtopolis/server/issues/968
def test_filter__startswith(self):
objs = HashType.objects.filter(description__startswith="net")
all_objs = HashType.objects.all()
self.assertEqual(
[x.id for x in all_objs if x.description.startswith('net')],
[x.id for x in objs])

def test_ordering(self):
model_objs = self.create_test_objects()
objs = HashType.objects.filter(hashTypeId__gte=90000, hashTypeId__lte=91000,
ordering=['-hashTypeId'])
sorted_model_objs = sorted(model_objs, key=lambda x: x.hashTypeId, reverse=True)
self.assertEqual([x.id for x in sorted_model_objs], [x.id for x in objs])
self.assertEqual(
[x.id for x in sorted_model_objs],
[x.id for x in objs])

def test_ordering_twice(self):
model_objs = self.create_test_objects()
objs = HashType.objects.filter(hashTypeId__gte=90000, hashTypeId__lte=91000,
ordering=['-isSalted', '-hashTypeId'])
sorted_model_objs = sorted(model_objs, key=lambda x: (x.isSalted, x.hashTypeId), reverse=True)
self.assertEqual([x.id for x in sorted_model_objs], [x.id for x in objs])
self.assertEqual(
[x.id for x in sorted_model_objs],
[x.id for x in objs])

0 comments on commit 8448321

Please sign in to comment.