def test_set_config_false(self): """Sets the sections to be nonexistent and checks if the file is correctly handled.""" with open(strings.get_config_path(), "w") as file: file.write("nonsense!") file = open(strings.get_config_path(), "r") self.assertIsNone(re.search('CONNECTION', file.read())) file.seek(0) self.assertIsNone(re.search('DATABASE', file.read())) file.seek(0) self.assertIsNone(re.search('METRICS', file.read())) file.close()
def set_config(self): """Creates and writes the parser.ini file.""" self.parser['CONNECTION'] = strings.get_config_connection() self.parser['DATABASE'] = strings.get_config_db() self.parser['METRICS'] = strings.get_config_metrics() with open(strings.get_config_path(), 'w') as file: self.parser.write(file)
def test_check_config_false(self): """First, given the incorrect file structure, checks whether the check_config method returns the expected result. Second, given the deleted file, checks for the same result.""" with open(strings.get_config_path(), "w") as file: file.write("More nonsense.") actual = self.test.check_config() expected = False self.assertEqual(expected, actual) os.remove(strings.get_config_path()) actual = self.test.check_config() expected = False self.assertEqual(expected, actual)
def test_get_c_keys_incorrect(self): """Tests that the keys of CONNECTION section after altering parser.ini are the expected ones.""" with open(strings.get_config_path(), "w") as file: file.write("Nonsense.") expected = ['send_time', 'address', 'port', 'flask_port'] actual = self.test.get_c_keys() self.assertEqual(expected, actual)
def test_get_d_keys_incorrect(self): """Tests that the values of DATABASE section after altering parser.ini are the expected ones.""" with open(strings.get_config_path(), "w") as file: file.write("Nonsense.") expected = ['db_name', 'db_url'] actual = self.test.get_d_keys() self.assertEqual(expected, actual)
def test_get_m_keys_incorrect(self): """Tests that the values of METRICS section after altering parser.ini are the expected ones.""" with open(strings.get_config_path(), "w") as file: file.write("Nonsense.") expected = ['YES', 'YES', 'YES', 'YES'] actual = self.test.get_m_value() self.assertEqual(expected, actual)
def get_c_keys(self): """Reads the keys for the CONNECTION section. Returns a list with the keys.""" self.validate_config() self.parser.read(strings.get_config_path()) conn = list(dict(self.parser.items('CONNECTION')).keys()) return conn
def get_d_keys(self): """Reads the keys from DATABASE section. Returns a list with the keys.""" self.validate_config() self.parser.read(strings.get_config_path()) data = list(dict(self.parser.items('DATABASE')).keys()) return data
def get_m_keys(self): """Reads the keys from the METRICS section. Returns a list with the keys.""" self.validate_config() self.parser.read(strings.get_config_path()) metr = list(dict(self.parser.items('METRICS')).keys()) return metr
def test_set_connection_altered(self): """Tests whether the connection to the RabbitMQ queue is successful after altering the address and port found in config.ini file.""" self.config.parser.read(strings.get_config_path()) self.config.parser.set('CONNECTION', 'address', 'bad coffee') self.config.parser.set('CONNECTION', 'port', 'not coffee') result = self.test.set_connection() self.assertIsNotNone(result)
def get_d_value(self): """Reads the values for db_name and db_url used to connect to database. Returns a list with the values.""" self.validate_config() self.parser.read(strings.get_config_path()) data = list(dict(self.parser.items('DATABASE')).values()) return data
def test_get_d_value_altered(self): """Tests that the values of DATABASE section after altering parser.ini are the expected ones.""" self.config.parser.set('DATABASE', 'db_name', 'Mercy') with open(strings.get_config_path(), "w") as file: self.config.parser.write(file) expected = ['Mercy', 'database_url'] actual = self.test.get_d_value() self.assertEqual(expected, actual)
def test_get_m_value_altered(self): """Tests that the values of METRICS section after altering parser.ini are the expected ones.""" self.config.parser.set('METRICS', 'disk_usage', 'NO') with open(strings.get_config_path(), "w") as file: self.config.parser.write(file) expected = ['NO', 'YES', 'YES', 'YES'] actual = self.test.get_m_value() self.assertEqual(expected, actual)
def get_c_value(self): """Reads the values for send_time and connection info used to connect to RabbitMQ and Flask. Returns a list with the values.""" self.validate_config() self.parser.read(strings.get_config_path()) conn = list(dict(self.parser.items('CONNECTION')).values()) return conn
def test_get_c_value_altered(self): """Tests that the values of CONNECTION section after altering parser.ini are the expected ones.""" self.config.parser.set('CONNECTION', 'send_time', '10') with open(strings.get_config_path(), "w") as file: self.config.parser.write(file) expected = ['10', 'localhost', '5672', '500'] actual = self.test.get_c_value() self.assertEqual(expected, actual)
def test_check_keys_false(self): """Given the incorrect file structure, checks whether the check_keys returns the correct answer after verifying the keys from parser.ini.""" with open(strings.get_config_path(), "w") as file: file.write("More nonsense.") actual = self.test.check_keys() expected = False self.assertEqual(expected, actual)
def get_m_value(self): """Reads the values YES or NO from the parser.ini file, and returns a list with the answer for each metric. Returns a list with the values.""" self.validate_config() self.parser.read(strings.get_config_path()) metr = list(dict(self.parser.items('METRICS')).values()) return metr
def check_config(self): """Checks if the parser.ini file exists and valid. If it isn't, it creates a new one.""" is_file = os.path.isfile(strings.get_config_path()) if is_file: valid_keys = self.check_keys() valid_values = self.check_values() return valid_keys and valid_values return is_file
def test_set_config_called(self, set_config): """Tests if the set_config functions is called when the file is not valid.""" self.mock.attach_mock(set_config, 'set_config') with open(strings.get_config_path(), "w") as file: file.write("Nonsense!") self.test.__init__() expected = mock.call.set_config() actual = self.mock.mock_calls.pop(0) self.assertEqual(expected, actual)
def test_set_config_true(self): """Tests whether the sections which should be existent in the parser.ini file are there.""" file = open(strings.get_config_path(), "r") actual = re.search('CONNECTION', file.read()).group() self.assertEqual('CONNECTION', actual) file.seek(0) actual = re.search('DATABASE', file.read()).group() self.assertEqual('DATABASE', actual) file.seek(0) actual = re.search('METRICS', file.read()).group() self.assertEqual('METRICS', actual) file.close()
def check_keys(self): """Checks whether the read sections are correct.""" try: self.config.read(strings.get_config_path()) keys = (self.config.options('CONNECTION') + self.config.options('DATABASE') + self.config.options('METRICS')) except configparser.Error: return False expected = list({ **strings.get_config_connection(), **strings.get_config_db(), **strings.get_config_metrics() }.keys()) return keys == expected
def check_values(self): """Checks whether the options for CONNECTION and METRICS sections are valid""" try: self.config.read(strings.get_config_path()) values = list({ **dict(self.config.items('CONNECTION')), **dict(self.config.items('METRICS')) }.values()) except configparser.Error: return False expected = list(strings.get_values_re()) for i, value in enumerate(values): if re.search(expected[i], value) is None: return False return True