implement simple backend search for items and tickets
This commit is contained in:
parent
d5eadbe4b1
commit
796b687719
3 changed files with 88 additions and 1 deletions
|
@ -20,9 +20,12 @@ jobs:
|
||||||
- name: Run django tests
|
- name: Run django tests
|
||||||
working-directory: core
|
working-directory: core
|
||||||
run: python3 manage.py test
|
run: python3 manage.py test
|
||||||
|
- name: Run django coverage
|
||||||
|
working-directory: core
|
||||||
|
run: coverage manage.py test
|
||||||
|
|
||||||
deploy:
|
deploy:
|
||||||
needs: [test]
|
needs: [ test ]
|
||||||
runs-on: docker
|
runs-on: docker
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
|
@ -13,6 +13,8 @@ from base64 import b64encode
|
||||||
|
|
||||||
from tickets.models import IssueThread, ItemRelation
|
from tickets.models import IssueThread, ItemRelation
|
||||||
|
|
||||||
|
from base64 import b64encode
|
||||||
|
|
||||||
|
|
||||||
class ItemTestCase(TestCase):
|
class ItemTestCase(TestCase):
|
||||||
|
|
||||||
|
@ -325,3 +327,74 @@ class ItemSearchTestCase(TestCase):
|
||||||
self.assertEqual(self.box.id, response.json()[1]['cid'])
|
self.assertEqual(self.box.id, response.json()[1]['cid'])
|
||||||
self.assertEqual(1, response.json()[1]['search_score'])
|
self.assertEqual(1, response.json()[1]['search_score'])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class ItemSearchTestCase(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super().setUp()
|
||||||
|
self.event = Event.objects.create(slug='EVENT', name='Event')
|
||||||
|
self.box = Container.objects.create(name='BOX')
|
||||||
|
self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test')
|
||||||
|
self.user.user_permissions.add(*Permission.objects.all())
|
||||||
|
self.token = AuthToken.objects.create(user=self.user)
|
||||||
|
self.client = Client(headers={'Authorization': 'Token ' + self.token[1]})
|
||||||
|
self.item1 = Item.objects.create(container=self.box, event=self.event, description='abc def')
|
||||||
|
self.item2 = Item.objects.create(container=self.box, event=self.event, description='def ghi')
|
||||||
|
self.item3 = Item.objects.create(container=self.box, event=self.event, description='jkl mno pqr')
|
||||||
|
self.item4 = Item.objects.create(container=self.box, event=self.event, description='stu vwx')
|
||||||
|
|
||||||
|
def test_search(self):
|
||||||
|
search_query = b64encode(b'abc').decode('utf-8')
|
||||||
|
response = self.client.get(f'/api/2/{self.event.slug}/items/{search_query}/')
|
||||||
|
self.assertEqual(200, response.status_code)
|
||||||
|
self.assertEqual(1, len(response.json()))
|
||||||
|
self.assertEqual(self.item1.uid, response.json()[0]['uid'])
|
||||||
|
self.assertEqual('abc def', response.json()[0]['description'])
|
||||||
|
self.assertEqual('BOX', response.json()[0]['box'])
|
||||||
|
self.assertEqual(self.box.cid, response.json()[0]['cid'])
|
||||||
|
self.assertEqual(1, response.json()[0]['search_score'])
|
||||||
|
|
||||||
|
def test_search2(self):
|
||||||
|
search_query = b64encode(b'def').decode('utf-8')
|
||||||
|
response = self.client.get(f'/api/2/{self.event.slug}/items/{search_query}/')
|
||||||
|
self.assertEqual(200, response.status_code)
|
||||||
|
self.assertEqual(2, len(response.json()))
|
||||||
|
self.assertEqual(self.item1.uid, response.json()[0]['uid'])
|
||||||
|
self.assertEqual('abc def', response.json()[0]['description'])
|
||||||
|
self.assertEqual('BOX', response.json()[0]['box'])
|
||||||
|
self.assertEqual(self.box.cid, response.json()[0]['cid'])
|
||||||
|
self.assertEqual(1, response.json()[0]['search_score'])
|
||||||
|
self.assertEqual(self.item2.uid, response.json()[1]['uid'])
|
||||||
|
self.assertEqual('def ghi', response.json()[1]['description'])
|
||||||
|
self.assertEqual('BOX', response.json()[1]['box'])
|
||||||
|
self.assertEqual(self.box.cid, response.json()[1]['cid'])
|
||||||
|
self.assertEqual(1, response.json()[0]['search_score'])
|
||||||
|
|
||||||
|
def test_search3(self):
|
||||||
|
search_query = b64encode(b'jkl').decode('utf-8')
|
||||||
|
response = self.client.get(f'/api/2/{self.event.slug}/items/{search_query}/')
|
||||||
|
self.assertEqual(200, response.status_code)
|
||||||
|
self.assertEqual(1, len(response.json()))
|
||||||
|
self.assertEqual(self.item3.uid, response.json()[0]['uid'])
|
||||||
|
self.assertEqual('jkl mno pqr', response.json()[0]['description'])
|
||||||
|
self.assertEqual('BOX', response.json()[0]['box'])
|
||||||
|
self.assertEqual(self.box.cid, response.json()[0]['cid'])
|
||||||
|
self.assertEqual(1, response.json()[0]['search_score'])
|
||||||
|
|
||||||
|
def test_search4(self):
|
||||||
|
search_query = b64encode(b'abc def').decode('utf-8')
|
||||||
|
response = self.client.get(f'/api/2/{self.event.slug}/items/{search_query}/')
|
||||||
|
self.assertEqual(200, response.status_code)
|
||||||
|
self.assertEqual(2, len(response.json()))
|
||||||
|
self.assertEqual(self.item1.uid, response.json()[0]['uid'])
|
||||||
|
self.assertEqual('abc def', response.json()[0]['description'])
|
||||||
|
self.assertEqual('BOX', response.json()[0]['box'])
|
||||||
|
self.assertEqual(self.box.cid, response.json()[0]['cid'])
|
||||||
|
self.assertEqual(2, response.json()[0]['search_score'])
|
||||||
|
self.assertEqual(self.item2.uid, response.json()[1]['uid'])
|
||||||
|
self.assertEqual('def ghi', response.json()[1]['description'])
|
||||||
|
self.assertEqual('BOX', response.json()[1]['box'])
|
||||||
|
self.assertEqual(self.box.cid, response.json()[1]['cid'])
|
||||||
|
self.assertEqual(1, response.json()[1]['search_score'])
|
||||||
|
|
||||||
|
|
|
@ -130,6 +130,17 @@ class IssueSerializer(BasicIssueSerializer):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class SearchResultSerializer(serializers.Serializer):
|
||||||
|
search_score = serializers.IntegerField()
|
||||||
|
item = IssueSerializer()
|
||||||
|
|
||||||
|
def to_representation(self, instance):
|
||||||
|
return {**IssueSerializer(instance['item']).data, 'search_score': instance['search_score']}
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = IssueThread
|
||||||
|
|
||||||
|
|
||||||
class SearchResultSerializer(serializers.Serializer):
|
class SearchResultSerializer(serializers.Serializer):
|
||||||
search_score = serializers.IntegerField()
|
search_score = serializers.IntegerField()
|
||||||
item = IssueSerializer()
|
item = IssueSerializer()
|
||||||
|
|
Loading…
Reference in a new issue