def test_parse_invalid(filename): """Ensures that an exception is thrown when parsing an invalid config file if it is supposed to be """ config = IndBiosConfig() with pytest.raises(ConfigFieldValueError): filename = get_config_file_path(filename) config.read(filename, False)
def get_config_editor_for_file(filename): """:returns: an IndBiosConfig instance that is using the values specified\ for all fields that are defined in filename and default values\ for all the other fields """ config_editor = IndBiosConfig() config_editor.read(filename) return config_editor
def test_config_file_output_no_changes(config): """A config file is read by IndBiosConfig then a copy is written to disk. This test ensures that both files contain the same values """ with tempfile.TemporaryFile() as written_file_pointer: config.write(written_file_pointer) written_file_pointer.seek(os.SEEK_SET) new_config = IndBiosConfig() new_config.readfp(written_file_pointer, True) for option in CONFIG_OPTIONS: assert config.get(option) == new_config.get(option), ( option + " has the same value")
def test_load_preset(preset_filename, preset_config, apply_to_fields): """Ensures that presets can be loaded and the values will only be applied to the fields that they are meant to be applied to """ config = IndBiosConfig() config.load_preset(preset_filename, apply_to_fields) # Ideally I would I use subtest here so that if one assert fails the others will still be executed. # Unfortunately subtest is not available in pytest. The other alternative would be to use parameterized to # perform a separate test for each field but that would be really slow. for field in CONFIG_OPTIONS: new_field_value = config.get(field) if field in apply_to_fields: assert new_field_value == preset_config.get(field), ( field + " value changed to the one from the preset") else: assert new_field_value == CONFIG_DEFAULTS[ field], field + " value unchanged"
def test_set_value_invalid(option, value): """Ensures that exceptions are thrown when trying to set invalid values and that the value is not actually set """ config = IndBiosConfig() old_value = config.get(option) with pytest.raises(ConfigFieldValueError): config.set(option, value) assert config.get(option) == old_value, "value not changed"
def test_parse_invalid_field_reset_to_default( filename, field_that_is_reset, other_fields_that_should_not_be_reset): """Ensures that invalid fields are set to default when read from a file if they are supposed to be """ config = IndBiosConfig() filename = get_config_file_path(filename) config.read(filename, True) # Ensure that this value is reset to default as it was invalid assert config.get(field_that_is_reset) == config.defaults( )[field_that_is_reset], (field_that_is_reset + " reset to default value") # Ensure that these values were not reset to default for field in other_fields_that_should_not_be_reset: expected_value = other_fields_that_should_not_be_reset[field] assert config.get( field) == expected_value, field + " has default value"
def test_parse_valid_config_no_errors(filename): """Ensures that no exceptions are thrown when parsing a valid confi""" filename = get_config_file_path(filename) IndBiosConfig().read(filename)
def test_set_value_valid(option, value): """Ensures that valid values are set correctly""" config = IndBiosConfig() config.set(option, value) assert config.get(option) == value, "value set correctly"
"""Tests for the IndBiosConfig class""" import re import sys import os import pytest import tempfile from lib.configs import ( IndBiosConfig, ConfigFieldValueError, ConfigFieldNameError, ) TEST_CONFIG_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_configs") DEFAULT_CONFIG = IndBiosConfig() CONFIG_OPTIONS = DEFAULT_CONFIG.options() CONFIG_DEFAULTS = { option: DEFAULT_CONFIG.get(option) for option in CONFIG_OPTIONS } def sub_lists(list1): """:returns: all possible sublists for the input""" # store all the sublists sublists = [[]] # first loop for i in range(len(list1) + 1): # second loop