Skip to content

Commit

Permalink
Moved pytest fixture to the conftest.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaiboldeanu committed Sep 13, 2023
1 parent 639d1ef commit 896df75
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 45 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ var/
wheels/
share/python-wheels/
*.egg-info/
<<<<<<< Updated upstream
notebooks
=======
notebooks/
>>>>>>> Stashed changes
.installed.cfg
*.egg
MANIFEST
Expand Down Expand Up @@ -165,3 +169,6 @@ test_data/
*.nc
*.zarr
*.raw
# For MAC stuff
.DS_Store
oceanstream/.DS_Store
53 changes: 53 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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)
45 changes: 0 additions & 45 deletions tests/test_raw_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 896df75

Please sign in to comment.