openff-toolkit 0.18.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. devtools/scripts/build_cookiecutter_json.py +22 -0
  2. docs/conf.py +296 -0
  3. docs/conftest.py +23 -0
  4. examples/conformer_energies/conformer_energies.py +143 -0
  5. examples/deprecated/SMIRNOFF_comparison/compare_molecule_energies.py +48 -0
  6. examples/deprecated/SMIRNOFF_comparison/compare_set_energies.py +58 -0
  7. examples/deprecated/liquid_box/create_mol2_pdb/create_mol2_pdb.py +193 -0
  8. examples/deprecated/liquid_box/openff_simulation.py +72 -0
  9. examples/deprecated/mixedFF_structure/generate_mixedFF_complex.py +56 -0
  10. examples/deprecated/testing/test_impropers/tleap_tools.py +120 -0
  11. examples/examples_helper.py +61 -0
  12. openff/toolkit/__init__.py +101 -0
  13. openff/toolkit/_tests/__init__.py +0 -0
  14. openff/toolkit/_tests/_stale_tests.py +146 -0
  15. openff/toolkit/_tests/conftest.py +108 -0
  16. openff/toolkit/_tests/create_molecules.py +494 -0
  17. openff/toolkit/_tests/mocking.py +145 -0
  18. openff/toolkit/_tests/molecule/__init__.py +1 -0
  19. openff/toolkit/_tests/molecule/test_state_enumeration.py +302 -0
  20. openff/toolkit/_tests/test_energies.py +136 -0
  21. openff/toolkit/_tests/test_examples.py +109 -0
  22. openff/toolkit/_tests/test_forcefield.py +3864 -0
  23. openff/toolkit/_tests/test_interchange.py +19 -0
  24. openff/toolkit/_tests/test_io.py +19 -0
  25. openff/toolkit/_tests/test_links.py +73 -0
  26. openff/toolkit/_tests/test_mm_molecule.py +404 -0
  27. openff/toolkit/_tests/test_molecule.py +4246 -0
  28. openff/toolkit/_tests/test_nagl.py +169 -0
  29. openff/toolkit/_tests/test_parameter_plugins.py +89 -0
  30. openff/toolkit/_tests/test_parameters.py +2898 -0
  31. openff/toolkit/_tests/test_toolkit_io.py +1088 -0
  32. openff/toolkit/_tests/test_toolkits.py +4418 -0
  33. openff/toolkit/_tests/test_topology.py +2485 -0
  34. openff/toolkit/_tests/test_utils.py +101 -0
  35. openff/toolkit/_tests/test_utils_callback.py +178 -0
  36. openff/toolkit/_tests/test_utils_collections.py +263 -0
  37. openff/toolkit/_tests/test_utils_serialization.py +168 -0
  38. openff/toolkit/_tests/utils.py +1747 -0
  39. openff/toolkit/data/README.md +8 -0
  40. openff/toolkit/data/molecules/AlkEthOH_test_filt1_ff.mol2 +2026 -0
  41. openff/toolkit/data/molecules/AlkEthOH_test_filt1_tripos.mol2 +2026 -0
  42. openff/toolkit/data/molecules/AlkEthOH_tripos.tar.gz +0 -0
  43. openff/toolkit/data/molecules/CID20742535_anion.sdf +50 -0
  44. openff/toolkit/data/molecules/CID20742535_neutral.sdf +51 -0
  45. openff/toolkit/data/molecules/FreeSolv.tar.gz +0 -0
  46. openff/toolkit/data/molecules/MiniDrugBank.sdf +33988 -0
  47. openff/toolkit/data/molecules/MiniDrugBank_tripos.mol2 +35462 -0
  48. openff/toolkit/data/molecules/PT2385.sdf +90 -0
  49. openff/toolkit/data/molecules/QL8.sdf +401 -0
  50. openff/toolkit/data/molecules/README.md +21 -0
  51. openff/toolkit/data/molecules/benzene.crd +8 -0
  52. openff/toolkit/data/molecules/benzene.mol2 +34 -0
  53. openff/toolkit/data/molecules/benzene.top +166 -0
  54. openff/toolkit/data/molecules/butane_multi.sdf +231 -0
  55. openff/toolkit/data/molecules/cyclohexane.mol2 +44 -0
  56. openff/toolkit/data/molecules/ethanol.mol2 +25 -0
  57. openff/toolkit/data/molecules/ethanol.sdf +23 -0
  58. openff/toolkit/data/molecules/ethanol_partial_charges.sdf +26 -0
  59. openff/toolkit/data/molecules/ethanol_reordered.mol2 +25 -0
  60. openff/toolkit/data/molecules/ethanol_reordered.sdf +23 -0
  61. openff/toolkit/data/molecules/ethene_rdkit.sdf +17 -0
  62. openff/toolkit/data/molecules/laromustine_ff.mol2 +71 -0
  63. openff/toolkit/data/molecules/laromustine_tripos.mol2 +71 -0
  64. openff/toolkit/data/molecules/methane_multiconformer.sdf +32 -0
  65. openff/toolkit/data/molecules/methane_multiconformer_properties.sdf +45 -0
  66. openff/toolkit/data/molecules/nh4.pdb +7 -0
  67. openff/toolkit/data/molecules/po4_phenylphosphate.pdb +46 -0
  68. openff/toolkit/data/molecules/ruxolitinib_conformers.sdf +910 -0
  69. openff/toolkit/data/molecules/toluene.mol2 +38 -0
  70. openff/toolkit/data/molecules/toluene.mol2.gz +0 -0
  71. openff/toolkit/data/molecules/toluene.pdb +30 -0
  72. openff/toolkit/data/molecules/toluene.sdf +36 -0
  73. openff/toolkit/data/molecules/toluene_charged.mol2 +38 -0
  74. openff/toolkit/data/molecules/z_3_hydroxy_propenal.sdf +23 -0
  75. openff/toolkit/data/proteins/5tbm_complex_solv.pdb +1928 -0
  76. openff/toolkit/data/proteins/5tbm_complex_solv_box.pdb +19563 -0
  77. openff/toolkit/data/proteins/CTerminal_ALA.pdb +20 -0
  78. openff/toolkit/data/proteins/CTerminal_ALA.sdf +43 -0
  79. openff/toolkit/data/proteins/CTerminal_ALA_ALA.pdb +30 -0
  80. openff/toolkit/data/proteins/CTerminal_ALA_ALA_misnamedH.pdb +30 -0
  81. openff/toolkit/data/proteins/CTerminal_ARG.pdb +34 -0
  82. openff/toolkit/data/proteins/CTerminal_TRP.pdb +34 -0
  83. openff/toolkit/data/proteins/MainChain_ALA.pdb +25 -0
  84. openff/toolkit/data/proteins/MainChain_ALA.sdf +52 -0
  85. openff/toolkit/data/proteins/MainChain_ALA_ALA.pdb +35 -0
  86. openff/toolkit/data/proteins/MainChain_ALA_ALA.sdf +69 -0
  87. openff/toolkit/data/proteins/MainChain_ALA_ALA_icodes.pdb +35 -0
  88. openff/toolkit/data/proteins/MainChain_ALA_ALA_no_hydrogens.pdb +17 -0
  89. openff/toolkit/data/proteins/MainChain_ARG.pdb +39 -0
  90. openff/toolkit/data/proteins/MainChain_ARG.sdf +81 -0
  91. openff/toolkit/data/proteins/MainChain_CYM.pdb +25 -0
  92. openff/toolkit/data/proteins/MainChain_CYS.pdb +26 -0
  93. openff/toolkit/data/proteins/MainChain_CYX.pdb +48 -0
  94. openff/toolkit/data/proteins/MainChain_CYX.sdf +96 -0
  95. openff/toolkit/data/proteins/MainChain_GLH.sdf +64 -0
  96. openff/toolkit/data/proteins/MainChain_GLU.sdf +63 -0
  97. openff/toolkit/data/proteins/MainChain_HID.pdb +32 -0
  98. openff/toolkit/data/proteins/MainChain_HIE.pdb +32 -0
  99. openff/toolkit/data/proteins/MainChain_HIE.sdf +67 -0
  100. openff/toolkit/data/proteins/MainChain_HIP.pdb +33 -0
  101. openff/toolkit/data/proteins/MainChain_HIP.sdf +70 -0
  102. openff/toolkit/data/proteins/MainChain_PRO.pdb +29 -0
  103. openff/toolkit/data/proteins/MainChain_TRP.pdb +39 -0
  104. openff/toolkit/data/proteins/NTerminal_ALA.pdb +21 -0
  105. openff/toolkit/data/proteins/NTerminal_ALA.sdf +45 -0
  106. openff/toolkit/data/proteins/NTerminal_ALA_ALA.pdb +31 -0
  107. openff/toolkit/data/proteins/NTerminal_ARG.pdb +35 -0
  108. openff/toolkit/data/proteins/NTerminal_TRP.pdb +35 -0
  109. openff/toolkit/data/proteins/T4-protein-unprocessed-4w51.pdb +1892 -0
  110. openff/toolkit/data/proteins/T4-protein.mol2 +5461 -0
  111. openff/toolkit/data/proteins/T4-protein.pdb +2637 -0
  112. openff/toolkit/data/proteins/T4-protein.sdf +5318 -0
  113. openff/toolkit/data/proteins/T4_protein_waters.pdb +2965 -0
  114. openff/toolkit/data/proteins/TwoMol_SER_CYS.pdb +50 -0
  115. openff/toolkit/data/proteins/aa_residues_substructures_explicit_bond_orders_with_caps.json +6435 -0
  116. openff/toolkit/data/proteins/aa_residues_substructures_explicit_bond_orders_with_caps_explicit_connectivity.json +6675 -0
  117. openff/toolkit/data/proteins/aa_residues_substructures_with_caps.json +6297 -0
  118. openff/toolkit/data/proteins/ace-ZZZ-gly-nme.pdb +78 -0
  119. openff/toolkit/data/proteins/ace-ZZZ-gly-nme.sdf +98 -0
  120. openff/toolkit/data/proteins/ace-a10-nme.sdf +229 -0
  121. openff/toolkit/data/proteins/ace-ala-nh2.pdb +21 -0
  122. openff/toolkit/data/proteins/fluoresceine_dyed_helix_capped.pdb +338 -0
  123. openff/toolkit/data/proteins/neutral_arginine.pdb +31 -0
  124. openff/toolkit/data/proteins/split_chain.pdb +74 -0
  125. openff/toolkit/data/proteins/split_chain_reordered.pdb +74 -0
  126. openff/toolkit/data/reference_energies/README.md +6 -0
  127. openff/toolkit/data/reference_energies/reference_0.8.0.json +1 -0
  128. openff/toolkit/data/reference_energies/reference_0.8.0.json.txt +180 -0
  129. openff/toolkit/data/reference_energies/reference_after_1007.json +1 -0
  130. openff/toolkit/data/reference_energies/reference_after_1007.json.txt +350 -0
  131. openff/toolkit/data/reference_energies/reference_after_802.json +1 -0
  132. openff/toolkit/data/reference_energies/reference_after_802.json.txt +291 -0
  133. openff/toolkit/data/systems/README.md +3 -0
  134. openff/toolkit/data/systems/amber/cyclohexane_ethanol_0.4_0.6.inpcrd +1511 -0
  135. openff/toolkit/data/systems/amber/cyclohexane_ethanol_0.4_0.6.prmtop +13367 -0
  136. openff/toolkit/data/systems/monomers/butanol.mol2 +38 -0
  137. openff/toolkit/data/systems/monomers/butanol.sdf +88 -0
  138. openff/toolkit/data/systems/monomers/cyclohexane.mol2 +45 -0
  139. openff/toolkit/data/systems/monomers/cyclohexane.sdf +105 -0
  140. openff/toolkit/data/systems/monomers/ethanol.mol2 +26 -0
  141. openff/toolkit/data/systems/monomers/ethanol.sdf +58 -0
  142. openff/toolkit/data/systems/monomers/methane.mol2 +18 -0
  143. openff/toolkit/data/systems/monomers/methane.sdf +38 -0
  144. openff/toolkit/data/systems/monomers/propane.mol2 +30 -0
  145. openff/toolkit/data/systems/monomers/propane.sdf +68 -0
  146. openff/toolkit/data/systems/monomers/tip3p_water.mol2 +14 -0
  147. openff/toolkit/data/systems/monomers/water.mol2 +14 -0
  148. openff/toolkit/data/systems/monomers/water.sdf +28 -0
  149. openff/toolkit/data/systems/packmol_boxes/cyclohexane_ethanol_0.4_0.6.pdb +6272 -0
  150. openff/toolkit/data/systems/packmol_boxes/cyclohexane_water.pdb +5524 -0
  151. openff/toolkit/data/systems/packmol_boxes/ethanol_water.pdb +5174 -0
  152. openff/toolkit/data/systems/packmol_boxes/propane_methane_butanol_0.2_0.3_0.5.pdb +7959 -0
  153. openff/toolkit/data/systems/test_systems/1_cyclohexane_1_ethanol.pdb +61 -0
  154. openff/toolkit/data/systems/test_systems/1_ethanol.pdb +24 -0
  155. openff/toolkit/data/systems/test_systems/1_ethanol_no_conect.pdb +15 -0
  156. openff/toolkit/data/systems/test_systems/1_ethanol_reordered.pdb +24 -0
  157. openff/toolkit/data/systems/test_systems/PE.pdb +67 -0
  158. openff/toolkit/data/systems/test_systems/README.md +3 -0
  159. openff/toolkit/data/systems/test_systems/T4_lysozyme_water_ions.pdb +29240 -0
  160. openff/toolkit/data/test_forcefields/Frosst_AlkEthOH.offxml +61 -0
  161. openff/toolkit/data/test_forcefields/Frosst_AlkEthOH_GBSA.offxml +78 -0
  162. openff/toolkit/data/test_forcefields/Frosst_AlkEthOH_MDL.offxml +62 -0
  163. openff/toolkit/data/test_forcefields/Frosst_AlkEthOH_parmAtFrosst.offxml +64 -0
  164. openff/toolkit/data/test_forcefields/GBSA_HCT-1.0.offxml +21 -0
  165. openff/toolkit/data/test_forcefields/GBSA_OBC1-1.0.offxml +18 -0
  166. openff/toolkit/data/test_forcefields/GBSA_OBC2-1.0.offxml +18 -0
  167. openff/toolkit/data/test_forcefields/README.md +23 -0
  168. openff/toolkit/data/test_forcefields/ammonia_minimal.offxml +27 -0
  169. openff/toolkit/data/test_forcefields/benzene_minimal.offxml +29 -0
  170. openff/toolkit/data/test_forcefields/chargeincrement-test.offxml +16 -0
  171. openff/toolkit/data/test_forcefields/frcmod.Frosst_AlkEthOH_withIDs +85 -0
  172. openff/toolkit/data/test_forcefields/ion_charges.offxml +18 -0
  173. openff/toolkit/data/test_forcefields/mangled.offxml +9 -0
  174. openff/toolkit/data/test_forcefields/old/README.md +9 -0
  175. openff/toolkit/data/test_forcefields/old/hbonds.offxml +11 -0
  176. openff/toolkit/data/test_forcefields/old/test_ff_0_0_2_spec_0_1.offxml +332 -0
  177. openff/toolkit/data/test_forcefields/old/test_ff_0_0_2_spec_0_2.offxml +338 -0
  178. openff/toolkit/data/test_forcefields/old/test_ff_0_0_2_spec_0_3.offxml +329 -0
  179. openff/toolkit/data/test_forcefields/old/test_ff_0_0_4_fixed_spec_0_1.offxml +342 -0
  180. openff/toolkit/data/test_forcefields/old/test_ff_0_0_4_fixed_spec_0_2.offxml +348 -0
  181. openff/toolkit/data/test_forcefields/old/test_ff_0_0_4_fixed_spec_0_3.offxml +337 -0
  182. openff/toolkit/data/test_forcefields/old/test_ff_0_0_4_spec_0_1.offxml +340 -0
  183. openff/toolkit/data/test_forcefields/old/test_ff_0_0_4_spec_0_2.offxml +346 -0
  184. openff/toolkit/data/test_forcefields/old/test_ff_0_0_4_spec_0_3.offxml +337 -0
  185. openff/toolkit/data/test_forcefields/smirff99Frosst_reference_0_1_spec.offxml +349 -0
  186. openff/toolkit/data/test_forcefields/smirnoff99Frosst_experimental.offxml +343 -0
  187. openff/toolkit/data/test_forcefields/smirnoff99Frosst_reference_0_1_spec.offxml +349 -0
  188. openff/toolkit/data/test_forcefields/smirnoff99Frosst_reference_0_2_spec.offxml +356 -0
  189. openff/toolkit/data/test_forcefields/test_forcefield.offxml +346 -0
  190. openff/toolkit/data/test_forcefields/tip3p.offxml +22 -0
  191. openff/toolkit/data/test_forcefields/unsupported_smirnoff_version.offxml +9 -0
  192. openff/toolkit/topology/__init__.py +43 -0
  193. openff/toolkit/topology/_mm_molecule.py +663 -0
  194. openff/toolkit/topology/molecule.py +6102 -0
  195. openff/toolkit/topology/topology.py +2555 -0
  196. openff/toolkit/typing/__init__.py +1 -0
  197. openff/toolkit/typing/engines/__init__.py +1 -0
  198. openff/toolkit/typing/engines/smirnoff/__init__.py +31 -0
  199. openff/toolkit/typing/engines/smirnoff/forcefield.py +1440 -0
  200. openff/toolkit/typing/engines/smirnoff/io.py +234 -0
  201. openff/toolkit/typing/engines/smirnoff/parameters.py +3839 -0
  202. openff/toolkit/typing/engines/smirnoff/plugins.py +91 -0
  203. openff/toolkit/utils/__init__.py +57 -0
  204. openff/toolkit/utils/_viz.py +104 -0
  205. openff/toolkit/utils/ambertools_wrapper.py +507 -0
  206. openff/toolkit/utils/base_wrapper.py +268 -0
  207. openff/toolkit/utils/builtin_wrapper.py +136 -0
  208. openff/toolkit/utils/callback.py +187 -0
  209. openff/toolkit/utils/collections.py +247 -0
  210. openff/toolkit/utils/constants.py +18 -0
  211. openff/toolkit/utils/exceptions.py +766 -0
  212. openff/toolkit/utils/nagl_wrapper.py +164 -0
  213. openff/toolkit/utils/openeye_wrapper.py +2750 -0
  214. openff/toolkit/utils/rdkit_wrapper.py +3302 -0
  215. openff/toolkit/utils/serialization.py +466 -0
  216. openff/toolkit/utils/toolkit_registry.py +509 -0
  217. openff/toolkit/utils/toolkits.py +217 -0
  218. openff/toolkit/utils/utils.py +805 -0
  219. openff_toolkit-0.18.0.dist-info/METADATA +109 -0
  220. openff_toolkit-0.18.0.dist-info/RECORD +231 -0
  221. openff_toolkit-0.18.0.dist-info/WHEEL +5 -0
  222. openff_toolkit-0.18.0.dist-info/licenses/LICENSE +21 -0
  223. openff_toolkit-0.18.0.dist-info/top_level.txt +5 -0
  224. utilities/deprecated/convert_frosst/convert_frcmod_0.1.py +251 -0
  225. utilities/deprecated/convert_frosst/convert_frcmod_0.2.py +249 -0
  226. utilities/deprecated/convert_frosst/convert_frcmod_0.3.py +249 -0
  227. utilities/make_substructure_dict/_cif_to_substructure_dict.py +955 -0
  228. utilities/make_substructure_dict/_make_chemical_substructures.py +46 -0
  229. utilities/make_substructure_dict/_make_metadata_assignment_substructures.py +35 -0
  230. utilities/test_plugins/custom_plugins/handler_plugins.py +48 -0
  231. utilities/test_plugins/setup.py +19 -0
