wip unit tests

This commit is contained in:
Tagadda 2022-02-17 22:07:25 +00:00
parent 2d92c93af1
commit dcb01a249b

View file

@ -10,66 +10,94 @@ import yunohost.settings as settings
from yunohost.settings import ( from yunohost.settings import (
settings_get, settings_get,
settings_list, settings_list,
_get_settings,
settings_set, settings_set,
settings_reset, settings_reset,
settings_reset_all, settings_reset_all,
SETTINGS_PATH_OTHER_LOCATION, SETTINGS_PATH
SETTINGS_PATH,
DEFAULTS,
) )
DEFAULTS["example.bool"] = {"type": "bool", "default": True} EXAMPLE_SETTINGS = """
DEFAULTS["example.int"] = {"type": "int", "default": 42} [example]
DEFAULTS["example.string"] = {"type": "string", "default": "yolo swag"} [example.example]
DEFAULTS["example.enum"] = {"type": "enum", "default": "a", "choices": ["a", "b", "c"]} [example.example.boolean]
type = "boolean"
yes = "True"
no = "False"
default = "True"
[example.example.number]
type = "number"
default = "42"
[example.example.string]
type = "string"
default = "yolo swag"
[example.example.select]
type = "select"
choices = ["a", "b", "c"]
default = "a"
"""
def setup_function(function): def setup_function(function):
os.system("mv /etc/yunohost/settings.json /etc/yunohost/settings.json.saved") os.system("mv /etc/yunohost/settings.yml /etc/yunohost/settings.yml.saved")
os.system("cp /usr/share/yunohost/config_settings.toml /usr/share/yunohost/config_settings.toml.saved")
with open("/usr/share/yunohost/config_settings.py", "a") as file:
file.write(EXAMPLE_SETTINGS)
def teardown_function(function): def teardown_function(function):
os.system("mv /etc/yunohost/settings.json.saved /etc/yunohost/settings.json") os.system("mv /etc/yunohost/settings.yml.saved /etc/yunohost/settings.yml")
for filename in glob.glob("/etc/yunohost/settings-*.json"): os.system("mv /usr/share/yunohost/config_settings.toml.saved /usr/share/yunohost/config_settings.toml")
os.remove(filename)
def monkey_get_setting_description(key): def _get_settings():
return "Dummy %s setting" % key.split(".")[-1] return yaml.load(open(SETTINGS_PATH, "r"))
settings._get_setting_description = monkey_get_setting_description
def test_settings_get_bool(): def test_settings_get_bool():
assert settings_get("example.bool") assert settings_get("example.example.boolean")
def test_settings_get_full_bool(): # FIXME : Testing this doesn't make sense ? This should be tested in a test_config.py ?
assert settings_get("example.bool", True) == { #def test_settings_get_full_bool():
"type": "bool", # assert settings_get("example.example.boolean", True) == {'version': '1.0',
"value": True, # 'i18n': 'global_settings_setting',
"default": True, # 'panels': [{'services': [],
"description": "Dummy bool setting", # 'actions': {'apply': {'en': 'Apply'}},
} # 'sections': [{'name': '',
# 'services': [],
# 'optional': True,
# 'options': [{'type': 'boolean',
# 'yes': 'True',
# 'no': 'False',
# 'default': 'True',
# 'id': 'boolean',
# 'name': 'boolean',
# 'optional': True,
# 'current_value': 'True',
# 'ask': 'global_settings_setting_boolean',
# 'choices': []}],
# 'id': 'example'}],
# 'id': 'example',
# 'name': {'en': 'Example'}}]}
def test_settings_get_int(): def test_settings_get_int():
assert settings_get("example.int") == 42 assert settings_get("example.example.number") == 42
def test_settings_get_full_int(): #def test_settings_get_full_int():
assert settings_get("example.int", True) == { # assert settings_get("example.int", True) == {
"type": "int", # "type": "int",
"value": 42, # "value": 42,
"default": 42, # "default": 42,
"description": "Dummy int setting", # "description": "Dummy int setting",
} # }
def test_settings_get_string(): def test_settings_get_string():
assert settings_get("example.string") == "yolo swag" assert settings_get("example.example.string") == "yolo swag"
def test_settings_get_full_string(): def test_settings_get_full_string():
@ -86,94 +114,85 @@ def test_settings_get_enum():
def test_settings_get_full_enum(): def test_settings_get_full_enum():
assert settings_get("example.enum", True) == { option = settings_get("example.enum", full=True).get('panels')[0].get('sections')[0].get('options')[0]
"type": "enum", assert option.get('choices') == ["a", "b", "c"]
"value": "a",
"default": "a",
"description": "Dummy enum setting",
"choices": ["a", "b", "c"],
}
def test_settings_get_doesnt_exists(): def test_settings_get_doesnt_exists():
with pytest.raises(YunohostError): with pytest.raises(YunohostValidationError):
settings_get("doesnt.exists") settings_get("doesnt.exists")
def test_settings_list(): #def test_settings_list():
assert settings_list() == _get_settings() # assert settings_list() == _get_settings()
def test_settings_set(): def test_settings_set():
settings_set("example.bool", False) settings_set("example.example.boolean", False)
assert settings_get("example.bool") is False assert settings_get("example.example.boolean") is False
settings_set("example.bool", "on") settings_set("example.example.boolean", "on")
assert settings_get("example.bool") is True assert settings_get("example.example.boolean") is True
def test_settings_set_int(): def test_settings_set_int():
settings_set("example.int", 21) settings_set("example.example.number", 21)
assert settings_get("example.int") == 21 assert settings_get("example.example.number") == 21
def test_settings_set_enum(): def test_settings_set_enum():
settings_set("example.enum", "c") settings_set("example.example.select", "c")
assert settings_get("example.enum") == "c" assert settings_get("example.example.select") == "c"
def test_settings_set_doesexit(): def test_settings_set_doesexit():
with pytest.raises(YunohostError): with pytest.raises(YunohostValidationError):
settings_set("doesnt.exist", True) settings_set("doesnt.exist", True)
def test_settings_set_bad_type_bool(): def test_settings_set_bad_type_bool():
with pytest.raises(YunohostError): with pytest.raises(YunohostError):
settings_set("example.bool", 42) settings_set("example.example.boolean", 42)
with pytest.raises(YunohostError): with pytest.raises(YunohostError):
settings_set("example.bool", "pouet") settings_set("example.example.boolean", "pouet")
def test_settings_set_bad_type_int(): def test_settings_set_bad_type_int():
with pytest.raises(YunohostError): with pytest.raises(YunohostError):
settings_set("example.int", True) settings_set("example.example.number", True)
with pytest.raises(YunohostError): with pytest.raises(YunohostError):
settings_set("example.int", "pouet") settings_set("example.example.number", "pouet")
def test_settings_set_bad_type_string(): def test_settings_set_bad_type_string():
with pytest.raises(YunohostError): with pytest.raises(YunohostError):
settings_set("example.string", True) settings_set("example.example.string", True)
with pytest.raises(YunohostError): with pytest.raises(YunohostError):
settings_set("example.string", 42) settings_set("example.example.string", 42)
def test_settings_set_bad_value_enum(): def test_settings_set_bad_value_enum():
with pytest.raises(YunohostError): with pytest.raises(YunohostError):
settings_set("example.enum", True) settings_set("example.example.select", True)
with pytest.raises(YunohostError): with pytest.raises(YunohostError):
settings_set("example.enum", "e") settings_set("example.example.select", "e")
with pytest.raises(YunohostError): with pytest.raises(YunohostError):
settings_set("example.enum", 42) settings_set("example.example.select", 42)
with pytest.raises(YunohostError): with pytest.raises(YunohostError):
settings_set("example.enum", "pouet") settings_set("example.example.select", "pouet")
def test_settings_list_modified(): def test_settings_list_modified():
settings_set("example.int", 21) settings_set("example.example.number", 21)
assert settings_list()["example.int"] == { assert settings_list()["number"] == 42
"default": 42,
"description": "Dummy int setting",
"type": "int",
"value": 21,
}
def test_reset(): def test_reset():
settings_set("example.int", 21) option = settings_get("example.example.number", full=True).get('panels')[0].get('sections')[0].get('options')[0]
assert settings_get("example.int") == 21 settings_set("example.example.number", 21)
settings_reset("example.int") assert settings_get("number") == 21
assert settings_get("example.int") == settings_get("example.int", True)["default"] settings_reset("example.example.number")
assert settings_get("example.example.number") == option["default"]
def test_settings_reset_doesexit(): def test_settings_reset_doesexit():
@ -183,10 +202,10 @@ def test_settings_reset_doesexit():
def test_reset_all(): def test_reset_all():
settings_before = settings_list() settings_before = settings_list()
settings_set("example.bool", False) settings_set("example.example.boolean", False)
settings_set("example.int", 21) settings_set("example.example.number", 21)
settings_set("example.string", "pif paf pouf") settings_set("example.example.string", "pif paf pouf")
settings_set("example.enum", "c") settings_set("example.example.select", "c")
assert settings_before != settings_list() assert settings_before != settings_list()
settings_reset_all() settings_reset_all()
if settings_before != settings_list(): if settings_before != settings_list():
@ -194,30 +213,30 @@ def test_reset_all():
assert settings_before[i] == settings_list()[i] assert settings_before[i] == settings_list()[i]
def test_reset_all_backup(): #def test_reset_all_backup():
settings_before = settings_list() # settings_before = settings_list()
settings_set("example.bool", False) # settings_set("example.bool", False)
settings_set("example.int", 21) # settings_set("example.int", 21)
settings_set("example.string", "pif paf pouf") # settings_set("example.string", "pif paf pouf")
settings_set("example.enum", "c") # settings_set("example.enum", "c")
settings_after_modification = settings_list() # settings_after_modification = settings_list()
assert settings_before != settings_after_modification # assert settings_before != settings_after_modification
old_settings_backup_path = settings_reset_all()["old_settings_backup_path"] # old_settings_backup_path = settings_reset_all()["old_settings_backup_path"]
#
for i in settings_after_modification: # for i in settings_after_modification:
del settings_after_modification[i]["description"] # del settings_after_modification[i]["description"]
#
assert settings_after_modification == json.load(open(old_settings_backup_path, "r")) # assert settings_after_modification == json.load(open(old_settings_backup_path, "r"))
def test_unknown_keys(): #def test_unknown_keys():
unknown_settings_path = SETTINGS_PATH_OTHER_LOCATION % "unknown" # unknown_settings_path = SETTINGS_PATH_OTHER_LOCATION % "unknown"
unknown_setting = { # unknown_setting = {
"unkown_key": {"value": 42, "default": 31, "type": "int"}, # "unkown_key": {"value": 42, "default": 31, "type": "int"},
} # }
open(SETTINGS_PATH, "w").write(json.dumps(unknown_setting)) # open(SETTINGS_PATH, "w").write(json.dumps(unknown_setting))
#
# stimulate a write # # stimulate a write
settings_reset_all() # settings_reset_all()
#
assert unknown_setting == json.load(open(unknown_settings_path, "r")) # assert unknown_setting == json.load(open(unknown_settings_path, "r"))