test_postgres_schema.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch
  13. )
  14. # Import Salt Libs
  15. import salt.states.postgres_schema as postgres_schema
  16. class PostgresSchemaTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Test cases for salt.states.postgres_schema
  19. '''
  20. def setup_loader_modules(self):
  21. return {postgres_schema: {}}
  22. # 'present' function tests: 1
  23. def test_present(self):
  24. '''
  25. Test to ensure that the named schema is present in the database.
  26. '''
  27. name = 'myname'
  28. dbname = 'mydb'
  29. ret = {'name': name,
  30. 'dbname': dbname,
  31. 'changes': {},
  32. 'result': True,
  33. 'comment': ''}
  34. mock = MagicMock(return_value=name)
  35. with patch.dict(postgres_schema.__salt__,
  36. {'postgres.schema_get': mock}):
  37. with patch.dict(postgres_schema.__opts__, {'test': False}):
  38. comt = ('Schema {0} already exists in database {1}'.format(name,
  39. dbname))
  40. ret.update({'comment': comt})
  41. self.assertDictEqual(postgres_schema.present(dbname, name), ret)
  42. # 'absent' function tests: 1
  43. def test_absent(self):
  44. '''
  45. Test to ensure that the named schema is absent.
  46. '''
  47. name = 'myname'
  48. dbname = 'mydb'
  49. ret = {'name': name,
  50. 'dbname': dbname,
  51. 'changes': {},
  52. 'result': True,
  53. 'comment': ''}
  54. mock_t = MagicMock(side_effect=[True, False])
  55. mock = MagicMock(side_effect=[True, True, True, False])
  56. with patch.dict(postgres_schema.__salt__,
  57. {'postgres.schema_exists': mock,
  58. 'postgres.schema_remove': mock_t}):
  59. with patch.dict(postgres_schema.__opts__, {'test': True}):
  60. comt = ('Schema {0} is set to be removed from database {1}'.
  61. format(name, dbname))
  62. ret.update({'comment': comt, 'result': None})
  63. self.assertDictEqual(postgres_schema.absent(dbname, name), ret)
  64. with patch.dict(postgres_schema.__opts__, {'test': False}):
  65. comt = ('Schema {0} has been removed from database {1}'.
  66. format(name, dbname))
  67. ret.update({'comment': comt, 'result': True,
  68. 'changes': {name: 'Absent'}})
  69. self.assertDictEqual(postgres_schema.absent(dbname, name), ret)
  70. comt = ('Schema {0} failed to be removed'.format(name))
  71. ret.update({'comment': comt, 'result': False, 'changes': {}})
  72. self.assertDictEqual(postgres_schema.absent(dbname, name), ret)
  73. comt = ('Schema {0} is not present in database {1},'
  74. ' so it cannot be removed'.format(name, dbname))
  75. ret.update({'comment': comt, 'result': True})
  76. self.assertDictEqual(postgres_schema.absent(dbname, name), ret)