def test_incomplete_delete(self): """Tests that when the --complete option is not specified, only the chemical name specified is removed from \ the library (and not its nicknames).""" temp_file = NamedTemporaryFile("w+") test_salt_and_pepper = chemical.Chemical(200, ["salt", "pepper"]) test_kcl = chemical.Chemical(123.4, ["KCl"]) test_pepper = chemical.Chemical(200, ["pepper"]) initial_library = { "salt": test_salt_and_pepper, "pepper": test_salt_and_pepper, "KCl": test_kcl } after_delete = {"KCl": test_kcl, "pepper": test_pepper} with mock.patch("buf.commands.chemical.chemical_library_file", temp_file.name): chemical.save_chemical_library(initial_library) chemical.delete_chemical("salt", complete_deletion=False, prompt_for_confirmation=False) self.assertEqual(after_delete, chemical.load_chemicals())
def test_correct_read(self): """Tests that the function correctly parses a file into a dictionary.""" one_chemical = chemical.Chemical(123.4, ["name1", "name2"]) other_chemical = chemical.Chemical(567.8, ["name3", "name4"]) chemical_dict = { "name1": one_chemical, "name2": one_chemical, "name3": other_chemical, "name4": other_chemical } with mock.patch("buf.commands.chemical.open") as mock_open: mock_open.return_value.__enter__.return_value = StringIO( str(one_chemical) + "\n" + str(other_chemical)) returned_dict = chemical.load_chemicals() self.assertEqual(chemical_dict, returned_dict)
def test_spaces_in_name(self): """Tests that the function does not allow spaces to be in a chemical name.""" with mock.patch( "buf.commands.chemical.error_messages.spaces_in_chemical_name", side_effect=SystemExit) as mock_error: nacl_chemical = chemical.Chemical(58.44, ["NaCl"]) with mock.patch("buf.commands.chemical.load_chemicals", return_value={"NaCl": nacl_chemical}): with self.assertRaises(SystemExit): chemical.nickname_chemical("NaCl", ["new name"]) mock_error.assert_called_with("new name")
def test_step_making(self): """Test that the class properly converts a Recipe into a series of Steps.""" nacl = chemical.Chemical(58.44, ["NaCl"]) kcl = chemical.Chemical(74.55, ["KCl"]) test_recipe = recipe.Recipe("my_recipe", ["300mM", "4g"], ["NaCl", "KCl"]) with mock.patch("buf.commands.make.chemical.load_chemicals", return_value={ "NaCl": nacl, "KCl": kcl }): test_buffer_instructions = make.BufferInstructions(2, test_recipe) correct_steps = [ make.Step( "NaCl", "300mM", unit.scale_and_round_physical_quantity( 58.44 * 0.3 * 2, "g")), make.Step("KCl", "4g", "4.0g") ] self.assertEqual(test_buffer_instructions.steps, correct_steps)
def test_molarity_units(self): """Tests that when a concentration in molar is passed into the function, it returns the chemicals molar mass \ * concentration in molar * buffer volume in litres, scaled and rounded properly.""" inputs = [i for i in range(1, 100)] nacl = chemical.Chemical(58.44, ["NaCl"]) chemical_library = {"NaCl": nacl} for buffer_volume in range(1, 10): for input in inputs: self.assertEqual( unit.scale_and_round_physical_quantity( 58.44 * buffer_volume * input, "g"), make.calculate_amount_to_add(buffer_volume, str(input) + "M", "NaCl", chemical_library))
def test_equals(self): """Tests that two chemicals with the same molar masses and names are said to be equal.""" one_chemical = chemical.Chemical(123.4, ["name1", "name2"]) other_chemical = chemical.Chemical(123.4, ["name1", "name2"]) self.assertEqual(one_chemical, other_chemical)
def test_string_cast(self): """Tests casting a Chemical object to a string.""" test_chemical = chemical.Chemical(300, ["salt", "pepper"]) self.assertTrue(str(test_chemical), "300 salt pepper")