示例#1
0
def test_placeholder_setattr(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir, use_placeholders=True)
    assert conf.fake_dict.is_placeholder
    assert conf.fake_dict.fake_key.is_placeholder
    assert conf.fake_dict.fake_key.fake_subkey.is_placeholder
    conf.fake_dict.fake_key.fake_subkey = "so fake ._. wow"
    assert not conf.fake_dict.is_placeholder
    assert not conf.fake_dict.fake_key.is_placeholder
    assert conf.fake_dict.fake_key.fake_subkey == "so fake ._. wow"
    conf.save()
    igur = confjson.Config(tmpdir, use_placeholders=False)
    assert not igur.fake_dict.is_placeholder
    assert not igur.fake_dict.fake_key.is_placeholder
    assert igur.fake_dict.fake_key.fake_subkey == "so fake ._. wow"
示例#2
0
def test_create_user_config_file_if_missing(tmpdir):
    conf = confjson.Config(tmpdir)
    conf["a"] = "b"
    conf.save()
    with open(os.path.join(tmpdir, USER_CONFIG_FILENAME)) as file:
        user_json = json.load(file)
    assert user_json["a"] == conf["a"]
示例#3
0
def test_add_item_to_list_present_only_in_user_config(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir)
    conf["list_in_user"].append("new_item")
    assert conf["list_in_user"] == USER_CONFIG["list_in_user"] + ["new_item"]
    with pytest.raises(KeyError):
        conf.get_default("list_in_user")
示例#4
0
def test_get_when_key_does_not_exist(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir)
    with pytest.raises(KeyError):
        _ = conf["key-does-not-exist"]
    assert conf.get("key-does-not-exist") is None
    assert conf.get("key-does-not-exist", "no-it-does-not") == "no-it-does-not"
示例#5
0
def test_set_default_list_as_attribute(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir)
    conf.list_in_default[0] = "new_value_0"
    conf.list_in_default.append("new_value_-1")
    assert conf["list_in_default"][0] == "new_value_0"
    assert conf["list_in_default"][-1] == "new_value_-1"
示例#6
0
def test_add_item_to_dict_present_only_in_default_config(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir)
    conf["dict_in_default"]["new_key"] = "new_value"
    assert conf["dict_in_default"]["new_key"] == "new_value"
    assert conf.get_default(
        "dict_in_default") == DEFAULT_CONFIG["dict_in_default"]
示例#7
0
def test_add_item_to_dict_present_only_in_user_config(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir)
    conf["dict_in_user"]["new_key"] = "new_value"
    assert conf["dict_in_user"]["new_key"] == "new_value"
    with pytest.raises(KeyError):
        conf.get_default("dict_in_user")
示例#8
0
def test_nested_items(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir)
    assert len(tuple(conf.dict_in_both.items())) == len(
        set(USER_CONFIG["dict_in_both"].keys()).union(
            DEFAULT_CONFIG["dict_in_both"].keys()))
    for key, value in conf.dict_in_both.items():
        assert conf["dict_in_both"][key] == value
示例#9
0
def test_placeholder_setitem(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir, use_placeholders=True)
    assert conf["fake_dict"].is_placeholder
    assert conf["fake_dict"]["fake_key"].is_placeholder
    assert conf["fake_dict"]["fake_key"]["fake_subkey"].is_placeholder
    conf["fake_dict"]["fake_key"]["fake_subkey"] = "._. much placeholder"
    assert not conf["fake_dict"].is_placeholder
    assert not conf["fake_dict"]["fake_key"].is_placeholder
    assert conf["fake_dict"]["fake_key"][
        "fake_subkey"] == "._. much placeholder"
    conf.save()
    igur = confjson.Config(tmpdir, use_placeholders=False)
    assert not igur["fake_dict"].is_placeholder
    assert not igur["fake_dict"]["fake_key"].is_placeholder
    assert igur["fake_dict"]["fake_key"][
        "fake_subkey"] == "._. much placeholder"
示例#10
0
def test_remove_items_from_user_config_if_identical_to_default_config(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir)
    assert conf["string_in_both"] == USER_CONFIG["string_in_both"]
    conf["string_in_both"] = DEFAULT_CONFIG["string_in_both"]
    conf.save()
    with conf.user_config_path.open() as file:
        json_from_user_config_file = json.load(file)
    assert "string_in_both" not in json_from_user_config_file
示例#11
0
def test_add_item_to_list_present_only_in_default_config(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir)
    conf["list_in_default"].append("new_item")
    assert conf["list_in_default"] == DEFAULT_CONFIG["list_in_default"] + [
        "new_item"
    ]
    assert conf.get_default(
        "list_in_default") == DEFAULT_CONFIG["list_in_default"]
