Exemple #1
0
    def test_nonexisting_file(self, tmp_module, raises_load_error):
        """
        If the base path provided to LazySettings exists, but the file corresponding
        to the Setting object does not exist, SettingFileDoesNotExist should
        be raised.

        Note that this exception actually gets triggered in the Setting object.

        [x] TODO:
        --------
        Should we test under various situations of strict load?  Should we allow
        strict load to ignore missing files as long as one valid file exists?
        """
        tmp_module('app/settings')

        settings = LazySettings('dev', base_dir=self.base_path)
        with raises_load_error(SettingFileDoesNotExist):
            settings.SETTINGS_VALUE

        # Test With Non Existing Environment Variable but Valid Init Setting
        tmp_module('app/settings/dev.py')
        os.environ['SETTINGS_FILE'] = 'dev2'

        settings = LazySettings('dev',
                                env_keys='SETTINGS_FILE',
                                base_dir=self.base_path)
        with raises_load_error(SettingFileDoesNotExist):
            settings.SETTINGS_VALUE
    def test_file_path(self, tmp_module):

        content = self.randomize_content(params=['TEST_VARIABLE_1', 'TEST_VARIABLE_2'])
        tmp_module(self.module_path, content=content.as_string)

        settings = LazySettings(self.settings_path, base_dir=self.base_path)

        assert settings.as_dict() == content.as_dict
    def test_command_file_path(self, tmp_module, module_string):

        sys.argv.append('--testargs=%s' % self.settings_path)

        content = self.randomize_content(params=['TEST_VARIABLE_1', 'TEST_VARIABLE_2'])
        tmp_module(self.module_path, content=content.as_string)

        settings = LazySettings(base_dir=self.base_path, command_line_args='--testargs')
        assert settings.as_dict() == content.as_dict
    def test_env_file_path(self, tmp_module):

        content = self.randomize_content(params=['TEST_VARIABLE_1', 'TEST_VARIABLE_2'])
        tmp_module(self.module_path, content=content.as_string)

        os.environ['TEST_FILE'] = self.settings_path
        settings = LazySettings(base_dir=self.base_path, env_keys='TEST_FILE')

        assert settings.as_dict() == content.as_dict
Exemple #5
0
    def test_imports_ignored(self, tmp_module):

        content = """
        import pathlib

        TEST_VARIABLE_1 = 5
        TEST_VARIABLE_2 = 10
        """

        tmp_module(self.module_path, content=content)
        settings = LazySettings('dev', base_dir=self.base_path)

        assert settings.as_dict() == {
            'TEST_VARIABLE_1': 5,
            'TEST_VARIABLE_2': 10,
        }
Exemple #6
0
    def test_env_settings_do_not_override(self, tmp_module):
        """
        ENV settings should be overridden by any settings specified via
        initialization of the LazySettings object.
        """
        tmp_module('app/settings/dev.py',
                   content={
                       'TEST_VARIABLE_1': 1,
                       'TEST_VARIABLE_2': 15,
                   })

        tmp_module('app/settings/dev2.py',
                   content={
                       'TEST_VARIABLE_1': 5,
                       'TEST_VARIABLE_3': 10,
                   })

        os.environ['TEST_FILE1'] = 'dev2'

        settings = LazySettings('dev',
                                base_dir=self.base_path,
                                env_keys=['TEST_FILE1'])

        assert settings.TEST_VARIABLE_1 == 1
        assert settings.TEST_VARIABLE_2 == 15
        assert settings.TEST_VARIABLE_3 == 10
Exemple #7
0
    def test_functions_ignored(self, tmp_module):

        content = """
        def sample_function():
            return 1

        TEST_VARIABLE_1 = 5
        TEST_VARIABLE_2 = 10
        """

        tmp_module(self.module_path, content=content)
        settings = LazySettings('dev', base_dir=self.base_path)

        assert settings.as_dict() == {
            'TEST_VARIABLE_1': 5,
            'TEST_VARIABLE_2': 10,
        }
Exemple #8
0
 def test_settings_not_configured(self):
     """
     If LazySettings is initialized without any specified settings, or
     any valid ENV variables or command line arguments, SettingsNotConfigured
     should be raised to indicate that there are no settings specified.
     """
     settings = LazySettings(base_dir='app/settings.py')
     with pytest.raises(SettingsNotConfigured):
         settings.SOME_VALUE
