| 1 |
from pytest import raises |
| 2 |
|
| 3 |
from invoke.config import merge_dicts, copy_dict, AmbiguousMergeError |
| 4 |
|
| 5 |
|
| 6 |
class merge_dicts_: |
| 7 |
# NOTE: don't usually like doing true unit tests of low level plumbing - |
| 8 |
# prefer to infer it's all working by examining higher level behavior - but |
| 9 |
# sometimes it's necessary to more easily stamp out certain bugs. |
| 10 |
|
| 11 |
def merging_data_onto_empty_dict(self): |
| 12 |
d1 = {} |
| 13 |
d2 = {"foo": "bar"} |
| 14 |
merge_dicts(d1, d2) |
| 15 |
assert d1 == d2 |
| 16 |
|
| 17 |
def updating_with_None_acts_like_merging_empty_dict(self): |
| 18 |
# When bug present, AttributeError is raised on a None.items() |
| 19 |
d1 = {"my": "data"} |
| 20 |
d2 = None |
| 21 |
merge_dicts(d1, d2) |
| 22 |
assert d1 == {"my": "data"} |
| 23 |
|
| 24 |
def orthogonal_data_merges(self): |
| 25 |
d1 = {"foo": "bar"} |
| 26 |
d2 = {"biz": "baz"} |
| 27 |
merge_dicts(d1, d2) |
| 28 |
assert d1 == {"foo": "bar", "biz": "baz"} |
| 29 |
|
| 30 |
def updates_arg_values_win(self): |
| 31 |
d1 = {"foo": "bar"} |
| 32 |
d2 = {"foo": "notbar"} |
| 33 |
merge_dicts(d1, d2) |
| 34 |
assert d1 == {"foo": "notbar"} |
| 35 |
|
| 36 |
def non_dict_type_mismatch_overwrites_ok(self): |
| 37 |
d1 = {"foo": "bar"} |
| 38 |
d2 = {"foo": [1, 2, 3]} |
| 39 |
merge_dicts(d1, d2) |
| 40 |
assert d1 == {"foo": [1, 2, 3]} |
| 41 |
|
| 42 |
def merging_dict_into_nondict_raises_error(self): |
| 43 |
# TODO: or...should it?! If a user really wants to take a pre-existing |
| 44 |
# config path and make it 'deeper' by overwriting e.g. a string with a |
| 45 |
# dict of strings (or whatever)...should they be allowed to? |
| 46 |
d1 = {"foo": "bar"} |
| 47 |
d2 = {"foo": {"uh": "oh"}} |
| 48 |
with raises(AmbiguousMergeError): |
| 49 |
merge_dicts(d1, d2) |
| 50 |
|
| 51 |
def merging_nondict_into_dict_raises_error(self): |
| 52 |
d1 = {"foo": {"uh": "oh"}} |
| 53 |
d2 = {"foo": "bar"} |
| 54 |
with raises(AmbiguousMergeError): |
| 55 |
merge_dicts(d1, d2) |
| 56 |
|
| 57 |
def nested_leaf_values_merge_ok(self): |
| 58 |
d1 = {"foo": {"bar": {"biz": "baz"}}} |
| 59 |
d2 = {"foo": {"bar": {"biz": "notbaz"}}} |
| 60 |
merge_dicts(d1, d2) |
| 61 |
assert d1 == {"foo": {"bar": {"biz": "notbaz"}}} |
| 62 |
|
| 63 |
def mixed_branch_levels_merges_ok(self): |
| 64 |
d1 = {"foo": {"bar": {"biz": "baz"}}, "meh": 17, "myown": "ok"} |
| 65 |
d2 = {"foo": {"bar": {"biz": "notbaz"}}, "meh": 25} |
| 66 |
merge_dicts(d1, d2) |
| 67 |
expected = { |
| 68 |
"foo": {"bar": {"biz": "notbaz"}}, |
| 69 |
"meh": 25, |
| 70 |
"myown": "ok", |
| 71 |
} |
| 72 |
assert d1 == expected |
| 73 |
|
| 74 |
def dict_value_merges_are_not_references(self): |
| 75 |
core = {} |
| 76 |
coll = {"foo": {"bar": {"biz": "coll value"}}} |
| 77 |
proj = {"foo": {"bar": {"biz": "proj value"}}} |
| 78 |
# Initial merge - when bug present, this sets core['foo'] to the entire |
| 79 |
# 'foo' dict in 'proj' as a reference - meaning it 'links' back to the |
| 80 |
# 'proj' dict whenever other things are merged into it |
| 81 |
merge_dicts(core, proj) |
| 82 |
assert core == {"foo": {"bar": {"biz": "proj value"}}} |
| 83 |
assert proj["foo"]["bar"]["biz"] == "proj value" |
| 84 |
# Identity tests can also prove the bug early |
| 85 |
assert ( |
| 86 |
core["foo"] is not proj["foo"] |
| 87 |
), "Core foo is literally proj foo!" # noqa |
| 88 |
# Subsequent merge - just overwrites leaf values this time (thus no |
| 89 |
# real change, but this is what real config merge code does, so why |
| 90 |
# not) |
| 91 |
merge_dicts(core, proj) |
| 92 |
assert core == {"foo": {"bar": {"biz": "proj value"}}} |
| 93 |
assert proj["foo"]["bar"]["biz"] == "proj value" |
| 94 |
# The problem merge - when bug present, core['foo'] references 'foo' |
| 95 |
# inside 'proj', so this ends up tweaking "core" but it actually |
| 96 |
# affects "proj" as well! |
| 97 |
merge_dicts(core, coll) |
| 98 |
# Expect that the core dict got the update from 'coll'... |
| 99 |
assert core == {"foo": {"bar": {"biz": "coll value"}}} |
| 100 |
# BUT that 'proj' remains UNTOUCHED |
| 101 |
assert proj["foo"]["bar"]["biz"] == "proj value" |
| 102 |
|
| 103 |
def merge_file_types_by_reference(self): |
| 104 |
with open(__file__) as fd: |
| 105 |
d1 = {} |
| 106 |
d2 = {"foo": fd} |
| 107 |
merge_dicts(d1, d2) |
| 108 |
assert d1["foo"].closed is False |
| 109 |
|
| 110 |
|
| 111 |
class copy_dict_: |
| 112 |
def returns_deep_copy_of_given_dict(self): |
| 113 |
# NOTE: not actual deepcopy... |
| 114 |
source = {"foo": {"bar": {"biz": "baz"}}} |
| 115 |
copy = copy_dict(source) |
| 116 |
assert copy["foo"]["bar"] == source["foo"]["bar"] |
| 117 |
assert copy["foo"]["bar"] is not source["foo"]["bar"] |
| 118 |
copy["foo"]["bar"]["biz"] = "notbaz" |
| 119 |
assert source["foo"]["bar"]["biz"] == "baz" |