Skip to content

Instantly share code, notes, and snippets.

@kalda341
Created September 4, 2018 01:30
Show Gist options
  • Save kalda341/e5f24552b97b185e8f4041639d49d69d to your computer and use it in GitHub Desktop.
Save kalda341/e5f24552b97b185e8f4041639d49d69d to your computer and use it in GitHub Desktop.
def test_create_and_search(self):
other_course = mommy.make('courses.Course')
mommy.make('courses.Membership', member=self.user, course=other_course)
other_topic = mommy.make('courses.Topic', course=other_course)
other_slideshow = mommy.make('slides.Slideshow', topic=other_topic)
self.create_slide_doc(self.slideshow, 11, 'Some other words to interfere with the words we are matching')
self.create_slide_doc(self.slideshow, 12, 'Apple banana pear fruit and some other delicious stuff')
self.create_slide_doc(self.slideshow, 13, 'Books and records are also items')
self.create_slide_doc(self.slideshow, 14, 'And some guitars, basses, fiddles, synthesizers')
self.create_slide_doc(self.slideshow, 10, 'An alto sax is a higher pitched saxophone')
self.create_slide_doc(self.slideshow, 34, 'A tenor sax is one of the lower pitched saxophones')
self.create_slide_doc(other_slideshow, 38, 'A saxophone in the wrong course should not match')
self.client.force_authenticate(self.user)
resp = self.client.post('/api/test_questions/', {
# Added some extra words to try and confuse the search
'question': 'What is a tenor saxophone? Please explain your answer, and include examples.',
'course': reverse('course-detail', args=[self.course.pk])
}, format='json')
self.assertEqual(resp.status_code, 201)
question_id = resp.data['id']
# We should have seven documents in our search now
self.assertEqual(len(Slide.search().execute()), 7)
resp = self.client.get('/api/test_questions/{}/search/'.format(question_id))
self.assertEqual(resp.status_code, 200)
# Ensure we don't match posts from another course
self.assertEqual(len(resp.data), 2)
# Check the results have the correct data, and were matched in the right order
self.assertEqual(resp.data[0]['slideshow'], reverse('slideshow-detail', args=[self.slideshow.pk]))
self.assertEqual(resp.data[0]['topic'], reverse('topic-detail', args=[self.topic.pk]))
self.assertEqual(resp.data[0]['slide_number'], 34)
self.assertEqual(resp.data[0]['slide_content'], 'A tenor sax is one of the lower pitched saxophones')
self.assertEqual(resp.data[1]['slideshow'], reverse('slideshow-detail', args=[self.slideshow.pk]))
self.assertEqual(resp.data[1]['topic'], reverse('topic-detail', args=[self.topic.pk]))
self.assertEqual(resp.data[1]['slide_number'], 10)
self.assertEqual(resp.data[1]['slide_content'], 'An alto sax is a higher pitched saxophone')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment