def test_load_bad_config(database: Database) -> None: "is an error thrown if a bad config is read from the database" example_bad_json: str = random_json() # Prove the example_bad_json is not a valid configuration structure with pytest.raises(pydantic.ValidationError) as err: CommonSettings.parse_raw(example_bad_json) with db_session: database.ConfigEntity(version="current", json=example_bad_json, class_name=CommonSettings.__name__) database.commit() with pytest.raises(BadConfigInDBError) as err: get_config(db=database) assert "bad configuration in database" in str(err.value)
def save_config(db: Database, new_settings: CommonSettings) -> CommonSettings: "encodes the settings and writes them to the database" bad_class_msg = "new_settings must be an instance of a settings class" try: class_name = SettingsClassName.validate( new_settings.__class__.__name__) except ValidationError as err: raise ValueError(bad_class_msg) from err settings_class = getattr(settings, class_name) if settings_class != type(new_settings): raise ValueError(bad_class_msg) with db_session: config_entity = db.ConfigEntity(version="current", class_name=class_name, json=new_settings.json()) return settings_class.parse_raw(config_entity.json)