Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix topic search #892

Merged
merged 1 commit into from
May 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- Fix CKAN harvester empty notes and `metadata` file type handling
- Remove (temporary) badges metrics
[#885](https://github.com/opendatateam/udata/issues/885)
- Test and fix topic search
[#892](https://github.com/opendatateam/udata/pull/892)

## 1.0.9 (2017-04-23)

Expand Down
2 changes: 1 addition & 1 deletion udata/core/topic/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, topic, params):
def search(self):
'''Override search to match on topic tags'''
s = super(TopicSearchMixin, self).search()
s = s.query('bool', should=[
s = s.filter('bool', should=[
Q('term', tags=tag) for tag in self.topic.tags
])
return s
Expand Down
63 changes: 63 additions & 0 deletions udata/tests/frontend/test_topic_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,73 @@

from flask import url_for

from udata import search
from udata.tests import SearchTestMixin, TestCase
from udata.tests.frontend import FrontTestCase
from udata.core.dataset.factories import VisibleDatasetFactory
from udata.core.dataset.search import DatasetSearch
from udata.core.reuse.factories import VisibleReuseFactory
from udata.core.topic.factories import TopicFactory
from udata.core.topic.views import topic_search_for


class TopicSearchTest(SearchTestMixin, TestCase):
def test_empty_search_no_match(self):
'''Should return no result if no data match the tags'''
with self.autoindex():
VisibleDatasetFactory.create_batch(2, tags=['whatever'])
topic = TopicFactory(tags=['no-match'])

query = topic_search_for(topic, DatasetSearch)
result = search.query(query)

self.assertEqual(len(result), 0)

def test_empty_search_with_match(self):
'''Should only return data with at least one tag'''
with self.autoindex():
included = VisibleDatasetFactory.create_batch(2, tags=['in'])
excluded = VisibleDatasetFactory.create_batch(2, tags=['out'])
topic = TopicFactory(tags=['in', 'no-match'])

query = topic_search_for(topic, DatasetSearch)
result = search.query(query)

found = [d.id for d in result]

self.assertEqual(len(found), 2)

for dataset in included:
self.assertIn(dataset.id, found)
for dataset in excluded:
self.assertNotIn(dataset.id, found)

def test_empty_search_with_filter_and_match(self):
'''Should match both the topic criteria and the query'''
with self.autoindex():
# Match both the topic condition but the queried tag
match = VisibleDatasetFactory.create_batch(2, tags=[
'in', 'filtered'
])
# Match the topic condition but not the queried tag
no_match = VisibleDatasetFactory.create_batch(2, tags=['in'])
# Excluded because not matching one of the topic tag
excluded = VisibleDatasetFactory.create_batch(2, tags=[
'out', 'filtered'
])
topic = TopicFactory(tags=['in', 'no-match'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the no-match required if not tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same


query = topic_search_for(topic, DatasetSearch, tag='filtered')
result = search.query(query)

found = [d.id for d in result]

self.assertEqual(len(found), 2)

for dataset in match:
self.assertIn(dataset.id, found)
for dataset in no_match + excluded:
self.assertNotIn(dataset.id, found)


class TopicsBlueprintTest(FrontTestCase):
Expand Down