Implements More Tests and Begins Dice Refactor Implementation

This commit is contained in:
2023-04-22 11:27:20 -03:00
parent 71744641ff
commit b869ee74fb
19 changed files with 719 additions and 297 deletions

View File

View File

@@ -0,0 +1,89 @@
from diceplayer.shared.config.dice_dto import DiceDTO
import unittest
class TestDiceDto(unittest.TestCase):
def test_class_instantiation(self):
dice_dto = DiceDTO(
ljname='test',
outname='test',
ncores=1,
dens=1.0,
nmol=[1],
nstep=[1],
)
self.assertIsInstance(dice_dto, DiceDTO)
def test_validate_jname(self):
with self.assertRaises(ValueError) as ex:
DiceDTO(
ljname=None,
outname='test',
ncores=1,
dens=1.0,
nmol=[1],
nstep=[1],
)
self.assertEqual(ex.exception, "Error: 'ljname' keyword not specified in config file")
def test_validate_outname(self):
with self.assertRaises(ValueError) as ex:
DiceDTO(
ljname='test',
outname=None,
ncores=1,
dens=1.0,
nmol=[1],
nstep=[1],
)
self.assertEqual(ex.exception, "Error: 'outname' keyword not specified in config file")
def test_validate_dens(self):
with self.assertRaises(ValueError) as ex:
DiceDTO(
ljname='test',
outname='test',
ncores=1,
dens=None,
nmol=[1],
nstep=[1],
)
self.assertEqual(ex.exception, "Error: 'dens' keyword not specified in config file")
def test_validate_nmol(self):
with self.assertRaises(ValueError) as ex:
DiceDTO(
ljname='test',
outname='test',
ncores=1,
dens=1.0,
nmol=0,
nstep=[1],
)
self.assertEqual(ex.exception, "Error: 'nmol' keyword not defined appropriately in config file")
def test_validate_nstep(self):
with self.assertRaises(ValueError) as ex:
DiceDTO(
ljname='test',
outname='test',
ncores=1,
dens=1.0,
nmol=[1],
nstep=0,
)
self.assertEqual(ex.exception, "Error: 'nstep' keyword not defined appropriately in config file")
def test_from_dict(self):
dice_dto = DiceDTO.from_dict({
'ljname': 'test',
'outname': 'test',
'ncores': 1,
'dens': 1.0,
'nmol': [1],
'nstep': [1],
})
self.assertIsInstance(dice_dto, DiceDTO)

View File

@@ -1,61 +0,0 @@
from diceplayer.shared.environment.molecule import Molecule
from diceplayer.shared.environment.atom import Atom
import numpy.testing as npt
import unittest
class TestMolecule(unittest.TestCase):
def test_class_instantiation(self):
mol = Molecule('test')
self.assertIsInstance(mol, Molecule)
def test_add_atom(self):
mol = Molecule('test')
mol.add_atom(
Atom(
lbl=1,
na=1,
rx=1.0,
ry=1.0,
rz=1.0,
chg=1.0,
eps=1.0,
sig=1.0,
)
)
self.assertEqual(len(mol.atom), 1)
npt.assert_equal(mol.com, [1., 1., 1.])
def test_center_of_mass(self):
mol = Molecule('test')
mol.add_atom(
Atom(
lbl=1,
na=1,
rx=1.0,
ry=1.0,
rz=1.0,
chg=1.0,
eps=1.0,
sig=1.0,
)
)
mol.add_atom(
Atom(
lbl=1,
na=1,
rx=0.0,
ry=0.0,
rz=0.0,
chg=1.0,
eps=1.0,
sig=1.0,
)
)
npt.assert_equal(mol.com, [.5, .5, .5])

View File

