def _get_image_search_results_multi_match(self): search_results = ImageSearchResults('test.com', self._get_image_search_settings(), checked_post=Post(post_id='abc123', post_type='image', subreddit='test')) search_results.search_times = ImageSearchTimes() search_results.search_times.total_search_time = 10 search_results.matches.append( ImageSearchMatch( 'test.com', 1, Post(post_id='abc123', created_at=datetime.strptime('2019-01-28 05:20:03', '%Y-%m-%d %H:%M:%S')), 10, 10, 32 ) ) search_results.matches.append( ImageSearchMatch( 'test.com', 1, Post(post_id='123abc', created_at=datetime.strptime('2019-06-28 05:20:03', '%Y-%m-%d %H:%M:%S')), 10, 10, 32 ) ) return search_results
def get_image_search_results_multi_match(): search_results = ImageSearchResults('test.com', get_image_search_settings(), checked_post=Post(post_id='abc123', post_type='image', subreddit='test')) search_results.search_times = ImageSearchTimes() search_results.search_times.total_search_time = 10 search_results.matches.append( ImageSearchMatch( 'test.com', 1, Post(id=1, post_id='1111', created_at=datetime.strptime('2019-01-28 05:20:03', '%Y-%m-%d %H:%M:%S')), 10, 10, 32)) search_results.matches.append( ImageSearchMatch( 'test.com', 1, Post(id=2, post_id='2222', created_at=datetime.strptime('2019-06-28 05:20:03', '%Y-%m-%d %H:%M:%S')), 10, 10, 32)) search_results.matches.append( ImageSearchMatch('test.com', 1, Post(id=3, post_id='3333', title='some normal title'), 10, 0.250, 32)) return search_results
def test_get_closest_image_match__return_closest(self): matches = [] match1 = ImageSearchMatch('test.com', 1, Post(id=1), 3, .077, 32) match2 = ImageSearchMatch('test.com', 1, Post(id=2), 5, .077, 32) match3 = ImageSearchMatch('test.com', 1, Post(id=3), 7, .077, 32) matches.append(match1) matches.append(match2) matches.append(match3) r = get_closest_image_match(matches, check_url=False) self.assertEqual(r, match1)
def test__remove_duplicates_one_dup_remove(self): matches = [ ImageSearchMatch('test.com', 123, Post(id=1), 10, 10, 32), ImageSearchMatch('test.com', 123, Post(id=1), 10, 10, 32), ImageSearchMatch('test.com', 123, Post(id=2), 10, 10, 32) ] dup_svc = DuplicateImageService(Mock(), Mock(), Mock(), config=MagicMock()) r = dup_svc._remove_duplicates(matches) self.assertEqual(2, len(r))
def test_sort_reposts_correct_order(self): match1 = RepostMatch() match2 = RepostMatch() match3 = RepostMatch() post1 = Post(id=1, created_at=datetime.fromtimestamp(1575508228)) post2 = Post(id=2, created_at=datetime.fromtimestamp(1572916228)) post3 = Post(id=3, created_at=datetime.fromtimestamp(1570237828)) match1.post = post1 match2.post = post2 match3.post = post3 matches = [match1, match2, match3] result = sort_reposts(matches) self.assertEqual(3, result[0].post.id)
def submission_to_post(submission: Submission, source: str = 'praw') -> Post: """ Convert a PRAW Submission object into a Post object :param submission: """ #log.debug('Converting submission %s to post', submission.id) post = Post() post.post_id = submission.id post.url = submission.url post.shortlink = submission.__dict__.get('shortlink', None) post.author = submission.author.name if submission.author else None post.created_at = datetime.utcfromtimestamp(submission.created_utc) post.subreddit = submission.subreddit.display_name post.title = submission.title post.perma_link = submission.permalink post.crosspost_parent = submission.__dict__.get('crosspost_parent', None) post.selftext = submission.__dict__.get('selftext', None) post.crosspost_checked = True post.ingested_from = source if submission.is_self: post.post_type = 'text' else: try: post.post_type = submission.__dict__.get('post_hint', None) except (AttributeError, Forbidden) as e: pass return post
def _get_link_search_results_no_match(self): search_times = ImageSearchTimes() search_times.total_search_time = 10 return SearchResults( 'test.com', self._get_search_settings(), checked_post=Post(post_id='abc123', post_type='link', subreddit='test'), search_times=search_times )
def test__should_check_post__already_checked_reject(self): sub_monitor = SubMonitor(MagicMock(), MagicMock(), MagicMock(), MagicMock(), MagicMock(), config=Config(redis_host='dummy')) post = Post(left_comment=True) self.assertFalse(sub_monitor.should_check_post(post, True, True))
def get_link_search_results_matches_match(): search_times = ImageSearchTimes() search_times.total_search_time = 10 search_results = SearchResults('test.com', get_search_settings(), checked_post=Post(post_id='abc123', post_type='link', subreddit='test'), search_times=search_times) search_results.matches.append( SearchMatch( 'test.com', Post(post_id='123abc', created_at=datetime.strptime('2019-06-28 05:20:03', '%Y-%m-%d %H:%M:%S')), )) return search_results
def get_image_search_results_no_match(): search_results = ImageSearchResults('test.com', get_image_search_settings(), checked_post=Post(post_id='abc123', post_type='image', subreddit='test')) search_results.search_times = ImageSearchTimes() search_results.search_times.total_search_time = 10 return search_results
def test__should_check_post__title_filter_accept(self): sub_monitor = SubMonitor(MagicMock(), MagicMock(), MagicMock(), MagicMock(), MagicMock(), config=Config(redis_host='dummy', supported_post_types=['image'])) post = Post(left_comment=False, post_type='image', title='some post') self.assertTrue(sub_monitor.should_check_post(post, True, True))
def test_build_default_comment__image_oc_all_enabled_close_match(self): response_builder = ResponseBuilder(MagicMock()) search_results = self._get_image_search_results_no_match() search_results.closest_match = ImageSearchMatch('test.com', 1, Post(post_id='abc123', created_at=datetime.strptime( '2019-01-28 05:20:03', '%Y-%m-%d %H:%M:%S')), 5, 3, 32) result = response_builder.build_default_comment(search_results, signature=True, stats=True, search_link=True, search_settings=True) self.assertEqual(IMAGE_OC_ALL_ENABLED_ALL_ENABLED_NO_MEME, result)
def test_get_first_active_match(self): def get_dummy_res(url, **kwargs): if url == 'www.bad.com': return Mock(status_code=400) else: return Mock(status_code=200) with mock.patch( 'redditrepostsleuth.core.util.repost_helpers.requests.head' ) as mock_head: mock_head.side_effect = get_dummy_res matches = [ SearchMatch('www.dummy.com', Post(id=1, url='www.bad.com')), SearchMatch('www.dummy.com', Post(id=2, url='www.bad.com')), SearchMatch('www.dummy.com', Post(id=3, url='www.good.com')), SearchMatch('www.dummy.com', Post(id=4, url='www.good.com')), ] r = get_first_active_match(matches) self.assertEqual(3, r.post.id)
def test__should_check_post__reject_crosspost(self): sub_monitor = SubMonitor(MagicMock(), MagicMock(), MagicMock(), MagicMock(), MagicMock(), config=Config(redis_host='dummy', supported_post_types=['image'])) post = Post(left_comment=False, post_type='image', crosspost_parent='dkjlsd') self.assertFalse(sub_monitor.should_check_post(post, True, True))
def test__get_image_search_match_from_index_result_valid_post_no_dhash( self): with mock.patch.object(DuplicateImageService, '_get_post_from_index_id') as dup: dup.return_value = Post(id=456) dup_svc = DuplicateImageService(Mock(), Mock(), Mock(), config=MagicMock()) r = dup_svc._get_image_search_match_from_index_result( { 'id': 123, 'distance': .123 }, 'test.com', '40bec6703e3f3c2b0fc491a1c0c16cff273f00c00c020ff91b6807cc060c0014' ) self.assertIsNone(r)
def pushshift_to_post(submission: Dict, source: str = 'pushshift') -> Post: post = Post() post.post_id = submission.get('id', None) post.url = submission.get('url', None) post.shortlink = submission.get('shortlink', None) post.author = submission.get('author', None) post.created_at = datetime.utcfromtimestamp( submission.get('created_utc', None)) post.subreddit = submission.get('subreddit', None) post.title = submission.get('title', None) post.perma_link = submission.get('permalink', None) post.crosspost_parent = submission.get('crosspost_parent', None) post.selftext = submission.get('selftext', None) post.crosspost_checked = True post.ingested_from = source post.post_type = get_post_type_pushshift(submission) return post
def return_post_with_id(id): return Post(id=id)
def test_build_image_report_link_positive(self): search_results = ImageSearchResults('test.com', Mock(), checked_post=Post(post_id='abc123')) search_results.matches.append(ImageSearchMatch('test.com', 123, Mock(), 1, 1, 32)) result = build_image_report_link(search_results) expected = "*I'm not perfect, but you can help. Report [ [False Positive](https://www.reddit.com/message/compose/?to=RepostSleuthBot&subject=False%20Positive&message={\"post_id\": \"abc123\", \"meme_template\": null}) ]*" self.assertEqual(expected, result)
def test_filter_search_results_hit_all_filters(self): search_results = get_image_search_results_multi_match() search_results.search_settings.filter_same_author = True search_results.search_settings.filter_crossposts = True search_results.search_settings.only_older_matches = True search_results.search_settings.same_sub = True search_results.search_settings.target_title_match = None search_results.search_settings.max_days_old = 4 search_results.checked_post.author = 'barry' search_results.checked_post.subreddit = 'sub1' search_results.checked_post.post_id = '1111' search_results.checked_post.created_at = datetime.utcfromtimestamp( 1573995250) matches = [] # Dropped by same author matches.append( ImageSearchMatch( 'test.com', 1, Post(id=1, author='barry', post_id='abc123', created_at=datetime.strptime('2019-01-28 05:20:03', '%Y-%m-%d %H:%M:%S')), 10, 10, 32)) # Dropped by crosspost matches.append( ImageSearchMatch( 'test.com', 1, Post(id=2, author='steve', post_id='123abc', crosspost_parent='abc', created_at=datetime.strptime('2019-06-28 05:20:03', '%Y-%m-%d %H:%M:%S')), 10, 10, 32)) # Dropped by only older matches.append( ImageSearchMatch( 'test.com', 1, Post(id=3, author='steve', post_id='3333', title='some normal title', created_at=datetime.utcfromtimestamp(1574081650)), 10, 0.250, 32)) # Dropped by same sub matches.append( ImageSearchMatch( 'test.com', 1, Post(id=4, author='steve', post_id='4444', title='some normal title', subreddit='sub2', created_at=datetime.utcfromtimestamp(1573908850)), 10, 0.250, 32)) matches.append( ImageSearchMatch( 'test.com', 1, Post(id=5, author='steve', post_id='5555', title='some normal title', subreddit='sub1', created_at=datetime.utcfromtimestamp(1573988200)), 10, 0.250, 32)) # Dropped by same post matches.append( ImageSearchMatch( 'test.com', 1, Post(id=6, post_id='1111', title='some normal title', subreddit='sub1', created_at=datetime.utcfromtimestamp(1573908850)), 10, 0.250, 32)) matches.append( ImageSearchMatch( 'test.com', 1, Post(id=7, post_id='6666', title='some normal title', subreddit='sub1', created_at=datetime.utcfromtimestamp(1573908850)), 10, 0.250, 32)) search_results.matches = matches with patch('redditrepostsleuth.core.util.repost_filters.datetime' ) as mock_date: mock_date.utcnow.return_value = datetime.utcfromtimestamp( 1574360460) r = filter_search_results(search_results) self.assertEqual(1, len(search_results.matches)) self.assertEqual('5555', r.matches[0].post.post_id) print('')
def test_searched_post_str_valid_count(self): post = Post(post_type='image') r = searched_post_str(post, 10) expected = '**Searched Images:** 10' self.assertEqual(expected, r)
def test_searched_post_str_formatting(self): post = Post(post_type='image') r = searched_post_str(post, 1000000) expected = '**Searched Images:** 1,000,000' self.assertEqual(expected, r)
def test_searched_post_str_unknowntype_valid_count(self): post = Post(post_type='video') r = searched_post_str(post, 10) expected = '**Searched:** 10' self.assertEqual(expected, r)
def test_searched_post_str_link_valid_count(self): post = Post(post_type='link') r = searched_post_str(post, 10) expected = '**Searched Links:** 10' self.assertEqual(expected, r)