Skip to content

Commit

Permalink
Add testing for Pull Requests (#44)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ktarbet authored Aug 22, 2024
1 parent eb9250f commit 151591e
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-to-test-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
41 changes: 41 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = hecdss
version = 0.1.13
version = 0.1.14
author = Hydrologic Engineering Center
author_email [email protected]
description = Python wrapper for the HEC-DSS file database C library.
Expand Down
4 changes: 2 additions & 2 deletions src/hecdss/download_hecdss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
32 changes: 19 additions & 13 deletions src/hecdss/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion tests/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 151591e

Please sign in to comment.