Adds Formatter to Project

This commit is contained in:
2023-06-11 16:04:25 -03:00
parent 82f3092f3e
commit c4dae5e8d1
29 changed files with 1151 additions and 721 deletions

View File

@@ -1,12 +1,10 @@
from diceplayer.player import Player
from diceplayer import logger
import io
from diceplayer.player import Player
from tests.mocks.mock_inputs import mock_open
from unittest import mock
import io
import unittest
from unittest import mock
class TestPlayer(unittest.TestCase):
@@ -59,36 +57,36 @@ class TestPlayer(unittest.TestCase):
def test_print_keywords(self, mock_date_func, mock_sys):
player = Player("control.test.yml")
mock_sys.version = 'TEST'
mock_date_func.return_value = '00 Test 0000 at 00:00:00'
mock_sys.version = "TEST"
mock_date_func.return_value = "00 Test 0000 at 00:00:00"
with self.assertLogs() as cm:
player.print_keywords()
expected_output = [
'INFO:diceplayer:##########################################################################################\n############# Welcome to DICEPLAYER version 1.0 #############\n##########################################################################################\n',
'INFO:diceplayer:Your python version is TEST\n',
'INFO:diceplayer:Program started on 00 Test 0000 at 00:00:00\n',
'INFO:diceplayer:Environment variables:',
'INFO:diceplayer:OMP_STACKSIZE = Not set\n',
'INFO:diceplayer:------------------------------------------------------------------------------------------\n DICE variables being used in this run:\n------------------------------------------------------------------------------------------\n',
'INFO:diceplayer:dens = 0.75',
'INFO:diceplayer:isave = 1000',
'INFO:diceplayer:ljname = phb.ljc',
'INFO:diceplayer:nmol = [ 1 50 ]',
'INFO:diceplayer:nstep = [ 2000 3000 4000 ]',
'INFO:diceplayer:outname = phb',
'INFO:diceplayer:press = 1.0',
'INFO:diceplayer:progname = ~/.local/bin/dice',
'INFO:diceplayer:randominit = first',
'INFO:diceplayer:temp = 300.0',
'INFO:diceplayer:------------------------------------------------------------------------------------------\n GAUSSIAN variables being used in this run:\n------------------------------------------------------------------------------------------\n',
'INFO:diceplayer:chg_tol = 0.01',
'INFO:diceplayer:keywords = freq',
'INFO:diceplayer:level = MP2/aug-cc-pVDZ',
'INFO:diceplayer:pop = chelpg',
'INFO:diceplayer:qmprog = g16',
'INFO:diceplayer:\n'
"INFO:diceplayer:##########################################################################################\n############# Welcome to DICEPLAYER version 1.0 #############\n##########################################################################################\n",
"INFO:diceplayer:Your python version is TEST\n",
"INFO:diceplayer:Program started on 00 Test 0000 at 00:00:00\n",
"INFO:diceplayer:Environment variables:",
"INFO:diceplayer:OMP_STACKSIZE = Not set\n",
"INFO:diceplayer:------------------------------------------------------------------------------------------\n DICE variables being used in this run:\n------------------------------------------------------------------------------------------\n",
"INFO:diceplayer:dens = 0.75",
"INFO:diceplayer:isave = 1000",
"INFO:diceplayer:ljname = phb.ljc",
"INFO:diceplayer:nmol = [ 1 50 ]",
"INFO:diceplayer:nstep = [ 2000 3000 4000 ]",
"INFO:diceplayer:outname = phb",
"INFO:diceplayer:press = 1.0",
"INFO:diceplayer:progname = ~/.local/bin/dice",
"INFO:diceplayer:randominit = first",
"INFO:diceplayer:temp = 300.0",
"INFO:diceplayer:------------------------------------------------------------------------------------------\n GAUSSIAN variables being used in this run:\n------------------------------------------------------------------------------------------\n",
"INFO:diceplayer:chg_tol = 0.01",
"INFO:diceplayer:keywords = freq",
"INFO:diceplayer:level = MP2/aug-cc-pVDZ",
"INFO:diceplayer:pop = chelpg",
"INFO:diceplayer:qmprog = g16",
"INFO:diceplayer:\n",
]
self.assertEqual(cm.output, expected_output)
@@ -99,12 +97,18 @@ class TestPlayer(unittest.TestCase):
molecule_type=0,
molecule_site=0,
atom_dict={
"lbl": 0, "na": 1, "rx": 1.0, "ry": 1.0, "rz": 1.0, "chg": 1.0, "eps": 1.0
}
"lbl": 0,
"na": 1,
"rx": 1.0,
"ry": 1.0,
"rz": 1.0,
"chg": 1.0,
"eps": 1.0,
},
)
self.assertEqual(
str(context.exception),
"Invalid number of fields for site 1 for molecule type 1."
"Invalid number of fields for site 1 for molecule type 1.",
)
with self.assertRaises(ValueError) as context:
@@ -112,12 +116,18 @@ class TestPlayer(unittest.TestCase):
molecule_type=0,
molecule_site=0,
atom_dict={
"lbl": '', "na": 1, "rx": 1.0, "ry": 1.0, "rz": 1.0, "chg": 1.0, "eps": 1.0, "sig": 1.0
}
"lbl": "",
"na": 1,
"rx": 1.0,
"ry": 1.0,
"rz": 1.0,
"chg": 1.0,
"eps": 1.0,
"sig": 1.0,
},
)
self.assertEqual(
str(context.exception),
"Invalid lbl fields for site 1 for molecule type 1."
str(context.exception), "Invalid lbl fields for site 1 for molecule type 1."
)
with self.assertRaises(ValueError) as context:
@@ -125,12 +135,18 @@ class TestPlayer(unittest.TestCase):
molecule_type=0,
molecule_site=0,
atom_dict={
"lbl": 1.0, "na": '', "rx": 1.0, "ry": 1.0, "rz": 1.0, "chg": 1.0, "eps": 1.0, "sig": 1.0
}
"lbl": 1.0,
"na": "",
"rx": 1.0,
"ry": 1.0,
"rz": 1.0,
"chg": 1.0,
"eps": 1.0,
"sig": 1.0,
},
)
self.assertEqual(
str(context.exception),
"Invalid na fields for site 1 for molecule type 1."
str(context.exception), "Invalid na fields for site 1 for molecule type 1."
)
with self.assertRaises(ValueError) as context:
@@ -138,12 +154,19 @@ class TestPlayer(unittest.TestCase):
molecule_type=0,
molecule_site=0,
atom_dict={
"lbl": 1.0, "na": 1, "rx": '', "ry": 1.0, "rz": 1.0, "chg": 1.0, "eps": 1.0, "sig": 1.0
}
"lbl": 1.0,
"na": 1,
"rx": "",
"ry": 1.0,
"rz": 1.0,
"chg": 1.0,
"eps": 1.0,
"sig": 1.0,
},
)
self.assertEqual(
str(context.exception),
"Invalid rx fields for site 1 for molecule type 1. Value must be a float."
"Invalid rx fields for site 1 for molecule type 1. Value must be a float.",
)
with self.assertRaises(ValueError) as context:
@@ -151,12 +174,19 @@ class TestPlayer(unittest.TestCase):
molecule_type=0,
molecule_site=0,
atom_dict={
"lbl": 1.0, "na": 1, "rx": 1.0, "ry": '', "rz": 1.0, "chg": 1.0, "eps": 1.0, "sig": 1.0
}
"lbl": 1.0,
"na": 1,
"rx": 1.0,
"ry": "",
"rz": 1.0,
"chg": 1.0,
"eps": 1.0,
"sig": 1.0,
},
)
self.assertEqual(
str(context.exception),
"Invalid ry fields for site 1 for molecule type 1. Value must be a float."
"Invalid ry fields for site 1 for molecule type 1. Value must be a float.",
)
with self.assertRaises(ValueError) as context:
@@ -164,12 +194,19 @@ class TestPlayer(unittest.TestCase):
molecule_type=0,
molecule_site=0,
atom_dict={
"lbl": 1.0, "na": 1, "rx": 1.0, "ry": 1.0, "rz": '', "chg": 1.0, "eps": 1.0, "sig": 1.0
}
"lbl": 1.0,
"na": 1,
"rx": 1.0,
"ry": 1.0,
"rz": "",
"chg": 1.0,
"eps": 1.0,
"sig": 1.0,
},
)
self.assertEqual(
str(context.exception),
"Invalid rz fields for site 1 for molecule type 1. Value must be a float."
"Invalid rz fields for site 1 for molecule type 1. Value must be a float.",
)
with self.assertRaises(ValueError) as context:
@@ -177,12 +214,19 @@ class TestPlayer(unittest.TestCase):
molecule_type=0,
molecule_site=0,
atom_dict={
"lbl": 1.0, "na": 1, "rx": 1.0, "ry": 1.0, "rz": 1.0, "chg": '', "eps": 1.0, "sig": 1.0
}
"lbl": 1.0,
"na": 1,
"rx": 1.0,
"ry": 1.0,
"rz": 1.0,
"chg": "",
"eps": 1.0,
"sig": 1.0,
},
)
self.assertEqual(
str(context.exception),
"Invalid chg fields for site 1 for molecule type 1. Value must be a float."
"Invalid chg fields for site 1 for molecule type 1. Value must be a float.",
)
with self.assertRaises(ValueError) as context:
@@ -190,12 +234,19 @@ class TestPlayer(unittest.TestCase):
molecule_type=0,
molecule_site=0,
atom_dict={
"lbl": 1.0, "na": 1, "rx": 1.0, "ry": 1.0, "rz": 1.0, "chg": 1.0, "eps": '', "sig": 1.0
}
"lbl": 1.0,
"na": 1,
"rx": 1.0,
"ry": 1.0,
"rz": 1.0,
"chg": 1.0,
"eps": "",
"sig": 1.0,
},
)
self.assertEqual(
str(context.exception),
"Invalid eps fields for site 1 for molecule type 1. Value must be a float."
"Invalid eps fields for site 1 for molecule type 1. Value must be a float.",
)
with self.assertRaises(ValueError) as context:
@@ -203,12 +254,19 @@ class TestPlayer(unittest.TestCase):
molecule_type=0,
molecule_site=0,
atom_dict={
"lbl": 1.0, "na": 1, "rx": 1.0, "ry": 1.0, "rz": 1.0, "chg": 1.0, "eps": 1.0, "sig": ''
}
"lbl": 1.0,
"na": 1,
"rx": 1.0,
"ry": 1.0,
"rz": 1.0,
"chg": 1.0,
"eps": 1.0,
"sig": "",
},
)
self.assertEqual(
str(context.exception),
"Invalid sig fields for site 1 for molecule type 1. Value must be a float."
"Invalid sig fields for site 1 for molecule type 1. Value must be a float.",
)
@mock.patch("builtins.open", mock_open)
@@ -234,10 +292,7 @@ class TestPlayer(unittest.TestCase):
with self.assertRaises(RuntimeError) as context:
player.read_potentials()
self.assertEqual(
str(context.exception),
"Potential file phb.ljc not found."
)
self.assertEqual(str(context.exception), "Potential file phb.ljc not found.")
# Enabling file found for next tests
mock_path_exists.return_value = True
@@ -249,7 +304,7 @@ class TestPlayer(unittest.TestCase):
self.assertEqual(
str(context.exception),
"Error: expected a '*' or a '+' sign in 1st line of file phb.error.combrule.ljc"
"Error: expected a '*' or a '+' sign in 1st line of file phb.error.combrule.ljc",
)
# Testing ntypes error
@@ -259,7 +314,7 @@ class TestPlayer(unittest.TestCase):
self.assertEqual(
str(context.exception),
"Error: expected an integer in the 2nd line of file phb.error.ntypes.ljc"
"Error: expected an integer in the 2nd line of file phb.error.ntypes.ljc",
)
# Testing ntypes error on config
@@ -270,7 +325,7 @@ class TestPlayer(unittest.TestCase):
self.assertEqual(
str(context.exception),
"Error: number of molecule types in file phb.error.ntypes.config.ljc "
"must match that of 'nmol' keyword in config file"
"must match that of 'nmol' keyword in config file",
)
# Testing nsite error
@@ -280,7 +335,7 @@ class TestPlayer(unittest.TestCase):
self.assertEqual(
str(context.exception),
"Error: expected nsites to be an integer for molecule type 1"
"Error: expected nsites to be an integer for molecule type 1",
)
# Testing molname error
@@ -290,7 +345,7 @@ class TestPlayer(unittest.TestCase):
self.assertEqual(
str(context.exception),
"Error: expected nsites and molname for the molecule type 1"
"Error: expected nsites and molname for the molecule type 1",
)
@mock.patch("builtins.open", mock_open)
@@ -299,31 +354,28 @@ class TestPlayer(unittest.TestCase):
player = Player("control.test.yml")
player.read_potentials()
with self.assertLogs(level='INFO') as context:
with self.assertLogs(level="INFO") as context:
player.print_potentials()
expected_output = [
'INFO:diceplayer:==========================================================================================\n Potential parameters from file phb.ljc:\n------------------------------------------------------------------------------------------\n',
'INFO:diceplayer:Combination rule: *',
'INFO:diceplayer:Types of molecules: 2\n',
'INFO:diceplayer:1 atoms in molecule type 1:',
'INFO:diceplayer:---------------------------------------------------------------------------------',
'INFO:diceplayer:Lbl AN X Y Z Charge Epsilon Sigma Mass',
'INFO:diceplayer:---------------------------------------------------------------------------------',
'INFO:diceplayer:1 1 0.00000 0.00000 0.00000 0.000000 0.00000 0.0000 1.0079',
'INFO:diceplayer:\n',
'INFO:diceplayer:1 atoms in molecule type 2:',
'INFO:diceplayer:---------------------------------------------------------------------------------',
'INFO:diceplayer:Lbl AN X Y Z Charge Epsilon Sigma Mass',
'INFO:diceplayer:---------------------------------------------------------------------------------',
'INFO:diceplayer:1 1 0.00000 0.00000 0.00000 0.000000 0.00000 0.0000 1.0079',
'INFO:diceplayer:\n'
"INFO:diceplayer:==========================================================================================\n Potential parameters from file phb.ljc:\n------------------------------------------------------------------------------------------\n",
"INFO:diceplayer:Combination rule: *",
"INFO:diceplayer:Types of molecules: 2\n",
"INFO:diceplayer:1 atoms in molecule type 1:",
"INFO:diceplayer:---------------------------------------------------------------------------------",
"INFO:diceplayer:Lbl AN X Y Z Charge Epsilon Sigma Mass",
"INFO:diceplayer:---------------------------------------------------------------------------------",
"INFO:diceplayer:1 1 0.00000 0.00000 0.00000 0.000000 0.00000 0.0000 1.0079",
"INFO:diceplayer:\n",
"INFO:diceplayer:1 atoms in molecule type 2:",
"INFO:diceplayer:---------------------------------------------------------------------------------",
"INFO:diceplayer:Lbl AN X Y Z Charge Epsilon Sigma Mass",
"INFO:diceplayer:---------------------------------------------------------------------------------",
"INFO:diceplayer:1 1 0.00000 0.00000 0.00000 0.000000 0.00000 0.0000 1.0079",
"INFO:diceplayer:\n",
]
self.assertEqual(
context.output,
expected_output
)
self.assertEqual(context.output, expected_output)
@mock.patch("builtins.open", mock_open)
def test_dice_start(self):
@@ -336,5 +388,5 @@ class TestPlayer(unittest.TestCase):
player.dice_interface.start.assert_called_once()
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()