From 896df75bf2f59b71f694a326897898958cf2d252 Mon Sep 17 00:00:00 2001 From: Mihai Boldeanu Date: Wed, 13 Sep 2023 11:26:29 +0300 Subject: [PATCH] Moved pytest fixture to the conftest.py --- .gitignore | 7 ++++++ tests/conftest.py | 53 ++++++++++++++++++++++++++++++++++++++++ tests/test_raw_reader.py | 45 ---------------------------------- 3 files changed, 60 insertions(+), 45 deletions(-) create mode 100644 tests/conftest.py diff --git a/.gitignore b/.gitignore index c6604ee..7e0bd1c 100644 --- a/.gitignore +++ b/.gitignore @@ -22,7 +22,11 @@ var/ wheels/ share/python-wheels/ *.egg-info/ +<<<<<<< Updated upstream notebooks +======= +notebooks/ +>>>>>>> Stashed changes .installed.cfg *.egg MANIFEST @@ -165,3 +169,6 @@ test_data/ *.nc *.zarr *.raw +# For MAC stuff +.DS_Store +oceanstream/.DS_Store diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..3fe3d4c --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,53 @@ +import os +from ftplib import FTP + +import pytest + + +current_directory = os.path.dirname(os.path.abspath(__file__)) +TEST_DATA_FOLDER = os.path.join(current_directory, "..", "test_data", "ek60") + +FTP_MAIN = "ftp.bas.ac.uk" +FTP_PARTIAL_PATH = "rapidkrill/ek60/" + + +def download_ftp_directory(ftp, remote_path, local_path): + try: + os.makedirs(local_path, exist_ok=True) + items = ftp.nlst(remote_path) + + for item in items: + local_item_path = os.path.join(local_path, os.path.basename(item)) + if is_directory(ftp, item): + download_ftp_directory(ftp, item, local_item_path) + else: + # Check if the file already exists locally + if not os.path.exists(local_item_path): + with open(local_item_path, "wb") as local_file: + ftp.retrbinary("RETR " + item, local_file.write) + else: + # print(f"File {local_item_path} already exists. Skipping download.") + continue + + except Exception as e: + print(f"Error downloading {remote_path}. Error: {e}") + + +def is_directory(ftp, name): + try: + current = ftp.pwd() + ftp.cwd(name) + ftp.cwd(current) + return True + except: + return False + + +@pytest.fixture(scope="session") +def ftp_data(): + with FTP(FTP_MAIN) as ftp: + ftp.login() # Add credentials if needed: ftp.login(user="username", passwd="password") + download_ftp_directory(ftp, FTP_PARTIAL_PATH, TEST_DATA_FOLDER) + yield TEST_DATA_FOLDER + # Optional: Cleanup after tests are done + # shutil.rmtree(TEST_DATA_FOLDER) \ No newline at end of file diff --git a/tests/test_raw_reader.py b/tests/test_raw_reader.py index 2935d59..eb416ee 100644 --- a/tests/test_raw_reader.py +++ b/tests/test_raw_reader.py @@ -16,51 +16,6 @@ current_directory = os.path.dirname(os.path.abspath(__file__)) TEST_DATA_FOLDER = os.path.join(current_directory, "..", "test_data", "ek60") -FTP_MAIN = "ftp.bas.ac.uk" -FTP_PARTIAL_PATH = "rapidkrill/ek60/" - - -def download_ftp_directory(ftp, remote_path, local_path): - try: - os.makedirs(local_path, exist_ok=True) - items = ftp.nlst(remote_path) - - for item in items: - local_item_path = os.path.join(local_path, os.path.basename(item)) - if is_directory(ftp, item): - download_ftp_directory(ftp, item, local_item_path) - else: - # Check if the file already exists locally - if not os.path.exists(local_item_path): - with open(local_item_path, "wb") as local_file: - ftp.retrbinary("RETR " + item, local_file.write) - else: - # print(f"File {local_item_path} already exists. Skipping download.") - continue - - except Exception as e: - print(f"Error downloading {remote_path}. Error: {e}") - - -def is_directory(ftp, name): - try: - current = ftp.pwd() - ftp.cwd(name) - ftp.cwd(current) - return True - except: - return False - - -@pytest.fixture(scope="session") -def ftp_data(): - with FTP(FTP_MAIN) as ftp: - ftp.login() # Add credentials if needed: ftp.login(user="username", passwd="password") - download_ftp_directory(ftp, FTP_PARTIAL_PATH, TEST_DATA_FOLDER) - yield TEST_DATA_FOLDER - # Optional: Cleanup after tests are done - # shutil.rmtree(TEST_DATA_FOLDER) - def test_file_finder(ftp_data): # Test with a valid directory path containing files