Skip to content

Commit 4c8aab3

Browse files
author
Alan Christie
committed
test: Add tests for new 'misc' module
1 parent 131b214 commit 4c8aab3

File tree

8 files changed

+82
-25
lines changed

8 files changed

+82
-25
lines changed

.coveragerc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[run]
2+
omit = tests/*,**/__init__.py
3+
4+
[report]
5+
include_namespace_packages = True

.github/workflows/build.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ jobs:
4949
pip install -r package-requirements.txt
5050
- name: Test
5151
run: |
52+
pytest
5253
pyroma .
5354
pylint dm_job_utilities
5455
mypy dm_job_utilities --install-types --non-interactive

build-requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
pylint
22
pyroma
3+
pytest
4+
pytest-cov
35
mypy

dm_job_utilities/misc.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,25 @@
33

44
# Extracted 19th March 2024
55
#
6-
# Includes style changes to align with the pre-existing modules.
6+
# Includes style changes to align with the pre-existing modules
7+
# and the following modifications/changes: -
8+
#
9+
# 'get_path_from_digest()' has been removed because it is obsolete.
10+
#
11+
# 'round_to_significant_number()' has been removed. Its implementation
12+
# was inaccurate and is easily replaced by round() from the sigfig package.
713

814
import os
915
import sys
1016
from math import log10, floor
1117
from typing import Any, List, Optional, Tuple
1218

13-
DEFAULT_NUM_CHARS = 2
14-
DEFAULT_NUM_LEVELS = 2
15-
1619

1720
def log(*args, **kwargs) -> None:
1821
"""Log output to STDERR"""
1922
print(*args, file=sys.stderr, **kwargs)
2023

2124

22-
def get_path_from_digest(
23-
digest, num_chars=DEFAULT_NUM_CHARS, num_levels=DEFAULT_NUM_LEVELS
24-
) -> List[str]:
25-
"""Splits a digest string into parts"""
26-
parts = []
27-
start = 0
28-
for _ in range(0, num_levels):
29-
end = start + num_chars
30-
part = digest[start:end]
31-
parts.append(part)
32-
start = start + num_chars
33-
return parts
34-
35-
3625
def expand_path(path) -> None:
3726
"""Create any necessary directories to ensure that the file path is valid.
3827
The path is a filename or directory that might or not exist.
@@ -175,15 +164,11 @@ def calc_geometric_mean(scores: List[float]) -> float:
175164
return total ** (1.0 / len(scores))
176165

177166

178-
def round_to_significant_number(val: float, sig: int) -> float:
179-
"""Round the value to the specified number of significant numbers
180-
"""
181-
return round(val, sig - int(floor(log10(abs(val)))) - 1)
182-
183-
184167
def is_type(value, typ) -> Tuple[int, Any]:
185168
"""Returns a tuple of the form (status, value) where status is
186-
-1 if the value is not of the given type, and 0 if it is."""
169+
-1 if the value cannot be of the given type, and 0 if it is. The value
170+
returned is cast to the given type on success.
171+
"""
187172
if value is None:
188173
return 0, value
189174
try:

test-data/atom-block-a-in.sdf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
RDKit 2D
3+
4+
4 3 0 0 0 0 0 0 0 0999 V2000
5+
0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
6+
1.2990 0.7500 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
7+
1.2990 2.2500 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
8+
2.5981 -0.0000 0.0000 O 0 0 0 0 0 1 0 0 0 0 0 0
9+
1 2 1 0
10+
2 3 2 0
11+
2 4 1 0
12+
M CHG 1 4 -1
13+
M END

test-data/atom-block-a-out.sdf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
RDKit 2D
3+
4+
4 3 0 0 0 0 0 0 0 0999 V2000
5+
0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
6+
1.2990 0.7500 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
7+
1.2990 2.2500 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
8+
2.5981 -0.0000 0.0000 O 0 5 0 0 0 1 0 0 0 0 0 0
9+
1 2 1 0
10+
2 3 2 0
11+
2 4 1 0
12+
M CHG 1 4 -1

tests/__init__.py

Whitespace-only changes.

tests/test_misc.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
import pathlib
3+
import unittest
4+
5+
from dm_job_utilities.misc import (
6+
calc_geometric_mean,
7+
is_type,
8+
read_delimiter,
9+
update_charge_flag_in_atom_block,
10+
)
11+
12+
class TestMiscMethods(unittest.TestCase):
13+
14+
def test_update_charge_flag_in_atom_block(self):
15+
atom_block_a_in = pathlib.Path('test-data/atom-block-a-in.sdf').read_text()
16+
atom_block_a_out = pathlib.Path('test-data/atom-block-a-out.sdf').read_text()
17+
output = update_charge_flag_in_atom_block(atom_block_a_in)
18+
self.assertEqual(output, atom_block_a_out)
19+
20+
def test_read_delimiter(self):
21+
self.assertEqual(read_delimiter("comma"), ",")
22+
self.assertEqual(read_delimiter("pipe"), "|")
23+
self.assertEqual(read_delimiter("space"), None)
24+
self.assertEqual(read_delimiter("tab"), "\t")
25+
self.assertEqual(read_delimiter("unknown"), "unknown")
26+
27+
def test_calc_geometric_mean(self):
28+
data = [1.0, 2.0, 3.0, 4.0, 5.0]
29+
mean = calc_geometric_mean(data)
30+
self.assertAlmostEqual(mean, 2.605, 3)
31+
32+
def test_is_type(self):
33+
self.assertEqual(is_type(None, int), (0, None))
34+
self.assertEqual(is_type(1, int), (1, 1))
35+
self.assertEqual(is_type("1", str), (1, '1'))
36+
self.assertEqual(is_type(1.0, float), (1, 1))
37+
self.assertEqual(is_type(1, str), (1, '1'))
38+
self.assertEqual(is_type("1", float), (1, 1.0))
39+
self.assertEqual(is_type(1.0, int), (1, 1))

0 commit comments

Comments
 (0)