def test_parse_get_response_from_stackexchange(self, requests_get): collector = Collector([ "--since", "2011-11-15 10:00:00", "--until", "2016-06-05 10:00:00" ]) f = open("test_question.json") class mock_requests: status_code = 200 text = None mock_object = mock_requests mock_object.text = f.read() f.close() requests_get.return_value = mock_object expected_answer = 6 self.assertEquals(collector.parse_answer_count(45535808), expected_answer) f = open("test.json") mock_object1 = mock_requests mock_object1.text = f.read() f.close() uri = "whatever" requests_get.return_value = mock_object1 collector.get_stackexchange_data() expected_answer = mock_object1.text self.assertEquals(collector.answer, expected_answer)
def test_read_success_percent(self, mo): collector = Collector(["--persuccess"]) options = collector.parser.options self.assertEquals(options.persuccess, True) collector.collect_statistics() self.assertEquals(collector.success_counter, 2) self.assertEquals(collector.request_counter, 3)
def test_collect_statistics_html(self, parse_answer_count, get_stackexchange_data): uri = "whatever" collector = Collector([ "--since", "2016-06-04 10:00:00", "--until", "2016-06-05 10:00:00", "--output-format", "html" ]) collector.get_stackexchange_data(uri) get_stackexchange_data.assert_called_once_with(uri) # fill asnwers with json file f = open("test.json") collector.answer = f.read() f.close() collector.collect_statistics() self.assertEqual(collector.results['total_accepted_answers'], 1) self.assertEqual(collector.results['accepted_answers_average_score'], 6.0) self.assertEqual(collector.results['average_answers_per_question'], 3.0) self.assertEqual(collector.results['top_ten_answers_comment_count'], { 45535808: 0, 46051675: 0, 45017555: 4 }) collector.produce_report()
def test_10topdict_general(self, mo): collector = Collector(["--persuccess"]) options = collector.parser.options self.assertEquals(options.persuccess, True) collector.collect_statistics() self.assertEquals(collector.success_counter, 4) self.assertEquals(collector.request_counter, 6) expected_dict = { 'in24.inetnebr.com': Counter({ '/shuttle/missions/sts-68/news/another.txt': 1, '/shuttle/missions/sts-68/news/sts-68-mcc-05.txt': 5 }) } self.assertEquals(collector.top10hosts_dict, expected_dict)
def test_request_build(self): collector = Collector([ "--since", "2011-11-15 10:00:00", "--until", "2016-06-05 10:00:00" ]) new_request = RequestBuilder(collector.since, collector.until) expected_uri1 = "https://api.stackexchange.com/2.2/answers?pagesize=20&fromdate=" + str( collector.since ) + "&todate=" + str( collector.until ) + "&order=desc&sort=activity&site=stackoverflow&filter=!40nvjHTUxmmwZk05W" self.assertEquals(expected_uri1, new_request.get_answer_uri()) question_id = "44118002" expected_uri2 = "https://api.stackexchange.com/2.2/questions/44118002?order=desc&sort=activity&site=stackoverflow&filter=!--KJ7Yfz(k3H" self.assertEquals(expected_uri2, new_request.get_question_uri(44118002))
def test_collect_success_percent(self): collector = Collector(["--persuccess"], input_file="../data/test_file", output_file="../data/report1.txt") collector.collect_statistics()
def test_top10hosts(self): collector = Collector(["--top10hosts"], input_file="../data/test_file", output_file="../data/report5.txt") collector.collect_statistics()
def test_collect_top10requests(self): collector = Collector(["--top10"], input_file="../data/test_file", output_file="../data/report3.txt") options = collector.parser.options collector.collect_statistics()
def test_collect_fail_percent(self): collector = Collector(["--perfail"], input_file="../data/test_file", output_file="../data/report2.txt") collector.collect_statistics()