Skip to content

Commit

Permalink
Apply Black formatting and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
glrs committed Oct 4, 2024
1 parent 982e6e7 commit bae746a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 44 deletions.
56 changes: 34 additions & 22 deletions tests/test_couch_utils.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,65 @@
import unittest
import asyncio
import unittest
from unittest.mock import MagicMock, mock_open, patch

from lib.core_utils.couch_utils import (
couch_login,
get_db_changes,
get_last_processed_seq,
has_required_fields,
save_last_processed_seq,
)

from pathlib import Path
from unittest.mock import patch, MagicMock, mock_open
from lib.core_utils.couch_utils import couch_login, get_db_changes, save_last_processed_seq, get_last_processed_seq, has_required_fields

class TestCouchUtils(unittest.TestCase):

def test_save_last_processed_seq(self):
# Test that the last processed sequence number is saved correctly
# This will require mocking the file write operation
with unittest.mock.patch('builtins.open', unittest.mock.mock_open()) as mock_file:
save_last_processed_seq('12345')
mock_file().write.assert_called_once_with('12345')
with unittest.mock.patch(
"builtins.open", unittest.mock.mock_open()
) as mock_file:
save_last_processed_seq("12345")
mock_file().write.assert_called_once_with("12345")

def test_get_last_processed_seq(self):
# Mock the file read operation
mock_open_instance = mock_open(read_data='12345')
with patch('builtins.open', mock_open_instance):
mock_open_instance = mock_open(read_data="12345")
with patch("builtins.open", mock_open_instance):
# Call the function
seq = get_last_processed_seq()
# Assert the function returned the correct sequence number
self.assertEqual(seq, '12345')
self.assertEqual(seq, "12345")

def test_has_required_fields(self):
# Test that the function correctly identifies when a document has all required fields
doc = {'field1': 'value1', 'field2': 'value2'}
required_fields = ['field1', 'field2']
doc = {"field1": "value1", "field2": "value2"}
required_fields = ["field1", "field2"]
self.assertTrue(has_required_fields(doc, required_fields))

# Test that the function correctly identifies when a document is missing a required field
doc = {'field1': 'value1'}
required_fields = ['field1', 'field2']
doc = {"field1": "value1"}
required_fields = ["field1", "field2"]
self.assertFalse(has_required_fields(doc, required_fields))

@patch('lib.utils.couch_utils.Ygg')
@patch('lib.utils.couch_utils.couchdb.Server')
@patch("lib.utils.couch_utils.Ygg")
@patch("lib.utils.couch_utils.couchdb.Server")
def test_couch_login(self, mock_server, mock_ygg):
# Mock the environment variables
mock_ygg.env_variable.side_effect = ['username', 'password']
mock_ygg.env_variable.side_effect = ["username", "password"]
# Mock the server instance
mock_server_instance = MagicMock()
mock_server.return_value = mock_server_instance
# Call the function
server = couch_login()
# Assert the server instance was called with the correct URL
mock_server.assert_called_once_with('http://username:[email protected]:5984')
mock_server.assert_called_once_with(
"http://username:[email protected]:5984"
)
# Assert the function returned the server instance
self.assertEqual(server, mock_server_instance)

@patch('lib.utils.couch_utils.couch_login')
@patch("lib.utils.couch_utils.couch_login")
def test_get_db_changes(self, mock_couch_login):
# Mock the CouchDB server and database
mock_couch = MagicMock()
Expand All @@ -59,16 +69,18 @@ def test_get_db_changes(self, mock_couch_login):
mock_couch_login.return_value = mock_couch

# Call the function and get the generator
changes_generator = get_db_changes(mock_couch['projects'])
changes_generator = get_db_changes(mock_couch["projects"])

# Define an async function to iterate over the generator
async def iterate_over_generator():
async for _ in changes_generator:
pass

# Run the async function
asyncio.run(iterate_over_generator())
# Assert the database's changes feed was accessed
mock_db.changes.assert_called_once()

if __name__ == '__main__':
unittest.main()

if __name__ == "__main__":
unittest.main()
38 changes: 26 additions & 12 deletions tests/test_couchdb_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,27 @@
# unittest.main()

import unittest
from unittest.mock import MagicMock, patch
from unittest.mock import patch

from trashcan.couchdb_feed import fetch_data_from_couchdb


class TestCouchDBFeed(unittest.TestCase):

