test_postgres_language.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Andrew Colin Kissa <andrew@topdog.za.net>
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import salt.states.postgres_language as postgres_language
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import MagicMock, patch
  9. from tests.support.unit import TestCase
  10. class PostgresLanguageTestCase(TestCase, LoaderModuleMockMixin):
  11. """
  12. Test cases for salt.states.postgres_language
  13. """
  14. def setup_loader_modules(self):
  15. return {postgres_language: {}}
  16. def setUp(self):
  17. """
  18. Setup data for the tests
  19. """
  20. self.name = "plpgsql"
  21. self.ret = {"name": self.name, "changes": {}, "result": False, "comment": ""}
  22. self.mock_true = MagicMock(return_value=True)
  23. self.mock_false = MagicMock(return_value=False)
  24. self.mock_empty_language_list = MagicMock(return_value={})
  25. self.mock_language_list = MagicMock(return_value={"plpgsql": self.name})
  26. def tearDown(self):
  27. del self.ret
  28. del self.mock_true
  29. del self.mock_false
  30. del self.mock_language_list
  31. del self.mock_empty_language_list
  32. def test_present_existing(self):
  33. """
  34. Test present, language is already present in database
  35. """
  36. with patch.dict(
  37. postgres_language.__salt__,
  38. {"postgres.language_list": self.mock_language_list},
  39. ):
  40. comt = "Language {0} is already installed".format(self.name)
  41. self.ret.update({"comment": comt, "result": True})
  42. self.assertDictEqual(
  43. postgres_language.present(self.name, "testdb"), self.ret
  44. )
  45. def test_present_non_existing_pass(self):
  46. """
  47. Test present, language not present in database - pass
  48. """
  49. with patch.dict(
  50. postgres_language.__salt__,
  51. {
  52. "postgres.language_list": self.mock_empty_language_list,
  53. "postgres.language_create": self.mock_true,
  54. },
  55. ):
  56. with patch.dict(postgres_language.__opts__, {"test": True}):
  57. comt = "Language {0} is set to be installed".format(self.name)
  58. self.ret.update({"comment": comt, "result": None})
  59. self.assertDictEqual(
  60. postgres_language.present(self.name, "testdb"), self.ret
  61. )
  62. with patch.dict(postgres_language.__opts__, {"test": False}):
  63. comt = "Language {0} has been installed".format(self.name)
  64. self.ret.update(
  65. {"comment": comt, "result": True, "changes": {"plpgsql": "Present"}}
  66. )
  67. self.assertDictEqual(
  68. postgres_language.present(self.name, "testdb"), self.ret
  69. )
  70. def test_present_non_existing_fail(self):
  71. """
  72. Test present, language not present in database - fail
  73. """
  74. with patch.dict(
  75. postgres_language.__salt__,
  76. {
  77. "postgres.language_list": self.mock_empty_language_list,
  78. "postgres.language_create": self.mock_false,
  79. },
  80. ):
  81. with patch.dict(postgres_language.__opts__, {"test": True}):
  82. comt = "Language {0} is set to be installed".format(self.name)
  83. self.ret.update({"comment": comt, "result": None})
  84. self.assertDictEqual(
  85. postgres_language.present(self.name, "testdb"), self.ret
  86. )
  87. with patch.dict(postgres_language.__opts__, {"test": False}):
  88. comt = "Failed to install language {0}".format(self.name)
  89. self.ret.update({"comment": comt, "result": False})
  90. self.assertDictEqual(
  91. postgres_language.present(self.name, "testdb"), self.ret
  92. )
  93. def test_absent_existing(self):
  94. """
  95. Test absent, language present in database
  96. """
  97. with patch.dict(
  98. postgres_language.__salt__,
  99. {
  100. "postgres.language_exists": self.mock_true,
  101. "postgres.language_remove": self.mock_true,
  102. },
  103. ):
  104. with patch.dict(postgres_language.__opts__, {"test": True}):
  105. comt = "Language {0} is set to be removed".format(self.name)
  106. self.ret.update({"comment": comt, "result": None})
  107. self.assertDictEqual(
  108. postgres_language.absent(self.name, "testdb"), self.ret
  109. )
  110. with patch.dict(postgres_language.__opts__, {"test": False}):
  111. comt = "Language {0} has been removed".format(self.name)
  112. self.ret.update(
  113. {"comment": comt, "result": True, "changes": {"plpgsql": "Absent"}}
  114. )
  115. self.assertDictEqual(
  116. postgres_language.absent(self.name, "testdb"), self.ret
  117. )
  118. def test_absent_non_existing(self):
  119. """
  120. Test absent, language not present in database
  121. """
  122. with patch.dict(
  123. postgres_language.__salt__, {"postgres.language_exists": self.mock_false}
  124. ):
  125. with patch.dict(postgres_language.__opts__, {"test": True}):
  126. comt = "Language {0} is not present so " "it cannot be removed".format(
  127. self.name
  128. )
  129. self.ret.update({"comment": comt, "result": True})
  130. self.assertDictEqual(
  131. postgres_language.absent(self.name, "testdb"), self.ret
  132. )