fix bug in tickets search api and add tests
All checks were successful
/ test (push) Successful in 2m43s
All checks were successful
/ test (push) Successful in 2m43s
This commit is contained in:
parent
598f758332
commit
c50470db27
3 changed files with 89 additions and 6 deletions
|
@ -147,7 +147,17 @@ def filter_issues(issues, query):
|
|||
for issue in issues:
|
||||
value = 0
|
||||
for token in query_tokens:
|
||||
if token in issue.description:
|
||||
if token in issue.name:
|
||||
value += 1
|
||||
for comment in issue.comments.all():
|
||||
for token in query_tokens:
|
||||
if token in comment.comment:
|
||||
value += 1
|
||||
for email in issue.emails.all():
|
||||
for token in query_tokens:
|
||||
if token in email.subject:
|
||||
value += 1
|
||||
if token in email.body:
|
||||
value += 1
|
||||
if value > 0:
|
||||
yield {'search_score': value, 'issue': issue}
|
||||
|
@ -160,7 +170,10 @@ def search_issues(request, event_slug, query):
|
|||
event = Event.objects.get(slug=event_slug)
|
||||
if not request.user.has_event_perm(event, 'view_issuethread'):
|
||||
return Response(status=403)
|
||||
items = filter_issues(IssueThread.objects.filter(event=event), b64decode(query).decode('utf-8'))
|
||||
serializer = IssueSerializer()
|
||||
queryset = IssueThread.objects.filter(event=event)
|
||||
items = filter_issues(queryset.prefetch_related(*serializer.Meta.prefetch_related_fields),
|
||||
b64decode(query).decode('utf-8'))
|
||||
return Response(SearchResultSerializer(items, many=True).data)
|
||||
except Event.DoesNotExist:
|
||||
return Response(status=404)
|
||||
|
|
|
@ -139,10 +139,10 @@ class IssueSerializer(BasicIssueSerializer):
|
|||
|
||||
class SearchResultSerializer(serializers.Serializer):
|
||||
search_score = serializers.IntegerField()
|
||||
item = IssueSerializer()
|
||||
issue = IssueSerializer()
|
||||
|
||||
def to_representation(self, instance):
|
||||
return {**IssueSerializer(instance['item']).data, 'search_score': instance['search_score']}
|
||||
return {**IssueSerializer(instance['issue']).data, 'search_score': instance['search_score']}
|
||||
|
||||
class Meta:
|
||||
model = IssueThread
|
||||
|
|
|
@ -383,15 +383,85 @@ class IssueSearchTest(TestCase):
|
|||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.event = Event.objects.create(slug='EVENT', name='Event')
|
||||
self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test')
|
||||
self.user.user_permissions.add(*Permission.objects.all())
|
||||
self.user.save()
|
||||
self.event = Event.objects.create(slug='EVENT', name='Event')
|
||||
self.box = Container.objects.create(name='box1')
|
||||
self.item = Item.objects.create(container=self.box, description="foo", event=self.event)
|
||||
self.token = AuthToken.objects.create(user=self.user)
|
||||
self.client = Client(headers={'Authorization': 'Token ' + self.token[1]})
|
||||
|
||||
def test_search(self):
|
||||
def test_search_empty_result(self):
|
||||
search_query = b64encode(b'abc').decode('utf-8')
|
||||
response = self.client.get(f'/api/2/{self.event.slug}/tickets/{search_query}/')
|
||||
self.assertEqual(200, response.status_code)
|
||||
self.assertEqual([], response.json())
|
||||
|
||||
def test_search(self):
|
||||
now = datetime.now()
|
||||
issue = IssueThread.objects.create(
|
||||
name="test issue abc",
|
||||
event=self.event,
|
||||
)
|
||||
mail1 = Email.objects.create(
|
||||
subject='test',
|
||||
body='test abc',
|
||||
sender='test',
|
||||
recipient='test',
|
||||
issue_thread=issue,
|
||||
timestamp=now,
|
||||
)
|
||||
mail2 = Email.objects.create(
|
||||
subject='test',
|
||||
body='test',
|
||||
sender='test',
|
||||
recipient='test',
|
||||
issue_thread=issue,
|
||||
in_reply_to=mail1.reference,
|
||||
timestamp=now + timedelta(seconds=2),
|
||||
)
|
||||
assignment = Assignment.objects.create(
|
||||
issue_thread=issue,
|
||||
assigned_to=self.user,
|
||||
timestamp=now + timedelta(seconds=3),
|
||||
)
|
||||
comment = Comment.objects.create(
|
||||
issue_thread=issue,
|
||||
comment="test def",
|
||||
timestamp=now + timedelta(seconds=4),
|
||||
)
|
||||
match = ItemRelation.objects.create(
|
||||
issue_thread=issue,
|
||||
item=self.item,
|
||||
timestamp=now + timedelta(seconds=5),
|
||||
)
|
||||
search_query = b64encode(b'abc').decode('utf-8')
|
||||
response = self.client.get(f'/api/2/{self.event.slug}/tickets/{search_query}/')
|
||||
self.assertEqual(200, response.status_code)
|
||||
self.assertEqual(1, len(response.json()))
|
||||
self.assertEqual(issue.id, response.json()[0]['id'])
|
||||
score2 = response.json()[0]['search_score']
|
||||
|
||||
search_query = b64encode(b'def').decode('utf-8')
|
||||
response = self.client.get(f'/api/2/{self.event.slug}/tickets/{search_query}/')
|
||||
self.assertEqual(200, response.status_code)
|
||||
self.assertEqual(1, len(response.json()))
|
||||
self.assertEqual(issue.id, response.json()[0]['id'])
|
||||
score1 = response.json()[0]['search_score']
|
||||
|
||||
search_query = b64encode(b'ghi').decode('utf-8')
|
||||
response = self.client.get(f'/api/2/{self.event.slug}/tickets/{search_query}/')
|
||||
self.assertEqual(200, response.status_code)
|
||||
self.assertEqual(0, len(response.json()))
|
||||
|
||||
search_query = b64encode(b'abc def').decode('utf-8')
|
||||
response = self.client.get(f'/api/2/{self.event.slug}/tickets/{search_query}/')
|
||||
self.assertEqual(200, response.status_code)
|
||||
self.assertEqual(1, len(response.json()))
|
||||
self.assertEqual(issue.id, response.json()[0]['id'])
|
||||
score3 = response.json()[0]['search_score']
|
||||
|
||||
self.assertGreater(score3, score2)
|
||||
self.assertGreater(score2, score1)
|
||||
self.assertGreater(score1, 0)
|
||||
|
|
Loading…
Add table
Reference in a new issue