示例#12
0
def test_save_only_changed_items_in_nested_dicts(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir)
    conf["dict_in_default"]["originally_empty_dict"][
        "key_added_to_user"] = "******"
    conf.save()
    with conf.user_config_path.open() as file:
        user_json = json.load(file)
    assert "dict_in_default" in user_json
    assert len(user_json["dict_in_default"]["originally_empty_dict"]) == 1
    assert (user_json["dict_in_default"]["originally_empty_dict"]
            ["key_added_to_user"] == "krafs")
示例#13
0
def test_save_changes_only_to_user_config(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir)
    conf["new_key"] = "new_value"
    conf.save()

    with open(os.path.join(tmpdir, USER_CONFIG_FILENAME)) as file:
        user_json = json.load(file)
    assert user_json["new_key"] == "new_value"

    with open(os.path.join(tmpdir, DEFAULT_CONFIG_FILENAME)) as file:
        default_json = json.load(file)
    assert "new_key" not in default_json
示例#14
0
def test_delete_user_config_file_on_save_if_identical_to_default(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir)
    # This is callable. Shut up.
    # pylint: disable=not-callable
    all_keys = conf.keys()
    # pylint: enable=not-callable
    default_keys = conf.default_keys()
    for key in all_keys:
        if key in default_keys:
            conf[key] = conf.get_default(key)
        else:
            del conf[key]
    assert conf.user_config_path.exists()
    conf.save()
    assert not conf.user_config_path.exists()
示例#15
0
def test_config_item_proxy_inequality_because_mindless_coverage_hunting_is_good(
        tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir)
    assert conf.dict_in_both != json.dumps(conf.dict_in_both.get_dict())
示例#16
0
def test_getitem_suppressed_key_error(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir, use_placeholders=True)
    assert conf["dict_in_both"]["does_not_exist"].is_placeholder
示例#17
0
def test_getitem_unsuppressed_key_error(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir, use_placeholders=False)
    with pytest.raises(KeyError):
        _ = conf["dict_in_both"]["does_not_exist"]
示例#18
0
def test_placeholder_getitem(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir, use_placeholders=True)
    assert not conf["does_not_exist"]["not_at_all"]
    assert conf["does_not_exist"].is_placeholder
示例#19
0
def test_placeholder_getattr(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir, use_placeholders=True)
    assert not conf.does_not_exist.not_at_all
    assert conf.does_not_exist.is_placeholder
示例#20
0
def test_placeholder_equality(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir, use_placeholders=True)
    assert conf.does_not_exist != "krafs"
示例#21
0
def test_nested_keys(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir)
    assert set(conf.dict_in_both.keys()) == set(
        USER_CONFIG["dict_in_both"].keys()).union(
            DEFAULT_CONFIG["dict_in_both"].keys())
示例#22
0
def test_get_backing_dict(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir)
    backing_dict = conf.dict_in_user.get_dict()
    assert isinstance(backing_dict, dict)
    assert backing_dict == USER_CONFIG["dict_in_user"]
示例#23
0
def test_config_item_proxy_equality(tmpdir):
    _generate_both_config_files(tmpdir)
    con = confjson.Config(tmpdir)
    fig = confjson.Config(tmpdir)
    assert con.dict_in_both == fig.dict_in_both
示例#24
0
def test_init_with_directory_path(tmpdir):
    conf = confjson.Config(tmpdir)
    assert str(conf.directory) == tmpdir
示例#25
0
def test_init_with_file_path(tmpdir):
    filename = os.path.join(tmpdir, "file.py")
    with open(filename, "w") as file:
        file.write("\\[T]/")
    conf = confjson.Config(filename)
    assert str(conf.directory) == tmpdir
示例#26
0
def test_nested_contains(tmpdir, key):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir)
    assert key in conf.dict_in_both
示例#27
0
def test_init_with_nonexistant_file_path(tmpdir):
    with pytest.raises(ValueError):
        _ = confjson.Config(os.path.join(tmpdir, "does_not_exist.wtf"))
示例#28
0
def test_nested_get_when_exists(tmpdir, key):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir)
    assert conf.dict_in_both.get(key) == conf.dict_in_both[key]
示例#29
0
def test_load_user_config_file_if_it_exists(tmpdir):
    _generate_user_config(tmpdir)
    conf = confjson.Config(tmpdir)
    assert conf["string_in_user"] == USER_CONFIG["string_in_user"]
示例#30
0
def test_nested_get_when_notexists(tmpdir):
    _generate_both_config_files(tmpdir)
    conf = confjson.Config(tmpdir)
    assert conf.dict_in_both.get("hubris") is None
    assert conf.dict_in_both.get("snorlax", "pikachu") == "pikachu"