-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented unit tests and configured CI/CD workflow & post-installat…
…ion checks
- Loading branch information
1 parent
0742021
commit e3ce551
Showing
13 changed files
with
173 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
LICENCE | ||
README.md | ||
pyproject.toml | ||
setup.py | ||
MetaDataScraper/FacebookScraper.py | ||
MetaDataScraper/__init__.py | ||
MetaDataScraper.egg-info/PKG-INFO | ||
MetaDataScraper.egg-info/SOURCES.txt | ||
MetaDataScraper.egg-info/dependency_links.txt | ||
MetaDataScraper.egg-info/requires.txt | ||
MetaDataScraper.egg-info/top_level.txt | ||
MetaDataScraper.egg-info/top_level.txt | ||
tests/test_logged_in_scraper.py | ||
tests/test_loginless_scraper.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
selenium==4.1.0 | ||
webdriver-manager==4.0.1 | ||
selenium==4.23.0 | ||
webdriver-manager==4.0.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import subprocess | ||
import sys | ||
from setuptools import setup | ||
from setuptools.command.install import install | ||
|
||
class PostInstallCommand(install): | ||
def run(self): | ||
install.run(self) | ||
subprocess.check_call([sys.executable, '-m', 'pytest', 'tests']) | ||
|
||
setup(cmdclass={'install': PostInstallCommand}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
from MetaDataScraper.FacebookScraper import LoggedInScraper | ||
|
||
class TestLoggedInScraper(unittest.TestCase): | ||
def setUp(self): | ||
self.page_id = "test_page_id" | ||
self.email = "test@example.com" | ||
self.password = "password" | ||
self.scraper = LoggedInScraper(self.page_id, self.email, self.password) | ||
|
||
@patch('MetaDataScraper.FacebookScraper.webdriver.Chrome') | ||
@patch('MetaDataScraper.FacebookScraper.ChromeDriverManager') | ||
def test_setup_driver(self, MockChromeDriverManager, MockChrome): | ||
mock_service = MagicMock() | ||
MockChromeDriverManager.return_value.install.return_value = mock_service | ||
mock_driver = MagicMock() | ||
MockChrome.return_value = mock_driver | ||
self.scraper._LoggedInScraper__setup_driver() | ||
|
||
MockChrome.assert_called_once() | ||
MockChromeDriverManager.assert_called_once() | ||
self.assertIsNotNone(self.scraper.driver) | ||
self.assertEqual(self.scraper.driver, mock_driver) | ||
|
||
@patch('MetaDataScraper.FacebookScraper.webdriver.Chrome') | ||
@patch('MetaDataScraper.FacebookScraper.ChromeDriverManager') | ||
def test_login(self, MockChromeDriverManager, MockChrome): | ||
mock_service = MagicMock() | ||
MockChromeDriverManager.return_value.install.return_value = mock_service | ||
mock_driver = MagicMock() | ||
MockChrome.return_value = mock_driver | ||
|
||
self.scraper._LoggedInScraper__login = MagicMock() | ||
self.scraper._LoggedInScraper__login.side_effect = None | ||
|
||
try: | ||
self.scraper._LoggedInScraper__setup_driver() | ||
self.scraper._LoggedInScraper__login() | ||
self.assertTrue(self.scraper._logged_in) | ||
MockChrome.assert_called_once() | ||
MockChromeDriverManager.assert_called_once() | ||
self.assertIsNotNone(self.scraper.driver) | ||
except Exception as e: | ||
self.assertFalse(self.scraper._logged_in) | ||
|
||
@patch.object(LoggedInScraper, '_LoggedInScraper__setup_driver') | ||
@patch.object(LoggedInScraper, '_LoggedInScraper__login') | ||
@patch.object(LoggedInScraper, '_LoggedInScraper__extract_followers_count') | ||
@patch.object(LoggedInScraper, '_LoggedInScraper__extract_post_details') | ||
def test_scrape(self, mock_extract_post_details, mock_extract_followers_count, mock_login, mock_setup_driver): | ||
# Set up mocks | ||
mock_login.return_value = None | ||
mock_setup_driver.return_value = None | ||
mock_extract_post_details.return_value = None | ||
mock_extract_followers_count.return_value = None | ||
|
||
mock_driver = MagicMock() | ||
self.scraper.driver = mock_driver | ||
|
||
result = self.scraper.scrape() | ||
|
||
self.assertIsInstance(result, dict) | ||
self.assertIn('followers', result) | ||
self.assertIn('post_texts', result) | ||
self.assertIn('post_likes', result) | ||
self.assertIn('post_shares', result) | ||
self.assertIn('is_video', result) | ||
self.assertIn('video_links', result) | ||
|
||
def test_initialization(self): | ||
self.assertEqual(self.scraper.page_id, self.page_id) | ||
self.assertEqual(self.scraper.email, self.email) | ||
self.assertEqual(self.scraper.password, self.password) | ||
self.assertIsNone(self.scraper.driver) | ||
self.assertIsNone(self.scraper.followers) | ||
self.assertEqual(self.scraper.post_texts, []) | ||
self.assertEqual(self.scraper.post_likes, []) | ||
self.assertEqual(self.scraper.post_shares, []) | ||
self.assertEqual(self.scraper.is_video, []) | ||
self.assertEqual(self.scraper.video_links, []) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
from MetaDataScraper.FacebookScraper import LoginlessScraper | ||
|
||
class TestLoginlessScraper(unittest.TestCase): | ||
def setUp(self): | ||
self.page_id = "test_page_id" | ||
self.scraper = LoginlessScraper(self.page_id) | ||
|
||
@patch.object(LoginlessScraper, '_LoginlessScraper__setup_driver') | ||
@patch.object(LoginlessScraper, '_LoginlessScraper__navigate_to_page') | ||
@patch.object(LoginlessScraper, '_LoginlessScraper__check_page_accessibility') | ||
@patch.object(LoginlessScraper, '_LoginlessScraper__extract_followers_count') | ||
@patch.object(LoginlessScraper, '_LoginlessScraper__scroll_to_top') | ||
@patch.object(LoginlessScraper, '_LoginlessScraper__get_xpath_constructor') | ||
@patch.object(LoginlessScraper, '_LoginlessScraper__extract_post_details') | ||
def test_scrape(self, mock_extract_post_details, mock_get_xpath, mock_scroll, mock_extract_followers, mock_check_access, mock_navigate, mock_setup): | ||
mock_extract_post_details.return_value = None | ||
mock_get_xpath.return_value = None | ||
mock_scroll.return_value = None | ||
mock_extract_followers.return_value = None | ||
mock_check_access.return_value = None | ||
mock_navigate.return_value = None | ||
mock_setup.return_value = None | ||
|
||
result = self.scraper.scrape() | ||
|
||
self.assertIsInstance(result, dict) | ||
self.assertIn('followers', result) | ||
self.assertIn('post_texts', result) | ||
self.assertIn('post_likes', result) | ||
self.assertIn('post_shares', result) | ||
self.assertIn('is_video', result) | ||
self.assertIn('video_links', result) | ||
|
||
@patch('MetaDataScraper.FacebookScraper.webdriver.Chrome') | ||
@patch('MetaDataScraper.FacebookScraper.ChromeDriverManager') | ||
def test_setup_driver(self, MockChromeDriverManager, MockChrome): | ||
mock_service = MagicMock() | ||
MockChromeDriverManager.return_value.install.return_value = mock_service | ||
self.scraper._LoginlessScraper__setup_driver() | ||
|
||
MockChrome.assert_called_once() | ||
MockChromeDriverManager.assert_called_once() | ||
self.assertIsNotNone(self.scraper.driver) | ||
|
||
def test_initialization(self): | ||
self.assertEqual(self.scraper.page_id, self.page_id) | ||
self.assertIsNone(self.scraper.driver) | ||
self.assertIsNone(self.scraper.followers) | ||
self.assertEqual(self.scraper.post_texts, []) | ||
self.assertEqual(self.scraper.post_likes, []) | ||
self.assertEqual(self.scraper.post_shares, []) | ||
self.assertEqual(self.scraper.is_video, []) | ||
self.assertEqual(self.scraper.video_links, []) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |