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_get_mechanism(self): M1_comp = Mechanism(name = "m1_comp", mechanism_type = "shared") M1_mix = Mechanism(name = "m1_mix", mechanism_type = "shared") M2_comp = Mechanism(name = "m2_comp", mechanism_type = "comp") M2_mix = Mechanism(name = "m2_mix", mechanism_type = "mixture") #Create a Mixture and Component with the above mechanisms C = Component(name = "comp", mechanisms = [M1_comp, M2_comp]) M = Mixture(mechanisms = [M1_mix, M2_mix], components = [C]) #Get the copy of C in M C_copy = M.get_component(component = C) self.assertTrue(C_copy.get_mechanism("shared").name == "m1_comp") self.assertTrue(M.get_mechanism("shared").name == "m1_mix") self.assertTrue(C_copy.get_mechanism("comp").name == "m2_comp") self.assertTrue(C_copy.get_mechanism("mixture").name == "m2_mix") #Make sure the Mixture get_mechanism works as well, just in case. self.assertTrue(M.get_mechanism("comp") is None) #test get Mechanism with no_key_error = False (Default) with self.assertRaisesRegex(KeyError, "Unable to find mechanism of type"): C_copy.get_mechanism("DNE") #test get_mechanism with no_key_error = True self.assertTrue(C_copy.get_mechanism("DNE", optional_mechanism = True) is None)
def test_compoents_in_multiple_mixtures(self): C = Component("comp") M1 = Mixture(components = [C]) #M1 should have a copy of C, not C itself self.assertTrue(type(M1.get_component(component = C)) == Component) self.assertTrue(C not in M1._components) #C.mixture should not be set, only it's copy self.assertTrue(C.mixture is None) self.assertTrue(M1.get_component(component = C).mixture is M1) #Add C to another Mixture M2 = Mixture(components = [C]) self.assertTrue(type(M2.get_component(component = C)) == Component) self.assertTrue(M2.get_component(component = C).mixture is M2) self.assertTrue(M1.get_component(component = C) != M2.get_component(component = C))
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)