Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_dict_level_transformer():
def generate_data(v1, v2):
level3 = lambda: {'l3_key1': v1, 'l3_key2': v2}
level2 = lambda: {'l2_key1': level3(), 'l2_key2': level3(), 'l2_key3': level3()}
level1 = lambda: {'l1_key1': level2(), 'l1_key2': level2()}
return level1()
transformer = chart.DictLevelsTransformer.from_str('13to2')
assert transformer.key == '13to2'
assert transformer.transform(None) == {}
assert transformer.transform([]) == {}
transform = transformer.transform([generate_data(1, 2), generate_data(3, 4)])
for k, v in transform.items():
transform[k]['series'] = sorted(transform[k]['series'], key=lambda s: s['name'])
assert transform == {
'l1_key1 l3_key1': {
'series': [{'data': [(0, 1), (0, 3)], 'name': 'l2_key1'},
{'data': [(0, 1), (0, 3)], 'name': 'l2_key2'},
{'data': [(0, 1), (0, 3)], 'name': 'l2_key3'}],
'title': 'l1_key1 l3_key1',
'type': 'line'
def test_dict_level_transformer_timed_tuples():
ts = 1000.12
expected_ts = int(ts * 1000)
transformer = chart.DictLevelsTransformer.from_str('1to2')
for ds in [list, tuple]:
transform = transformer.transform([ds((ts, {'c': {'d': 1234}}))])
assert transform == {
'c': {'series': [{'data': [(expected_ts, 1234)], 'name': 'd'}], 'title': 'c', 'type': 'line'}
}
def _get_transformer(transform):
transformer = transform or 'noop'
if isinstance(transformer, Transformer):
return transformer
elif isinstance(transformer, dict):
transformer_key = transformer.pop('type')
return transformer_classes[transformer_key](**transformer)
if isinstance(transformer, six.string_types):
if transformer in transformers:
return transformers[transformer]
maybe_dict_transformer = DictLevelsTransformer.from_str(transformer)
if maybe_dict_transformer:
transformers[maybe_dict_transformer.key] = maybe_dict_transformer
return maybe_dict_transformer
raise ValueError('No matching transformer found for {}'.format(transform))