Beispiel #1
0
    def test_copy_to_dict(self):
        """

        :return:
        """
        settings = Settings(settings={"A": 1, "B": 2}, default_settings=False)

        self.assertDictEqual(settings.copy_to_dict(), {"A": 1, "B": 2})
Beispiel #2
0
    def test_load_yaml(self) -> None:
        """

        :return:
        :rtype: None
        """
        test_yaml = {"A": 1, "B": 2}

        yaml_file = NamedTemporaryFile(mode="w")
        yaml.dump(test_yaml, yaml_file)

        settings = Settings()
        with settings.unfreeze() as settings_:
            settings_.load_yaml(yaml_file.name)

        self.assertIn("A", settings)
        self.assertEqual(settings._data["A"], Setting("project", "A", 1))
Beispiel #3
0
    def test_load_json(self) -> None:
        """

        :return:
        :rtype: None
        """
        test_json = {"A": 1, "B": 2}

        json_file = NamedTemporaryFile()
        json_file.write(orjson.dumps(test_json))
        json_file.seek(0)

        settings = Settings()
        with settings.unfreeze() as settings_:
            settings_.load_json(json_file.name)

        self.assertIn("A", settings)
        self.assertEqual(settings._data["A"], Setting("project", "A", 1))
Beispiel #4
0
    def test_from_module(self) -> None:
        """
        test the method of from_module
        :return:
        :rtype: None
        """
        settings = Settings.from_module(self.test_module, "default")

        self.assertIn("A", settings)
        self.assertEqual(
            settings._data["A"],  # pylint: disable = protected-access
            Setting("default", "A", 1),
        )

        settings = Settings.from_module("tests.samples.settings", "default")

        self.assertIn("A", settings)
        self.assertEqual(
            settings._data["A"],  # pylint: disable = protected-access
            Setting("default", "A", 1),
        )
Beispiel #5
0
    def test_load_module(self) -> None:
        """
        test the method of load_module
        :return:
        :rtype: None
        """
        settings = Settings()
        with settings.unfreeze() as settings_:
            settings_.load_module(self.test_module)  # pylint: disable=no-member

        self.assertIn("A", settings)
        self.assertEqual(
            settings._data["A"],  # pylint: disable = protected-access
            Setting("project", "A", 1),
        )

        settings = Settings()
        with settings.unfreeze() as settings_:
            settings_.load_module("tests.samples.settings")

        self.assertIn("A", settings)
        self.assertEqual(
            settings._data["A"],  # pylint: disable = protected-access
            Setting("project", "A", 1),
        )
Beispiel #6
0
    def test_from_yaml(self) -> None:
        """

        :return:
        :rtype: None
        """
        test_yaml = {"A": 1, "B": 2}

        yaml_file = NamedTemporaryFile(mode="w")
        yaml.dump(test_yaml, yaml_file)

        settings = Settings.from_yaml(yaml_file.name, "default")

        self.assertIn("A", settings)
        self.assertEqual(settings._data["A"], Setting("default", "A", 1))
Beispiel #7
0
    def test_init(self) -> None:
        """

        :return:
        :rtype: None
        """
        settings = Settings()
        self.assertEqual(len(settings), 0)

        settings = Settings(default_settings=True)
        self.assertIn("LOG_LEVEL", settings)
        self.assertEqual(
            settings._data["LOG_LEVEL"],  # pylint: disable = protected-access
            Setting("default", "LOG_LEVEL", logging.INFO),
        )

        settings = Settings(default_settings="tests.samples.settings")
        self.assertIn("A", settings)
        self.assertEqual(
            settings._data["A"],  # pylint: disable = protected-access
            Setting("default", "A", 1),
        )

        settings = Settings({"LOG_LEVEL": logging.DEBUG}, default_settings=True)
        self.assertIn("LOG_LEVEL", settings)
        self.assertEqual(
            settings._data["LOG_LEVEL"],  # pylint: disable = protected-access
            Setting("project", "LOG_LEVEL", logging.DEBUG),
        )

        settings = Settings({"A": 0}, default_settings="tests.samples.settings")
        self.assertIn("A", settings)
        self.assertEqual(
            settings._data["A"],  # pylint: disable = protected-access
            Setting("project", "A", 0),
        )
Beispiel #8
0
def main(*args):
    """

    :param args:
    :return:
    """
    ns_args: Namespace = get_arguments(*args)

    settings = Settings(
        settings={
            **ns_args.setting,
            **ns_args.config
        },
        priority="cmd",
        default_settings=True,
    )
    set_logging(settings)
Beispiel #9
0
    def test_configure_logging(self, add_handler: MagicMock) -> None:
        """

        :param add_handler:
        :type add_handler: MagicMock
        :return:
        :rtype: None
        """
        settings = Settings(default_settings="tests.samples.settings")
        configure_logging(settings)

        add_handler.assert_called()

        (handler,), _ = add_handler.call_args
        self.assertIsInstance(handler, logging.StreamHandler)

        self.assertEqual(handler.level, settings["LOG_LEVEL"])
        self.assertEqual(handler.formatter.datefmt, settings["LOG_FORMATTER_DATEFMT"])
        self.assertEqual(handler.formatter._fmt, settings["LOG_FORMATTER_FMT"])
Beispiel #10
0
    def test_set_logging(
        self, get_runtime_info: MagicMock, configure_logging: MagicMock
    ) -> None:
        """

        :param configure_logging:
        :type configure_logging: MagicMock
        :param get_runtime_info:
        :type get_runtime_info: MagicMock
        :return:
        :rtype: None
        """
        settings = Settings(default_settings="tests.samples.settings")
        set_logging(settings)

        logger = logging.getLogger("amphisbaena")

        configure_logging.assert_called_once_with(settings)
        get_runtime_info.assert_called_once_with(logger)
        self.assertEqual(logger.level, settings["LOG_LEVEL"])