示例#1
0
	def test_set(self):
		with self.mocked_config():
			settings = octoprint.settings.Settings()

			settings.set(["server", "host"], "127.0.0.1")

			self.assertEqual("127.0.0.1", settings._config["server"]["host"])
示例#2
0
	def test_set_invalid(self, path):
		with self.mocked_config():
			settings = octoprint.settings.Settings()

			try:
				settings.set(path, "value", error_on_path=True)
				self.fail("Expected NoSuchSettingsPath")
			except octoprint.settings.NoSuchSettingsPath:
				pass
示例#3
0
	def test_set_none(self):
		with self.mocked_config():
			settings = octoprint.settings.Settings()

			self.assertTrue("port" in settings._config["server"])

			settings.set(["server", "port"], None)

			self.assertFalse("port" in settings._config["server"])
示例#4
0
	def test_set_default(self):
		with self.mocked_config():
			settings = octoprint.settings.Settings()

			self.assertEqual(8080, settings._config["server"]["port"])

			settings.set(["server", "port"], 5000)

			self.assertNotIn("port", settings._config["server"])
			self.assertEqual(5000, settings.get(["server", "port"]))
示例#5
0
	def test_set_preprocessor(self):
		with self.mocked_config():
			config = dict()
			defaults = dict(foo=dict(bar="fnord"))
			preprocessors = dict(foo=dict(bar=lambda x: x.upper()))

			settings = octoprint.settings.Settings()
			settings.set(["foo", "bar"],
			             "value",
			             config=config,
			             defaults=defaults,
			             preprocessors=preprocessors)

			self.assertEqual("VALUE", config["foo"]["bar"])
示例#6
0
    def test_set_preprocessor(self):
        with self.settings() as settings:
            config = {}
            defaults = {"foo_preprocessor": {"bar": "fnord"}}
            preprocessors = {"foo_preprocessor": {"bar": lambda x: x.upper()}}

            settings.set(
                ["foo_preprocessor", "bar"],
                "value",
                config=config,
                defaults=defaults,
                preprocessors=preprocessors,
            )

            self.assertEqual("VALUE", config["foo_preprocessor"]["bar"])
示例#7
0
	def test_save(self):
		with self.mocked_config() as paths:
			basedir, config_path = paths
			settings = octoprint.settings.Settings()

			# current modification date of config.yaml
			current_modified = os.stat(config_path).st_mtime

			# set a new value
			settings.set(["api", "key"], "newkey")

			# should not be written automatically
			self.assertEqual(current_modified, os.stat(config_path).st_mtime)

			# should be updated after calling save though
			settings.save()
			self.assertNotEqual(current_modified, os.stat(config_path).st_mtime)
示例#8
0
    def test_set_external_modification(self):
        with self.mocked_config() as paths:
            basedir, configfile = paths
            settings = octoprint.settings.Settings()

            self.assertEqual("0.0.0.0", settings.get(["server", "host"]))

            # modify yaml file externally
            config = self._load_yaml(configfile)
            config["server"]["host"] = "127.0.0.1"
            self._dump_yaml(configfile, config)

            # set some value, should also reload file before setting new api key
            settings.set(["api", "key"], "key")

            # verify updated values
            self.assertEqual("127.0.0.1", settings.get(["server", "host"]))
            self.assertEqual("key", settings.get(["api", "key"]))
示例#9
0
    def test_save(self):
        with self.settings() as settings:
            config_path = settings._configfile

            # current modification date of config.yaml
            current_modified = os.stat(config_path).st_mtime

            # sleep a bit to make sure we do have a change in the timestamp
            time.sleep(1.0)

            # set a new value
            settings.set(["api", "key"], "newkey")

            # should not be written automatically
            self.assertEqual(current_modified, os.stat(config_path).st_mtime)

            # should be updated after calling save though
            settings.save()
            self.assertNotEqual(current_modified, os.stat(config_path).st_mtime)
示例#10
0
    def test_set_external_modification(self):
        with self.settings() as settings:
            configfile = settings._configfile

            # Make sure the config files last modified time changes
            time.sleep(1.0)

            self.assertEqual("0.0.0.0", settings.get(["server", "host"]))

            # modify yaml file externally
            config = _load_yaml(configfile)
            config["server"]["host"] = "127.0.0.1"
            _dump_yaml(configfile, config)

            # set some value, should also reload file before setting new api key
            settings.set(["api", "key"], "key")

            # verify updated values
            self.assertEqual("127.0.0.1", settings.get(["server", "host"]))
            self.assertEqual("key", settings.get(["api", "key"]))
示例#11
0
	def test_set_external_modification(self):
		with self.mocked_config() as paths:
			basedir, configfile = paths
			settings = octoprint.settings.Settings()

			self.assertEqual("0.0.0.0", settings.get(["server", "host"]))

			# modify yaml file externally
			with open(configfile, "r+b") as f:
				config = yaml.safe_load(f)
			config["server"]["host"] = "127.0.0.1"
			with open(configfile, "w+b") as f:
				yaml.safe_dump(config, f)

			# set some value, should also reload file before setting new api key
			settings.set(["api", "key"], "key")

			# verify updated values
			self.assertEqual("127.0.0.1", settings.get(["server", "host"]))
			self.assertEqual("key", settings.get(["api", "key"]))
示例#12
0
 def test_set(self):
     with self.settings() as settings:
         settings.set(["server", "host"], "127.0.0.1")
         self.assertEqual("127.0.0.1", settings._config["server"]["host"])