def test_json_response_contains_error_when_there_are_no_links(self): response = get_json_response_of_word_links_archive(word='ananas', start_date=self.today_date, end_date=self.today_date) response_json = json.loads((response.content.decode())) self.assertEqual(response_json['word'], 'ananas') self.assertTrue(response_json['error']) self.assertTrue('errorMessage' in response_json) self.assertIn('There is no links collocated to word "ananas"', response_json['errorMessage'])
def test_json_response_contains_links_occured_anywhen_when_no_data_specified(self): response = get_json_response_of_word_links_archive(word='bus') response_json = json.loads((response.content.decode())) titles = get_response_jsons_items_attributes(response_json, 'links', 'title') self.assertIn('10 Reasons to Get on a Bus.', titles) self.assertIn('A is an B Watson-powered driverless electric bus', titles) links = get_response_jsons_items_attributes(response_json, 'links', 'link') self.assertIn('www.wp.pl/articles/015', links) self.assertIn('www.tvn.pl/art/005/', links)
def test_json_response_does_not_contain_links_not_occured_in_date_range(self): response = get_json_response_of_word_links_archive(word='bus', start_date=self.two_weeks_ago, end_date=self.yesterday_date) response_json = json.loads((response.content.decode())) titles = get_response_jsons_items_attributes(response_json, 'links', 'title') self.assertNotIn('A is an B Watson-powered driverless electric bus', titles) self.assertNotIn('Police find gun in bus.', titles) links = get_response_jsons_items_attributes(response_json, 'links', 'link') self.assertNotIn('www.tvn.pl/art/005/', links) self.assertNotIn('www.wp.pl/articles/011', links)
def test_json_response_does_not_contain_links_not_occured_in_certain_date(self): response = get_json_response_of_word_links_archive(word='bus', start_date=self.today_date) response_json = json.loads((response.content.decode())) self.assertEqual(1, len(response_json['links'])) titles = get_response_jsons_items_attributes(response_json, 'links', 'title') self.assertNotIn('10 Reasons to Get on a Bus.', titles) sources = get_response_jsons_items_attributes(response_json, 'links', 'source') self.assertNotIn('ONET', sources) links = get_response_jsons_items_attributes(response_json, 'links', 'link') self.assertNotIn('www.wp.pl/articles/015', links)
def test_json_response_contains_links_occured_in_certain_date(self): response = get_json_response_of_word_links_archive(word='bus', start_date=self.today_date) response_json = json.loads((response.content.decode())) self.assertEqual(1, len(response_json['links'])) self.assertEqual('bus', response_json['word']) titles = get_response_jsons_items_attributes(response_json, 'links', 'title') self.assertIn('Police find gun in bus.', titles) sources = get_response_jsons_items_attributes(response_json, 'links', 'source') self.assertIn('WP', sources) links = get_response_jsons_items_attributes(response_json, 'links', 'link') self.assertIn('www.wp.pl/articles/011', links)
def test_json_proper_format(self): response = get_json_response_of_word_links_archive( word='bus', start_date=self.yesterday_date, end_date=self.today_date) response_json = json.loads((response.content.decode())) self.assertEqual(response_json['word'], 'bus') self.assertTrue('links' in response_json) self.assertTrue('error' in response_json) self.assertTrue('startdate' in response_json) self.assertEqual(self.yesterday_date.strftime('%Y-%m-%d'), response_json['startdate']) self.assertTrue('enddate' in response_json) self.assertEqual(self.today_date.strftime('%Y-%m-%d'), response_json['enddate']) self.assertFalse('errorMessage' in response_json) link = response_json['links'][0] self.assertTrue('title' in link) self.assertTrue('link' in link)
def test_json_response_contains_links_occured_in_date_range(self): response = get_json_response_of_word_links_archive(word='bus', start_date=self.month_ago_date, end_date=self.today_date) response_json = json.loads((response.content.decode())) self.assertEqual(3, len(response_json['links'])) titles = get_response_jsons_items_attributes(response_json, 'links', 'title') self.assertIn('Police find gun in bus.', titles) self.assertIn('10 Reasons to Get on a Bus.', titles) self.assertIn('A is an B Watson-powered driverless electric bus', titles) sources = get_response_jsons_items_attributes(response_json, 'links', 'source') self.assertIn('ONET', sources) self.assertIn('TVN', sources) self.assertIn('WP', sources) links = get_response_jsons_items_attributes(response_json, 'links', 'link') self.assertIn('www.wp.pl/articles/011', links) self.assertIn('www.wp.pl/articles/015', links) self.assertIn('www.tvn.pl/art/005/', links)
def archive(request): if not request.GET: form = WordsQueryForm(data=request.GET) return render(request, "archive.html", {"form": form}) if request.method == "GET": print(request.GET) start_date = request.GET["startdate"] end_date = request.GET["enddate"] if request.GET["info"] == "get_word_details": return get_json_response_of_word_links_archive(request.GET["word"], start_date, end_date) if request.GET["info"] == "get_words_archive": form = WordsQueryForm(request.GET or None) print(form.is_valid()) if form.is_valid(): print("form is valid") response_data = get_worddetails_text_and_weight_dict(start_date, end_date) from crispy_forms.utils import render_crispy_form from django.core.context_processors import csrf ctx = {} ctx.update(csrf(request)) form_html = render_crispy_form(form, context=ctx) response_data["form_html"] = form_html else: print("form is NOT valid") from crispy_forms.utils import render_crispy_form from django.core.context_processors import csrf ctx = {} ctx.update(csrf(request)) form_html = render_crispy_form(form, context=ctx) response_data = {"success": False, "form_html": form_html} return JsonResponse(response_data, content_type="application/json")
def render_view_or_return_ajax_response(request, start_date=None, end_date=None): if not request.GET: wordtemp = get_worddetails_text_and_weight_dict(start_date, end_date) return render(request, "home.html", {"words": wordtemp, "start_date": start_date, "end_date": end_date}) if request.GET: return get_json_response_of_word_links_archive(request.GET["word"], start_date, end_date)