Exemple #9
0
    def test_file_base_path(self, tmp_module):
        """
        If the path of the base directory (in this case, the temp directory
        joined with 'users/john/settings') indicates a file, an exception should be
        raised.
        """
        tmp_module('users/john/settings.py')
        settings = LazySettings('dev.py', base_dir='users/john/settings.py')

        with pytest.raises(BasePathIsNotDir):
            settings.SETTINGS_VALUE
Exemple #10
0
    def test_nonexisting_path(self):
        """
        When trying to access a value on the settings object for the first time, the
        settings will be loaded.

        If the absolute path of the base directory (in this case, the temp directory
        joined with '/Users/John/settings') does not exist, an exception should be
        raised.
        """
        settings = LazySettings('dev', 'base.py', base_dir='app/settings')
        with pytest.raises(BasePathDoesNotExist):
            settings.SETTINGS_VALUE
Exemple #11
0
    def test_case_insensitive(self, tmp_module):

        tmp_module(self.module_path,
                   content={
                       'TEST_VARIABLE_1': 1,
                       'TEST_VARIABLE_2': 5,
                   })

        settings = LazySettings('dev', base_dir=self.base_path)

        assert settings.test_variable_1 == 1
        assert settings.test_variable_2 == 5
Exemple #12
0
    def test_env_variables_missing(self, tmp_module):
        """
        If LazySettings is initialized with ENV keys and there is no value
        in os.environ associated with those ENV keys, then the EnvVarsNotFound
        exception should be raised.
        """
        tmp_module('app/settings/dev.py')
        settings = LazySettings(env_keys=['dev'],
                                base_dir='app/settings/dev.py')

        with pytest.raises(EnvVarsNotFound):
            settings.TEST_VARIABLE_1
def test_module_reload(tmp_module, tmpdir):
    """
    Helps us to ensure that updating the content of the same file in a given
    test will have the changes reflected in LazySettings when the module due to
    proper module reloading/cache clearing.
    """
    content = """
    TEST_VARIABLE_1 = 5
    TEST_VARIABLE_3 = 10
    """

    tmp_module('app/settings/dev.py', content=content)
    settings = LazySettings('dev', base_dir='app/settings')
    assert settings.TEST_VARIABLE_1 == 5

    content2 = """
    TEST_VARIABLE_1 = 50
    TEST_VARIABLE_3 = 100
    """

    tmp_module('app/settings/dev.py', content=content2)
    settings = LazySettings('dev', base_dir='app/settings')
    assert settings.TEST_VARIABLE_1 == 50
def test_module_reload_second_func(tmp_module, tmpdir):
    """
    Helps us ensure that settings modules created off of the tmpdir for any
    given test are properly reloaded and handled independently across various
    tests.

    This has to do with the `__init__.py` problem, which is mentioned in the
    /tests/module.py file.
    """
    content = """
    TEST_VARIABLE_1 = 50
    TEST_VARIABLE_3 = 100
    """

    tmp_module('app/settings/dev.py', content=content)
    settings = LazySettings('dev', base_dir='app/settings')

    # Important to check .as_dict() instead of individual parameters to make sure
    # settings parameters are not getting included from a setting module created
    # in another test.
    assert settings.as_dict() == {
        'TEST_VARIABLE_1': 50,
        'TEST_VARIABLE_3': 100,
    }
    def test_module_file_path(self, tmp_module, module_string):
        """
        Specifying an extension on a module path (i.e. app.settings.dev.py) will
        cause the extension to be treated as the filename, raising an exception.
        """
        content = self.randomize_content(params=['TEST_VARIABLE_1', 'TEST_VARIABLE_2'])
        tmp_module(self.module_path, content=content.as_string)

        settings_path = module_string(self.base_path)
        settings_path += '.py'

        settings = LazySettings(settings_path, base_dir=self.base_path)
        with pytest.raises(SettingsLoadError) as e:
            settings.TEST_VARIABLE_1

        assert [e.exc.__class__ for e in e.value.errors] == [SettingFileDoesNotExist]
    def test_multiple_files(self, tmp_module):

        tmp_module('app/settings/dev.py',
                   content={
                       'TEST_VARIABLE_1': 1,
                       'TEST_VARIABLE_2': 15,
                   })

        tmp_module('app/settings/dev2.py',
                   content={
                       'TEST_VARIABLE_1': 5,
                       'TEST_VARIABLE_3': 10,
                   })

        settings = LazySettings('dev', 'dev2', base_dir=self.base_path)

        assert settings.TEST_VARIABLE_1 == 5
        assert settings.TEST_VARIABLE_2 == 15
        assert settings.TEST_VARIABLE_3 == 10
