Commit d0a4f82c by Douglas Hall Committed by Douglas Hall

Use set comprehension instead of list comprehension when collecting Course model primary keys.

This is an attempt to fix an issue we are seeing with unreliable search results.

ENT-444
parent 9296a555
...@@ -46,7 +46,7 @@ class AffiliateWindowViewSetTests(ElasticsearchTestMixin, SerializationMixin, AP ...@@ -46,7 +46,7 @@ class AffiliateWindowViewSetTests(ElasticsearchTestMixin, SerializationMixin, AP
def test_affiliate_with_supported_seats(self): def test_affiliate_with_supported_seats(self):
""" Verify that endpoint returns course runs for verified and professional seats only. """ """ Verify that endpoint returns course runs for verified and professional seats only. """
with self.assertNumQueries(7): with self.assertNumQueries(8):
response = self.client.get(self.affiliate_url) response = self.client.get(self.affiliate_url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
......
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-06-20 13:51
from __future__ import unicode_literals
from django.db import migrations
def create_switch(apps, schema_editor):
"""Create the log_course_search_queries switch if it does not already exist."""
Switch = apps.get_model('waffle', 'Switch')
Switch.objects.get_or_create(name='log_course_search_queries', defaults={'active': False})
def delete_switch(apps, schema_editor):
"""Delete the log_course_search_queries switch."""
Switch = apps.get_model('waffle', 'Switch')
Switch.objects.filter(name='log_course_search_queries').delete()
class Migration(migrations.Migration):
dependencies = [
('course_metadata', '0055_program_hidden'),
('waffle', '0001_initial'),
]
operations = [
migrations.RunPython(create_switch, delete_switch),
]
...@@ -334,7 +334,14 @@ class Course(TimeStampedModel): ...@@ -334,7 +334,14 @@ class Course(TimeStampedModel):
""" """
query = clean_query(query) query = clean_query(query)
results = SearchQuerySet().models(cls).raw_search(query) results = SearchQuerySet().models(cls).raw_search(query)
ids = [result.pk for result in results] ids = {result.pk for result in results}
if waffle.switch_is_active('log_course_search_queries'):
logger.info('Course search query {query} returned the following ids: {course_ids}'.format(
query=query,
course_ids=ids
))
return cls.objects.filter(pk__in=ids) return cls.objects.filter(pk__in=ids)
......
...@@ -44,6 +44,7 @@ class CourseTests(ElasticsearchTestMixin, TestCase): ...@@ -44,6 +44,7 @@ class CourseTests(ElasticsearchTestMixin, TestCase):
def test_search(self): def test_search(self):
""" Verify the method returns a filtered queryset of courses. """ """ Verify the method returns a filtered queryset of courses. """
toggle_switch('log_course_search_queries', active=True)
title = 'Some random title' title = 'Some random title'
courses = factories.CourseFactory.create_batch(3, title=title) courses = factories.CourseFactory.create_batch(3, title=title)
# Sort lowercase keys to prevent different sort orders due to casing. # Sort lowercase keys to prevent different sort orders due to casing.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment