test_config_manager.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import json
  2. import os
  3. import shutil
  4. import tempfile
  5. from notebook.config_manager import BaseJSONConfigManager
  6. def test_json():
  7. tmpdir = tempfile.mkdtemp()
  8. try:
  9. root_data = dict(a=1, x=2, nest={'a':1, 'x':2})
  10. with open(os.path.join(tmpdir, 'foo.json'), 'w') as f:
  11. json.dump(root_data, f)
  12. # also make a foo.d/ directory with multiple json files
  13. os.makedirs(os.path.join(tmpdir, 'foo.d'))
  14. with open(os.path.join(tmpdir, 'foo.d', 'a.json'), 'w') as f:
  15. json.dump(dict(a=2, b=1, nest={'a':2, 'b':1}), f)
  16. with open(os.path.join(tmpdir, 'foo.d', 'b.json'), 'w') as f:
  17. json.dump(dict(a=3, b=2, c=3, nest={'a':3, 'b':2, 'c':3}, only_in_b={'x':1}), f)
  18. manager = BaseJSONConfigManager(config_dir=tmpdir, read_directory=False)
  19. data = manager.get('foo')
  20. assert 'a' in data
  21. assert 'x' in data
  22. assert 'b' not in data
  23. assert 'c' not in data
  24. assert data['a'] == 1
  25. assert 'x' in data['nest']
  26. # if we write it out, it also shouldn't pick up the subdirectoy
  27. manager.set('foo', data)
  28. data = manager.get('foo')
  29. assert data == root_data
  30. manager = BaseJSONConfigManager(config_dir=tmpdir, read_directory=True)
  31. data = manager.get('foo')
  32. assert 'a' in data
  33. assert 'b' in data
  34. assert 'c' in data
  35. # files should be read in order foo.d/a.json foo.d/b.json foo.json
  36. assert data['a'] == 1
  37. assert data['b'] == 2
  38. assert data['c'] == 3
  39. assert data['nest']['a'] == 1
  40. assert data['nest']['b'] == 2
  41. assert data['nest']['c'] == 3
  42. assert data['nest']['x'] == 2
  43. # when writing out, we don't want foo.d/*.json data to be included in the root foo.json
  44. manager.set('foo', data)
  45. manager = BaseJSONConfigManager(config_dir=tmpdir, read_directory=False)
  46. data = manager.get('foo')
  47. assert data == root_data
  48. finally:
  49. shutil.rmtree(tmpdir)