def test_saving_and_retrieving_searches(self): search = Search(search_terms = 'atrazine') search.save() searches = Searches(search_id = search) searches.save() saved_searches = Searches.objects.all() saved_search = saved_searches[0] self.assertEqual(saved_searches.count(),1) self.assertEqual(saved_search.search_id.search_terms,search.search_terms)
def test_saving_and_retrieving_results(self): search = Search(search_terms = 'atrazine') search.save() searches = Searches(search_id = search) searches.save() document = Document(doc_id = '1', filename='test.pdf', author="Slick Willy",title='here is an title') document.save() result = Result(doc_id = document, searches_id = searches) result.save() results = Result.objects.all() saved_result = results[0] self.assertEqual(results.count(),1) self.assertEqual(saved_result.pk,result.pk) self.assertEqual(saved_result.doc_id.pk, document.pk) self.assertEqual(saved_result.searches_id.pk,searches.pk) self.assertEqual(saved_result.searches_id.search_id.pk,search.pk)
def search(request): form = SearchForm(data=request.POST,initial={ 'choice_field':request.POST['choice_field'] }) if form.is_valid(): search_terms = request.POST['search_terms'].lower().split() search_terms.sort() search_results_order = request.POST['choice_field'] search_terms_url = '_'.join(search_terms + [search_results_order]) search_terms_str = ' '.join(search_terms) try: search = Search(search_terms = search_terms_str) search.full_clean() search.save() except ValidationError: pass return redirect('get_search_results',search_terms_url) else: return render(request, 'home.html',{'form':form})
def test_saving_and_retrieving_search(self): first_search = Search() first_search.search_terms = 'atrazine' first_search.save() second_search = Search() second_search.search_terms = 'missouri' second_search.save() saved_searches = Search.objects.all() self.assertEqual(saved_searches.count(),2) first_saved_search = saved_searches[0] second_saved_search = saved_searches[1] self.assertEqual(first_saved_search.search_terms, 'atrazine') self.assertEqual(second_saved_search.search_terms, 'missouri')
def test_cannot_save_empty_search(self): search = Search(search_terms = '') with self.assertRaises(ValidationError): search.save() search.full_clean()