Exemple #17
0
    def test_invalid_import_raises(self, tmp_module):

        invalid_content = """
        import missing_package

        TEST_VARIABLE_1 = 5
        TEST_VARIABLE_3 = 10
        """

        tmp_module('app/settings/dev.py',
                   content=invalid_content,
                   invalid=True)

        settings = LazySettings('dev', base_dir=self.base_path)
        with pytest.raises(SettingsLoadError) as e:
            settings.TEST_VARIABLE_1

        assert ([err.exc.__class__
                 for err in e.value.errors] == [ModuleNotFoundError])
    def test_multiple_env_files(self, tmp_module):

        tmp_module('app/settings/dev.py',
                   content={
                       'TEST_VARIABLE_1': 1,
                       'TEST_VARIABLE_2': 15,
                   })

        tmp_module('app/settings/dev2.py',
                   content={
                       'TEST_VARIABLE_1': 5,
                       'TEST_VARIABLE_3': 10,
                   })

        os.environ['TEST_FILE1'] = 'dev'
        os.environ['TEST_FILE2'] = 'dev2'

        settings = LazySettings(base_dir=self.base_path,
                                env_keys=['TEST_FILE1', 'TEST_FILE2'])

        assert settings.TEST_VARIABLE_1 == 5
        assert settings.TEST_VARIABLE_2 == 15
        assert settings.TEST_VARIABLE_3 == 10
    def test_multiple_files_with_invalid(self, tmp_module):
        """
        If LazySettings encounters an invalid/unimportable settings file during
        load, it's treatment of the initialization depends on the value of the
        `validation` __init__ param.

        If `validation = strict` (by default), SettingsLoadError should be
        raised when the LazySettings instance attempts to load the settings
        files.

        If `validation = none`:
            - If there is at least one valid file that was loaded, LazySettings
              should issue a warning for the invalid files encountered.
            - If there are no valid files loaded, SettingsLoadError should still
              raised.
        """
        valid_content = """
        import pathlib

        TEST_VARIABLE_1 = 1
        TEST_VARIABLE_2 = 15
        """

        invalid_content = """
        import missing_package

        TEST_VARIABLE_1 = 5
        TEST_VARIABLE_3 = 10
        """

        tmp_module('app/settings/dev.py', content=valid_content)
        tmp_module('app/settings/dev2.py',
                   content=invalid_content,
                   invalid=True)
        tmp_module('app/settings/dev3.py',
                   content=invalid_content,
                   invalid=True)

        # Condition: `validation = strict`,  one file invalid.
        # Expected: SettingsLoadError raised
        settings = LazySettings('dev', 'dev2', base_dir=self.base_path)
        with pytest.raises(SettingsLoadError) as e:
            settings.TEST_VARIABLE_1

        assert ([err.exc.__class__
                 for err in e.value.errors] == [ModuleNotFoundError])

        # Condition: `validation = none`, one file invalid.
        # Expected: SettingsLoadError not raised
        settings = LazySettings('dev',
                                'dev2',
                                base_dir=self.base_path,
                                validation=None)

        assert settings.TEST_VARIABLE_1 == 1
        assert settings.TEST_VARIABLE_2 == 15

        # Condition: `validation = strict`, both files invalid.
        # Expected: SettingsLoadError raised (value of `validation` is irrelevant).
        settings = LazySettings('dev3', 'dev2', base_dir=self.base_path)
        with pytest.raises(SettingsLoadError) as e:
            settings.TEST_VARIABLE_1

        assert ([err.exc.__class__ for err in e.value.errors
                 ] == [ModuleNotFoundError, ModuleNotFoundError])

        # Condition: `validation = none`, both files invalid.
        # Expected: SettingsLoadError raised (value of `validation` is irrelevant).
        settings = LazySettings('dev3',
                                'dev2',
                                base_dir=self.base_path,
                                validation=None)
        with pytest.raises(SettingsLoadError) as e:
            settings.TEST_VARIABLE_1

        assert ([err.exc.__class__ for err in e.value.errors
                 ] == [ModuleNotFoundError, ModuleNotFoundError])