Skip to content

Commit

Permalink
Updating download data tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-George-T committed Oct 26, 2023
1 parent 3e1bec3 commit 7a64fd8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/download_data.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""
Function to download and ingest the data file
"""
import os
import requests

DEFAULT_FILE_URL = "https://archive.ics.uci.edu/static/public/352/online+retail.zip"

# Set the root directory variable using a relative path
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))

def ingest_data(file_url=DEFAULT_FILE_URL):
"""
Function to download file from URL
Expand All @@ -17,13 +21,13 @@ def ingest_data(file_url=DEFAULT_FILE_URL):
response = requests.get(file_url, timeout=30)

# Path to store the zipfile
zipfile_path="data/data.zip"
zipfile_path=os.path.join(ROOT_DIR, 'data','data.zip')
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Save file to data
with open(zipfile_path, "wb") as file:
file.write(response.content)
print("File downloaded successfully. Zip file available under data folder")
print(f"File downloaded successfully. Zip file available under {zipfile_path}")
else:
print(f"Failed to download the file. Status code: {response.status_code}")

Expand Down
30 changes: 23 additions & 7 deletions test/test_download_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""
Tests for downloda_data.py
"""
import os
import requests
import requests_mock
from src import download_data

DEFAULT_FILE_URL = "https://archive.ics.uci.edu/static/public/352/online+retail.zip"
Expand All @@ -18,14 +21,27 @@ def test_ingest_data(mocker):
# assert: todo
assert 1 == mock_print.call_count


def test_ingest_data_successful_download(requests_mock):
def test_ingest_data_successful_download():
"""
Test for checking successful download of the file
"""
# Mock the requests.get() method to return a successful response
requests_mock.get(DEFAULT_FILE_URL, text="Test file content", status_code=200)
# Create a session and attach the requests_mock to it
with requests.Session() as session:
adapter = requests_mock.Adapter()
# session.mount('http://', adapter)
session.mount('https://', adapter)

# Set the root directory variable using a relative path
root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))

# Path to store the zipfile
zipfile_path=os.path.join(root_dir, 'data','data.zip')

# Define the mock response
adapter.register_uri('GET', DEFAULT_FILE_URL, text=zipfile_path)

# Call your function that makes the HTTP requests
result = download_data.ingest_data(DEFAULT_FILE_URL) # Replace with your actual function

# Call the function and check if it returns the correct zipfile path
result = download_data.ingest_data(DEFAULT_FILE_URL)
assert result == "data/data.zip"
# Perform assertions
assert result == zipfile_path

0 comments on commit 7a64fd8

Please sign in to comment.