Skip to content

Commit

Permalink
Unit testing (#41)
Browse files Browse the repository at this point in the history
* developed unit testing

* fixed asynchronous errors

* test action script

* fix

* test

* fix

* test

* took out redundant workflow

* took out debug

* secret test

* attempted fix

* test

* unmocking

* fix
  • Loading branch information
mrzengel authored Jul 23, 2024
1 parent 96ec6e0 commit c447110
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 81 deletions.
75 changes: 0 additions & 75 deletions .github/actions/setup/action.yml

This file was deleted.

10 changes: 10 additions & 0 deletions .github/actions/test/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: "Test"

runs:
using: "composite"
steps:
- name: Run Pytest
env:
CI_ZENODO_API_KEY: ${{ secrets.CI_ZENODO_API_KEY }}
run: pytest zenodo_jupyterlab/server/tests
shell: bash
6 changes: 6 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ jobs:
jupyter server extension enable zenodo_jupyterlab.server
shell: bash

- name: Run Pytest
env:
CI_ZENODO_API_KEY: ${{ secrets.CI_ZENODO_API_KEY }}
run: pytest zenodo_jupyterlab/server/tests
shell: bash


# install_extension:
# runs-on: ubuntu-latest
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
jupyterlab>4,<5
notebook<7
eossr
eossr
pytest
pytest-asyncio
4 changes: 2 additions & 2 deletions zenodo_jupyterlab/server/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from jupyter_server.base.handlers import APIHandler, JupyterHandler
from jupyter_server.utils import url_path_join
import os
from .testConnection import testZenodoConnection
from .testConnection import checkZenodoConnection
from .search import searchRecords, searchCommunities, recordInformation


Expand All @@ -25,7 +25,7 @@ async def post(self):

class ZenodoTestHandler(APIHandler):
async def get(self):
response = await testZenodoConnection()
response = await checkZenodoConnection(sandbox = False)
self.finish({'status': response})

class XSRFTokenHandler(JupyterHandler):
Expand Down
5 changes: 2 additions & 3 deletions zenodo_jupyterlab/server/testConnection.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from eossr.api.zenodo import ZenodoAPI
from eossr.api.zenodo import http_status
import os

#ZenodoHTTPStatus

async def testZenodoConnection():
async def checkZenodoConnection(sandbox: bool):
access_token = os.environ['ZENODO_API_KEY']
z = ZenodoAPI(access_token=access_token)
z = ZenodoAPI(access_token=access_token, sandbox = sandbox)
try:
response = z.query_user_deposits()
return response.status_code
Expand Down
74 changes: 74 additions & 0 deletions zenodo_jupyterlab/server/tests/test_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from unittest.mock import Mock, patch, AsyncMock
from eossr.api.zenodo import search_records, search_communities, get_record
import pytest
from zenodo_jupyterlab.server.search import searchRecords, searchCommunities, recordInformation # Replace `your_module` with the actual module name

@pytest.mark.asyncio
@patch('zenodo_jupyterlab.server.search.search_records')
async def test_searchRecords_success(mock_search_records):
mock_search_records.return_value = [
Mock(id='1', title='Record One', metadata={'publication_date': '2022-01-01', 'resource_type': {'title': 'Dataset'}}),
Mock(id='2', title='Record Two', metadata={'publication_date': '2023-01-01', 'resource_type': {'title': 'Article'}})
]

response = await searchRecords('', 1)
expected_response = [
{'id': '1', 'title': 'Record One', 'date': '2022-01-01', 'resource_type': 'Dataset'},
{'id': '2', 'title': 'Record Two', 'date': '2023-01-01', 'resource_type': 'Article'}
]

assert response == expected_response

@pytest.mark.asyncio
@patch('zenodo_jupyterlab.server.search.search_records')
async def test_searchRecords_failure(mock_search_records):
mock_search_records.side_effect = Exception('API Error')

response = await searchRecords('test', 1)
assert response == ["failed"]

@pytest.mark.asyncio
@patch('zenodo_jupyterlab.server.search.search_communities')
async def test_searchCommunities_success(mock_search_communities):
mock_search_communities.return_value = [
{'id': '1', 'metadata': {'title': 'Community One'}, 'created': '2022-01-01T00:00:00Z'},
{'id': '2', 'metadata': {'title': 'Community Two'}, 'created': '2023-01-01T00:00:00Z'}
]

response = await searchCommunities('test', 1)
expected_response = [
{'id': '1', 'title': 'Community One', 'date': '2022-01-01'},
{'id': '2', 'title': 'Community Two', 'date': '2023-01-01'}
]
assert response == expected_response

@pytest.mark.asyncio
@patch('zenodo_jupyterlab.server.search.search_communities')
async def test_searchCommunities_failure(mock_search_communities):
mock_search_communities.side_effect = Exception('API Error')

response = await searchCommunities('test', 1)
assert response == ["failed"]

@pytest.mark.asyncio
@patch('zenodo_jupyterlab.server.search.get_record')
async def test_recordInformation_success(mock_get_record):
mock_get_record.return_value = AsyncMock(
metadata={'creators': [{'name': 'Author One'}, {'name': 'Author Two'}]},
filelist=['file1.pdf', 'file2.pdf']
)

response = await recordInformation('12345')
expected_response = {
'authors': [{'name': 'Author One'}, {'name': 'Author Two'}],
'filelist': ['file1.pdf', 'file2.pdf']
}
assert response == expected_response

@pytest.mark.asyncio
@patch('zenodo_jupyterlab.server.search.get_record')
async def test_recordInformation_failure(mock_get_record):
mock_get_record.side_effect = Exception('API Error')

response = await recordInformation('12345')
assert response == {'status': 'failed'}
37 changes: 37 additions & 0 deletions zenodo_jupyterlab/server/tests/test_testConnection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pytest
from unittest.mock import patch, Mock
from eossr.api.zenodo import ZenodoAPI
import os
from zenodo_jupyterlab.server.testConnection import checkZenodoConnection

@pytest.mark.asyncio
async def test_zenodo_connection_success():
# Mock the ZenodoAPI instance and its method
""" mock_instance = MockZenodoAPI.return_value
mock_instance.query_user_deposits = Mock()
mock_instance.query_user_deposits.return_value.status_code = 200 """

# Mock the environment variable
with patch.dict(os.environ, {'ZENODO_API_KEY': os.environ['CI_ZENODO_API_KEY']}):
# Call the function to test
status_code = await checkZenodoConnection(sandbox = True)

print(f"Returned status code: {status_code}")

# Assert the expected status code
assert status_code == 200

@pytest.mark.asyncio
async def test_zenodo_connection_failure():
# Mock the ZenodoAPI instance to raise an exception
""" mock_instance = MockZenodoAPI.return_value
mock_instance.query_user_deposits = Mock()
mock_instance.query_user_deposits.side_effect = Exception('Failed') """

# Mock the environment variable
with patch.dict('os.environ', {'ZENODO_API_KEY': 'fake_false_api_key'}):
# Call the function to test
status_code = await checkZenodoConnection(sandbox = True)

# Assert the expected status code
assert status_code == 0

0 comments on commit c447110

Please sign in to comment.