def test_settings_get_section_name(): class SomeSectionSettings: class Config: title = "Some Section Settings" section = SettingsManager._get_section_name(SomeSectionSettings()) assert section == "some_section"
def test_settings_load_invalid_content(tmp_path): # This is invalid content data = ":" with open(tmp_path / SettingsManager._FILENAME, "w") as fh: fh.write(data) with pytest.warns(UserWarning): SettingsManager(tmp_path)
def test_settings_loads(tmp_path): data = """ appearance: theme: light """ with open(tmp_path / SettingsManager._FILENAME, "w") as fh: fh.write(data) settings = SettingsManager(tmp_path) assert settings.appearance.theme == "light"
def test_settings_load_invalid_section(tmp_path): # The invalid section will be removed from the file data = """ non_existing_section: foo: bar """ with open(tmp_path / SettingsManager._FILENAME, "w") as fh: fh.write(data) settings = SettingsManager(tmp_path) assert getattr(settings, "non_existing_section") is None
def test_settings_load_invalid_key(tmp_path): # The invalid key will be removed data = """ application: non_existing_key: [1, 2] """ with open(tmp_path / SettingsManager._FILENAME, "w") as fh: fh.write(data) settings = SettingsManager(tmp_path) assert getattr(settings, "non_existing_key") is None
def test_settings_load_invalid_type(tmp_path): # The invalid data will be replaced by the default value data = """ application: first_time: [1, 2] """ with open(tmp_path / SettingsManager._FILENAME, "w") as fh: fh.write(data) settings = SettingsManager(tmp_path) assert settings.application.first_time is True
def test_settings_env_variables_do_not_write_to_disk(tmp_path, monkeypatch): data = """ appearance: theme: light """ with open(tmp_path / SettingsManager._FILENAME, "w") as fh: fh.write(data) value = 'dark' monkeypatch.setenv('NAPARI_THEME', value) settings = SettingsManager(tmp_path, save_to_disk=True) settings._save() with open(tmp_path / SettingsManager._FILENAME) as fh: saved_data = fh.read() model_values = settings._to_dict(safe=True) saved_values = safe_load(saved_data) assert model_values["appearance"]["theme"] == value assert saved_values.get("appearance", {}).get("theme", "") == ""
def test_settings_only_saves_non_default_values(tmp_path): settings = SettingsManager(tmp_path, save_to_disk=False) all_data = settings._to_dict(safe=True) with open(tmp_path / SettingsManager._FILENAME, "w") as fh: fh.write(safe_dump(all_data)) settings = SettingsManager(tmp_path, save_to_disk=True) settings._save() with open(tmp_path / SettingsManager._FILENAME) as fh: data = safe_load(fh.read()) assert all_data.keys() == data.keys() assert all_data != data
def test_settings_get_section_name(): class SomeSectionSettings: pass section = SettingsManager._get_section_name(SomeSectionSettings) assert section == "somesection"
def test_settings_file_not_created(tmp_path): SettingsManager(tmp_path, save_to_disk=False) fpath = tmp_path / "settings.yaml" assert not fpath.exists()
def test_settings_file(tmp_path): SettingsManager(tmp_path, save_to_disk=True) fpath = tmp_path / "settings.yaml" assert fpath.exists()
def settings(tmp_path): return SettingsManager(tmp_path, save_to_disk=True)
def test_settings_file_not_created(tmp_path): SettingsManager(tmp_path, save_to_disk=False) fpath = tmp_path / SettingsManager._FILENAME assert not fpath.exists()
def test_settings_env_variables_fails(tmp_path, monkeypatch): value = 'FOOBAR' monkeypatch.setenv('NAPARI_THEME', value) with pytest.raises(pydantic.error_wrappers.ValidationError): SettingsManager(tmp_path, save_to_disk=True)
def test_settings_env_variables(tmp_path, monkeypatch): value = 'light' monkeypatch.setenv('NAPARI_THEME', value) settings = SettingsManager(tmp_path, save_to_disk=True) assert CORE_SETTINGS[0]().theme == value assert settings.appearance.theme == value
def test_settings_file(tmp_path): SettingsManager(tmp_path, save_to_disk=True) fpath = tmp_path / SettingsManager._FILENAME assert fpath.exists()