@@ -0,0 +1,101 @@
1
+ """
2
+ Tests for utility methods
3
+
4
+ """
5
+
6
+ import os
7
+
8
+ import pytest
9
+
10
+ from openff.toolkit import unit
11
+
12
+
13
+ def test_subclasses():
14
+ """Test that all subclasses (and descendents) are correctly identified by all_subclasses()"""
15
+
16
+ from openff.toolkit.utils.utils import all_subclasses
17
+
18
+ class Foo:
19
+ pass
20
+
21
+ class FooSubclass1(Foo):
22
+ pass
23
+
24
+ class FooSubclass2(Foo):
25
+ pass
26
+
27
+ class FooSubSubclass(FooSubclass1):
28
+ pass
29
+
30
+ subclass_names = [cls.__name__ for cls in all_subclasses(Foo)]
31
+ assert set(subclass_names) == set(["FooSubclass1", "FooSubclass2", "FooSubSubclass"])
32
+
33
+
34
+ def test_temporary_cd():
35
+ """Test temporary_cd() context manager"""
36
+ initial_dir = os.getcwd()
37
+ temporary_dir = "/"
38
+ from openff.toolkit.utils import temporary_cd
39
+
40
+ with temporary_cd(temporary_dir):
41
+ assert os.getcwd() == temporary_dir
42
+ assert os.getcwd() == initial_dir
43
+
44
+
45
+ def test_get_data_file_path():
46
+ """Test get_data_file_path()"""
47
+ from openff.toolkit.utils import get_data_file_path
48
+
49
+ filename = get_data_file_path("test_forcefields/tip3p.offxml")
50
+ assert os.path.exists(filename)
51
+
52
+
53
+ def test_get_data_file_path_does_not_exist():
54
+ from openff.toolkit.utils import get_data_file_path
55
+
56
+ with pytest.raises(ValueError, match="does not exist"):
57
+ get_data_file_path("bogus.offxml")
58
+
59
+
60
+ def test_dimensionless_units():
61
+ from openff.toolkit.utils.utils import string_to_unit, unit_to_string
62
+
63
+ assert string_to_unit("dimensionless") == unit.dimensionless
64
+
65
+ unit_string = unit_to_string(unit.dimensionless)
66
+ unit_value = string_to_unit(unit_string)
67
+
68
+ assert unit_value == unit.dimensionless
69
+
70
+
71
+ def test_object_to_quantity_accepts_openmm():
72
+ import openmm
73
+
74
+ from openff.toolkit.utils.utils import object_to_quantity
75
+
76
+ val = object_to_quantity(1.0 * openmm.unit.angstrom)
77
+ assert val == 1.0 * unit.angstrom
78
+ val = object_to_quantity(1.0 * openmm.unit.nanometer)
79
+ assert val == 10.0 * unit.angstrom
80
+ val = object_to_quantity(
81
+ [
82
+ 2.0 * openmm.unit.angstrom,
83
+ 2.0 * openmm.unit.nanometer,
84
+ 2.0 * openmm.unit.dimensionless,
85
+ ]
86
+ )
87
+ assert val == [2.0 * unit.angstrom, 20.0 * unit.angstrom, 2 * unit.dimensionless]
88
+
89
+
90
+ def test_sort_smirnoff_dict():
91
+ from collections import OrderedDict
92
+
93
+ from openff.toolkit.typing.engines.smirnoff import ForceField
94
+ from openff.toolkit.utils.utils import get_data_file_path, sort_smirnoff_dict
95
+
96
+ forcefield = ForceField(get_data_file_path("test_forcefields/test_forcefield.offxml"))
97
+ smirnoff_dict = forcefield._to_smirnoff_data()
98
+
99
+ # Ensure data is not created or destroyed
100
+ # dict.__eq__ does not check order
101
+ assert smirnoff_dict == OrderedDict(sort_smirnoff_dict(forcefield._to_smirnoff_data()))
@@ -0,0 +1,178 @@
1
+ """
2
+ Tests for callback utility classes and functions.
3
+
4
+ """
5
+
6
+ import pytest
7
+
8
+ from openff.toolkit.utils.callback import Callbackable, callback_method
9
+ from openff.toolkit.utils.exceptions import CallbackRegistrationError
10
+
11
+
12
+ class CallHistory:
13
+ """Used to keep track of the order in which callbacks and methods are called by Callbackable."""
14
+
15
+ history = None
16
+
17
+ def reset_history(self):
18
+ CallHistory.history = []
19
+
20
+ @classmethod
21
+ def add_history_entry(cls, name, *args, **kwargs):
22
+ # Store args and kwargs in the history only if they are given.
23
+ history_entry = []
24
+ if len(args) != 0:
25
+ history_entry.append(args)
26
+ if len(kwargs) != 0:
27
+ history_entry.append(kwargs)
28
+
29
+ if len(history_entry) == 0:
30
+ cls.history.append(name)
31
+ else:
32
+ cls.history.append([name, *history_entry])
33
+
34
+ def instance_callback(self, callbackable, event_name, *args, **kwargs):
35
+ assert isinstance(self, object)
36
+ CallHistory.add_history_entry("callback_" + event_name, *args, **kwargs)
37
+
38
+ @classmethod
39
+ def class_callback(cls, callbackable, event_name, *args, **kwargs):
40
+ assert isinstance(cls, type)
41
+ CallHistory.add_history_entry("callback_" + event_name, *args, **kwargs)
42
+
43
+ @staticmethod
44
+ def static_callback(callbackable, event_name, *args, **kwargs):
45
+ CallHistory.add_history_entry("callback_" + event_name, *args, **kwargs)
46
+
47
+
48
+ call_history = CallHistory()
49
+
50
+
51
+ class TestCallbackable:
52
+ """Test suite for the Callbackable base class."""
53
+
54
+ class MyCallbackable(Callbackable):
55
+ @callback_method
56
+ def instance_method(self, *args, **kwargs):
57
+ CallHistory.add_history_entry("instance_method", *args, **kwargs)
58
+
59
+ @callback_method
60
+ def __iadd__(self, other):
61
+ CallHistory.add_history_entry("__iadd__", other)
62
+
63
+ @callback_method(events=["event1"])
64
+ def event_method1(self, *args, **kwargs):
65
+ CallHistory.add_history_entry("event_method1", *args, **kwargs)
66
+
67
+ @callback_method(events=["event1", "event2"])
68
+ def event_method2(self, *args, **kwargs):
69
+ CallHistory.add_history_entry("event_method2", *args, **kwargs)
70
+
71
+ def check_method_call_order(self, callbackable, event_name, event_sequence, *args, **kwargs):
72
+ """Check that callback and methods/attributes are invoked in the correct order.
73
+
74
+ This also formats the history correctly if args and kwargs are given.
75
+ """
76
+ # Modify the expected history if args and kwargs are given.
77
+ if len(args) == 0 and len(kwargs) == 0:
78
+ expected_history = event_sequence
79
+ else:
80
+ expected_history = [[event_name] for event_name in event_sequence]
81
+ if len(args) != 0:
82
+ for event in expected_history:
83
+ event.append(args)
84
+ if len(kwargs) != 0:
85
+ for event in expected_history:
86
+ event.append(kwargs)
87
+
88
+ # Reset history and verify that the calls are in the correct order.
89
+ call_history.reset_history()
90
+ getattr(callbackable, event_name)(*args, **kwargs)
91
+ assert call_history.history == expected_history
92
+
93
+ @pytest.mark.parametrize("event_name", ["instance_method"])
94
+ @pytest.mark.parametrize(
95
+ "callback",
96
+ [
97
+ call_history.instance_callback,
98
+ CallHistory.class_callback,
99
+ CallHistory.static_callback,
100
+ ],
101
+ )
102
+ @pytest.mark.parametrize("args,kwargs", [([], {}), ([1, 2.0], {"kwarg1": 0, "kwarg2": None})])
103
+ def test_register_method_callback(self, event_name, callback, args, kwargs):
104
+ """Methods' callbacks are invoked in the correct order and with the correct arguments."""
105
+ callbackable = TestCallbackable.MyCallbackable()
106
+
107
+ # No callback is called before registration.
108
+ event_sequence = [event_name]
109
+ self.check_method_call_order(callbackable, event_name, event_sequence, *args, **kwargs)
110
+
111
+ # Register the callback.
112
+ callbackable.register_callback(event_name, callback)
113
+
114
+ # After the registration, the callback is invoked correctly.
115
+ event_sequence = [event_name, "callback_" + event_name]
116
+ self.check_method_call_order(callbackable, event_name, event_sequence, *args, **kwargs)
117
+
118
+ def test_register_magic_method_callback(self):
119
+ """Callbacks registered to magic methods are invoked correctly."""
120
+ callbackable = TestCallbackable.MyCallbackable()
121
+ callbackable.register_callback("__iadd__", call_history.instance_callback)
122
+
123
+ extension = [1, 2]
124
+ call_history.reset_history()
125
+ callbackable += extension
126
+ assert call_history.history == [
127
+ ["__iadd__", (extension,)],
128
+ ["callback___iadd__", (extension,)],
129
+ ]
130
+
131
+ def test_register_event_callback(self):
132
+ """Callbacks registered to a event are handled corectly."""
133
+ callbackable = TestCallbackable.MyCallbackable()
134
+
135
+ # Register the callbacks to event1 (event_method1 and event_method2).
136
+ callbackable.register_callback("event1", call_history.instance_callback)
137
+ callbackable.register_callback("event1", CallHistory.class_callback)
138
+ # Register one callback to event2 (only event_method2).
139
+ callbackable.register_callback("event2", CallHistory.static_callback)
140
+
141
+ # Check the event sequence for both methods belong to the two events.
142
+ event_sequence = [
143
+ "event_method1",
144
+ "callback_event_method1",
145
+ "callback_event_method1",
146
+ ]
147
+ self.check_method_call_order(callbackable, "event_method1", event_sequence)
148
+
149
+ event_sequence = [
150
+ "event_method2",
151
+ "callback_event_method2",
152
+ "callback_event_method2",
153
+ "callback_event_method2",
154
+ ]
155
+ self.check_method_call_order(callbackable, "event_method2", event_sequence)
156
+
157
+ def test_not_callback_method_raise_exception(self):
158
+ """An exception is raised if a callback is registered for a method not tagged with callback_method."""
159
+
160
+ class TempCallbackable(Callbackable):
161
+ def not_callback_method(self):
162
+ pass
163
+
164
+ callbackable = TempCallbackable()
165
+ with pytest.raises(
166
+ CallbackRegistrationError,
167
+ match="is not tagged with the @callback_method decorator",
168
+ ):
169
+ callbackable.register_callback("not_callback_method", call_history.instance_callback)
170
+
171
+ def test_unknown_event_raise_exception(self):
172
+ """An exception is raised if a callback is registered for an unknown callback event."""
173
+ callbackable = TestCallbackable.MyCallbackable()
174
+ with pytest.raises(
175
+ CallbackRegistrationError,
176
+ match='is associated to the callback event "unknown"',
177
+ ):
178
+ callbackable.register_callback("unknown", call_history.instance_callback)
@@ -0,0 +1,263 @@
1
+ """
2
+ Tests for custom collections classes.
3
+
4
+ """
5
+
6
+ import copy
7
+
8
+ import pytest
9
+
10
+ from openff.toolkit.utils.collections import ValidatedDict, ValidatedList
11
+
12
+
13
+ class TestValidatedMixin:
14
+ def test_pickle(self):
15
+ """Test pickle roundtripping"""
16
+ # TODO: implement this test
17
+ pass
18
+
19
+
20
+ class TestValidatedList(TestValidatedMixin):
21
+ """Test suite for the ValidatedList class."""
22
+
23
+ def test_validators(self):
24
+ """Validators of ValidatedList are called correctly."""
25
+
26
+ def is_positive(value):
27
+ if value <= 0:
28
+ raise TypeError("value is not positive")
29
+
30
+ # The constructor should check all elements.
31
+ with pytest.raises(TypeError, match="value is not positive"):
32
+ ValidatedList([1, -2], validator=is_positive)
33
+
34
+ vlist = ValidatedList([1, 2, 3], validator=is_positive)
35
+
36
+ # __setitem__()
37
+ with pytest.raises(TypeError, match="value is not positive"):
38
+ vlist[2] = -1
39
+ with pytest.raises(TypeError, match="value is not positive"):
40
+ vlist[0:2] = [2, 3, -1]
41
+
42
+ # append()
43
+ with pytest.raises(TypeError, match="value is not positive"):
44
+ vlist.append(-4)
45
+
46
+ # extend() and __iadd__()
47
+ with pytest.raises(TypeError, match="value is not positive"):
48
+ vlist.extend([6, -1])
49
+ with pytest.raises(TypeError, match="value is not positive"):
50
+ vlist += [6, -1]
51
+
52
+ # insert()
53
+ with pytest.raises(TypeError, match="value is not positive"):
54
+ vlist.insert(1, -3)
55
+
56
+ def test_converters(self):
57
+ """Custom converters of ValidatedList are called correctly."""
58
+ # All elements are converted on construction.
59
+ vlist = ValidatedList([1, 2.0, "3"], converter=int)
60
+ assert vlist == [1, 2, 3]
61
+
62
+ # __setitem__()
63
+ vlist[2] = "4"
64
+ assert vlist[2] == 4
65
+ vlist[0:3] = ["2", "3", 4]
66
+ assert vlist == [2, 3, 4]
67
+
68
+ # append()
69
+ vlist.append("5")
70
+ assert vlist[3] == 5
71
+
72
+ # extend() and __iadd__()
73
+ vlist.extend([6, "7"])
74
+ assert vlist[5] == 7
75
+ vlist += ["8", 9]
76
+ assert vlist[6] == 8
77
+
78
+ # insert()
79
+ vlist.insert(5, "10")
80
+ assert vlist[5] == 10
81
+
82
+ def test_validators_and_converters(self):
83
+ """Custom converters of ValidatedList are called correctly."""
84
+
85
+ def is_positive(value):
86
+ if value <= 0:
87
+ raise TypeError("value is not positive")
88
+
89
+ # Validators are run after converters.
90
+ vlist = ValidatedList([1, 2, -3], converter=abs, validator=is_positive)
91
+ assert vlist == [1, 2, 3]
92
+
93
+ # __setitem__
94
+ vlist[2] = -1
95
+ assert vlist[2] == 1
96
+ with pytest.raises(TypeError, match="value is not positive"):
97
+ vlist[2] = 0
98
+ vlist[0:3] = [2, 3, -1]
99
+ assert vlist == [2, 3, 1]
100
+ with pytest.raises(TypeError, match="value is not positive"):
101
+ vlist[0:3] = [2, 3, 0]
102
+
103
+ # append()
104
+ vlist.append(-4)
105
+ assert vlist[-1] == 4
106
+ with pytest.raises(TypeError, match="value is not positive"):
107
+ vlist.append(0)
108
+
109
+ # extend() and __iadd__()
110
+ vlist.extend([6, -1])
111
+ assert vlist[-2:] == [6, 1]
112
+ with pytest.raises(TypeError, match="value is not positive"):
113
+ vlist.extend([6, 0])
114
+ vlist += [6, -2]
115
+ assert vlist[-2:] == [6, 2]
116
+ with pytest.raises(TypeError, match="value is not positive"):
117
+ vlist += [6, 0]
118
+
119
+ # insert()
120
+ vlist.insert(1, -3)
121
+ assert vlist[1] == 3
122
+ with pytest.raises(TypeError, match="value is not positive"):
123
+ vlist.insert(1, 0)
124
+
125
+ def test_multiple_converters(self):
126
+ """Multiple converters of ValidatedList are called in order."""
127
+ vlist = ValidatedList([1, 2, -3], converter=[abs, str])
128
+ assert vlist == ["1", "2", "3"]
129
+
130
+ def test_multiple_validators(self):
131
+ """Multiple converters of ValidatedList are called in order."""
132
+
133
+ def is_positive(value):
134
+ if value <= 0:
135
+ raise TypeError("value must be positive")
136
+
137
+ def is_odd(value):
138
+ if value % 2 == 0:
139
+ raise TypeError("value must be odd")
140
+
141
+ with pytest.raises(TypeError, match="value must be positive"):
142
+ ValidatedList([-1, -3], validator=[is_positive, is_odd])
143
+
144
+ with pytest.raises(TypeError, match="value must be odd"):
145
+ ValidatedList([2, 4], validator=[is_positive, is_odd])
146
+
147
+ def test_copy(self):
148
+ """A copy of a ValidatedList returns another ValidatedList."""
149
+ vlist = ValidatedList([1, 2, 3])
150
+ assert isinstance(vlist.copy(), ValidatedList)
151
+ assert isinstance(copy.copy(vlist), ValidatedList)
152
+ assert isinstance(copy.deepcopy(vlist), ValidatedList)
153
+
154
+ def test_slice(self):
155
+ """A slice of a ValidatedList returns another ValidatedList."""
156
+ vlist = ValidatedList([1, 2, 3])
157
+ assert isinstance(vlist[:2], ValidatedList)
158
+
159
+
160
+ class TestValidatedDict(TestValidatedMixin):
161
+ """Test suite for the ValidatedDict class."""
162
+
163
+ def test_validators(self):
164
+ """Validators of ValidatedDict are called correctly."""
165
+
166
+ def is_positive(value):
167
+ if value <= 0:
168
+ raise TypeError("value is not positive")
169
+
170
+ # The constructor should check all elements.
171
+ with pytest.raises(TypeError, match="value is not positive"):
172
+ ValidatedDict({"a": 1, "b": -2}, validator=is_positive)
173
+
174
+ d = ValidatedDict({"x": 1, "y": 2, "z": 3}, validator=is_positive)
175
+
176
+ # __setitem__()
177
+ with pytest.raises(TypeError, match="value is not positive"):
178
+ d["me!"] = -1
179
+
180
+ # update()
181
+ with pytest.raises(TypeError, match="value is not positive"):
182
+ d.update((("moe", 2), ("larry", 3), ("curly", -4)))
183
+ with pytest.raises(TypeError, match="value is not positive"):
184
+ d.update({"moe": 2, "larry": 3, "curly": -4})
185
+ with pytest.raises(TypeError, match="exactly one positional"):
186
+ d.update({"moe": 2}, {"larry": 3}, {"curly": -4})
187
+ with pytest.raises(TypeError, match="not accept named"):
188
+ d.update({"moe": 2}, moe=2, larry=3, curly=-4)
189
+
190
+ def test_converters(self):
191
+ """Custom converters of ValidatedDict are called correctly."""
192
+ # All elements are converted on construction.
193
+ d = ValidatedDict({"x": 1, "y": 2.0, "z": "3"}, converter=int)
194
+ assert d == {"x": 1, "y": 2, "z": 3}
195
+
196
+ # __setitem__()
197
+ d["y"] = "4"
198
+ assert d["y"] == 4
199
+ d["w"] = "-20"
200
+ assert d["w"] == -20
201
+
202
+ # update()
203
+ d.update({6: "7"})
204
+ assert d[6] == 7
205
+
206
+ d.update(((6, "8"), ("shemp", "-30")))
207
+ assert d[6] == 8
208
+ assert d["shemp"] == -30
209
+
210
+ def test_validators_and_converters(self):
211
+ """Custom converters of ValidatedDict are called correctly."""
212
+
213
+ def is_positive(value):
214
+ if value <= 0:
215
+ raise TypeError("value is not positive")
216
+
217
+ # Validators are run after converters.
218
+ d = ValidatedDict({"a": 1, "b": 2, "c": -3}, converter=abs, validator=is_positive)
219
+ assert d == {"a": 1, "b": 2, "c": 3}
220
+
221
+ # __setitem__
222
+ d[2] = -1
223
+ assert d[2] == 1
224
+ with pytest.raises(TypeError, match="value is not positive"):
225
+ d[2] = 0
226
+
227
+ # update()
228
+ d.update({"x": 6, "y": -1})
229
+ assert d["y"] == 1
230
+ with pytest.raises(TypeError, match="value is not positive"):
231
+ d.update({6: 0})
232
+
233
+ d.update((("x", 6), ("y", -1)))
234
+ assert d["y"] == 1
235
+
236
+ def test_multiple_converters(self):
237
+ """Multiple converters of ValidatedDict are called in order."""
238
+ d = ValidatedDict({"u": 1, "v": 2, "w": -3}, converter=[abs, str])
239
+ assert d == {"u": "1", "v": "2", "w": "3"}
240
+
241
+ def test_multiple_validators(self):
242
+ """Multiple converters of ValidatedDict are called in order."""
243
+
244
+ def is_positive(value):
245
+ if value <= 0:
246
+ raise TypeError("value must be positive")
247
+
248
+ def is_odd(value):
249
+ if value % 2 == 0:
250
+ raise TypeError("value must be odd")
251
+
252
+ with pytest.raises(TypeError, match="value must be positive"):
253
+ ValidatedDict({"first": -1, "second": -3}, validator=[is_positive, is_odd])
254
+
255
+ with pytest.raises(TypeError, match="value must be odd"):
256
+ ValidatedDict({"first": 2, "second": 4}, validator=[is_positive, is_odd])
257
+
258
+ def test_copy(self):
259
+ """A copy of a ValidatedDict returns another ValidatedDict."""
260
+ d = ValidatedDict({"a": 1, "b": 2, "c": 3})
261
+ assert isinstance(d.copy(), ValidatedDict)
262
+ assert isinstance(copy.copy(d), ValidatedDict)
263
+ assert isinstance(copy.deepcopy(d), ValidatedDict)