test_dict_collapse.py 1.1 KB

123456789101112131415161718192021222324252627282930
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. from ioex import dict_collapse
  4. @pytest.mark.parametrize(('src', 'key_repl', 'expected'), [
  5. [{'a': 'a'}, lambda k: k.upper(), {'A': 'a'}],
  6. [{'a1': 'a', 'a2': None}, lambda k: k[0], {'a': 'a'}],
  7. [{'a1': None, 'a2': 'a'}, lambda k: k[0], {'a': 'a'}],
  8. [{'a1': None, 'a2': False}, lambda k: k[0], {'a': False}],
  9. [{'a1': None, 'a2': (None,)}, lambda k: k[0], {'a': (None,)}],
  10. [{'1': 'a', 1.0: 'b'}, lambda k: k, {'1': 'a', 1.0: 'b'}],
  11. [{'1': None, 1.0: 'b'}, lambda k: int(k), {1: 'b'}],
  12. ])
  13. def test_collapse(src, key_repl, expected):
  14. assert expected == dict_collapse(src, key_repl)
  15. @pytest.mark.parametrize(('src', 'key_repl'), [
  16. [{'a1': 1, 'a2': 2}, lambda k: k[0]],
  17. [{'a1': None, 'a2': 2, 'a3': 3}, lambda k: k[0]],
  18. [{'a1': True, 'a2': False}, lambda k: k[0]],
  19. [{'a1': True, 'a2': (None,)}, lambda k: k[0]],
  20. [{'a1': True, 'a2': []}, lambda k: k[0]],
  21. [{'1': 'a', 1.0: 'b'}, lambda k: int(k)],
  22. ])
  23. def test_collapse_fail(src, key_repl):
  24. with pytest.raises(ValueError):
  25. dict_collapse(src, key_repl)