def __load_configuration(self, apiKey): config = ConfigChecker() config.set_expectation(settings.API_section_name, settings.API_option_private_key, str, settings.API_option_privatate_key_default) config.set_expectation(settings.API_section_name, settings.API_option_conversion_currency, str, settings.API_option_conversion_currency_default) config.set_expectation( settings.API_section_name, settings.API_option_conversion_currency_symbol, str, settings.API_option_conversion_currency_symbol_default) config.set_expectation(settings.API_section_name, settings.API_option_start_index, int, settings.API_option_start_index_default) config.set_expectation(settings.API_section_name, settings.API_option_end_index, int, settings.API_option_end_index_default) config.set_expectation(settings.API_section_name, settings.API_option_interval, int, settings.API_option_interval_default) config.set_expectation( settings.general_section_name, settings.general_option_status_file_format, str, settings.general_option_status_file_format_default) fileLocation = os.path.join(self.__workingDirectory, settings.input_configuation_filename) config.set_configuration_file(fileLocation) if apiKey is not None: config.set_value(settings.API_section_name, settings.API_option_private_key, apiKey) if config.write_configuration_file(fileLocation) is False: log.critical("Failed to create required configuration file.") raise return config
class FileOperationTests(unittest.TestCase): def setUp(self): self.checker = ConfigChecker() self.makeGoodConfigFile() self.makeBadConfigFile() def tearDown(self): self.removeGoodConfigFile() self.removeBadConfigFile() try: os.remove('test_write.ini') except: pass def test_opening_bad_config_file_return_false(self): self.checker.set_expectation("FirstSection", "key_integer", int, 23, "TestMessage") opened = self.checker.set_configuration_file('test_bad_config.ini') self.assertIs(opened, False) def test_opening_file_with_no_dxpectations_returns_false(self): opened = self.checker.set_configuration_file('test_good_config.ini') self.assertIs(opened, False) def test_file_which_exists_opens_correctly(self): self.checker.set_expectation("FirstSection", "key_integer", int, 23, "TestMessage") opened = self.checker.set_configuration_file('test_good_config.ini') self.assertIs( len(self.checker.get_config_parser_object().sections()), 3, "A well fomatted config file contained no sections when opened") self.assertIs(opened, True, "Opened was reported for a well formatted config file") def test_file_which_doesnt_exist_returns_false(self): self.checker.set_expectation("FirstSection", "key_integer", int, 23, "TestMessage") opened = self.checker.set_configuration_file('bad_file_name.ini') self.assertIs( len(self.checker.get_config_parser_object().sections()), 0, "A file which didn't exist resulted in config sections being made." ) self.assertIs( opened, False, "A file which didn't exist resulted in a true open status (should be false)" ) def test_only_expectation_are_stored_as_config_values_after_file_is_read( self): opened = self.checker.set_configuration_file('test_good_config.ini') self.checker.set_expectation("FirstSection", "key_integer", int, 23, "TestMessage") self.checker.set_expectation("FirstSection", "key_boolean", bool, False, "TestMessage") self.assertIs(len(self.checker.get_expectations()), 2) def test_expectations_which_match_have_their_value_updated(self): self.checker.set_expectation("FirstSection", "key_integer", int, 23, "TestMessage") self.checker.set_expectation("FirstSection", "key_boolean", bool, False, "TestMessage") self.checker.set_expectation("FirstSection", "key_float", float, 12123.1, "TestMessage") self.checker.set_expectation("FirstSection", "key_string", str, 'A string', "TestMessage") opened = self.checker.set_configuration_file('test_good_config.ini') self.assertIs(self.checker.get_value("FirstSection", "key_integer"), 45) self.assertIs(self.checker.get_value("FirstSection", "key_boolean"), True) self.assertEqual(self.checker.get_value("FirstSection", "key_float"), 23.2) self.assertEqual(self.checker.get_value("FirstSection", "key_string"), "I am a string") def test_bad_data_types_cause_their_default_to_be_loaded_bool(self): self.checker.set_expectation("FirstSection", "key_integer", bool, True, "TestMessage") opened = self.checker.set_configuration_file('test_good_config.ini') self.assertIs(self.checker.get_value("FirstSection", "key_integer"), True) def test_bad_data_types_cause_their_default_to_be_loaded_float(self): self.checker.set_expectation("FirstSection", "key_string", float, 123.1, "TestMessage") opened = self.checker.set_configuration_file('test_good_config.ini') self.assertIs(self.checker.get_value("FirstSection", "key_string"), 123.1) def test_bad_data_types_cause_their_default_to_be_loaded_int(self): self.checker.set_expectation("FirstSection", "key_string", int, 10, "TestMessage") opened = self.checker.set_configuration_file('test_good_config.ini') self.assertIs(self.checker.get_value("FirstSection", "key_string"), 10) def test_reading_value_not_updated_by_config_file_returns_default(self): self.checker.set_expectation("FirstSection", "unspec_key", int, 23, "TestMessage") opened = self.checker.set_configuration_file('test_good_config.ini') self.assertIs(self.checker.get_value("FirstSection", "unspec_key"), 23) def test_values_are_set_to_default_if_file_cant_be_opened(self): self.checker.set_expectation("FirstSection", "unspec_key", int, 23, "TestMessage") opened = self.checker.set_configuration_file('random_file_name.ini') self.assertIs(self.checker.get_value("FirstSection", "unspec_key"), 23) def test_writing_file_produces_produces_expected_result(self): self.checker.set_expectation("FirstSection", "key_integer", int, 23, "TestMessage") self.checker.set_expectation("FirstSection", "key_boolean", bool, False, "TestMessage") self.checker.set_expectation("FirstSection", "key_float", float, 12123.1, "TestMessage") self.checker.set_expectation("FirstSection", "key_string", str, 'A string', "TestMessage") self.checker.set_expectation("SecondSection", "key_string", str, 'default', "TestMessage") opened = self.checker.set_configuration_file('test_write.ini') self.assertIs(opened, False) written = self.checker.write_configuration_file('test_write.ini') self.assertIs(written, True) # Make a new object and set get_expectations() to different values self.checker = ConfigChecker() self.checker.set_expectation("FirstSection", "key_integer", int, 1, "TestMessage") self.checker.set_expectation("FirstSection", "key_boolean", bool, True, "TestMessage") self.checker.set_expectation("FirstSection", "key_float", float, 1.1, "TestMessage") self.checker.set_expectation("FirstSection", "key_string", str, 'sfs', "TestMessage") self.checker.set_expectation("SecondSection", "key_string", str, 'asfs', "TestMessage") # If the values match the original dump then all everythin was successful opened = self.checker.set_configuration_file('test_write.ini') self.assertIs(opened, True) self.assertIs(self.checker.get_value("FirstSection", "key_integer"), 23) self.assertIs(self.checker.get_value("FirstSection", "key_boolean"), False) self.assertEqual(self.checker.get_value("FirstSection", "key_float"), 12123.1) self.assertEqual(self.checker.get_value("FirstSection", "key_string"), "A string") self.assertEqual(self.checker.get_value("SecondSection", "key_string"), "default") os.remove('test_write.ini') def test_writing_file_with_none_uses_existing_config_file_name(self): self.checker.set_expectation("FirstSection", "key_integer", int, 23, "TestMessage") self.checker.set_expectation("FirstSection", "key_boolean", bool, False, "TestMessage") self.checker.set_expectation("FirstSection", "key_float", float, 12123.1, "TestMessage") self.checker.set_expectation("FirstSection", "key_string", str, 'A string', "TestMessage") self.checker.set_expectation("SecondSection", "key_string", str, 'default', "TestMessage") opened = self.checker.set_configuration_file('test_write.ini') self.assertIs(opened, False) written = self.checker.write_configuration_file(None) self.assertIs(written, True) self.assertIs(os.path.exists('test_write.ini'), True) os.remove('test_write.ini') def test_writing_file_permission_error_returs_false(self): self.checker.set_expectation("FirstSection", "key_integer", int, 23, "TestMessage") self.checker.set_expectation("FirstSection", "key_boolean", bool, False, "TestMessage") self.checker.set_expectation("FirstSection", "key_float", float, 12123.1, "TestMessage") self.checker.set_expectation("FirstSection", "key_string", str, 'A string', "TestMessage") self.checker.set_expectation("SecondSection", "key_string", str, 'default', "TestMessage") opened = self.checker.set_configuration_file('test_write.ini') self.assertIs(opened, False) written = self.checker.write_configuration_file( '/root/test_config.ini') self.assertIs( written, False, "These tests and module shouldn't be run with root acccess.") def test_writing_os_error_returs_false(self): self.checker.set_expectation("FirstSection", "key_integer", int, 23, "TestMessage") self.checker.set_expectation("FirstSection", "key_boolean", bool, False, "TestMessage") self.checker.set_expectation("FirstSection", "key_float", float, 12123.1, "TestMessage") self.checker.set_expectation("FirstSection", "key_string", str, 'A string', "TestMessage") self.checker.set_expectation("SecondSection", "key_string", str, 'default', "TestMessage") opened = self.checker.set_configuration_file('test_write.ini') self.assertIs(opened, False) written = self.checker.write_configuration_file('sdf/asdf') self.assertIs(written, False) def test_writing_file_with_no_expectations_returns_false(self): written = self.checker.write_configuration_file('test_write.ini') self.assertIs(written, False) def test_can_change_the_value_of_an_expectation_int(self): self.checker.set_expectation("FirstSection", "key_integer", int, 23, "TestMessage") opened = self.checker.set_configuration_file('test_write.ini') written = self.checker.set_value("FirstSection", "key_integer", 45) self.assertIs(written, True) self.assertIs(self.checker.get_value("FirstSection", "key_integer"), 45) def test_can_change_the_value_of_an_expectation_float(self): self.checker.set_expectation("FirstSection", "key_float", float, 23.3, "TestMessage") opened = self.checker.set_configuration_file('test_write.ini') written = self.checker.set_value("FirstSection", "key_float", 45.3) self.assertIs(written, True) self.assertIs(self.checker.get_value("FirstSection", "key_float"), 45.3) def test_can_change_the_value_of_an_expectation_bool(self): self.checker.set_expectation("FirstSection", "key_boolean", bool, True, "TestMessage") opened = self.checker.set_configuration_file('test_write.ini') written = self.checker.set_value("FirstSection", "key_boolean", False) self.assertIs(written, True) self.assertIs(self.checker.get_value("FirstSection", "key_boolean"), False) def test_can_change_the_value_of_an_expectation_str(self): self.checker.set_expectation("FirstSection", "key_string", str, 'asdf', "TestMessage") opened = self.checker.set_configuration_file('test_write.ini') written = self.checker.set_value("FirstSection", "key_string", 'qwer') self.assertIs(written, True) self.assertIs(self.checker.get_value("FirstSection", "key_string"), 'qwer') def test_can_change_the_value_of_multiple_without_changing_others(self): self.checker.set_expectation("FirstSection", "key_string", str, 'asdf', "TestMessage") self.checker.set_expectation("FirstSection", "key_boolean", bool, True, "TestMessage") self.checker.set_expectation("FirstSection", "key_float", float, 23.3, "TestMessage") self.checker.set_expectation("FirstSection", "key_integer", int, 23, "TestMessage") opened = self.checker.set_configuration_file('test_write.ini') written = self.checker.set_value("FirstSection", "key_string", 'qwer') self.assertIs(written, True) written = self.checker.set_value("FirstSection", "key_float", 45.3) self.assertIs(written, True) written = self.checker.set_value("FirstSection", "key_integer", 45) self.assertIs(written, True) written = self.checker.set_value("FirstSection", "key_boolean", False) self.assertIs(written, True) self.assertIs(self.checker.get_value("FirstSection", "key_string"), 'qwer') self.assertIs(self.checker.get_value("FirstSection", "key_integer"), 45) written = self.checker.set_value("FirstSection", "key_float", 45.3) self.assertIs(self.checker.get_value("FirstSection", "key_boolean"), False) def test_changing_value_of_expectation_which_doesnt_exist_returns_false( self): self.checker.set_expectation("FirstSection", "key_integer", int, 23, "TestMessage") opened = self.checker.set_configuration_file('test_write.ini') written = self.checker.set_value("WrongSection", "key_integer", 45) self.assertIs(written, False) def test_cannot_set_values_if_file_target_is_not_set(self): self.checker.set_expectation("FirstSection", "key_integer", int, 23, "TestMessage") written = self.checker.set_value("FirstSection", "key_integer", 45) self.assertIs(written, False) def test_writing_incorrect_type_returns_false_integer(self): self.checker.set_expectation("FirstSection", "key_integer", int, 23, "TestMessage") opened = self.checker.set_configuration_file('test_write.ini') written = self.checker.set_value("FirstSection", "key_integer", 'sdfs') self.assertIs(written, False) def test_writing_incorrect_type_returns_false_float(self): self.checker.set_expectation("FirstSection", "key_float", float, 23.3, "TestMessage") opened = self.checker.set_configuration_file('test_write.ini') written = self.checker.set_value("FirstSection", "key_float", 'sdfs') self.assertIs(written, False) def test_writing_incorrect_type_returns_false_bool(self): self.checker.set_expectation("FirstSection", "key_boolean", bool, False, "TestMessage") opened = self.checker.set_configuration_file('test_write.ini') written = self.checker.set_value("FirstSection", "key_boolean", 'sdf') self.assertIs(written, False) def makeGoodConfigFile(self): try: with open('test_good_config.ini', 'w') as f: f.write(good_config) except: print( "Warning: Cannot write test configuration files to filesystem, tests failed" ) def makeBadConfigFile(self): try: with open('test_bad_config.ini', 'w') as f: f.write(bad_config) except: print( "Warning: Cannot write test configuration files to filesystem, tests failed" ) def removeGoodConfigFile(self): try: os.remove('test_good_config.ini') except: pass def removeBadConfigFile(self): try: os.remove('test_bad_config.ini') except: pass