From 151591e1dd51770dbe7b476a7d9b5b80e966cc1f Mon Sep 17 00:00:00 2001 From: Karl Tarbet Date: Thu, 22 Aug 2024 12:34:57 -0700 Subject: [PATCH] Add testing for Pull Requests (#44) * increment version number include array testing in publish task add create_test_file to file_manager * add test.yml * show location of downloaded file * refactor .so path searching * handle None properly * more refactor on library loading --- .github/workflows/publish-to-test-pypi.yml | 2 +- .github/workflows/test.yml | 41 ++++++++++++++++++++++ setup.cfg | 2 +- src/hecdss/download_hecdss.py | 4 +-- src/hecdss/native.py | 32 ++++++++++------- tests/file_manager.py | 9 ++++- tests/test_array.py | 3 +- 7 files changed, 74 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/publish-to-test-pypi.yml b/.github/workflows/publish-to-test-pypi.yml index dfdca76..8b4e9a0 100644 --- a/.github/workflows/publish-to-test-pypi.yml +++ b/.github/workflows/publish-to-test-pypi.yml @@ -99,7 +99,7 @@ jobs: run: pip install numpy - name: Run tests - run: pytest tests/test_basics.py tests/test_gridded_data.py tests/test_paired_data.py tests/test_regular_timeseries.py tests/test_text.py + run: pytest tests/test_basics.py tests/test_gridded_data.py tests/test_paired_data.py tests/test_regular_timeseries.py tests/test_text.py tests/test_array.py # The development distribution is a snapshot of the package and is deployed each time # changes are merged into the main branch. diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..a8488e9 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,41 @@ +name: Test + +on: + pull_request: + types: [opened, edited, synchronize] + + # Allow the workflow to be manually triggered from the Actions tab. + workflow_dispatch: + +jobs: + testing: + name: testing HECDSS functions + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: [ubuntu-latest, windows-latest] + steps: + - name: Set Up Python + uses: actions/setup-python@v5 + with: + python-version: '3.x' + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install requests + run: pip install requests + + - name: add dll + run: python3 src/hecdss/download_hecdss.py + + - name: Install Packages + run: pip install pytest + + - name: Install numpy + run: pip install numpy + + - name: Run tests + run: pytest tests/test_basics.py tests/test_gridded_data.py tests/test_paired_data.py tests/test_regular_timeseries.py tests/test_text.py tests/test_array.py + diff --git a/setup.cfg b/setup.cfg index 91600cb..3a706cd 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = hecdss -version = 0.1.13 +version = 0.1.14 author = Hydrologic Engineering Center author_email =hec.dss@usace.army.mil description = Python wrapper for the HEC-DSS file database C library. diff --git a/src/hecdss/download_hecdss.py b/src/hecdss/download_hecdss.py index bc070d4..06119ac 100644 --- a/src/hecdss/download_hecdss.py +++ b/src/hecdss/download_hecdss.py @@ -30,13 +30,13 @@ def download_and_unzip(url, zip_file, destination_dir): with zipfile.ZipFile(zip_file_path, "r") as zip_ref: zip_ref.extractall(destination_dir) - print("Contents extracted successfully.") + print(f"Contents extracted successfully to.'{destination_dir}'") os.remove(zip_file_path) else: print(f"Failed to download zip file. Status code: {response.status_code}") base_url = "https://www.hec.usace.army.mil/nexus/repository/maven-public/mil/army/usace/hec/hecdss/" -version = "7-IS-7" +version = "7-IT-2" destination_dir = Path(__file__).parent.joinpath("lib") zip_url = f"{base_url}{version}-win-x86_64/hecdss-{version}-win-x86_64.zip" diff --git a/src/hecdss/native.py b/src/hecdss/native.py index 5c25208..eafea26 100644 --- a/src/hecdss/native.py +++ b/src/hecdss/native.py @@ -14,22 +14,28 @@ class _Native: """Wrapper for Native method calls to hecdss.dll or libhecdss.so""" + def load_hecdss_library(self, libname): + """ + searches and loads [lib]hecdss.[dll|so] + """ + paths_to_try = [ + libname, + os.path.join(os.path.dirname(__file__), 'lib', libname), + find_library("hecdss") + ] + found_libs = [path for path in paths_to_try if path is not None and os.path.exists(path)] + if len(found_libs) == 0: + raise FileNotFoundError(f"{libname} not found Paths searched: {paths_to_try}") + return ctypes.CDLL(found_libs[0]) + + def __init__(self): - if sys.platform == "linux" or sys.platform == "darwin": - libc_path = ( - "libhecdss.so" - if find_library("libhecdss") - else os.path.join(os.environ["LD_LIBRARY_PATH"], "libhecdss.so") - ) - self.dll = ctypes.CDLL(libc_path) - elif sys.platform == "win32": - dll_dir = os.path.join(os.path.dirname(sys.modules["hecdss"].__file__), 'lib') - dll_path = os.path.join(dll_dir, 'hecdss.dll') - - self.dll = ctypes.CDLL(dll_path) + self.handle = None + if sys.platform == "win32": + self.dll = self.load_hecdss_library("hecdss.dll") else: - raise Exception("Unsupported platform") + self.dll = self.load_hecdss_library("libhecdss.so") def hec_dss_open(self, dss_filename: str): f = self.dll.hec_dss_open diff --git a/tests/file_manager.py b/tests/file_manager.py index 083836c..7eb771e 100644 --- a/tests/file_manager.py +++ b/tests/file_manager.py @@ -2,7 +2,7 @@ import tempfile import os import uuid - +import random class FileManager: """ This FileManager is used to create uniquely named copies of test input files @@ -18,6 +18,13 @@ def __init__(self): self.test_data_dir = d self.temp_dir = tempfile.mkdtemp() + def create_test_file(self,extension): + """ + creates a random test filename given file extension (including the period '.' ) + the file is not created + """ + return os.path.join(self.temp_dir, 'temp_' + str(random.randint(10**8, 10**9 - 1)) + extension) + def get_copy(self, filename): """Copies a file to the temporary directory.""" src = os.path.join(self.test_data_dir, filename) diff --git a/tests/test_array.py b/tests/test_array.py index e82eaf2..b5001c1 100644 --- a/tests/test_array.py +++ b/tests/test_array.py @@ -4,6 +4,7 @@ import os import sys import numpy as np + sys.path.append(r'src') sys.path.append(os.path.dirname(__file__)) from file_manager import FileManager @@ -22,7 +23,7 @@ def test_arrays(self): a = ArrayContainer.create_float_array([1.0, 3.0, 5.0, 7.0]) a.id = "/test/float-array/redshift////" - dss = HecDss("test_dss_array.dss") + dss = HecDss(self.test_files.create_test_file(".dss")) print(f" record_count = {dss.record_count()}") dss.put(a) # dss.set_debug_level(15)