Skip to content

Commit

Permalink
unpack data function and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sparrow0hawk committed Jan 28, 2021
1 parent f2aa15e commit 3fec9f5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
14 changes: 13 additions & 1 deletion microsim/utilities.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Contains some useful utility functionality
import os
import requests
import tarfile
import pandas as pd
from typing import List

Expand Down Expand Up @@ -68,4 +69,15 @@ def download_data(url="https://example0blob0store.blob.core.windows.net/test1/de

if response.status_code == 200:
with open(target_path, 'wb') as f:
f.write(response.raw.read())
f.write(response.raw.read())

def unpack_data(archive : str):
"""unpack tar data archive
Args:
archive (str): A string directory path to archive file using
"""

tar_file = tarfile.open(archive)

tar_file.extractall(".")
15 changes: 14 additions & 1 deletion tests/test_utilities.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import unittest
from unittest.mock import patch, mock_open
from microsim.utilities import download_data
from microsim.utilities import download_data, unpack_data

class TestDownloadData(unittest.TestCase):

Expand All @@ -24,5 +24,18 @@ def test_download_fail(self):
with self.assertRaises(Exception):
download_data(url="not_a_url")

@patch("microsim.utilities.tarfile")
def test_unpack_data(self, mock_tar):
"""
A test of the unpack_data function using mocks to check tarfile functions are called
"""

unpack_data("example_tar")

mock_tar.open.assert_called_with("example_tar")
mock_tar.open().extractall.assert_called_with(".")



if __name__ == '__main__':
unittest.main()

0 comments on commit 3fec9f5

Please sign in to comment.