def test_fetch_from_date(self): """Test whether a list of questions is returned""" question = read_file('data/stackexchange_question') httpretty.register_uri(httpretty.GET, STACKEXCHANGE_QUESTIONS_URL, body=question, status=200) from_date = datetime.datetime(2016, 4, 5) stack = StackExchange(site="stackoverflow", tagged="python", token="aaa", max_questions=1) questions = [question for question in stack.fetch(from_date=from_date)] self.assertEqual(questions[0]['origin'], 'stackoverflow') self.assertEqual(questions[0]['uuid'], '43953bd75d1d4dbedb457059acb4b79fcf6712a8') self.assertEqual(questions[0]['updated_on'], 1459975066.0) self.assertEqual(questions[0]['category'], 'question') self.assertEqual(questions[0]['tag'], 'stackoverflow') #The date on the questions must be greater than from_date self.assertGreater(questions[0]['updated_on'], 1459900800) data = json.loads(question) self.assertDictEqual(questions[0]['data'], data['items'][0])
def test_fetch_from_non_set_cache(self): """Test if a error is raised when the cache was not set""" stack = StackExchange(site="stackoverflow", tagged="python", token="aaa", max_questions=1) with self.assertRaises(CacheError): _ = [cache_question for cache_question in stack.fetch_from_cache()]
def test_fetch_from_cache(self): """ Test whether a list of questions is returned from cache """ question = read_file('data/stackexchange_question') httpretty.register_uri(httpretty.GET, STACKEXCHANGE_QUESTIONS_URL, body=question, status=200) # First, we fetch the bugs from the server, storing them # in a cache cache = Cache(self.tmp_path) stack = StackExchange(site="stackoverflow", tagged="python", token="aaa", max_questions=1, cache=cache) questions = [question for question in stack.fetch(from_date=None)] del questions[0]['timestamp'] # Now, we get the bugs from the cache. # The contents should be the same and there won't be # any new request to the server cache_questions = [ cache_question for cache_question in stack.fetch_from_cache() ] del cache_questions[0]['timestamp'] self.assertEqual(cache_questions, questions)
def test_fetch_from_date(self): """Test whether a list of questions is returned""" question = read_file('data/stackexchange_question') httpretty.register_uri(httpretty.GET, STACKEXCHANGE_QUESTIONS_URL, body=question, status=200) expected = { "origin": "stackoverflow", "perceval_version": "0.1.0", "backend_name": "StackExchange", "backend_version": "0.2.0" } #unixtime = 1459900800 from_date = datetime.datetime(2016, 4, 5) stack = StackExchange(site="stackoverflow", tagged="python", token="aaa", max_questions=1) questions = [question for question in stack.fetch(from_date=from_date)] #The date on the questions must be greater than from_date self.assertGreater(questions[0]['updated_on'], 1459900800) for key in expected: self.assertEqual(questions[0][key], expected[key]) data = json.loads(question) self.assertDictEqual(questions[0]['data'], data['items'][0])
def test_initialization(self): """Test whether attributes are initializated""" stack = StackExchange(site='stackoverflow', tagged='python', max_questions=1, tag='test') self.assertEqual(stack.site, 'stackoverflow') self.assertEqual(stack.tagged, 'python') self.assertEqual(stack.max_questions, 1) self.assertEqual(stack.origin, 'stackoverflow') self.assertEqual(stack.tag, 'test') # When tag is empty or None it will be set to # the value in site stack = StackExchange(site='stackoverflow') self.assertEqual(stack.site, 'stackoverflow') self.assertEqual(stack.origin, 'stackoverflow') self.assertEqual(stack.tag, 'stackoverflow') stack = StackExchange(site='stackoverflow', tag='') self.assertEqual(stack.site, 'stackoverflow') self.assertEqual(stack.origin, 'stackoverflow') self.assertEqual(stack.tag, 'stackoverflow')
def test_fetch_from_non_set_cache(self): """Test if a error is raised when the cache was not set""" stack = StackExchange(site="stackoverflow", tagged="python", token="aaa", max_questions=1) with self.assertRaises(CacheError): _ = [cache_question for cache_question in stack.fetch_from_cache()]
def test_fetch(self): """Test whether a list of questions is returned""" question = read_file('data/stackexchange_question') httpretty.register_uri(httpretty.GET, STACKEXCHANGE_QUESTIONS_URL, body=question, status=200) stack = StackExchange(site="stackoverflow", tagged="python", token="aaa", max_questions=1) questions = [question for question in stack.fetch(from_date=None)] self.assertEqual(questions[0]['origin'], 'stackoverflow') self.assertEqual(questions[0]['uuid'], '43953bd75d1d4dbedb457059acb4b79fcf6712a8') self.assertEqual(questions[0]['updated_on'], 1459975066.0) self.assertEqual(questions[0]['category'], 'question') self.assertEqual(questions[0]['tag'], 'stackoverflow') data = json.loads(question) self.assertDictEqual(questions[0]['data'], data['items'][0])
def test_fetch_from_empty_cache(self): """Test if there are not any questions returned when the cache is empty""" cache = Cache(self.tmp_path) stack = StackExchange(site="stackoverflow", tagged="python", token="aaa", max_questions=1, cache=cache) cache_questions = [cache_question for cache_question in stack.fetch_from_cache()] self.assertEqual(len(cache_questions), 0)
def test_fetch_from_empty_cache(self): """Test if there are not any questions returned when the cache is empty""" cache = Cache(self.tmp_path) stack = StackExchange(site="stackoverflow", tagged="python", token="aaa", max_questions=1, cache=cache) cache_questions = [cache_question for cache_question in stack.fetch_from_cache()] self.assertEqual(len(cache_questions), 0)
def test_fetch_empty(self): """Test whether a list of questions is returned""" # Required fields question = '{"total": 0, "page_size": 0, "quota_remaining": 0, "quota_max": 0, "has_more": false, "items": []}' httpretty.register_uri(httpretty.GET, STACKEXCHANGE_QUESTIONS_URL, body=question, status=200) stack = StackExchange(site="stackoverflow", tagged="python", token="aaa", max_questions=1) questions = [question for question in stack.fetch(from_date=None)] self.assertEqual(len(questions), 0)
def test_fetch_empty(self): """Test whether a list of questions is returned""" # Required fields question = '{"total": 0, "page_size": 0, "quota_remaining": 0, "quota_max": 0, "has_more": false, "items": []}' httpretty.register_uri(httpretty.GET, STACKEXCHANGE_QUESTIONS_URL, body=question, status=200) stack = StackExchange(site="stackoverflow", tagged="python", token="aaa", max_questions=1) questions = [question for question in stack.fetch(from_date=None)] self.assertEqual(len(questions), 0)
def test_parse_questions(self): """Test question parsing""" raw_parse = read_file('data/stackexchange_question_page') parse = read_file('data/stackexchange_question_parse') parse = json.loads(parse) questions = StackExchange.parse_questions(raw_parse) result = [question for question in questions] self.assertDictEqual(result[0], parse[0]) self.assertDictEqual(result[1], parse[1])
def test_parse_questions(self): """Test question parsing""" raw_parse = read_file('data/stackexchange_question_page') parse = read_file('data/stackexchange_question_parse') parse = json.loads(parse) questions = StackExchange.parse_questions(raw_parse) result = [question for question in questions] self.assertDictEqual(result[0], parse[0]) self.assertDictEqual(result[1], parse[1])
def test_fetch(self): """Test whether a list of questions is returned""" question = read_file('data/stackexchange_question') httpretty.register_uri(httpretty.GET, STACKEXCHANGE_QUESTIONS_URL, body=question, status=200) expected = { "origin": "stackoverflow", "perceval_version": "0.1.0", "backend_name": "StackExchange", "backend_version": "0.2.0" } stack = StackExchange(site="stackoverflow", tagged="python", token="aaa", max_questions=1) questions = [question for question in stack.fetch(from_date=None)] for key in expected: self.assertEqual(questions[0][key], expected[key]) data = json.loads(question) self.assertDictEqual(questions[0]['data'], data['items'][0])
def test_fetch_from_cache(self): """ Test whether a list of questions is returned from cache """ question = read_file('data/stackexchange_question') httpretty.register_uri(httpretty.GET, STACKEXCHANGE_QUESTIONS_URL, body=question, status=200) # First, we fetch the bugs from the server, storing them # in a cache cache = Cache(self.tmp_path) stack = StackExchange(site="stackoverflow", tagged="python", token="aaa", max_questions=1, cache=cache) questions = [question for question in stack.fetch(from_date=None)] del questions[0]['timestamp'] # Now, we get the bugs from the cache. # The contents should be the same and there won't be # any new request to the server cache_questions = [cache_question for cache_question in stack.fetch_from_cache()] del cache_questions[0]['timestamp'] self.assertEqual(cache_questions, questions)
def test_has_resuming(self): """Test if it returns True when has_resuming is called""" self.assertEqual(StackExchange.has_resuming(), True)
def test_has_resuming(self): """Test if it returns True when has_resuming is called""" self.assertEqual(StackExchange.has_resuming(), True)