Advertisement
Uno-Dan

More Tests

Sep 14th, 2019
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. import unittest
  2.  
  3. from os import path, remove
  4. from copy import deepcopy
  5. from cache import Cache
  6. import random
  7. import string
  8.  
  9.  
  10. class TestCache(unittest.TestCase):
  11.  
  12. def setUp(self):
  13. """Set up test fixtures, if any."""
  14. self.test_file = 'test.json'
  15. self.default_data = {
  16. 'node1': {
  17. 'test1': 123,
  18. 'test2': 123,
  19. },
  20. 'node2': {
  21. 'options': {
  22. 'background': 'white',
  23. 'foreground': 'black',
  24. },
  25. 'settings': {
  26. 'user': 'Dan',
  27. }
  28. }
  29. }
  30. self.cache = Cache(**self.default_data)
  31.  
  32. def tearDown(self):
  33. """Tear down test fixtures, if any."""
  34. self.cache.destroy()
  35. if path.exists(self.test_file):
  36. remove(self.test_file)
  37.  
  38. def test_destroy(self):
  39. # Action
  40. self.assertEqual(self.cache.get('node1/test1'), 123)
  41. self.cache.destroy()
  42.  
  43. # Asert
  44. self.assertEqual(self.cache.get(), {})
  45.  
  46. def test_save(self):
  47. # Action
  48. self.cache.save(self.test_file)
  49. self.cache.destroy()
  50. self.assertEqual(self.cache.get(), {})
  51. self.cache.load(self.test_file)
  52.  
  53. # Asert
  54. self.assertEqual(self.cache.get('node1/test1'), 123)
  55.  
  56. def test_load(self):
  57. # Action
  58. self.cache.save(self.test_file)
  59. self.cache.destroy()
  60. self.cache.load(self.test_file)
  61.  
  62. # Asert
  63. self.assertEqual(self.cache.get('node1/test1'), 123)
  64.  
  65. def test_copy(self):
  66. # Action
  67. new_cache = self.cache.copy()
  68.  
  69. # Asert
  70. self.assertTrue(self.cache is not new_cache)
  71. self.assertEqual(self.cache.get(), new_cache.get())
  72.  
  73. def test_merge(self):
  74. # Assume
  75. self.cache.destroy()
  76. self.cache.set('abc/test1', 123)
  77. new_cache = Cache('abc/test2', 456)
  78. merged_cache = Cache(**{'abc': {'test1': 123, 'test2': 456}})
  79.  
  80. # Action
  81. self.cache.merge(new_cache)
  82.  
  83. # Asert
  84. self.assertTrue(self.cache is not new_cache)
  85. self.assertEqual(self.cache, merged_cache)
  86.  
  87. def test_remove(self):
  88. # Action
  89. self.assertEqual(self.cache.get('node1/test1'), 123)
  90. self.cache.remove('node1/test1')
  91. self.cache.remove('node2/options/background')
  92.  
  93. # Asert
  94. self.assertIsNone(self.cache.get('node1/test1'))
  95. self.assertIsNone(self.cache.get('node2/options/background'))
  96.  
  97. def test_exists(self):
  98. # Action
  99. self.assertEqual(self.cache.get('node1/test1'), 123)
  100.  
  101. # Asert
  102. self.assertTrue(self.cache.exists('node1/test1'))
  103.  
  104. def test_not_exists(self):
  105. # Asert
  106. key = ''.join(random.choice(string.ascii_lowercase) for i in range(10))
  107.  
  108. self.assertFalse(self.cache.exists(key))
  109.  
  110. def test_has_nodes(self):
  111. # Asert
  112. self.assertTrue(self.cache.has_nodes())
  113. self.cache.destroy()
  114. self.assertFalse(self.cache.has_nodes())
  115.  
  116. def test_equality(self):
  117. # Action
  118. new_cache = Cache(**deepcopy(self.cache.get()))
  119.  
  120. # Asert
  121. a, b = self.cache, new_cache
  122. self.assertEqual(a, b)
  123. self.assertEqual(b, a)
  124. self.assertTrue(a == b)
  125. self.assertTrue(b == a)
  126. self.assertFalse(a != b)
  127. self.assertFalse(b != a)
  128.  
  129. def test_inequality(self):
  130. # Action
  131. new_cache = Cache(**self.default_data)
  132. new_cache.remove('node1')
  133.  
  134. # Asert
  135. a, b = self.cache, new_cache
  136. self.assertNotEqual(a, b)
  137. self.assertNotEqual(b, a)
  138. self.assertFalse(a == b)
  139. self.assertFalse(b == a)
  140. self.assertTrue(a != b)
  141. self.assertTrue(b != a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement