def test_add_components(self): mixture = Mixture() # test that new mixture has no components self.assertTrue(len(mixture.components) == 0) component = Component('test_comp') mixture.add_component(component) # test that the new components was added to the mixture self.assertTrue(mixture.get_component(component = component) is not None) #test that it was added by copying self.assertTrue(component not in mixture._components) #test that the same component cannot be added again with self.assertRaisesRegex(ValueError,f'{component} of the same type and name already in Mixture!'): mixture.add_component(component) #use the constructor the other way mixture = Mixture(components = [component]) # test that the new components was added to the mixture self.assertTrue(mixture.get_component(component = component) is not None) #test that it was added by copying self.assertTrue(component not in mixture._components) species = Species('test_species') # species are invalid components with self.assertRaisesRegex(ValueError,f'add_components expected a list of Components.'): mixture.add_components(species)
def test_update_species(self): species = Species(name='H2O') mixture = Mixture(species=[species]) self.assertTrue(species in mixture.update_species()) dna = DNA(name='test_DNA') mixture.add_components(dna) crn_list = mixture.update_species() for s_dna in dna.update_species(): self.assertTrue(s_dna in crn_list)
def test_add_species_to_crn(self): species = Species(name='H2O') mixture = Mixture(species=[species]) mixture.add_species_to_crn(species, None) self.assertTrue(species in mixture.crn.species) dna = DNA(name='test_DNA') mixture.add_components(dna) mixture.add_species_to_crn(dna.update_species(), dna) for s_dna in dna.update_species(): self.assertTrue(s_dna in mixture.crn.species)
def test_add_components(self): mixture = Mixture() # test that new mixture has no components self.assertTrue(len(mixture.components) == 0) component = Component('test_comp') mixture.add_components(component) # test that the new components was added to the mixture self.assertTrue(component in mixture.components) species = Species('test_species') # species are invalid components with self.assertRaisesRegexp(AssertionError,f'the object: {species} passed into mixture as component must be of the class Component'): mixture.add_components(species)
def test_add_components(self): from biocrnpyler import Mixture from biocrnpyler import Component from biocrnpyler import Species mixture = Mixture() self.assertTrue(len(mixture.components) == 0) component = Component('test_comp') mixture.add_components(component) self.assertTrue(component in mixture.components) species = Species('test_species') with self.assertRaises(AssertionError): mixture.add_components(species)
def test_update_species(self): from biocrnpyler import Mixture from biocrnpyler import Species from biocrnpyler import DNA # from biocrnpyler import Dilution species = Species(name='H2O') mixture = Mixture(species=[species]) self.assertTrue(species in mixture.update_species()) dna = DNA(name='test_DNA') mixture.add_components(dna) crn_list = mixture.update_species() for s_dna in dna.update_species(): self.assertTrue(s_dna in crn_list)
def test_update_reactions(self): mixture = Mixture() with self.assertRaisesRegexp(AttributeError, 'Mixture.crn_species not defined.'): mixture.update_reactions() component = Component(name='test_component') # creating a mock update function to decouple the update process from the rest of the code def mock_update_reactions(): rxn = Reaction(inputs=[], outputs=[], k=0.1) return [rxn] component.update_reactions = mock_update_reactions mixture.add_components(component) mixture.update_species() crn_rxn = mixture.update_reactions() crn_rxn_mock = mock_update_reactions() self.assertEqual(crn_rxn, crn_rxn_mock)
def test_get_component(self): c1 = Component('c1') c2 = Component('c2') c3 = DNA('c1') c4 = DNA("c2") mixture = Mixture() mixture.add_components([c1, c2, c3]) #Test with name #nothing named c3 is in the mixture self.assertTrue(mixture.get_component(name = "c3") is None) #two things named c2 are in the mixture self.assertTrue(type(mixture.get_component(name = "c1")) is list) #one thing named c1 is in the mixture self.assertTrue(type(mixture.get_component(name = "c2")) is Component) #test with component self.assertTrue(type(mixture.get_component(component = c1)) is Component) self.assertTrue(type(mixture.get_component(component = c2)) is Component) self.assertTrue(type(mixture.get_component(component = c3)) is DNA) self.assertTrue(mixture.get_component(component = c4) is None) #Test with index self.assertTrue(type(mixture.get_component(index = 1)) is Component) self.assertTrue(type(mixture.get_component(index = 2)) is DNA) with self.assertRaisesRegex(IndexError,""): mixture.get_component(index = 3) with self.assertRaisesRegex(ValueError,""): mixture.get_component(index = 3, component = c1) with self.assertRaisesRegex(ValueError,""): mixture.get_component(component = "c1") with self.assertRaisesRegex(ValueError,""): mixture.get_component(name = c1) with self.assertRaisesRegex(ValueError,""): mixture.get_component(index = c1)
def test_update_reactions(self): from biocrnpyler import Mixture from biocrnpyler import Reaction from biocrnpyler import Component mixture = Mixture() with self.assertRaises(AttributeError): mixture.update_reactions() component = Component(name='test_component') def mock_update_reactions(): rxn = Reaction(inputs=[], outputs=[], k=0.1) return [rxn] component.update_reactions = mock_update_reactions mixture.add_components(component) mixture.update_species() crn_rxn = mixture.update_reactions() crn_rxn_mock = mock_update_reactions() self.assertEqual(crn_rxn, crn_rxn_mock)