|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# encoding: utf-8 |
| 3 | + |
| 4 | +""" |
| 5 | +This module contains unit tests for the kinetic families defined under arc.data.families. |
| 6 | +""" |
| 7 | + |
| 8 | +import unittest |
| 9 | +import os |
| 10 | + |
| 11 | +from arc.family.family import ReactionFamily, get_reaction_family_products, get_recipe_actions |
| 12 | +from arc.imports import settings |
| 13 | +from arc.reaction.reaction import ARCReaction |
| 14 | +from arc.species.species import ARCSpecies |
| 15 | + |
| 16 | +ARC_FAMILIES_PATH = settings['ARC_FAMILIES_PATH'] |
| 17 | + |
| 18 | + |
| 19 | +class TestCarbonylBasedHydrolysisReactionFamily(unittest.TestCase): |
| 20 | + """ |
| 21 | + Contains unit tests for the carbonyl-based hydrolysis reaction family. |
| 22 | + """ |
| 23 | + |
| 24 | + @classmethod |
| 25 | + def setUpClass(cls): |
| 26 | + """Set up the test by defining the carbonyl-based hydrolysis reaction family.""" |
| 27 | + cls.family = ReactionFamily('carbonyl_based_hydrolysis') |
| 28 | + |
| 29 | + def test_carbonyl_based_hydrolysis_reaction(self): |
| 30 | + """Test if carbonyl_based hydrolysis products are correctly generated.""" |
| 31 | + carbonyl = ARCSpecies(label='carbonyl', smiles='CC(=O)OC') |
| 32 | + water = ARCSpecies(label='H2O', smiles='O') |
| 33 | + acid = ARCSpecies(label='acid', smiles='CC(=O)O') |
| 34 | + alcohol = ARCSpecies(label='alcohol', smiles='CO') |
| 35 | + rxn = ARCReaction(r_species=[carbonyl, water], p_species=[acid, alcohol]) |
| 36 | + products = get_reaction_family_products(rxn) |
| 37 | + product_smiles = [p.to_smiles() for p in products[0]['products']] |
| 38 | + expected_product_smiles = ['CC(=O)O', 'CO'] |
| 39 | + self.assertEqual(product_smiles, expected_product_smiles) |
| 40 | + |
| 41 | + def test_recipe_actions(self): |
| 42 | + """Test if the reaction recipe is applied correctly.""" |
| 43 | + groups_file_path = os.path.join(ARC_FAMILIES_PATH, 'carbonyl_based_hydrolysis.py') |
| 44 | + with open(groups_file_path, 'r') as f: |
| 45 | + groups_as_lines = f.readlines() |
| 46 | + actions = get_recipe_actions(groups_as_lines) |
| 47 | + expected_actions = [ |
| 48 | + ['BREAK_BOND', '*1', 1, '*2'], |
| 49 | + ['BREAK_BOND', '*3', 1, '*4'], |
| 50 | + ['FORM_BOND', '*1', 1, '*4'], |
| 51 | + ['FORM_BOND', '*2', 1, '*3'], |
| 52 | + ] |
| 53 | + self.assertEqual(actions, expected_actions) |
| 54 | + |
| 55 | + def test_carbonyl_based_hydrolysis_withP(self): |
| 56 | + """Test if carbonyl-based hydrolysis products are correctly generated.""" |
| 57 | + carbonyl= ARCSpecies(label='carbonyl', smiles='CP(=O)(OC)O') |
| 58 | + water = ARCSpecies(label='H2O', smiles='O') |
| 59 | + acid = ARCSpecies(label='acid', smiles='CP(=O)(O)O') |
| 60 | + alcohol = ARCSpecies(label='alcohol', smiles='CO') |
| 61 | + rxn = ARCReaction(r_species=[carbonyl, water], p_species=[acid, alcohol]) |
| 62 | + products = get_reaction_family_products(rxn) |
| 63 | + product_smiles = [p.to_smiles() for p in products[0]['products']] |
| 64 | + expected_product_smiles = ['CP(=O)(O)O', 'CO'] |
| 65 | + self.assertEqual(product_smiles, expected_product_smiles) |
| 66 | + |
| 67 | + |
| 68 | +class TestNitrileHydrolysisReactionFamily(unittest.TestCase): |
| 69 | + """ |
| 70 | + Contains unit tests for the nitrile hydrolysis reaction family. |
| 71 | + """ |
| 72 | + |
| 73 | + @classmethod |
| 74 | + def setUpClass(cls): |
| 75 | + """Set up the test by defining the nitrile hydrolysis reaction family.""" |
| 76 | + cls.family = ReactionFamily('nitrile_hydrolysis') |
| 77 | + |
| 78 | + def test_nitrile_hydrolysis_reaction(self): |
| 79 | + """Test if nitrile hydrolysis products are correctly generated.""" |
| 80 | + nitrile = ARCSpecies(label='nitrile', smiles='CC#N') |
| 81 | + water = ARCSpecies(label='H2O', smiles='O') |
| 82 | + acid = ARCSpecies(label='acid', smiles='CC(=N)O') |
| 83 | + rxn = ARCReaction(r_species=[nitrile, water], p_species=[acid]) |
| 84 | + products = get_reaction_family_products(rxn) |
| 85 | + product_smiles = [p.to_smiles() for p in products[0]['products']] |
| 86 | + expected_product_smiles = ['CC(=N)O'] |
| 87 | + self.assertEqual(product_smiles, expected_product_smiles) |
| 88 | + |
| 89 | + def test_recipe_actions(self): |
| 90 | + """Test if the reaction recipe is applied correctly for nitrile hydrolysis.""" |
| 91 | + groups_file_path = os.path.join(ARC_FAMILIES_PATH, 'nitrile_hydrolysis.py') |
| 92 | + with open(groups_file_path, 'r') as f: |
| 93 | + groups_as_lines = f.readlines() |
| 94 | + actions = get_recipe_actions(groups_as_lines) |
| 95 | + expected_actions =[ |
| 96 | + ['CHANGE_BOND', '*1', -1, '*2'], |
| 97 | + ['BREAK_BOND', '*3', 1, '*4'], |
| 98 | + ['FORM_BOND', '*1', 1, '*4'], |
| 99 | + ['FORM_BOND', '*2', 1, '*3'], |
| 100 | + ] |
| 101 | + self.assertEqual(actions, expected_actions) |
| 102 | + |
| 103 | + |
| 104 | +class TestEtherHydrolysisReactionFamily(unittest.TestCase): |
| 105 | + """ |
| 106 | + Contains unit tests for the ether hydrolysis reaction family. |
| 107 | + """ |
| 108 | + |
| 109 | + @classmethod |
| 110 | + def setUpClass(cls): |
| 111 | + """Set up the test by defining the ether hydrolysis reaction family.""" |
| 112 | + cls.family = ReactionFamily('ether_hydrolysis') |
| 113 | + |
| 114 | + def test_ether_hydrolysis_reaction(self): |
| 115 | + """Test if ether hydrolysis products are correctly generated.""" |
| 116 | + ether = ARCSpecies(label='ether', smiles='CCOC') |
| 117 | + water = ARCSpecies(label='H2O', smiles='O') |
| 118 | + alcohol1 = ARCSpecies(label='alcohol1', smiles='CCO') |
| 119 | + alcohol2 = ARCSpecies(label='alcohol2', smiles='CO') |
| 120 | + rxn = ARCReaction(r_species=[ether, water], p_species=[alcohol1, alcohol2]) |
| 121 | + products = get_reaction_family_products(rxn) |
| 122 | + product_smiles = [p.to_smiles() for p in products[0]['products']] |
| 123 | + expected_product_smiles = ['CCO', 'CO'] |
| 124 | + self.assertEqual(product_smiles, expected_product_smiles) |
| 125 | + |
| 126 | + def test_recipe_actions(self): |
| 127 | + """Test if the reaction recipe is applied correctly.""" |
| 128 | + groups_file_path = os.path.join(ARC_FAMILIES_PATH, 'ether_hydrolysis.py') |
| 129 | + with open(groups_file_path, 'r') as f: |
| 130 | + groups_as_lines = f.readlines() |
| 131 | + actions = get_recipe_actions(groups_as_lines) |
| 132 | + expected_actions = [ |
| 133 | + ['BREAK_BOND', '*1', 1, '*2'], |
| 134 | + ['BREAK_BOND', '*3', 1, '*4'], |
| 135 | + ['FORM_BOND', '*1', 1, '*4'], |
| 136 | + ['FORM_BOND', '*2', 1, '*3'], |
| 137 | + ] |
| 138 | + self.assertEqual(actions, expected_actions) |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == '__main__': |
| 142 | + unittest.main() |
0 commit comments