@@ -0,0 +1,147 @@
from diceplayer.shared.environment.molecule import Molecule
from diceplayer.shared.environment.atom import Atom
import numpy.testing as npt
import unittest
class TestMolecule(unittest.TestCase):
def test_class_instantiation(self):
mol = Molecule('test')
self.assertIsInstance(mol, Molecule)
def test_add_atom(self):
mol = Molecule('test')
mol.add_atom(
Atom(lbl=1, na=1, rx=1.0, ry=1.0, rz=1.0, chg=1.0, eps=1.0, sig=1.0)
)
self.assertEqual(len(mol.atom), 1)
npt.assert_equal(mol.com, [1., 1., 1.])
def test_center_of_mass(self):
mol = Molecule('test')
mol.add_atom(
Atom(lbl=1, na=1, rx=1.0, ry=1.0, rz=1.0, chg=1.0, eps=1.0, sig=1.0)
)
mol.add_atom(
Atom(lbl=1, na=1, rx=0.0, ry=0.0, rz=0.0, chg=1.0, eps=1.0, sig=1.0)
)
npt.assert_equal(mol.com, [.5, .5, .5])
def test_center_of_mass_to_origin(self):
mol = Molecule('test')
mol.add_atom(
Atom(lbl=1, na=1, rx=1.0, ry=1.0, rz=1.0, chg=1.0, eps=1.0, sig=1.0)
)
mol.center_of_mass_to_origin()
npt.assert_equal(mol.com, [0, 0, 0])
def test_charges_and_dipole(self):
mol = Molecule('test')
mol.add_atom(
Atom(lbl=1, na=1, rx=0.0, ry=0.0, rz=0.0, chg=1.0, eps=1.0, sig=1.0)
)
actual_charge_dipole_array = mol.charges_and_dipole()
expected_charge_dipole_array = [1.0, 0.0, 0.0, 0.0, 0.0]
npt.assert_equal(
actual_charge_dipole_array,
expected_charge_dipole_array
)
def test_distances_between_atoms(self):
mol = Molecule('test')
mol.add_atom(
Atom(lbl=1, na=1, rx=0.0, ry=0.0, rz=0.0, chg=1.0, eps=1.0, sig=1.0)
)
mol.add_atom(
Atom(lbl=1, na=1, rx=1.0, ry=1.0, rz=1.0, chg=1.0, eps=1.0, sig=1.0)
)
expected_distance_between_atoms = [[1.73205081], [1.73205081]]
actual_distance_between_atoms = mol.distances_between_atoms()
npt.assert_almost_equal(
expected_distance_between_atoms,
actual_distance_between_atoms
)
def test_inertia_tensor(self):
mol = Molecule('test')
mol.add_atom(
Atom(lbl=1, na=1, rx=0.0, ry=0.0, rz=0.0, chg=1.0, eps=1.0, sig=1.0)
)
mol.add_atom(
Atom(lbl=1, na=1, rx=1.0, ry=1.0, rz=1.0, chg=1.0, eps=1.0, sig=1.0)
)
expected_inertia_tensor = [[1.00790, -0.50395, -0.50395],
[-0.50395, 1.0079, -0.50395],
[-0.50395, -0.50395, 1.0079]]
actual_inertia_tensor = mol.inertia_tensor()
npt.assert_equal(
expected_inertia_tensor,
actual_inertia_tensor
)
def test_principal_axes(self):
mol = Molecule('test')
mol.add_atom(
Atom(lbl=1, na=1, rx=0.0, ry=0.0, rz=0.0, chg=1.0, eps=1.0, sig=1.0)
)
expected_evals, expected_evecs = [0., 0., 0.], [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]
evals, evecs = mol.principal_axes()
npt.assert_equal(expected_evals, evals)
npt.assert_equal(expected_evecs, evecs)
def test_read_position(self):
mol = Molecule('test')
mol.add_atom(
Atom(lbl=1, na=1, rx=0.0, ry=0.0, rz=0.0, chg=1.0, eps=1.0, sig=1.0)
)
expected_position = mol.read_position()
actual_position = mol.read_position()
npt.assert_equal(
expected_position,
actual_position
)
def test_update_charges(self):
mol = Molecule('test')
mol.add_atom(
Atom(lbl=1, na=1, rx=0.0, ry=0.0, rz=0.0, chg=1.0, eps=1.0, sig=1.0)
)
expected_charges = [2.]
mol.update_charges(expected_charges)
actual_charges = list(map(lambda a: a.chg, mol.atom))
npt.assert_equal(
expected_charges,
actual_charges
)

View File

