| Conditions | 2 |
| Total Lines | 54 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | """ |
||
| 19 | def test_update_nested_dict(): |
||
| 20 | """test update_nested_dict by checking outputs values""" |
||
| 21 | # two simple dicts with different keys |
||
| 22 | d = dict(d=1) |
||
| 23 | v = dict(v=0) |
||
| 24 | got = update_nested_dict(d, v) |
||
| 25 | expected = dict(d=1, v=0) |
||
| 26 | assert got == expected |
||
| 27 | |||
| 28 | # two simple dicts with same key |
||
| 29 | d = dict(d=1) |
||
| 30 | v = dict(d=0) |
||
| 31 | got = update_nested_dict(d, v) |
||
| 32 | expected = dict(d=0) |
||
| 33 | assert got == expected |
||
| 34 | |||
| 35 | # dict with nested dict without common key |
||
| 36 | d = dict(d=1) |
||
| 37 | v = dict(v=dict(x=0)) |
||
| 38 | got = update_nested_dict(d, v) |
||
| 39 | expected = dict(d=1, v=dict(x=0)) |
||
| 40 | assert got == expected |
||
| 41 | |||
| 42 | # dict with nested dict with common key |
||
| 43 | # fail because can not use dict to overwrite non dict values |
||
| 44 | d = dict(v=1) |
||
| 45 | v = dict(v=dict(x=0)) |
||
| 46 | with pytest.raises(TypeError) as err_info: |
||
| 47 | update_nested_dict(d, v) |
||
| 48 | assert "'int' object does not support item assignment" in str(err_info.value) |
||
| 49 | |||
| 50 | # dict with nested dict with common key |
||
| 51 | # pass because can use non dict to overwrite dict |
||
| 52 | d = dict(v=dict(x=0)) |
||
| 53 | v = dict(v=1) |
||
| 54 | got = update_nested_dict(d, v) |
||
| 55 | expected = dict(v=1) |
||
| 56 | assert got == expected |
||
| 57 | |||
| 58 | # dict with nested dict with common key |
||
| 59 | # overwrite a value |
||
| 60 | d = dict(v=dict(x=0, y=1)) |
||
| 61 | v = dict(v=dict(x=1)) |
||
| 62 | got = update_nested_dict(d, v) |
||
| 63 | expected = dict(v=dict(x=1, y=1)) |
||
| 64 | assert got == expected |
||
| 65 | |||
| 66 | # dict with nested dict with common key |
||
| 67 | # add a value |
||
| 68 | d = dict(v=dict(x=0, y=1)) |
||
| 69 | v = dict(v=dict(z=1)) |
||
| 70 | got = update_nested_dict(d, v) |
||
| 71 | expected = dict(v=dict(x=0, y=1, z=1)) |
||
| 72 | assert got == expected |
||
| 73 | |||
| 146 |