From 0cf5ad4b797a985b8cc868854c7d6f92b5b2227e Mon Sep 17 00:00:00 2001 From: Thomas Luechtefeld Date: Mon, 15 Apr 2024 09:48:38 -0400 Subject: [PATCH] add tests and fix some function loading issues --- .gitignore | 7 ++++++ docs/documentation.md | 0 setup.py | 2 +- sysrev/__init__.py | 2 +- tests/__init__.py | 0 tests/test_client.py | 54 +++++++++++++++++++++++++++++++++++++++++++ tests/test_utils.py | 32 +++++++++++++++++++++++++ 7 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 docs/documentation.md create mode 100644 tests/__init__.py create mode 100644 tests/test_client.py create mode 100644 tests/test_utils.py diff --git a/.gitignore b/.gitignore index 95c2b78..2874c8d 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,10 @@ .ipynb_checkpoints/ /py3env +__pycache__/ +*.pyc +*.pyo +*.pyd +/dist/ +/build/ +/sysrev.egg-info/ diff --git a/docs/documentation.md b/docs/documentation.md new file mode 100644 index 0000000..e69de29 diff --git a/setup.py b/setup.py index a5b3786..d518691 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name='sysrev', - version='1.2.3', + version='1.2.4', description='get sysrev project data and use the sysrev api', long_description=long_description, long_description_content_type='text/markdown', # Specify the content type here diff --git a/sysrev/__init__.py b/sysrev/__init__.py index 513134d..3ff722b 100644 --- a/sysrev/__init__.py +++ b/sysrev/__init__.py @@ -1 +1 @@ -from .funcs import Client +from .client import Client diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_client.py b/tests/test_client.py new file mode 100644 index 0000000..f74591c --- /dev/null +++ b/tests/test_client.py @@ -0,0 +1,54 @@ +import unittest +from unittest.mock import patch +from sysrev.client import Client + +class TestClient(unittest.TestCase): + def setUp(self): + # Create a Client instance with a fake API key for testing + self.client = Client(api_key='fake_api_key') + + @patch('sysrev.client.requests.get') + def test_get_project_info(self, mock_get): + # Configure the mock to return a JSON response + mock_get.return_value.status_code = 200 + mock_get.return_value.json.return_value = {'project': {'id': 123, 'name': 'Test Project'}} + + # Call the function + response = self.client.get_project_info(project_id=123) + + # Check that the requests.get was called correctly + mock_get.assert_called_once_with( + 'https://www.sysrev.com/api-json/project-info', + headers={'Authorization': 'Bearer fake_api_key'}, + params={'project-id': 123} + ) + + # Verify the response + self.assertEqual(response, {'project': {'id': 123, 'name': 'Test Project'}}) + + @patch('sysrev.client.requests.post') + def test_set_labels(self, mock_post): + # Configure the mock to return a JSON response + mock_post.return_value.status_code = 200 + mock_post.return_value.json.return_value = {'success': True} + + # Dummy data for the test + label_ids = [1, 2] + label_values = ['yes', 'no'] + label_types = ['boolean', 'boolean'] + + # Call the function + response = self.client.set_labels( + project_id=456, article_id=789, label_ids=label_ids, + label_values=label_values, label_types=label_types, + confirm=True, change=False, resolve=False + ) + + # Check that the requests.post was called correctly + mock_post.assert_called_once() + + # Verify the response + self.assertEqual(response, {'success': True}) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..6a220e9 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,32 @@ +import unittest +from sysrev.client import LabelTransformer + +class TestLabelTransformer(unittest.TestCase): + def test_handle_boolean_true(self): + lt = LabelTransformer() + self.assertTrue(lt.handle_boolean('yes')) + + def test_handle_boolean_false(self): + lt = LabelTransformer() + self.assertFalse(lt.handle_boolean('no')) + + def test_handle_boolean_raises(self): + lt = LabelTransformer() + with self.assertRaises(ValueError): + lt.handle_boolean('maybe') + + def test_handle_categorical_or_string_single(self): + lt = LabelTransformer() + self.assertEqual(lt.handle_categorical_or_string('test'), ['test']) + + def test_handle_categorical_or_string_list(self): + lt = LabelTransformer() + self.assertEqual(lt.handle_categorical_or_string(['test', 'test2']), ['test', 'test2']) + + def test_handle_categorical_or_string_raises(self): + lt = LabelTransformer() + with self.assertRaises(ValueError): + lt.handle_categorical_or_string(['test', 123]) + +if __name__ == '__main__': + unittest.main()