test_completions.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import h5py
  2. from .common import TestCase, make_name
  3. class TestCompletions(TestCase):
  4. def test_root_group_completions(self):
  5. # Test completions on top-level file.
  6. with h5py.File(self.mktemp(), 'w') as f:
  7. f.create_group('h')
  8. f.create_group('g')
  9. f.create_dataset('data', [1, 2, 3], "f4")
  10. self.assertEqual(
  11. f._ipython_key_completions_(),
  12. ['data', 'g', 'h'],
  13. )
  14. f.create_group('data2')
  15. # Test that order is alphabetical, and that there is no
  16. # internal ordering between groups and datasets.
  17. self.assertEqual(
  18. f._ipython_key_completions_(),
  19. ['data', 'data2', 'g', 'h'],
  20. )
  21. def test_subgroup_completions(self):
  22. g = self.f.create_group(make_name())
  23. g.create_dataset('g_data2', [4, 5, 6], "f4")
  24. g.create_dataset('g_data1', [1, 2, 3], "f4")
  25. self.assertEqual(
  26. g._ipython_key_completions_(),
  27. ['g_data1', 'g_data2'], # Order is alphabetical
  28. )
  29. g.create_dataset('g_data3', [7, 8, 9], "f")
  30. self.assertEqual(
  31. g._ipython_key_completions_(),
  32. ['g_data1', 'g_data2', 'g_data3'],
  33. )
  34. def test_attrs_completions(self):
  35. # In pytest-run-parallel, let every thread run on a different set of attrs
  36. attrs = self.f.create_group(make_name()).attrs
  37. # Write out of alphabetical order to test that completions come back in
  38. # alphabetical order, as opposed to, say, insertion order.
  39. attrs['b'] = 1
  40. attrs['a'] = 2
  41. self.assertEqual(
  42. attrs._ipython_key_completions_(),
  43. ['a', 'b']
  44. )
  45. attrs['c'] = 3
  46. self.assertEqual(
  47. attrs._ipython_key_completions_(),
  48. ['a', 'b', 'c']
  49. )