def test_load_configuration(self): Configuration().core_clear() rsp = RsParameters() self.assertEquals("test_rs_paras", rsp.configuration_name()) rsp.max_items_in_list = 5566 rsp.save_configuration_as("realy_not_a_name_for_config") self.assertEquals("realy_not_a_name_for_config", rsp.configuration_name()) rsp = RsParameters(config_name="realy_not_a_name_for_config") self.assertEquals(5566, rsp.max_items_in_list) self.assertTrue("realy_not_a_name_for_config" in Configurations.list_configurations()) Configurations.remove_configuration("realy_not_a_name_for_config") with self.assertRaises(Exception) as context: RsParameters(config_name="realy_not_a_name_for_config") self.assertIsInstance(context.exception, ValueError) Configuration().reset() rsp = RsParameters() self.assertEquals("realy_not_a_name_for_config", rsp.configuration_name())
def save_configuration_test(self, rsp): Configuration.reset() rsp.save_configuration() Configuration.reset() rsp2 = RsParameters() self.assertEquals(rsp.__dict__, rsp2.__dict__)
def test_current_configuration_name(self): # No name change current_name = Configurations.current_configuration_name() Configuration.reset() self.assertEquals(current_name, Configurations.current_configuration_name()) cfg1 = Configuration() Configurations.save_configuration_as("test_current") self.assertEquals("test_current", Configurations.current_configuration_name()) Configurations.remove_configuration("test_current")
def setUpClass(cls): root = logging.getLogger() root.setLevel(logging.DEBUG) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.DEBUG) formatter = logging.Formatter( '%(asctime)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s' ) ch.setFormatter(formatter) root.addHandler(ch) #Configuration().persist() Configuration.reset()
def test01_set_test_config(self): # print("\n>>> Testing set_test_config") Configuration._set_configuration_filename(None) assert not Configuration._configuration_filename assert Configuration._get_configuration_filename() == config.CFG_FILENAME Configuration._set_configuration_filename("foo.bar") assert Configuration._get_configuration_filename() == "foo.bar" Configuration._set_configuration_filename(None) assert Configuration._get_configuration_filename() == config.CFG_FILENAME
def test_strategy(self): # defaults to configuration defaults Configuration().core_clear() rsp = RsParameters() self.assertEquals(Strategy.resourcelist, rsp.strategy) rsp = RsParameters(strategy=Strategy.inc_changelist) self.assertEquals(Strategy.inc_changelist, rsp.strategy) # contamination test rsp2 = RsParameters(**rsp.__dict__) self.assertEquals(rsp2.strategy, Strategy.inc_changelist) rsp = RsParameters(strategy=1) self.assertEquals(Strategy.new_changelist, rsp.strategy) rsp = RsParameters(strategy="inc_changelist") self.assertEquals(Strategy.inc_changelist, rsp.strategy) with self.assertRaises(Exception) as context: rsp.strategy = 20056 #print(context.exception) self.assertEquals("20056 is not a valid Strategy", context.exception.args[0]) self.assertIsInstance(context.exception, ValueError) self.save_configuration_test(rsp)
def test_history_dir(self): # defaults to configuration defaults Configuration().core_clear() rsp = RsParameters() self.assertEquals(None, rsp.history_dir) rsp = RsParameters(history_dir="foo/bar/baz") self.assertEquals("foo/bar/baz", rsp.history_dir) # contamination test rsp2 = RsParameters(**rsp.__dict__) self.assertEquals("foo/bar/baz", rsp2.history_dir) expected = os.path.join(rsp.abs_metadata_dir(), "foo/bar/baz") self.assertEquals(expected, rsp.abs_history_dir()) rsp.history_dir = None self.assertIsNone(rsp.abs_history_dir()) rsp.history_dir = "history" self.assertEquals("history", rsp.history_dir) rsp.history_dir = "" self.assertEquals(None, rsp.history_dir) with self.assertRaises(Exception) as context: rsp.history_dir = 42 #print(context.exception) self.assertEquals( "Value for history_dir should be string. 42 is <class 'int'>", context.exception.args[0]) self.assertIsInstance(context.exception, ValueError) self.save_configuration_test(rsp)
def test_last_strategy(self): Configuration().core_clear() rsp = RsParameters() self.assertIsNone(rsp.last_strategy) rsp.last_strategy = Strategy.inc_changelist rsp2 = RsParameters(**rsp.__dict__) self.assertEqual(Strategy.inc_changelist, rsp2.last_strategy) self.save_configuration_test(rsp)
def test_metadata_dir(self): user_home = os.path.expanduser("~") # defaults to configuration defaults Configuration().core_clear() rsp = RsParameters() self.assertEquals("metadata", rsp.metadata_dir) self.assertEquals(rsp.abs_metadata_dir(), os.path.join(user_home, "metadata")) resource_dir = user_home rsp = RsParameters(metadata_dir=os.path.join("foo", "md1"), resource_dir=resource_dir) # print(rsp.abs_metadata_dir()) self.assertEquals(rsp.abs_metadata_dir(), os.path.join(resource_dir, "foo", "md1")) # @ToDo test for windows pathnames: 'foo\md1', 'C:foo\bar\baz' here = os.path.dirname(__file__) rsp.resource_dir = here self.assertEquals(rsp.abs_metadata_dir(), os.path.join(here, "foo", "md1")) # contamination test rsp2 = RsParameters(**rsp.__dict__) self.assertEquals(rsp2.abs_metadata_dir(), os.path.join(here, "foo", "md1")) with self.assertRaises(Exception) as context: rsp.metadata_dir = os.path.expanduser("~") # print(context.exception) self.assertEquals( "Invalid value for metadata_dir: path should not be absolute: " + os.path.expanduser("~"), context.exception.args[0]) self.assertIsInstance(context.exception, ValueError) with self.assertRaises(Exception) as context: rsp.metadata_dir = "/foo/bar" # print(context.exception) self.assertEquals( "Invalid value for metadata_dir: path should not be absolute: /foo/bar", context.exception.args[0]) self.assertIsInstance(context.exception, ValueError) # cannot check if metadata_dir will be a directory, because relative to resource_dir # this = os.path.basename(__file__) # with self.assertRaises(Exception) as context: # rsp.metadata_dir = this # #print(context.exception) # self.assertIsInstance(context.exception, ValueError) self.save_configuration_test(rsp)
def do_reset(self, line): """ reset:: Reset the configuration to default settings. """ global PARAS if self.__confirm__("Reset configuration '%s' to default settings?" % PARAS.configuration_name()): Configuration().core_clear() PARAS = RsParameters() PARAS.save_configuration() self.do_list_parameters(line)
def test_zero_fill_filename(self): # defaults to configuration defaults Configuration().core_clear() rsp = RsParameters() self.assertEquals(4, rsp.zero_fill_filename) rsp = RsParameters(zero_fill_filename=10) self.assertEquals(10, rsp.zero_fill_filename) # contamination test rsp.zero_fill_filename = 8 rsp2 = RsParameters(**rsp.__dict__) self.assertEquals(8, rsp2.zero_fill_filename) self.save_configuration_test(rsp)
def test_exp_scp_document_root(self): # defaults to configuration defaults Configuration().core_clear() rsp = RsParameters() self.assertEquals("/var/www/html", rsp.exp_scp_document_root) # contamination test rsp.exp_scp_document_root = "/opt/rs/" rsp2 = RsParameters(**rsp.__dict__) self.assertEquals("/opt/rs", rsp2.exp_scp_document_root) rsp.exp_scp_document_root = "/var/www/html/ehri/rs" rsp3 = RsParameters(**rsp.__dict__) self.assertEquals("/var/www/html/ehri/rs", rsp3.exp_scp_document_root) self.save_configuration_test(rsp)
def test_imp_scp_port(self): # defaults to configuration defaults Configuration().core_clear() rsp = RsParameters() self.assertEquals(22, rsp.imp_scp_port) # contamination test rsp.imp_scp_port = 2222 rsp2 = RsParameters(**rsp.__dict__) self.assertEquals(2222, rsp2.imp_scp_port) rsp.imp_scp_port = 1234 rsp3 = RsParameters(**rsp.__dict__) self.assertEquals(1234, rsp3.imp_scp_port) self.save_configuration_test(rsp)
def test_imp_scp_server(self): # defaults to configuration defaults Configuration().core_clear() rsp = RsParameters() self.assertEquals("example.com", rsp.imp_scp_server) # contamination test rsp.imp_scp_server = "imp.server.name.com" rsp2 = RsParameters(**rsp.__dict__) self.assertEquals("imp.server.name.com", rsp2.imp_scp_server) rsp.imp_scp_server = "imp.server.name.nl" rsp3 = RsParameters(**rsp.__dict__) self.assertEquals("imp.server.name.nl", rsp3.imp_scp_server) self.save_configuration_test(rsp)
def test_imp_scp_remote_path(self): # defaults to configuration defaults Configuration().core_clear() rsp = RsParameters() self.assertEquals("~", rsp.imp_scp_remote_path) # contamination test rsp.imp_scp_remote_path = "/var/rs/" rsp2 = RsParameters(**rsp.__dict__) self.assertEquals("/var/rs", rsp2.imp_scp_remote_path) rsp.imp_scp_remote_path = "/opt/ehri/rs" rsp3 = RsParameters(**rsp.__dict__) self.assertEquals("/opt/ehri/rs", rsp3.imp_scp_remote_path) self.save_configuration_test(rsp)
def test_imp_scp_user(self): # defaults to configuration defaults Configuration().core_clear() rsp = RsParameters() self.assertEquals("username", rsp.imp_scp_user) # contamination test rsp.imp_scp_user = "******" rsp2 = RsParameters(**rsp.__dict__) self.assertEquals("kees", rsp2.imp_scp_user) rsp.imp_scp_user = "******" rsp3 = RsParameters(**rsp.__dict__) self.assertEquals("joe", rsp3.imp_scp_user) self.save_configuration_test(rsp)
def test_imp_scp_local_path(self): # defaults to configuration defaults user_home = os.path.expanduser("~") Configuration().core_clear() rsp = RsParameters() self.assertEquals(user_home, rsp.imp_scp_local_path) # contamination test rsp.imp_scp_local_path = os.path.join(user_home, "local", "rs") rsp2 = RsParameters(**rsp.__dict__) self.assertEquals(os.path.join(user_home, "local", "rs"), rsp2.imp_scp_local_path) rsp.imp_scp_local_path = os.path.join(user_home, "rs", "local") rsp3 = RsParameters(**rsp.__dict__) self.assertEquals(os.path.join(user_home, "rs", "local"), rsp3.imp_scp_local_path) self.save_configuration_test(rsp)
def test_plugin_dir(self): # defaults to configuration defaults Configuration().core_clear() rsp = RsParameters() self.assertEquals(None, rsp.plugin_dir) user_home = os.path.expanduser("~") rsp.plugin_dir = user_home self.assertEquals(user_home, rsp.plugin_dir) # contamination test rsp.plugin_dir = None rsp2 = RsParameters(**rsp.__dict__) self.assertEquals(None, rsp2.plugin_dir) rsp.plugin_dir = user_home rsp3 = RsParameters(**rsp.__dict__) self.assertEquals(user_home, rsp3.plugin_dir) self.save_configuration_test(rsp)
def test_description_dir(self): # defaults to configuration defaults Configuration().core_clear() rsp = RsParameters() self.assertIsNone(rsp.description_dir) rsp.description_dir = "." self.assertEquals(os.getcwd(), rsp.description_dir) # contamination test rsp2 = RsParameters(**rsp.__dict__) self.assertEquals(os.getcwd(), rsp2.description_dir) with self.assertRaises(Exception) as context: rsp.description_dir = "/foo/bar" #print(context.exception) self.assertIsInstance(context.exception, ValueError) self.assertEquals(os.getcwd(), rsp.description_dir) self.save_configuration_test(rsp)
def test_booleans(self): # defaults to configuration defaults Configuration().core_clear() rsp = RsParameters() self.assertTrue(rsp.is_saving_pretty_xml) self.assertTrue(rsp.is_saving_sitemaps) self.assertTrue(rsp.has_wellknown_at_root) rsp = RsParameters(is_saving_pretty_xml=False, is_saving_sitemaps=False, has_wellknown_at_root=False) self.assertFalse(rsp.is_saving_pretty_xml) self.assertFalse(rsp.is_saving_sitemaps) self.assertFalse(rsp.has_wellknown_at_root) # contamination test rsp2 = RsParameters(**rsp.__dict__) self.assertFalse(rsp2.is_saving_pretty_xml) self.assertFalse(rsp2.is_saving_sitemaps) self.assertFalse(rsp2.has_wellknown_at_root) self.save_configuration_test(rsp)
def test_max_items_in_list(self): # defaults to configuration defaults Configuration().core_clear() rsp = RsParameters() self.assertEquals(50000, rsp.max_items_in_list) rsp = RsParameters(max_items_in_list=1) self.assertEquals(1, rsp.max_items_in_list) # contamination test rsp.max_items_in_list = 12345 rsp2 = RsParameters(**rsp.__dict__) self.assertEquals(12345, rsp2.max_items_in_list) with self.assertRaises(Exception) as context: rsp.max_items_in_list = "foo" #print(context.exception) self.assertEquals( "Invalid value for max_items_in_list: not a number foo", context.exception.args[0]) self.assertIsInstance(context.exception, ValueError) with self.assertRaises(Exception) as context: rsp.max_items_in_list = 0 #print(context.exception) self.assertEquals( "Invalid value for max_items_in_list: value should be between 1 and 50000", context.exception.args[0]) self.assertIsInstance(context.exception, ValueError) with self.assertRaises(Exception) as context: rsp.max_items_in_list = 50001 #print(context.exception) self.assertEquals( "Invalid value for max_items_in_list: value should be between 1 and 50000", context.exception.args[0]) self.assertIsInstance(context.exception, ValueError) self.save_configuration_test(rsp)
def test_zip_filename(self): # defaults to configuration defaults Configuration().core_clear() rsp = RsParameters() self.assertEqual( os.path.join(os.path.expanduser("~"), "resourcesync.zip"), rsp.zip_filename) rsp.zip_filename = "bar" self.assertEqual("bar.zip", rsp.zip_filename) rsp.zip_filename = "foo." self.assertEqual("foo.zip", rsp.zip_filename) rsp.zip_filename = "/" self.assertEqual("/.zip", rsp.zip_filename) # contamination test rsp.zip_filename = "/foo/bar.zip" rsp2 = RsParameters(**rsp.__dict__) self.assertEqual("/foo/bar.zip", rsp2.zip_filename) self.save_configuration_test(rsp)
def test_resource_dir(self): user_home = os.path.expanduser("~") # defaults to configuration defaults Configuration().core_clear() rsp = RsParameters() self.assertEquals(rsp.resource_dir, user_home + os.sep) resource_dir = user_home rsp = RsParameters(resource_dir=resource_dir) self.assertEquals(rsp.resource_dir, resource_dir + os.sep) # contamination test rsp2 = RsParameters(**rsp.__dict__) self.assertEquals(rsp2.resource_dir, resource_dir + os.sep) assert (rsp.__dict__ == rsp2.__dict__) with self.assertRaises(Exception) as context: rsp.resource_dir = "/foo/bar" #print(context.exception) self.assertIsInstance(context.exception, ValueError) self.save_configuration_test(rsp)
def tearDown(self): Configuration._set_configuration_filename(None)
def setUp(self): Configuration._set_configuration_filename("test_rs_paras.cfg")
def test_persist(self): filename = "rspub_test_persist.cfg" Configuration._set_configuration_filename(filename) cfg = Configuration() if os.path.exists(cfg.config_file): os.remove(cfg.config_file) # not atomic # time.sleep(2) self.assertFalse(os.path.exists(cfg.config_file)) cfg.set_metadata_dir("foo/bar/md1") self.assertEquals(cfg.metadata_dir(), "foo/bar/md1") cfg.set_history_dir(None) self.assertIsNone(cfg.history_dir()) cfg.set_max_items_in_list(42) self.assertEquals(42, cfg.max_items_in_list()) cfg.set_is_saving_pretty_xml(False) self.assertEquals(False, cfg.is_saving_pretty_xml()) cfg.set_last_sitemaps([]) self.assertEquals([], cfg.last_sitemaps()) cfg.set_last_sitemaps( ["foo/bar/bas.txt", "foo/bar/bord.txt", "foo/bar/barst.txt"]) self.assertEquals( ["foo/bar/bas.txt", "foo/bar/bord.txt", "foo/bar/barst.txt"], cfg.last_sitemaps()) cfg.persist() self.assertTrue(os.path.exists(cfg.config_file)) #time.sleep(1) Configuration._set_configuration_filename(filename) cfg2 = Configuration() self.assertEquals(42, cfg2.max_items_in_list()) self.assertEquals(False, cfg2.is_saving_pretty_xml()) self.assertEquals( ["foo/bar/bas.txt", "foo/bar/bord.txt", "foo/bar/barst.txt"], cfg.last_sitemaps())
def test03_read(self): # print("\n>>> Testing read") Configuration._set_configuration_filename("rspub_test.cfg") new_path = os.path.dirname(os.path.realpath(__file__)) config1 = Configuration() config2 = Configuration() config1.set_plugin_dir(new_path) self.assertEquals(config1.plugin_dir(), new_path) config1.set_plugin_dir(None) self.assertEquals(config1.plugin_dir(), None) self.assertEquals(config2.plugin_dir(), None) Configuration.reset()
def test02_instance(self): # print("\n>>> Testing _instance") Configuration._set_configuration_filename("rspub_core.cfg") config1 = Configuration() config2 = Configuration() assert config1 == config2 path1 = config1.config_path if platform.system() == "Darwin": assert path1 == os.path.expanduser("~") + "/.config/rspub/core" elif platform.system() == "Windows": path_expected = os.path.join(os.path.expanduser("~"), "AppData", "Local", "rspub", "core") assert path1 == path_expected elif platform.system() == "Linux": assert path1 == os.path.expanduser("~") + "/.config/rspub/core" else: assert path1 == os.path.expanduser("~") + "/rspub/core" config1.core_clear() assert config1.resource_dir() == os.path.expanduser("~") new_path = os.path.dirname(os.path.realpath(__file__)) config1.set_resource_dir(new_path) assert config2.resource_dir() == new_path config2.persist() config1 = None config2 = None Configuration.reset()
def test_load_configuration(self): Configurations.remove_configuration("test_load_1") Configurations.remove_configuration("test_load_2") Configuration.reset() cfg1 = Configuration() cfg2 = Configuration() self.assertIs(cfg1, cfg2) self.assertEquals(Configurations.current_configuration_name(), cfg1.name()) self.assertEquals(Configurations.current_configuration_name(), cfg2.name()) Configurations.save_configuration_as("test_load_1") self.assertEquals("test_load_1", cfg1.name()) Configuration.reset() cfg3 = Configuration() self.assertIsNot(cfg1, cfg3) self.assertEquals(Configurations.current_configuration_name(), cfg3.name()) Configurations.save_configuration_as("test_load_2") self.assertEquals("test_load_1", cfg1.name()) self.assertEquals("test_load_1", cfg2.name()) self.assertEquals("test_load_2", cfg3.name()) Configurations.load_configuration("test_load_1") cfg4 = Configuration() self.assertEquals("test_load_1", cfg1.name()) self.assertEquals("test_load_1", cfg2.name()) self.assertEquals("test_load_2", cfg3.name()) self.assertEquals("test_load_1", cfg4.name())
def test_url_prefix(self): # defaults to configuration defaults Configuration().core_clear() rsp = RsParameters() self.assertEquals(rsp.url_prefix, "http://www.example.com/") rsp = RsParameters(url_prefix="http://foo.bar.com") self.assertEquals(rsp.url_prefix, "http://foo.bar.com/") # contamination test rsp2 = RsParameters(**rsp.__dict__) self.assertEquals(rsp2.url_prefix, "http://foo.bar.com/") with self.assertRaises(Exception) as context: rsp.url_prefix = "nix://foo.bar.com" #print(context.exception) self.assertEquals( "URL schemes allowed are 'http' or 'https'. Given: 'nix://foo.bar.com'", context.exception.args[0]) self.assertIsInstance(context.exception, ValueError) with self.assertRaises(Exception) as context: rsp.url_prefix = "https://.nl" #print(context.exception) self.assertEquals( "URL has invalid domain name: '.nl'. Given: 'https://.nl'", context.exception.args[0]) self.assertIsInstance(context.exception, ValueError) with self.assertRaises(Exception) as context: rsp.url_prefix = "https://foo.bar.com#foo" #print(context.exception) self.assertEquals( "URL should not have a fragment. Given: 'https://foo.bar.com#foo'", context.exception.args[0]) self.assertIsInstance(context.exception, ValueError) with self.assertRaises(Exception) as context: rsp.url_prefix = "http://foo.bar.com?what=this" #print(context.exception) self.assertEquals( "URL should not have a query string. Given: 'http://foo.bar.com?what=this'", context.exception.args[0]) self.assertIsInstance(context.exception, ValueError) with self.assertRaises(Exception) as context: rsp.url_prefix = "http://foo.bar.com/fragment#is" #print(context.exception) self.assertEquals( "URL should not have a fragment. Given: 'http://foo.bar.com/fragment#is'", context.exception.args[0]) self.assertIsInstance(context.exception, ValueError) with self.assertRaises(Exception) as context: rsp.url_prefix = "http://foo.bar.com/ uhr.isrong" #print(context.exception) self.assertEquals( "URL is invalid. Given: 'http://foo.bar.com/ uhr.isrong'", context.exception.args[0]) self.assertIsInstance(context.exception, ValueError) self.save_configuration_test(rsp)