Skip to content

Commit

Permalink
modified tests to work with pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
Acribbs committed Oct 26, 2024
1 parent 8d695e5 commit 324610e
Show file tree
Hide file tree
Showing 6 changed files with 381 additions and 716 deletions.
21 changes: 13 additions & 8 deletions all-tests.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#! /bin/bash
#!/bin/bash

set -xue

# Run code style checks
pycodestyle

# Run the template pipeline script with the `make all` command
python tests/template_pipeline.py make all
nosetests -v tests/test_import.py
nosetests -v tests/test_iotools.py
nosetests -v tests/test_pipeline_cluster.py
nosetests -v tests/test_pipeline_control.py
nosetests -v tests/test_pipeline_execution.py
pytest tests/test_pipeline_cli.py
pytest tests/test_pipeline_actions.py

# Run tests using pytest
pytest -v tests/test_import.py
pytest -v tests/test_iotools.py
pytest -v tests/test_pipeline_cluster.py
pytest -v tests/test_pipeline_control.py
pytest -v tests/test_pipeline_execution.py
pytest -v tests/test_pipeline_cli.py
pytest -v tests/test_pipeline_actions.py
67 changes: 27 additions & 40 deletions tests/test_import.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'''test_import - test importing all modules
===========================================
:Author: Andreas Heger
:Author: Adam Cribbs
:Release: $Id$
:Date: |today|
:Tags: Python
Expand All @@ -17,71 +17,58 @@
will fail within sphinx.
'''

import os
import glob
import traceback
import imp
import importlib.util
import pytest

# DIRECTORIES to examine
EXPRESSIONS = (
('FirstLevel', 'cgatcore/*.py'),
('SecondLevel', 'cgatcore/pipeline/*.py'))
('SecondLevel', 'cgatcore/pipeline/*.py')
)

# Code to exclude
EXCLUDE = ()
EXCLUDE = set()


def check_import(filename, outfile):
"""Attempt to import a module and handle errors."""
module_name = os.path.splitext(os.path.basename(filename))[0]

prefix, suffix = os.path.splitext(filename)
dirname, basename = os.path.split(prefix)

if basename in EXCLUDE:
if module_name in EXCLUDE:
return

if os.path.exists(prefix + ".pyc"):
os.remove(prefix + ".pyc")
if os.path.exists(filename + "c"):
os.remove(filename + "c")

# ignore script with pyximport for now, something does not work
pyxfile = os.path.join(dirname, "_") + basename + "x"
pyxfile = os.path.join(os.path.dirname(filename), "_") + module_name + "x"
if os.path.exists(pyxfile):
return

try:
imp.load_source(basename, filename)

spec = importlib.util.spec_from_file_location(module_name, filename)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
except ImportError as msg:
outfile.write("FAIL %s\n%s\n" % (basename, msg))
outfile.write(f"FAIL {module_name}\n{msg}\n")
outfile.flush()
traceback.print_exc(file=outfile)
assert False, '%s scripts/modules - ImportError: %s' % (basename, msg)
pytest.fail(f"{module_name} - ImportError: {msg}")
except Exception as msg:
outfile.write("FAIL %s\n%s\n" % (basename, msg))
outfile.write(f"FAIL {module_name}\n{msg}\n")
outfile.flush()

traceback.print_exc(file=outfile)
assert False, '%s scripts/modules - Exception: %s' % (basename, str(msg))

assert True


def test_import():
'''test importing
Relative imports will cause a failure because
imp.load_source does not import modules that are in the same
directory as the module being loaded from source.
'''
outfile = open('test_import.log', 'a')
pytest.fail(f"{module_name} - Exception: {msg}")

for label, expression in EXPRESSIONS:

files = glob.glob(expression)
files.sort()
@pytest.mark.parametrize("label, expression", EXPRESSIONS)
def test_import(label, expression):
"""Test importing all modules in the specified expressions."""
with open('test_import.log', 'a') as outfile:
files = sorted(glob.glob(expression))

for f in files:
if os.path.isdir(f):
continue
check_import.description = os.path.abspath(f)
yield check_import, os.path.abspath(f), outfile
for filename in files:
if not os.path.isdir(filename):
check_import(filename, outfile)
153 changes: 81 additions & 72 deletions tests/test_iotools.py
Original file line number Diff line number Diff line change
@@ -1,85 +1,94 @@
"""Test cases for the cgatcore.iotools module."""

import unittest
import os
import shutil
import tempfile
import time
import cgatcore.iotools as iotools


