def test_set_globalplaceholderfile(self): """Set the global placeholder file and initialize the placeholders with the file content""" with patch('businesslogic.placeholders.Placeholder._initialize_global_placeholders') as init_mock: Placeholder.set_globalplaceholderfile('./testfiles/test.txt') self.assertEqual(Placeholder._globalplaceholderfile, './testfiles/test.txt') init_mock.assert_called()
def test_set_globalplaceholderfile_with_testfile(self): """ Set the global placeholder file and initialize the placeholders with the file content with no mocking """ Placeholder.set_globalplaceholderfile(self._placeholder_test_file) self.assertEqual(Placeholder._globalplaceholderfile, self._placeholder_test_file)
def test_get_placeholder_casesensitive(self): """Should raise KeyError because of case insitivity""" Placeholder.set_globalplaceholderfile(self._placeholder_test_file) exisiting_placeholder = "client" nonexisting_placeholder = "Client" expected_value = "Sgt Mustman" self.assertEqual(Placeholder.get_placeholder(exisiting_placeholder), expected_value) with self.assertRaises(KeyError): Placeholder.get_placeholder(nonexisting_placeholder)
def test_get_placeholder_from_testfile(self): """ Should return content of test file if key is in the file. """ existing_placeholder = "client" expected_value = "Sgt Mustman" Placeholder.set_globalplaceholderfile(self._placeholder_test_file) actual_value = Placeholder.get_placeholder(existing_placeholder) self.assertEqual(actual_value, expected_value)
def get_collector( cls, instructionsfile: str, examiner: str = '', placeholderfile: str = Placeholder.get_globalplaceholderfile(), protocollogfile: str = ''): Placeholder.set_globalplaceholderfile(placeholderfile) cls._logger.debug(f"Protocol log file: '{protocollogfile}'") protocol = LogFileProtocol(examiner, own_protocol_filename=protocollogfile) xmlparser = XmlParser(instructionsfile, protocol) collector = cls(parser=xmlparser, protocol=protocol) return collector
def test_set_globalplaceholderfile_invalid_json(self): """ Should not change _globalplaceholderfile :return: """ from businesslogic.placeholders import Placeholder expected_test_file = "./testfiles/test.txt" expected_file = Placeholder.GLOBAL_PLACEHOLDER_FILE_PATH with patch('businesslogic.placeholders.path.exists', MagicMock(return_value=True)): Placeholder.set_globalplaceholderfile(expected_test_file) self.assertEqual(expected_file, Placeholder._globalplaceholderfile)
def test__call__should_replace_placeholder_in_string(self): """Using the @Placeholder decorator on a function returing a string should replace placeholders in that string with the values stored in the placeholder dictionary.""" Placeholder.set_globalplaceholderfile(self._placeholder_test_file) string_with_placeholder = f"This was it! {Placeholder.PLACEHOLDER_START}client{Placeholder.PLACEHOLDER_END} " \ f"had enough. 'This has to end now!', " \ f"{Placeholder.PLACEHOLDER_START}client{Placeholder.PLACEHOLDER_END} thought." expected_string = "This was it! Sgt Mustman had enough. 'This has to end now!', Sgt Mustman thought." @Placeholder def test_function(): return string_with_placeholder actual_string = test_function() self.assertEqual(actual_string, expected_string)
def test_set_globalplaceholderfile(self): """ Should store file in _globalplaceholderfile Should call _initialize_global_placeholders() :return: """ from businesslogic.placeholders import Placeholder expected_file = "dummy" mock = MagicMock(return_value=None) with patch('businesslogic.placeholders.path.exists', MagicMock(return_value=True)): with patch('businesslogic.placeholders.Placeholder._initialize_global_placeholders', mock): Placeholder.set_globalplaceholderfile(expected_file) mock.assert_called_once() self.assertEqual(expected_file, Placeholder._globalplaceholderfile)