| | import unittest |
| | from rdkit.Chem import MolFromSmiles as fromSmiles |
| | from rdkit.Chem import MolToSmiles as toSmiles |
| | from typing import Sequence, Tuple, Union, Type |
| |
|
| | import ClickReaction |
| | import ClickReaction.Exceptions |
| | import ClickReaction.BaseReaction |
| |
|
| | Reactants = Sequence[str] |
| | ReactantNames = Sequence[str] |
| | NoProductTestCase = Reactants |
| | OneProductTestCase = Tuple[Reactants, str] |
| | MultipleProductTestCase = Tuple[Reactants, Sequence[str]] |
| | AnyProductTestCase = Union[NoProductTestCase, OneProductTestCase, MultipleProductTestCase] |
| |
|
| |
|
| | class ReactionTestCase(unittest.TestCase): |
| | def set_reaction(self, reaction: Type): |
| | self.reaction = reaction |
| |
|
| | def prepare_testcases(self, |
| | case: AnyProductTestCase, |
| | reactant_names: ReactantNames, |
| | with_product: bool = True, |
| | ): |
| | product_expected = None |
| |
|
| | if with_product is True: |
| | |
| |
|
| | if isinstance(case[1], str): |
| | product_expected = toSmiles(fromSmiles(case[1])) |
| | else: |
| | product_expected = [toSmiles(fromSmiles(x)) for x in case[1]] |
| |
|
| | reactants = case[0] |
| | else: |
| | reactants = case |
| |
|
| | |
| | reactants = [fromSmiles(x) for x in reactants] |
| |
|
| | |
| | reactants = {reactant_names[x]: reactants[x] for x in range(len(reactants))} |
| |
|
| | return product_expected, reactants |
| |
|
| | def _test_one_product(self, |
| | tests: Sequence[OneProductTestCase], |
| | reactant_names: ReactantNames, |
| | symmetrical_as_one: bool = False, |
| | ): |
| | """ Tests for giving exactly one expected product. """ |
| | for case in tests: |
| | with self.subTest(case=case): |
| | product_expected, reactants = self.prepare_testcases(case, reactant_names) |
| |
|
| | |
| | product = self.reaction(**reactants).get_product(symmetrical_as_one=symmetrical_as_one) |
| |
|
| | |
| | self.assertIsNotNone(product) |
| |
|
| | |
| | product_smiles = toSmiles(product) |
| | self.assertEqual(product_expected, product_smiles) |
| |
|
| | def _test_no_product(self, |
| | tests: Sequence[NoProductTestCase], |
| | reactant_names: ReactantNames, |
| | symmetrical_as_one: bool = False, |
| | ): |
| | """ Tests for failing to give a product. """ |
| | for case in tests: |
| | with self.subTest(case=case): |
| | _, reactants = self.prepare_testcases(case, reactant_names, with_product=False) |
| |
|
| | with self.assertRaises(ClickReaction.Exceptions.NoProductError): |
| | test_product = self.reaction(**reactants).get_product(symmetrical_as_one=symmetrical_as_one) |
| |
|
| | def _test_all_possible_products(self, |
| | tests: Sequence[MultipleProductTestCase], |
| | reactant_names: ReactantNames, |
| | symmetrical_as_one: bool = False, |
| | ): |
| | """ Tests if all possible expected products are covered from one reaction. """ |
| | for case in tests: |
| | with self.subTest(case=case): |
| | products_expected, reactants = self.prepare_testcases(case, reactant_names) |
| |
|
| | |
| | with self.assertRaises(ClickReaction.Exceptions.AmbiguousProductError): |
| | product = self.reaction(**reactants).get_product() |
| |
|
| | |
| | products = self.reaction(**reactants).get_products(symmetrical_as_one=symmetrical_as_one) |
| |
|
| | products_found = 0 |
| | for p in products: |
| | |
| | p_smiles = toSmiles(p) |
| |
|
| | self.assertIn(p_smiles, products_expected) |
| | products_found += 1 |
| |
|
| | self.assertEqual(products_found, len(products), "Not all products wer expected") |
| | self.assertEqual(len(products), len(products_expected), "Mismatch between expected products and actual products.") |
| |
|