testing product search utils directly, now they return sets
This commit is contained in:
@@ -13,6 +13,7 @@ from rest_framework import status
|
||||
from companies.factories import CompanyFactory
|
||||
from products.factories import ProductFactory
|
||||
from products.models import Product
|
||||
from products.utils import find_related_products_v3
|
||||
|
||||
from core.factories import CustomUserFactory
|
||||
from core.utils import get_tokens_for_user
|
||||
@@ -536,3 +537,34 @@ class MyProductsViewTest(APITestCase):
|
||||
|
||||
# check response
|
||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||
|
||||
|
||||
class FindRelatedProductsTest(APITestCase):
|
||||
|
||||
def setUp(self):
|
||||
"""Tests setup
|
||||
"""
|
||||
self.factory = ProductFactory
|
||||
self.model = Product
|
||||
# clear table
|
||||
self.model.objects.all().delete()
|
||||
|
||||
def test_v3_find_by_single_tag(self):
|
||||
# create tagged product
|
||||
tag = 'cool'
|
||||
expected_instances = [
|
||||
self.factory(tags=tag)
|
||||
]
|
||||
# instance = self.factory()
|
||||
# instance.tags.set(tag)
|
||||
# instance.save()
|
||||
# searh for it
|
||||
results = find_related_products_v3(tag)
|
||||
import ipdb; ipdb.set_trace()
|
||||
|
||||
# assert result
|
||||
self.assertTrue(len(results) == len(expected_instances))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ def find_related_products_v5(keyword):
|
||||
Q(category__name__icontains=keyword)|
|
||||
Q(attributes__label__icontains=keyword)
|
||||
)
|
||||
return products_qs
|
||||
return set(products_qs)
|
||||
|
||||
|
||||
def find_related_products_v2(keyword):
|
||||
@@ -104,7 +104,7 @@ def find_related_products_v2(keyword):
|
||||
products_qs = Product.objects.annotate(
|
||||
search=vector
|
||||
).filter(search=keyword)
|
||||
return products_qs
|
||||
return set(products_qs)
|
||||
|
||||
|
||||
def find_related_products_v3(keyword):
|
||||
@@ -118,13 +118,16 @@ def find_related_products_v3(keyword):
|
||||
PROBLEM: returns unrelated instances
|
||||
"""
|
||||
# TODO: figure out why it includes unrelated instances
|
||||
fields=('name', 'description', 'tags__label', 'attributes__label', 'category__name')
|
||||
vector = SearchVector(*fields)
|
||||
# fields=('name', 'description', 'tags__label', 'attributes__label', 'category__name')
|
||||
|
||||
vector = SearchVector('name') + SearchVector('description') + SearchVector('tags__label') + SearchVector('attributes__label') + SearchVector('category__name')
|
||||
query = SearchQuery(keyword)
|
||||
|
||||
products_qs = Product.objects.annotate(
|
||||
rank=SearchRank(vector, query)
|
||||
).order_by('-rank')
|
||||
return products_qs
|
||||
).filter(rank__gt=0.05).order_by('-rank')
|
||||
|
||||
return set(products_qs)
|
||||
|
||||
|
||||
def find_related_products_v4(keyword):
|
||||
@@ -137,4 +140,4 @@ def find_related_products_v4(keyword):
|
||||
similarity=TrigramSimilarity('name', keyword),
|
||||
).order_by('-similarity')
|
||||
|
||||
return products_qs
|
||||
return set(products_qs)
|
||||
|
||||
@@ -155,12 +155,11 @@ def product_search(request):
|
||||
chunks = query_string.split(' ')
|
||||
|
||||
for chunk in chunks:
|
||||
# import ipdb; ipdb.set_trace()
|
||||
products_qs = find_related_products_v5(chunk)
|
||||
# products_qs = find_related_products_v4(chunk)
|
||||
# products_qs = find_related_products_v3(chunk)
|
||||
for instance in products_qs:
|
||||
result_set.add(instance)
|
||||
product_set = find_related_products_v5(chunk)
|
||||
# product_set = find_related_products_v4(chunk)
|
||||
# product_set = find_related_products_v3(chunk)
|
||||
# add to result set
|
||||
result_set.update(product_set)
|
||||
# TODO: add search for entire phrase
|
||||
|
||||
# extract filters from result_set
|
||||
|
||||
Reference in New Issue
Block a user