Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jngrad committed May 16, 2024
1 parent 6248af6 commit 8f4e608
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ docs:
PDOC_ALLOW_EXEC=0 ${PYTHON} -m pdoc ./pyMBE.py -o ./documentation --docformat google

unit_tests:
${PYTHON} testsuite/serialization_test.py
${PYTHON} testsuite/lj_tests.py
${PYTHON} testsuite/set_particle_acidity_test.py
${PYTHON} testsuite/bond_tests.py
Expand Down
3 changes: 1 addition & 2 deletions lib/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def get_params_from_dir_name(name):
entries = name.split('_')
params = {}
for entry in entries:
sp_entry = entry.split('-')
sp_entry = entry.split('-', 1)
params[sp_entry[0]] = sp_entry[-1] #float(sp_entry[-1]) # creates a dictionary of parameters and their values.
return params

Expand Down Expand Up @@ -343,7 +343,6 @@ def get_distribution_from_df(df, key):
distribution_list (`lst`): list stored under `key`
"""
import pandas as pd
distribution_list=[]
for row in df[key]:
if pd.isnull(row):
Expand Down
8 changes: 4 additions & 4 deletions pyMBE.py
Original file line number Diff line number Diff line change
Expand Up @@ -2157,10 +2157,10 @@ def print_reduced_units(self):
unit_length=self.units.Quantity(1,'reduced_length')
unit_energy=self.units.Quantity(1,'reduced_energy')
unit_charge=self.units.Quantity(1,'reduced_charge')
print(unit_length.to('nm'), "=", unit_length)
print(unit_energy.to('J'), "=", unit_energy)
print('Temperature:', (self.kT/self.Kb).to("K"))
print(unit_charge.to('C'), "=", unit_charge)
print(f"{unit_length.to('nm'):.5g} = {unit_length}")
print(f"{unit_energy.to('J'):.5g} = {unit_energy}")
print(f"{unit_charge.to('C'):.5g} = {unit_charge}")
print(f"Temperature: {(self.kT/self.Kb).to('K'):.5g}")
print()

def propose_unused_type(self):
Expand Down
52 changes: 52 additions & 0 deletions testsuite/serialization_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import io
import json
import contextlib
import unittest as ut
import numpy as np
import pandas as pd
import pyMBE
import lib.analysis


class Serialization(ut.TestCase):

def test_json_encoder(self):
encoder = pyMBE.pymbe_library.NumpyEncoder
# Python types
self.assertEqual(json.dumps(1, cls=encoder), "1")
self.assertEqual(json.dumps([1, 2], cls=encoder), "[1, 2]")
self.assertEqual(json.dumps((1, 2), cls=encoder), "[1, 2]")
self.assertEqual(json.dumps({1: 2}, cls=encoder), """{"1": 2}""")
# NumPy types
self.assertEqual(json.dumps(np.array([1, 2]), cls=encoder), "[1, 2]")
self.assertEqual(json.dumps(np.array(1), cls=encoder), "1")
self.assertEqual(json.dumps(np.int32(1), cls=encoder), "1")
# Pandas types
with self.assertRaisesRegex(TypeError, "Object of type Series is not JSON serializable"):
json.dumps(pd.Series([1, 2]), cls=encoder)

def test_parameters_to_path(self):
params = {"kT": 2., "phi": -np.pi, "n": 3, "fene": True, "name": "pep"}
name = lib.analysis.built_output_name(params)
self.assertEqual(name, "kT-2_phi--3.14_n-3_fene-True_name-pep")
params_out = lib.analysis.get_params_from_dir_name(name)
params_ref = {"kT": "2", "phi": "-3.14", "n": "3",
"fene": "True", "name": "pep"}
self.assertEqual(params_out, params_ref)

def test_pint_units(self):
ref_output = [
"Current set of reduced units:",
"0.355 nanometer = 1 reduced_length",
"4.1164e-21 joule = 1 reduced_energy",
"1.6022e-19 coulomb = 1 reduced_charge",
"Temperature: 298.15 kelvin",
]
pmb = pyMBE.pymbe_library(SEED=42)
with contextlib.redirect_stdout(io.StringIO()) as f:
pmb.print_reduced_units()
self.assertEqual(f.getvalue().strip("\n").split("\n"), ref_output)


if __name__ == "__main__":
ut.main()

0 comments on commit 8f4e608

Please sign in to comment.