Skip to content

Commit

Permalink
add tests and fix some function loading issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlue committed Apr 15, 2024
1 parent 51c5118 commit 0cf5ad4
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@
.ipynb_checkpoints/

/py3env
__pycache__/
*.pyc
*.pyo
*.pyd
/dist/
/build/
/sysrev.egg-info/
Empty file added docs/documentation.md
Empty file.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sysrev/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .funcs import Client
from .client import Client
Empty file added tests/__init__.py
Empty file.
54 changes: 54 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -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()
32 changes: 32 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 0cf5ad4

Please sign in to comment.