Skip to content
This repository has been archived by the owner on Feb 23, 2020. It is now read-only.

Commit

Permalink
#645 Search by series (#648)
Browse files Browse the repository at this point in the history
* #645  Search by series

* #645  Fix code style
  • Loading branch information
duker33 authored May 15, 2019
1 parent 386e8f6 commit 7db5d29
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
19 changes: 19 additions & 0 deletions stroyprombeton/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ def get_series(self) -> models.QuerySet:
)


class SeriesQuerySet(models.QuerySet):

def bind_fields(self):
"""Prefetch or select typical related fields to reduce sql queries count."""
return self.select_related('page')

def active(self):
return self.filter(page__is_active=True)


class SeriesManager(models.Manager.from_queryset(SeriesQuerySet)):
"""Get all products of given category by Category's id or instance."""

def active(self):
return self.get_queryset().active()


# @todo #510:60m Create Series navigation.
# See the parent's task body for details about navigation.
# This task depends on Series page creation.
Expand All @@ -89,6 +106,8 @@ class Series(pages.models.PageMixin):
It's like Category, but has no hierarchy.
"""

objects = SeriesManager()

SLUG_HASH_SIZE = 5
SLUG_MAX_LENGTH = 50

Expand Down
20 changes: 17 additions & 3 deletions stroyprombeton/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,10 @@ def test_robots_success(self):
self.assertEqual(self.response.status_code, 200)


# @todo #60m: Create arch for the Search tests.
# Create code, that will be able to test search
# by dataset presented in pattern "term -> results".
# Maybe hypothesis lib will be helpful for it.
@tag('fast')
class Search(TestCase):
"""Test all search methods: search page and autocompletes."""
Expand Down Expand Up @@ -613,15 +617,15 @@ def test_search_by_product_name(self):
first = soup.find(class_='table-link')
self.assertEqual(product.name, first.text.strip())

def test_search_by_id(self):
def test_id_results(self):
"""Search view should return redirect on model page, if id was received as term."""
product = models.Product.objects.first()
self.assertContains(
self.search(term=str(product.id)),
product.name,
)

def test_search_has_results(self):
def test_some_results(self):
"""Search page should contain at least one result for right term."""
response = self.search(term=self.TERM)
self.assertEqual(response.status_code, 200)
Expand All @@ -631,7 +635,17 @@ def test_search_has_results(self):
self.assertContains(response, '<title>')
self.assertContains(response, '<td class="table-td table-name">')

def test_search_no_results(self):
def test_series_results(self):
"""Search page should contain at least one result for right term."""
response = self.search(term='Serie')
self.assertEqual(response.status_code, 200)
self.assertContains(response, _('Series'))
# search page should contain not only results.
# But markup, menu links and so on.
self.assertContains(response, '<title>')
self.assertContains(response, '<td class="table-td table-name">')

def test_no_results(self):
"""Search page should not contain results for wrong term."""
response = self.search(term=self.WRONG_TERM)
self.assertEqual(response.status_code, 200)
Expand Down
16 changes: 15 additions & 1 deletion stroyprombeton/views/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ class Search(search_views.SearchView):
name='category',
qs=stb_models.Category.objects.active(),
# ignore CPDBear
fields=['name', 'specification'],
fields=['name'],
min_similarity=settings.TRIGRAM_MIN_SIMILARITY,
),
search_engine.Search(
name='series',
qs=stb_models.Series.objects.active(),
# ignore CPDBear
fields=['name'],
min_similarity=settings.TRIGRAM_MIN_SIMILARITY,
),
search_engine.Search(
Expand Down Expand Up @@ -59,6 +66,13 @@ class Autocomplete(search_views.AutocompleteView):

# ignore CPDBear
search_entities = [
search_engine.Search(
name='category',
qs=stb_models.Category.objects.active(),
# ignore CPDBear
fields=['name'],
min_similarity=settings.TRIGRAM_MIN_SIMILARITY,
),
search_engine.Search(
name='product',
qs=stb_models.Option.objects.active(),
Expand Down

1 comment on commit 7db5d29

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 7db5d29 May 15, 2019

Choose a reason for hiding this comment

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

Puzzle 60m-1d4cf566 discovered in stroyprombeton/tests/tests_views.py and submitted as #651. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.