Skip to content

Commit

Permalink
More test simplification.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyue Ping Ong committed Sep 5, 2023
1 parent 380799b commit acdfeb4
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 68 deletions.
17 changes: 8 additions & 9 deletions tests/test_bisect.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from monty.bisect import find_ge, find_gt, find_le, find_lt, index


class TestFunc:
def test_funcs(self):
l = [0, 1, 2, 3, 4]
assert index(l, 1) == 1
assert find_lt(l, 1) == 0
assert find_gt(l, 1) == 2
assert find_le(l, 1) == 1
assert find_ge(l, 2) == 2
# assert index([0, 1, 1.5, 2], 1.501, atol=0.1) == 4
def test_funcs():
l = [0, 1, 2, 3, 4]
assert index(l, 1) == 1
assert find_lt(l, 1) == 0
assert find_gt(l, 1) == 2
assert find_le(l, 1) == 1
assert find_ge(l, 2) == 2
# assert index([0, 1, 1.5, 2], 1.501, atol=0.1) == 4
Binary file modified tests/test_files/3000_lines.txt.gz
Binary file not shown.
9 changes: 4 additions & 5 deletions tests/test_fnmatch.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from monty.fnmatch import WildCard


class TestFunc:
def test_match(self):
wc = WildCard("*.pdf")
assert wc.match("A.pdf")
assert not wc.match("A.pdg")
def test_match():
wc = WildCard("*.pdf")
assert wc.match("A.pdf")
assert not wc.match("A.pdg")
17 changes: 9 additions & 8 deletions tests/test_fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
from monty.fractions import gcd, gcd_float, lcm


class TestFunc:
def test_gcd(self):
assert gcd(7, 14, 63) == 7
def test_gcd():
assert gcd(7, 14, 63) == 7

def test_lcm(self):
assert lcm(2, 3, 4) == 12

def test_gcd_float(self):
vs = [6.2, 12.4, 15.5 + 5e-9]
assert gcd_float(vs, 1e-8) == pytest.approx(3.1)
def test_lcm():
assert lcm(2, 3, 4) == 12


def test_gcd_float():
vs = [6.2, 12.4, 15.5 + 5e-9]
assert gcd_float(vs, 1e-8) == pytest.approx(3.1)
9 changes: 4 additions & 5 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ def add(a, b):
return a + b


class TestFunc:
def test_logged(self):
s = StringIO()
logging.basicConfig(level=logging.DEBUG, stream=s)
add(1, 2)
def test_logged():
s = StringIO()
logging.basicConfig(level=logging.DEBUG, stream=s)
add(1, 2)
10 changes: 5 additions & 5 deletions tests/test_math.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from monty.math import nCr, nPr


class TestFunc:
def test_nCr(self):
assert nCr(4, 2) == 6
def test_nCr():
assert nCr(4, 2) == 6

def test_deprecated_property(self):
assert nPr(4, 2) == 12

def test_nPr():
assert nPr(4, 2) == 12
22 changes: 10 additions & 12 deletions tests/test_multiprocessing.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import unittest
from math import sqrt

from monty.multiprocessing import imap_tqdm


class FuncCase(unittest.TestCase):
def test_imap_tqdm(self):
results = imap_tqdm(4, sqrt, range(10000))
assert len(results) == 10000
assert results[0] == 0
assert results[400] == 20
assert results[9999] == 99.99499987499375
results = imap_tqdm(4, sqrt, (i**2 for i in range(10000)))
assert len(results) == 10000
assert results[0] == 0
assert results[400] == 400
def test_imap_tqdm():
results = imap_tqdm(4, sqrt, range(10000))
assert len(results) == 10000
assert results[0] == 0
assert results[400] == 20
assert results[9999] == 99.99499987499375
results = imap_tqdm(4, sqrt, (i**2 for i in range(10000)))
assert len(results) == 10000
assert results[0] == 0
assert results[400] == 400
5 changes: 2 additions & 3 deletions tests/test_operator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from monty.operator import operator_from_str


class TestOperator:
def test_something(self):
assert operator_from_str("==")(1, 1) and operator_from_str("+")(1, 1) == 2
def test_operator_from_str():
assert operator_from_str("==")(1, 1) and operator_from_str("+")(1, 1) == 2
41 changes: 20 additions & 21 deletions tests/test_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@
test_dir = os.path.join(os.path.dirname(__file__), "test_files")


class TestRegrep:
def test_regrep(self):
"""
We are making sure a file containing line numbers is read in reverse
order, i.e. the first line that is read corresponds to the last line.
number
"""
fname = os.path.join(test_dir, "3000_lines.txt")
matches = regrep(fname, {"1": r"1(\d+)", "3": r"3(\d+)"}, postprocess=int)
assert len(matches["1"]) == 1380
assert len(matches["3"]) == 571
assert matches["1"][0][0][0] == 0
def test_regrep():
"""
We are making sure a file containing line numbers is read in reverse
order, i.e. the first line that is read corresponds to the last line.
number
"""
fname = os.path.join(test_dir, "3000_lines.txt")
matches = regrep(fname, {"1": r"1(\d+)", "3": r"3(\d+)"}, postprocess=int)
assert len(matches["1"]) == 1380
assert len(matches["3"]) == 571
assert matches["1"][0][0][0] == 0

matches = regrep(
fname,
{"1": r"1(\d+)", "3": r"3(\d+)"},
reverse=True,
terminate_on_match=True,
postprocess=int,
)
assert len(matches["1"]) == 1
assert len(matches["3"]) == 11
matches = regrep(
fname,
{"1": r"1(\d+)", "3": r"3(\d+)"},
reverse=True,
terminate_on_match=True,
postprocess=int,
)
assert len(matches["1"]) == 1
assert len(matches["3"]) == 11

0 comments on commit acdfeb4

Please sign in to comment.