def test_init(self, mock_os): # Save directory exists; 'mkdir' NOT called mock_os.path.join.return_value = 'joined-path' mock_os.path.exists.return_value = True obj = hack.PostCollector(self.base_url) self.assertEqual(obj.title, 'url') self.assertFalse(mock_os.mkdir.called, "Directory creation method called") # Save directory doesn't exists; 'mkdir' called mock_os.path.join.return_value = 'joined-path' mock_os.path.exists.return_value = False hack.PostCollector(self.base_url) mock_os.mkdir.assert_called_with('joined-path')
def test_scrap_comments_for_range_of_post_pages(self, mocked_ripper): """Test length and type of object returned""" obj = hack.PostCollector(self.base_url) page_range = obj.scrap_comments_for_range_of_post_pages(0, 3, False) self.assertIsInstance(page_range, types.GeneratorType) self.assertEqual(len(list(page_range)), 4) # Set maximum page to 3 obj.max_page = mock.MagicMock(return_value=3) page_range = obj.scrap_comments_for_range_of_post_pages(0, 0, True) self.assertEqual(len(list(page_range)), 3)
def test_scrap_comment_for_single_page(self, mocked_ripper): """Test return object type""" obj = hack.PostCollector(self.base_url) single_page = obj._scrap_comment_for_single_page("page-url") mocked_ripper.assert_called() self.assertIsInstance(single_page, OrderedDict)
def test_max_page(self, mocked_requests): obj = hack.PostCollector(self.base_url) obj._check_if_url_exists_and_is_valid = mock.MagicMock( return_value=False) self.assertEqual(obj.max_page(), 0)
def test_url_checker(self, mocked_requests): check = hack.PostCollector( self.base_url)._check_if_url_exists_and_is_valid('whatever-url') # Assert that requests head method was called during function execution mocked_requests.head.assert_called_with("whatever-url") self.assertIsInstance(check, bool)