class TestiotoolsTouchFile(unittest.TestCase):

basename = "test_iotools_touch_file.txt"

def setUp(self):
self.tempdir = tempfile.mkdtemp()
self.filename = os.path.join(self.tempdir,
self.basename)

def tearDown(self):
shutil.rmtree(self.tempdir)

def test_touch_file_creates_empty_file(self):
self.assertFalse(os.path.exists(self.filename))
iotools.touch_file(self.filename)
self.assertTrue(os.path.exists(self.filename))
if self.filename.endswith(".gz"):
self.assertFalse(iotools.is_empty(self.filename))
else:
self.assertTrue(iotools.is_empty(self.filename))

with iotools.open_file(self.filename) as inf:
data = inf.read()
self.assertEqual(len(data), 0)

def test_touch_file_updates_existing_file(self):
with iotools.open_file(self.filename, "w") as outf:
outf.write("some data\n")
created = os.stat(self.filename).st_mtime
time.sleep(1)
iotools.touch_file(self.filename)
modified = os.stat(self.filename).st_mtime
self.assertGreater(modified, created)
with iotools.open_file(self.filename) as inf:
data = inf.read()
self.assertEqual(data, "some data\n")


class TestiotoolsTouchFileCompressed(TestiotoolsTouchFile):

basename = "test_iotools_touch_file.txt.gz"


class TestiottoolsIsNested(unittest.TestCase):

def test_is_nested_with_dict(self):
test_data = {
"key1": {
"nested_key1": "nested_key1"
}
import pytest


@pytest.fixture
def temp_file():
"""Fixture to create and clean up a temporary file."""
tempdir = tempfile.mkdtemp()
filename = os.path.join(tempdir, "test_iotools_touch_file.txt")
yield filename
shutil.rmtree(tempdir)


@pytest.fixture
def temp_file_compressed():
"""Fixture to create and clean up a temporary compressed file."""
tempdir = tempfile.mkdtemp()
filename = os.path.join(tempdir, "test_iotools_touch_file.txt.gz")
yield filename
shutil.rmtree(tempdir)


def test_touch_file_creates_empty_file(temp_file):
assert not os.path.exists(temp_file)
iotools.touch_file(temp_file)
assert os.path.exists(temp_file)
if temp_file.endswith(".gz"):
assert not iotools.is_empty(temp_file)
else:
assert iotools.is_empty(temp_file)

with iotools.open_file(temp_file) as inf:
data = inf.read()
assert len(data) == 0


def test_touch_file_updates_existing_file(temp_file):
with iotools.open_file(temp_file, "w") as outf:
outf.write("some data\n")
created = os.stat(temp_file).st_mtime
time.sleep(1)
iotools.touch_file(temp_file)
modified = os.stat(temp_file).st_mtime
assert modified > created
with iotools.open_file(temp_file) as inf:
data = inf.read()
assert data == "some data\n"


def test_touch_file_compressed_creates_empty_file(temp_file_compressed):
assert not os.path.exists(temp_file_compressed)
iotools.touch_file(temp_file_compressed)
assert os.path.exists(temp_file_compressed)
if temp_file_compressed.endswith(".gz"):
assert not iotools.is_empty(temp_file_compressed)
else:
assert iotools.is_empty(temp_file_compressed)

with iotools.open_file(temp_file_compressed) as inf:
data = inf.read()
assert len(data) == 0


def test_is_nested_with_dict():
test_data = {
"key1": {
"nested_key1": "nested_key1"
}
self.assertTrue(iotools.is_nested(test_data))

}
assert iotools.is_nested(test_data)

class TestiottoolsNestedIter(unittest.TestCase):

def test_nested_iter_with_dict_of_dicts(self):
test_data = {
"key1": {
"nested_key1": "nested_key1"
}
def test_nested_iter_with_dict_of_dicts():
test_data = {
"key1": {
"nested_key1": "nested_key1"
}
list(iotools.nested_iter(test_data))
}
list(iotools.nested_iter(test_data))

def test_nested_iter_with_list_of_dicts(self):
test_data = [
{
"nested_key1": "nested_key1"
}
]
list(iotools.nested_iter(test_data))


if __name__ == "__main__":
unittest.main()
def test_nested_iter_with_list_of_dicts():
test_data = [
{
"nested_key1": "nested_key1"
}
]
list(iotools.nested_iter(test_data))
Loading

0 comments on commit 324610e

Please sign in to comment.