Skip to content

Commit

Permalink
finished first draft of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ttimbers committed Nov 24, 2023
1 parent c08b409 commit d18b02a
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@

# Jupyter Notebook
**.ipynb_checkpoints

**__pycache__
src/__pycache__
tests/__pycache__
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ RUN conda install -y \
scikit-learn=1.3.2 \
requests=2.31.0 \
notebook=7.0.6 \
pytest=7.4.3
pytest=7.4.3 \
responses=0.24.1
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ where you launched the container, and then type `docker compose rm`

5. Send a pull request to merge the changes into the `main` branch.

#### Running the tests
Tests are run using the `pytest` command in the root of the project.
More details about the test suite can be found in the
[`tests`](tests) directory.

## License

The Breast Cancer Predictor report contained herein are licensed under the
Expand Down
Binary file removed src/__pycache__/read_zip.cpython-311.pyc
Binary file not shown.
13 changes: 13 additions & 0 deletions src/read_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ def read_zip(url, directory):
None
"""
request = requests.get(url)

# check if URL exists, if not raise an error
if request.status_code != 200:
raise ValueError('The URL provided does not exist.')

# check if the URL points to a zip file, if not raise an error
if request.headers['content-type'] != 'application/zip':
raise ValueError('The URL provided does not point to a zip file.')

# check if the directory exists, if not raise an error
if not os.path.isdir(directory):
raise ValueError('The directory provided does not exist.')

filename_from_url = os.path.basename(url)
path_to_zip_file = os.path.join(directory, filename_from_url)
with open(path_to_zip_file, 'wb') as f:
Expand Down
2 changes: 0 additions & 2 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ If for some reason they go missing from the remote repository,
we can re-run the `generate_test_zip_files.py` script to re-generate them
and then push them to the remote repository.



### Test teardown
`conftest.py` contains the code to delete the files and directories
created by the tests which need to be deleted at the end of the tests.
Binary file not shown.
24 changes: 14 additions & 10 deletions tests/test_read_zip.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import os
import shutil
import responses
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from src.read_zip import read_zip
Expand Down Expand Up @@ -29,6 +30,13 @@
# URL for Case 3 (empty zip file)
url_empty_zip = 'https://github.com/ttimbers/breast_cancer_predictor_py/raw/main/tests/empty.zip'

# mock non-existing URL
@pytest.fixture
def mock_response():
# Mock a response with a non-200 status code
with responses.RequestsMock() as rsps:
rsps.add(responses.GET, 'https://example.com', status=404)
yield

# Tests

Expand Down Expand Up @@ -59,17 +67,13 @@ def test_read_zip_subdir():
if os.path.exists('testss/test_zip_data1/subdir'):
shutil.rmtree('testss/test_zip_data1/subdir')

# test read_zip function throws an error if the zip file is empty
#def test_read_zip_error_on_empty():
# add tests here

# test read_zip function throws an error if the input URL is invalid
# (e.g., points to a non-existent file or a non-zip file)
#def test_read_zip_error_on_invalid_url():
# add tests here
def test_read_zip_error_on_invalid_url(mock_response):
with pytest.raises(ValueError, match='The URL provided does not exist.'):
read_zip('https://example.com', 'tests/test_zip_data1') # add tests here

# test read_zip function throws an error
# if the directory path provided does not exist
#def test_read_zip_error_on_missing_dir():
# add tests here

def test_read_zip_error_on_missing_dir():
with pytest.raises(ValueError, match='The directory provided does not exist.'):
read_zip(url_txt_csv_zip, 'tests/test_zip_data3')

0 comments on commit d18b02a

Please sign in to comment.