-
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.
Merge pull request #18 from Muddyblack/15-add-unittests
15 add unittests
- Loading branch information
Showing
6 changed files
with
246 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,11 +46,13 @@ def check_for_event(file): | |
# Check if event by using the filename | ||
logging.debug("Matched. Checking by filename") | ||
date_parts = file_name.split("@") | ||
logging.debug("Date parts: %s", date_parts) | ||
|
||
if len(date_parts) == 2: | ||
start = convert_to_datetime(match.group(1)) | ||
end = convert_to_datetime(match.group(2)) | ||
start = convert_to_datetime(date_parts[0]) | ||
end = convert_to_datetime(date_parts[1]) | ||
elif len(date_parts) == 1: | ||
end = convert_to_datetime(match.group(1)) | ||
end = convert_to_datetime(date_parts[0]) | ||
if start is None and end is None: | ||
logging.debug("Nothing useful in filename") | ||
match = False | ||
|
@@ -79,12 +81,15 @@ def check_for_event(file): | |
): | ||
logging.debug("Event is currently happening") | ||
return True | ||
if (start is not None and end is not None) and (start > datetime.now()): | ||
logging.debug("Start date is in the future: %s", start) | ||
return False | ||
|
||
logging.warning("File %s does not match anything!", file) | ||
return False | ||
|
||
|
||
if __name__ == "__main__": | ||
logging.basicConfig(level=logging.DEBUG) | ||
TEST = check_for_event("./content/OLAdasdF230.12.23eqwe.png") | ||
TEST = check_for_event("./app_data/content/[email protected].png") | ||
print(f"{TEST}") |
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,6 @@ | ||
""" This file is used to add the project path to the sys.path so that the tests can be run from the root directory. """ | ||
import os | ||
import sys | ||
|
||
PARENT_DIRECTORY_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
sys.path.append(PARENT_DIRECTORY_PATH) |
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,49 @@ | ||
# pylint: disable=C | ||
# pylint: disable=unused-import | ||
import setup | ||
import json | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
from googleapiclient.errors import HttpError | ||
import drive_change_detector as dcd | ||
|
||
|
||
class TestDriveChangeDetector(unittest.TestCase): | ||
@patch( | ||
"builtins.open", | ||
new_callable=unittest.mock.mock_open, | ||
read_data='{"key": "value"}', | ||
) | ||
def test_load_state(self, mock_open): | ||
result = dcd.load_state("dummy_path") | ||
self.assertEqual(result, {"key": "value"}) | ||
mock_open.assert_called_once_with("dummy_path", "r", encoding="utf-8") | ||
|
||
@patch("builtins.open", new_callable=unittest.mock.mock_open) | ||
@patch("json.dump") | ||
def test_save_state(self, mock_dump, mock_open): | ||
dcd.save_state({"key": "value"}, "dummy_path") | ||
mock_open.assert_called_once_with("dummy_path", "w", encoding="utf-8") | ||
mock_dump.assert_called_once_with({"key": "value"}, mock_open.return_value) | ||
|
||
@patch("drive_change_detector.load_state", return_value={}) | ||
@patch("drive_change_detector.save_state") | ||
def test_get_changes(self, mock_save_state, mock_load_state): | ||
mock_service = MagicMock() | ||
mock_service.files().list().execute.return_value = { | ||
"files": [{"id": "file1", "name": "file1", "createdTime": "time1"}] | ||
} | ||
|
||
new_files, deleted_files = dcd.get_changes(mock_service, "dummy_dir_id") | ||
|
||
mock_load_state.assert_called_once_with("./state.json") | ||
assert mock_service.files().list.call_count == 2 | ||
mock_save_state.assert_called_once() | ||
self.assertEqual( | ||
new_files, [{"id": "file1", "name": "file1", "createdTime": "time1"}] | ||
) | ||
self.assertEqual(deleted_files, {}) | ||
|
||
|
||
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,51 @@ | ||
""" Unit tests for drive_connector.py """ | ||
# pylint: disable=C | ||
# pylint: disable=unused-import | ||
|
||
import setup | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
from google.auth.exceptions import GoogleAuthError | ||
from googleapiclient.errors import HttpError | ||
|
||
from drive_connector import connect_to_drive | ||
|
||
|
||
class TestDriveConnector(unittest.TestCase): | ||
GOOGLE_API_ACCESS = "dummy.json" | ||
|
||
def setUp(self): | ||
self.mock_credentials = patch( | ||
"drive_connector.service_account.Credentials.from_service_account_file" | ||
).start() | ||
self.mock_build = patch("drive_connector.build").start() | ||
|
||
def tearDown(self): | ||
patch.stopall() | ||
|
||
def test_connect_to_drive_success(self): | ||
self.mock_credentials.return_value = MagicMock() | ||
self.mock_build.return_value = MagicMock() | ||
result = connect_to_drive(self.GOOGLE_API_ACCESS) | ||
self.assertIsNotNone(result) | ||
|
||
def test_connect_to_drive_auth_error(self): | ||
self.mock_credentials.side_effect = GoogleAuthError("Auth error") | ||
result = connect_to_drive(self.GOOGLE_API_ACCESS) | ||
self.assertIsNone(result) | ||
|
||
def test_connect_to_drive_http_error(self): | ||
mock_response = MagicMock() | ||
mock_response.reason = "Http error" | ||
self.mock_build.side_effect = HttpError(mock_response, b"HttpError") | ||
result = connect_to_drive(self.GOOGLE_API_ACCESS) | ||
self.assertIsNone(result) | ||
|
||
def test_connect_to_drive_generic_error(self): | ||
self.mock_build.side_effect = Exception("Generic error") | ||
result = connect_to_drive(self.GOOGLE_API_ACCESS) | ||
self.assertIsNone(result) | ||
|
||
|
||
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,74 @@ | ||
# pylint: disable=C | ||
# pylint: disable=unused-import | ||
import setup | ||
import os | ||
import unittest | ||
from unittest.mock import MagicMock, patch | ||
from googleapiclient.errors import HttpError | ||
import drive_downloader | ||
|
||
|
||
class TestDownloadFile(unittest.TestCase): | ||
def setUp(self): | ||
self.service = MagicMock() | ||
self.file_item = { | ||
"name": "test_file", | ||
"id": "test_id", | ||
"mimeType": "application/vnd.google-apps.file", | ||
} | ||
self.target_directory = "./temp" | ||
|
||
@patch("drive_downloader.os.path.exists", return_value=False) | ||
@patch("drive_downloader.os.makedirs") | ||
@patch("drive_downloader.io.BytesIO") | ||
@patch("drive_downloader.MediaIoBaseDownload") | ||
@patch("drive_downloader.open", new_callable=unittest.mock.mock_open()) | ||
def test_download_file_for_file( | ||
self, mock_open, mock_download, mock_bytesio, mock_makedirs, mock_exists | ||
): | ||
self.file_item["mimeType"] = "application/vnd.google-apps.file" | ||
mock_request = self.service.files().get_media.return_value | ||
mock_fh = mock_bytesio.return_value | ||
mock_downloader = mock_download.return_value | ||
mock_downloader.next_chunk.side_effect = [ | ||
(MagicMock(), False), | ||
(MagicMock(), True), | ||
] | ||
|
||
drive_downloader.download_file( | ||
self.service, self.file_item, self.target_directory | ||
) | ||
|
||
mock_exists.assert_called_once_with(self.target_directory) | ||
mock_makedirs.assert_called_once_with(self.target_directory) | ||
self.service.files().get_media.assert_called_once_with( | ||
fileId=self.file_item["id"] | ||
) | ||
mock_bytesio.assert_called_once() | ||
mock_download.assert_called_once_with(mock_fh, mock_request) | ||
mock_open.assert_called_once_with( | ||
os.path.join(self.target_directory, self.file_item["name"]), "wb" | ||
) | ||
|
||
@patch("drive_downloader.os.path.exists", return_value=True) | ||
@patch("drive_downloader.os.makedirs") | ||
@patch("drive_downloader.logging.warning") | ||
def test_download_file_http_error(self, mock_warning, mock_makedirs, mock_exists): | ||
self.service.files().get_media.side_effect = HttpError( | ||
resp=MagicMock(), content=b"" | ||
) | ||
|
||
drive_downloader.download_file( | ||
self.service, self.file_item, self.target_directory | ||
) | ||
|
||
mock_exists.assert_called_once_with(self.target_directory) | ||
mock_makedirs.assert_not_called() | ||
self.service.files().get_media.assert_called_once_with( | ||
fileId=self.file_item["id"] | ||
) | ||
mock_warning.assert_called_once() | ||
|
||
|
||
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,57 @@ | ||
# pylint: disable=C | ||
# pylint: disable=unused-import | ||
import setup | ||
import logging | ||
from datetime import datetime | ||
import unittest | ||
from unittest.mock import patch | ||
from show_file_checker import check_for_event, convert_to_datetime | ||
|
||
|
||
class TestShowFileChecker(unittest.TestCase): | ||
def setUp(self): | ||
self.mock_datetime = patch("show_file_checker.datetime").start() | ||
self.mock_read_metadata = patch("show_file_checker.read_metadata").start() | ||
self.mock_datetime.now.return_value = datetime(2022, 2, 2) | ||
self.mock_datetime.strptime.side_effect = datetime.strptime | ||
|
||
def tearDown(self): | ||
patch.stopall() | ||
|
||
def check_event(self, filename, metadata, expected_result): | ||
self.mock_read_metadata.return_value = metadata | ||
self.assertEqual(check_for_event(filename), expected_result) | ||
|
||
def test_check_for_event(self): | ||
self.check_event("./content/03_03_2022.png", {}, True) | ||
self.check_event("./content/03_03_2021.png", {}, False) | ||
self.check_event("./content/[email protected]", {}, False) | ||
self.check_event("./content/[email protected]", {}, False) | ||
self.check_event( | ||
"./content/OLA.png", | ||
{"STARTDATE": "01_01_2022", "ENDDATE": "04_04_2022"}, | ||
True, | ||
) | ||
self.check_event( | ||
"./content/OLA.png", | ||
{"STARTDATE": "01_01_2021", "ENDDATE": "01_01_2021"}, | ||
False, | ||
) | ||
self.check_event( | ||
"./content/OLA.png", | ||
{"STARTDATE": "01_01_2023", "ENDDATE": "01_01_2023"}, | ||
False, | ||
) | ||
|
||
def test_convert_to_datetime(self): | ||
self.assertEqual(convert_to_datetime("01_01_2022"), datetime(2022, 1, 1)) | ||
self.assertEqual(convert_to_datetime("01.01.2022"), datetime(2022, 1, 1)) | ||
self.assertEqual(convert_to_datetime("01-01-2022"), datetime(2022, 1, 1)) | ||
self.assertIsNone(convert_to_datetime("2022_01_01")) | ||
self.assertIsNone(convert_to_datetime("01-01_2022")) | ||
self.assertIsNone(convert_to_datetime("01_01_202asda2")) | ||
|
||
|
||
if __name__ == "__main__": | ||
logging.basicConfig(level=logging.DEBUG) | ||
unittest.main() |