Skip to content

Commit

Permalink
added test for onedrive module
Browse files Browse the repository at this point in the history
  • Loading branch information
giumas committed Oct 20, 2024
1 parent 70b2dcf commit 741b082
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 16 deletions.
20 changes: 15 additions & 5 deletions examples/lib/ex_onedrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@
logger = logging.getLogger(__name__)
set_logging(ns_list=["hyo2.abc2"])

# onedrive_link = r"https://universitysystemnh-my.sharepoint.com/:u:/g/personal/" \
# r"gma72_usnh_edu/EaMqI1w9pplDsqCapeqJYYgBo0LP8CqHnkyXlDKkoHeBLg?e=4MEVzV&download=1"
onedrive_link = r"https://universitysystemnh-my.sharepoint.com/:u:/g/personal/" \
r"gma72_usnh_edu/ET4kv3t8CuBGuyHUqThonvMBMmxWp5f3ZTt08XG_u9COHQ?e=mVEJij&download=1"
r"gma72_usnh_edu/EabgRi9pTtdEvIVDXoEXHOYBH4FBHtN07i_8VkjpAAstYQ?e=Wn7q98&download=1"

root_folder = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)
tp = Testing(root_folder=root_folder)
file_dst = os.path.join(tp.output_data_folder(), "onedrive.zip")
zip_path = os.path.join(tp.output_data_folder(), "test.zip")
unzip_path = os.path.join(tp.output_data_folder(), "test.txt")

if os.path.exists(zip_path):
os.remove(zip_path)
if os.path.exists(unzip_path):
os.remove(unzip_path)

od = OneDrive(show_progress=True, debug_mode=True)
od.get_file(file_src=onedrive_link, file_dst=file_dst, unzip_it=True)
od.get_file(file_src=onedrive_link, file_dst=zip_path, unzip_it=True)

if os.path.exists(unzip_path):
with open(unzip_path) as fid:
text = fid.read().strip()
if text == "test":
logger.debug(text)
12 changes: 3 additions & 9 deletions hyo2/abc2/lib/onedrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

class OneDrive:

def __init__(self, show_progress: bool = False, debug_mode: bool = False, progress: AbstractProgress = None):
def __init__(self, show_progress: bool = False, debug_mode: bool = False, progress: AbstractProgress = None) \
-> None:
if debug_mode:
self.debug_level = 2
else:
Expand All @@ -26,14 +27,7 @@ def __init__(self, show_progress: bool = False, debug_mode: bool = False, progre
else:
self.progress = progress

def get_file(self, file_src: str, file_dst: str, unzip_it: bool = False):
""" Retrieve a file
Args:
file_src: File source
file_dst: File destination
unzip_it: Unzip the retrieved file
"""
def get_file(self, file_src: str, file_dst: str, unzip_it: bool = False) -> None:

file_dst = os.path.abspath(file_dst)
if os.path.exists(file_dst):
Expand Down
2 changes: 0 additions & 2 deletions tests/lib/test_ftp.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
import unittest

from hyo2.abc2.lib.ftp import Ftp
from hyo2.abc2.lib.testing import Testing


class TestABCLibFtp(unittest.TestCase):
Expand Down
39 changes: 39 additions & 0 deletions tests/lib/test_onedrive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
import unittest

from hyo2.abc2.lib.testing import Testing
from hyo2.abc2.lib.onedrive import OneDrive


class TestABCOneDrive(unittest.TestCase):

def test_init(self):
_ = OneDrive()

def test_download_and_unzip(self):
onedrive_link = r"https://universitysystemnh-my.sharepoint.com/:u:/g/personal/" \
r"gma72_usnh_edu/EabgRi9pTtdEvIVDXoEXHOYBH4FBHtN07i_8VkjpAAstYQ?e=Wn7q98&download=1"

root_folder = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)
tp = Testing(root_folder=root_folder)
zip_path = os.path.join(tp.output_data_folder(), "test.zip")
unzip_path = os.path.join(tp.output_data_folder(), "test.txt")

if os.path.exists(zip_path):
os.remove(zip_path)
if os.path.exists(unzip_path):
os.remove(unzip_path)

od = OneDrive(show_progress=True, debug_mode=True)
od.get_file(file_src=onedrive_link, file_dst=zip_path, unzip_it=True)

if os.path.exists(unzip_path):
with open(unzip_path) as fid:
text = fid.read().strip()
self.assertEqual(text, "test")


def suite():
s = unittest.TestSuite()
s.addTests(unittest.TestLoader().loadTestsFromTestCase(TestABCOneDrive))
return s

0 comments on commit 741b082

Please sign in to comment.