def test_merge_dicts_on_faulty_input(): assert merge_dicts(None, None) is None assert merge_dicts({}, None) is None assert merge_dicts(None, {}) is None assert merge_dicts('a', {}) == 'a' assert merge_dicts({}, 'b') == 'b' assert merge_dicts(False, {'a': 'b'}) is False assert merge_dicts({'a': 'b'}, False) is False assert merge_dicts('a', {'b': 'c'}) == 'a' assert merge_dicts({'a': 'b'}, 'c') == 'c'
def _sidecar_load(ff, sidepath, fields, as_yaml): ''' Loads content from ``sidepath`` if it exists, otherwise returns the values from the :attr:`api` instead. This is only done, if ``fields`` exist in :attr:`api`. :param ff: running :class:`ffflash.main.FFFlash` instance :param sidepath: full path to the sidecar :param fields: key-names into api-file :param as_yaml: load as *yaml* instead of *json* :return: The loaded content of ``sidepath`` or ``False``/``None`` on error ''' if not ff.access_for('sidecars'): return False ff.log('searching for {}'.format('.'.join(fields))) apicont = ff.api.pull(*fields) if apicont is None: return ff.log('{} does not exist. skipping'.format('.'.join(fields)), level=None) sidecont = load_file(sidepath, as_yaml=as_yaml) if sidecont is None: ff.log('sidecar {} does not exit yet. pulled data from api'.format( '.'.join(fields))) return apicont return merge_dicts(apicont, sidecont)
def _sidecar_load(ff, sidepath, fields, as_yaml): ''' Loads content from ``sidepath`` if it exists, otherwise returns the values from the :attr:`api` instead. This is only done, if ``fields`` exist in :attr:`api`. :param ff: running :class:`ffflash.main.FFFlash` instance :param sidepath: full path to the sidecar :param fields: key-names into api-file :param as_yaml: load as *yaml* instead of *json* :return: The loaded content of ``sidepath`` or ``False``/``None`` on error ''' if not ff.access_for('sidecars'): return False ff.log('searching for {}'.format('.'.join(fields))) apicont = ff.api.pull(*fields) if apicont is None: return ff.log( '{} does not exist. skipping'.format('.'.join(fields)), level=None ) sidecont = load_file(sidepath, as_yaml=as_yaml) if sidecont is None: ff.log('sidecar {} does not exit yet. pulled data from api'.format( '.'.join(fields) )) return apicont return merge_dicts(apicont, sidecont)
def test_merge_dicts_nested(): A = {'a': {'b': {'c': {'d': 'e'}}}} Z = {'z': {'y': {'x': {'w': 'v'}}}} assert merge_dicts( A['a']['b']['c'], Z['z']['y']['x'] ) == {'d': 'e', 'w': 'v'} assert merge_dicts( A['a']['b'], Z['z']['y'] ) == {'c': {'d': 'e'}, 'x': {'w': 'v'}} assert merge_dicts( A['a'], Z['z'] ) == {'b': {'c': {'d': 'e'}}, 'y': {'x': {'w': 'v'}}} assert merge_dicts( A, Z ) == {'a': {'b': {'c': {'d': 'e'}}}, 'z': {'y': {'x': {'w': 'v'}}}}
def test_merge_dicts_simple(): assert merge_dicts({}, {}) == {} assert merge_dicts( {'a': 'b'}, {} ) == {'a': 'b'} assert merge_dicts( {'a': 'b'}, {'a': 'c'} ) == {'a': 'c'} assert merge_dicts( {'a': 'b'}, {'c': 'd'} ) == {'a': 'b', 'c': 'd'} assert merge_dicts( {'a': {'b': {'c': 'd'}}}, {'a': {'b': {'c': 'e'}}} ) == {'a': {'b': {'c': 'e'}}} assert merge_dicts( {'a': {'b': {'c': 'd'}}}, {'a': {'b': {'e': 'f'}}} ) == {'a': {'b': {'c': 'd', 'e': 'f'}}}