@@ -0,0 +1,50 @@
from diceplayer.shared.environment.atom import Atom
from diceplayer.shared.environment.molecule import Molecule
from diceplayer.shared.environment.system import System
import unittest
class TestSystem(unittest.TestCase):
def test_class_instantiation(self):
system = System()
self.assertIsInstance(system, System)
def test_add_type(self):
system = System()
system.add_type(0, Molecule('test'))
self.assertIsInstance(system.molecule, list)
self.assertIsInstance(system.nmols, list)
with self.assertRaises(TypeError) as ex:
system.add_type(0, 'test')
self.assertEqual(ex.exception, 'Error: molecule is not a Molecule instance')
with self.assertRaises(TypeError) as ex:
system.add_type('test', Molecule('test'))
self.assertEqual(ex.exception, 'Error: nmols is not an integer')
def test_center_of_mass_distance(self):
system = System()
a = Molecule('test')
a.add_atom(
Atom(lbl=0, na=1, rx=0, ry=0, rz=0, chg=0, eps=0, sig=0)
)
system.add_type(1, a)
b = Molecule('test')
b.add_atom(
Atom(lbl=0, na=1, rx=0, ry=0, rz=0, chg=0, eps=0, sig=0)
)
system.add_type(1, b)
self.assertIsInstance(system.center_of_mass_distance(0, 1), float)
if __name__ == '__main__':
unittest.main()

0
tests/shared/external/__init__.py vendored Normal file
View File

116
tests/shared/external/test_dice.py vendored Normal file
View File

@@ -0,0 +1,116 @@
from diceplayer.shared.external.dice import Dice
from unittest import mock
import unittest
class TestDice(unittest.TestCase):
def test_class_instantiation(self):
dice = Dice(
{
'ljname': 'test',
'outname': 'test',
'ncores': 1,
'dens': 1.0,
'nmol': [1],
'nstep': [1],
}
)
self.assertIsInstance(dice, Dice)
def test_configure(self):
dice = Dice(
{
'ljname': 'test',
'outname': 'test',
'ncores': 1,
'dens': 1.0,
'nmol': [1],
'nstep': [1],
}
)
self.assertFalse(hasattr(dice, 'step'))
dice.configure('test')
self.assertTrue(hasattr(dice, 'step'))
def test_reset(self):
dice = Dice(
{
'ljname': 'test',
'outname': 'test',
'ncores': 1,
'dens': 1.0,
'nmol': [1],
'nstep': [1],
}
)
dice.configure('test')
self.assertTrue(hasattr(dice, 'step'))
dice.reset()
self.assertFalse(hasattr(dice, 'step'))
@mock.patch('diceplayer.shared.external.dice.connection')
@mock.patch('diceplayer.shared.external.dice.Process')
def test_start(self, mock_process, mock_connection):
dice = Dice(
{
'ljname': 'test',
'outname': 'test',
'ncores': 1,
'dens': 1.0,
'nmol': [1],
'nstep': [1],
}
)
dice.start(1)
self.assertTrue(mock_process.called)
self.assertTrue(mock_connection.wait.called)
def test_simulation_process_raises_exception(self):
dice = Dice(
{
'ljname': 'test',
'outname': 'test',
'ncores': 1,
'dens': 1.0,
'nmol': [1],
'nstep': [1],
}
)
with self.assertRaises(SystemExit):
dice._simulation_process(1, 1)
@mock.patch('diceplayer.shared.external.dice.Dice._make_proc_dir')
@mock.patch('diceplayer.shared.external.dice.Dice._make_dice_inputs')
@mock.patch('diceplayer.shared.external.dice.Dice._run_dice')
def test_simulation_process(self, mock_run_dice, mock_make_dice_inputs, mock_make_proc_dir):
dice = Dice(
{
'ljname': 'test',
'outname': 'test',
'ncores': 1,
'dens': 1.0,
'nmol': [1],
'nstep': [1],
}
)
dice._simulation_process(1, 1)
self.assertTrue(dice._make_proc_dir.called)
self.assertTrue(dice._make_dice_inputs.called)
self.assertTrue(dice._run_dice.called)
if __name__ == '__main__':
unittest.main()