yedit_test.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python2
  2. '''
  3. Unit tests for yedit
  4. '''
  5. import unittest
  6. import os
  7. # Removing invalid variable names for tests so that I can
  8. # keep them brief
  9. # pylint: disable=invalid-name,no-name-in-module
  10. from yedit import Yedit
  11. class YeditTest(unittest.TestCase):
  12. '''
  13. Test class for yedit
  14. '''
  15. data = {'a': 'a',
  16. 'b': {'c': {'d': [{'e': 'x'}, 'f', 'g']}},
  17. }
  18. filename = 'yedit_test.yml'
  19. def setUp(self):
  20. ''' setup method will create a file and set to known configuration '''
  21. yed = Yedit(YeditTest.filename)
  22. yed.yaml_dict = YeditTest.data
  23. yed.write()
  24. def test_load(self):
  25. ''' Testing a get '''
  26. yed = Yedit('yedit_test.yml')
  27. self.assertEqual(yed.yaml_dict, self.data)
  28. def test_write(self):
  29. ''' Testing a simple write '''
  30. yed = Yedit('yedit_test.yml')
  31. yed.put('key1', 1)
  32. yed.write()
  33. self.assertTrue(yed.yaml_dict.has_key('key1'))
  34. self.assertEqual(yed.yaml_dict['key1'], 1)
  35. def test_write_x_y_z(self):
  36. '''Testing a write of multilayer key'''
  37. yed = Yedit('yedit_test.yml')
  38. yed.put('x.y.z', 'modified')
  39. yed.write()
  40. yed.load()
  41. self.assertEqual(yed.get('x.y.z'), 'modified')
  42. def test_delete_a(self):
  43. '''Testing a simple delete '''
  44. yed = Yedit('yedit_test.yml')
  45. yed.delete('a')
  46. yed.write()
  47. yed.load()
  48. self.assertTrue(not yed.yaml_dict.has_key('a'))
  49. def test_delete_b_c(self):
  50. '''Testing delete of layered key '''
  51. yed = Yedit('yedit_test.yml')
  52. yed.delete('b.c')
  53. yed.write()
  54. yed.load()
  55. self.assertTrue(yed.yaml_dict.has_key('b'))
  56. self.assertFalse(yed.yaml_dict['b'].has_key('c'))
  57. def test_create(self):
  58. '''Testing a create '''
  59. os.unlink(YeditTest.filename)
  60. yed = Yedit('yedit_test.yml')
  61. yed.create('foo', 'bar')
  62. yed.write()
  63. yed.load()
  64. self.assertTrue(yed.yaml_dict.has_key('foo'))
  65. self.assertTrue(yed.yaml_dict['foo'] == 'bar')
  66. def test_create_content(self):
  67. '''Testing a create with content '''
  68. content = {"foo": "bar"}
  69. yed = Yedit("yedit_test.yml", content)
  70. yed.write()
  71. yed.load()
  72. self.assertTrue(yed.yaml_dict.has_key('foo'))
  73. self.assertTrue(yed.yaml_dict['foo'], 'bar')
  74. def test_array_insert(self):
  75. '''Testing a create with content '''
  76. yed = Yedit("yedit_test.yml")
  77. yed.put('b.c.d[0]', 'inject')
  78. self.assertTrue(yed.get('b.c.d[0]') == 'inject')
  79. def test_array_insert_first_index(self):
  80. '''Testing a create with content '''
  81. yed = Yedit("yedit_test.yml")
  82. yed.put('b.c.d[0]', 'inject')
  83. self.assertTrue(yed.get('b.c.d[1]') == 'f')
  84. def test_array_insert_second_index(self):
  85. '''Testing a create with content '''
  86. yed = Yedit("yedit_test.yml")
  87. yed.put('b.c.d[0]', 'inject')
  88. self.assertTrue(yed.get('b.c.d[2]') == 'g')
  89. def test_dict_array_dict_access(self):
  90. '''Testing a create with content'''
  91. yed = Yedit("yedit_test.yml")
  92. yed.put('b.c.d[0]', [{'x': {'y': 'inject'}}])
  93. self.assertTrue(yed.get('b.c.d[0].[0].x.y') == 'inject')
  94. def test_dict_array_dict_replace(self):
  95. '''Testing multilevel delete'''
  96. yed = Yedit("yedit_test.yml")
  97. yed.put('b.c.d[0]', [{'x': {'y': 'inject'}}])
  98. yed.put('b.c.d[0].[0].x.y', 'testing')
  99. self.assertTrue(yed.yaml_dict.has_key('b'))
  100. self.assertTrue(yed.yaml_dict['b'].has_key('c'))
  101. self.assertTrue(yed.yaml_dict['b']['c'].has_key('d'))
  102. self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'], list))
  103. self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'][0], list))
  104. self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'][0][0], dict))
  105. self.assertTrue(yed.yaml_dict['b']['c']['d'][0][0]['x'].has_key('y'))
  106. self.assertTrue(yed.yaml_dict['b']['c']['d'][0][0]['x']['y'], 'testing')
  107. def test_dict_array_dict_remove(self):
  108. '''Testing multilevel delete'''
  109. yed = Yedit("yedit_test.yml")
  110. yed.put('b.c.d[0]', [{'x': {'y': 'inject'}}])
  111. yed.delete('b.c.d[0].[0].x.y')
  112. self.assertTrue(yed.yaml_dict.has_key('b'))
  113. self.assertTrue(yed.yaml_dict['b'].has_key('c'))
  114. self.assertTrue(yed.yaml_dict['b']['c'].has_key('d'))
  115. self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'], list))
  116. self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'][0], list))
  117. self.assertTrue(isinstance(yed.yaml_dict['b']['c']['d'][0][0], dict))
  118. self.assertFalse(yed.yaml_dict['b']['c']['d'][0][0]['x'].has_key('y'))
  119. def tearDown(self):
  120. '''TearDown method'''
  121. os.unlink(YeditTest.filename)
  122. if __name__ == "__main__":
  123. unittest.main()