Skip to content
This repository has been archived by the owner on Mar 21, 2021. It is now read-only.

Commit

Permalink
Merge branch feature/search into develop
Browse files Browse the repository at this point in the history
* Close #9
* Add the search feature
  • Loading branch information
winterjung committed Nov 14, 2018
2 parents 2c119eb + 2e55cfb commit ff56c2c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
26 changes: 26 additions & 0 deletions api/chat/tests/test_views.py
Expand Up @@ -147,3 +147,29 @@ def test_our_messages(self, room, conversation):
res = self.client.get('/v1/rooms/1/messages/')
msgs_of_002 = res.data
assert msgs_of_001 == msgs_of_002

def test_one(self, conversation):
self.login('001', '001')
res = self.client.get('/v1/rooms/1/messages/1/')
assert res.data['id'] == 1
assert res.data['sender'] == 1
assert res.data['content'] == 'hello'

def test_search(self, conversation):
self.login('001', '001')
res = self.client.get('/v1/rooms/1/messages/?q=you')
assert len(res.data) == 2
assert res.data[0]['content'] == 'how are you?'
assert res.data[1]['content'] == 'see you later'

def test_search_empty(self, conversation):
self.login('001', '001')
res = self.client.get('/v1/rooms/1/messages/?q=')
assert len(res.data) == 6

def test_search_only_in_list(self, conversation):
self.login('001', '001')
res = self.client.get('/v1/rooms/1/messages/1/?q=you')
assert res.data['id'] == 1
assert res.data['sender'] == 1
assert res.data['content'] == 'hello'
7 changes: 6 additions & 1 deletion api/chat/views.py
Expand Up @@ -40,4 +40,9 @@ def get_queryset(self):
user = self.request.user
if not user.room_set.filter(id=room_id):
raise Http404
return super().get_queryset()

queryset = super().get_queryset()
query = self.request.GET.get('q')
if query and self.action == 'list':
queryset = queryset.filter(content__icontains=query)
return queryset

0 comments on commit ff56c2c

Please sign in to comment.