def test_singleton_contextually_presents_different_values(self):
        Mode = settings_handler.Mode
        modes = list(Mode)
        metaclass = settings_handler.Singleton

        results = []
        for some_mode in modes:
            try:
                instantiated = False
                singleton = settings_handler.Settings(self.inifile,
                                                      mode=some_mode)
            except Exception as e:
                raise
            else:
                instantiated = True
                contextual_value = singleton.log_filename
                results.append(contextual_value)
            finally:
                # cleanup and reset singeton
                if instantiated:
                    metaclass.reset_singleton(type(singleton))

        msg = "The values of the attribute did not vary with context."
        set_length = len(set(results))
        self.assertEqual(set_length, len(results), msg=msg)
    def test_specific_attribute_SCHEMA(self):
        singleton = settings_handler.Settings(self.inifile)
        var = singleton.schema

        expected_type = etree.XMLSchema

        self.assertIsInstance(var, expected_type)
    def test_singleton_has_mode_attr(self):
        live = settings_handler.Mode.LIVE
        expected_attr = "mode"

        singleton = settings_handler.Settings(self.inifile, mode=live)

        self.assertHasAttr(obj=singleton, attrname=expected_attr)
    def test_singleton_mode_attr_is_enum(self):
        import enum
        live = settings_handler.Mode.LIVE

        singleton = settings_handler.Settings(self.inifile, mode=live)

        self.assertIsInstance(singleton.mode, enum.Enum)
        self.assertIsInstance(singleton.mode, settings_handler.Mode)
    def test_has_expected_attributes(self):
        attrs = self.expected_attributes
        singleton = settings_handler.Settings(self.inifile)

        for expected in attrs:
            with self.subTest(attrname=expected):

                self.assertHasAttr(obj=singleton, attrname=expected)
    def test_has_no_unexpected_attributes(self):
        expected_attrs = set(self.expected_attributes)
        singleton = settings_handler.Settings(self.inifile)
        attrs = (a for a in dir(singleton) if not a.startswith("_"))

        for actual in attrs:
            with self.subTest(unepexcted_attr=actual):
                msg = f"Suprise: did not expect to find 'singleton.{actual}'"
                self.assertIn(actual, expected_attrs, msg=msg)
    def test_singleton_mode_defaults_to_live(self):
        Mode = settings_handler.Mode
        expected_mode = Mode.LIVE
        wrong_modes = [m for m in list(Mode) if m is not expected_mode]

        singleton = settings_handler.Settings(self.inifile)

        self.assertIs(singleton.mode, expected_mode)
        self.assertEqual(singleton.mode, expected_mode)
        for some_mode in wrong_modes:
            with self.subTest(wrong_mode=some_mode):
                self.assertIsNot(singleton.mode, some_mode)
                self.assertNotEqual(singleton.mode, some_mode)
    def test_attributes_are_not_settable(self):
        singleton = settings_handler.Settings(self.inifile)
        # No magic or 'private' attributes
        attributes = [a for a in dir(singleton) if not a.startswith("_")]
        if len(attributes) < 1:
            self.fail("Not Written")

        some_var = str(time.time())  # Unique string.

        for attribute in attributes:
            with self.subTest(attr=attribute):
                with self.assertRaises(AttributeError):
                    setattr(singleton, attribute, some_var)
 def test_singleton_class_accepts_mode_kwarg(self):
     live = settings_handler.Mode.LIVE
     try:
         settings_handler.Settings(self.inifile, mode=live)
     except TypeError as err:
         err_msg = str(err)
         err_kwarg = "keyword argument"
         if err_msg in err_kwarg:
             assertion_msg = "TypeError due to bad keyword argument."
             self.fail(assertion_msg)
         else:
             raise
     else:
         pass  # pass test if kwarg is accepted
    def test_specific_attribute_LOG_FILENAME(self):
        singleton = settings_handler.Settings(self.inifile)
        var = singleton.log_filename

        dest_dir = os.path.dirname(var)
        isAbsolute = dest_dir.startswith("/")
        isExpandedUser = "******" in dest_dir

        self.assertIsInstance(var, (str, pathlib.Path))
        self.assertTrue(os.path.isdir(dest_dir), f"Dir: {dest_dir}")
        self.assertTrue(isAbsolute,
                        f"Dir: {dest_dir} is not an absolute path.")
        self.assertFalse(isExpandedUser,
                         f"Dir: {dest_dir} needs userexpansion.")
 def test_failed_instantiation_raises_package_error(self):
     inifile = "Foo"
     assert not os.path.isfile(inifile), os.path.join(os.getcwd(), inifile)
     with self.assertRaises(exceptions.FileNotFound):
         settings_handler.Settings(inifile)
 def test_instantiation_requires_ini_argument(self):
     inifile = self.inifile
     settings_handler.Settings(inifile)