@patch('lib.couchdb_feed.get_db_changes')
@patch('lib.couchdb_feed.couch_login')
@patch('lib.couchdb_feed.ConfigLoader')
async def test_fetch_data_from_couchdb(self, mock_ConfigLoader, mock_couch_login, mock_get_db_changes):
@patch("lib.couchdb_feed.get_db_changes")
@patch("lib.couchdb_feed.couch_login")
@patch("lib.couchdb_feed.ConfigLoader")
async def test_fetch_data_from_couchdb(
self, mock_ConfigLoader, mock_couch_login, mock_get_db_changes
):
# Mock the dependencies
mock_couch_login.return_value = {'projects': 'mock_db'}
mock_get_db_changes.return_value = iter([{'details': {'library_construction_method': 'SmartSeq 3'}, 'seq': 'seq1'}])
mock_ConfigLoader.return_value.load_config.return_value = {'SmartSeq 3': {'module': 'module1'}}
mock_couch_login.return_value = {"projects": "mock_db"}
mock_get_db_changes.return_value = iter(
[{"details": {"library_construction_method": "SmartSeq 3"}, "seq": "seq1"}]
)
mock_ConfigLoader.return_value.load_config.return_value = {
"SmartSeq 3": {"module": "module1"}
}

# Call the function
result = await fetch_data_from_couchdb()
Expand All @@ -52,9 +60,15 @@ async def test_fetch_data_from_couchdb(self, mock_ConfigLoader, mock_couch_login

# Assert the dependencies were called with the correct arguments
mock_couch_login.assert_called_once()
mock_get_db_changes.assert_called_once_with('mock_db', last_processed_seq='72679-g1AAAACheJzLYWBgYMpgTmEQTM4vTc5ISXIwNDLXMwBCwxyQVCJDUv3___-zMpiTGBgak3OBYuxpKWaJBgaG2PTgMSmPBUgyNACp_3ADZ7WADTQwTDYzsDTDpjULAC7GKhU')
mock_get_db_changes.assert_called_once_with(
"mock_db",
last_processed_seq="72679-g1AAAACheJzLYWBgYMpgTmEQTM4vTc5ISXIwNDLXMwBCwxyQVCJDUv3___-zMpiTGBgak3OBYuxpKWaJBgaG2PTgMSmPBUgyNACp_3ADZ7WADTQwTDYzsDTDpjULAC7GKhU",
)
mock_ConfigLoader.assert_called_once()
mock_ConfigLoader.return_value.load_config.assert_called_once_with("module_registry.json")
mock_ConfigLoader.return_value.load_config.assert_called_once_with(
"module_registry.json"
)


if __name__ == '__main__':
unittest.main()
if __name__ == "__main__":
unittest.main()
27 changes: 17 additions & 10 deletions tests/test_logging_utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import unittest
from unittest.mock import patch, MagicMock
from unittest.mock import MagicMock, patch

from lib.core_utils.logging_utils import configure_logging


class TestConfigureLogging(unittest.TestCase):
@patch('lib.utils.logging_utils.logging')
@patch('lib.utils.logging_utils.datetime')
@patch('lib.utils.logging_utils.Path')
@patch("lib.utils.logging_utils.logging")
@patch("lib.utils.logging_utils.datetime")
@patch("lib.utils.logging_utils.Path")
def test_configure_logging(self, mock_path, mock_datetime, mock_logging):
# Set up the mocks
mock_path.return_value.mkdir.return_value = None
mock_datetime.now.return_value.strftime.return_value = '2022-01-01_00.00.00'
mock_datetime.now.return_value.strftime.return_value = "2022-01-01_00.00.00"
mock_logging.DEBUG = 10
mock_logging.INFO = 20
mock_logging.getLogger.return_value = MagicMock()
Expand All @@ -18,10 +20,14 @@ def test_configure_logging(self, mock_path, mock_datetime, mock_logging):
configure_logging(debug=True)

# Assert the mocks were called correctly
mock_path.assert_called_once_with('yggdrasil_workspace/logs')
mock_path.return_value.mkdir.assert_called_once_with(parents=True, exist_ok=True)
mock_path.assert_called_once_with("yggdrasil_workspace/logs")
mock_path.return_value.mkdir.assert_called_once_with(
parents=True, exist_ok=True
)
mock_datetime.now.assert_called_once()
mock_datetime.now.return_value.strftime.assert_called_once_with("%Y-%m-%d_%H.%M.%S")
mock_datetime.now.return_value.strftime.assert_called_once_with(
"%Y-%m-%d_%H.%M.%S"
)
mock_logging.getLogger.assert_called_once()
mock_logging.getLogger.return_value.setLevel.assert_called_once_with(10)
mock_logging.getLogger.return_value.addHandler.assert_called()
Expand All @@ -32,5 +38,6 @@ def test_configure_logging(self, mock_path, mock_datetime, mock_logging):
# Assert the logger's level was set to INFO
mock_logging.getLogger.return_value.setLevel.assert_called_with(20)

if __name__ == '__main__':
unittest.main()

if __name__ == "__main__":
unittest.main()

0 comments on commit bae746a

Please sign in to comment.