Skip to content

Commit

Permalink
test: Add url and filelikes to tests
Browse files Browse the repository at this point in the history
ci: Install pybigtools[test] for testing
  • Loading branch information
nvictus committed Apr 12, 2024
1 parent fa8f099 commit f4160f4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ jobs:

- name: Install pybigtools
run: |
pip install maturin pytest
pip install maturin
cd pybigtools
pip install -e .
pip install -e .[test]
- name: Install
run: |
Expand Down
24 changes: 24 additions & 0 deletions pybigtools/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,34 @@ build-backend = "maturin"
name = "pybigtools"
description = "Python bindings to the Bigtools Rust library for high-performance BigWig and BigBed I/O"
license = { text = "MIT" }
keywords = [
"bigwig",
"bigbed",
"bbi",
"bioinformatics",
"genomics",
"kent",
"ucsc",
"rust",
]
classifiers = [
"Development Status :: 4 - Beta",
"Operating System :: OS Independent",
"Topic :: Scientific/Engineering :: Bio-Informatics",
"Programming Language :: Rust",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
]
dependencies = [
"numpy"
]

[project.optional-dependencies]
test = [
"pytest",
"smart_open[http]"
]

[project.urls]
homepage = "https://github.com/jackh726/bigtools"
documentation = "https://bigtools.readthedocs.io"
Expand Down
44 changes: 36 additions & 8 deletions pybigtools/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import numpy as np
import pytest
import smart_open

import pybigtools

Expand All @@ -17,12 +18,6 @@ def test_open_close():
b.close()
assert pytest.raises(pybigtools.BBIFileClosed, b.chroms)

# Works with pathlib.Path
path = REPO_ROOT / "bigtools/resources/test/valid.bigWig"
b = pybigtools.open(path, "r")
b.close()
assert pytest.raises(pybigtools.BBIFileClosed, b.chroms)

# Files are closed when exiting a context manager
with pybigtools.open(path, "r") as b:
pass
Expand All @@ -33,6 +28,39 @@ def test_open_close():
assert pytest.raises(pybigtools.BBIReadError, pybigtools.open, s, "r")


def test_open_pathlib_path():
path = REPO_ROOT / "bigtools/resources/test/valid.bigWig"
with pybigtools.open(path, "r") as b:
assert b.chroms() == {"chr17": 83_257_441}


def test_open_raw_url():
url = "http://genome.ucsc.edu/goldenPath/help/examples/bigLollyExample2.bb"
with pybigtools.open(url, "r") as b:
assert b.chroms() == {'chr21': 46_709_983}


def test_open_filelike():
# Regular file
with open(REPO_ROOT / "bigtools/resources/test/valid.bigWig", "rb") as f:
with pybigtools.open(f, "r") as b:
assert b.chroms() == {"chr17": 83_257_441}

# BytesIO
with open(REPO_ROOT / "bigtools/resources/test/valid.bigWig", "rb") as f:
bw_bytes = f.read()

with BytesIO(bw_bytes) as f:
with pybigtools.open(f, "r") as b:
assert b.chroms() == {"chr17": 83_257_441}

# Other file-like objects
url = "http://genome.ucsc.edu/goldenPath/help/examples/bigWigExample.bw"
with smart_open.open(url, "rb") as f:
with pybigtools.open(f, "r") as b:
assert b.chroms() == {'chr21': 48_129_895}


@pytest.fixture
def bw():
path = str(REPO_ROOT / "bigtools/resources/test/valid.bigWig")
Expand Down Expand Up @@ -77,8 +105,8 @@ def test_chroms(bw, bb):
assert bb.chroms() == {"chr21": 48_129_895}

# Arg with chrom name => length
assert bw.chroms("chr17") == 83257441
assert bb.chroms("chr21") == 48129895
assert bw.chroms("chr17") == 83_257_441
assert bb.chroms("chr21") == 48_129_895

# Missing chrom => KeyError
pytest.raises(KeyError, bw.chroms, "chr11")
Expand Down

0 comments on commit f4160f4

Please sign in to comment.