1
0

test_oracle.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase
  11. from tests.support.mock import (
  12. MagicMock,
  13. patch,
  14. )
  15. # Import Salt Libs
  16. import salt.modules.oracle as oracle
  17. class OracleTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Test cases for salt.modules.oracle
  20. '''
  21. def setup_loader_modules(self):
  22. return {oracle: {'cx_Oracle': object()}}
  23. def test_run_query(self):
  24. '''
  25. Test for Run SQL query and return result
  26. '''
  27. with patch.object(oracle, '_connect', MagicMock()) as mock_connect:
  28. mock_connect.cursor.execute.fetchall.return_value = True
  29. with patch.object(oracle, 'show_dbs', MagicMock()):
  30. self.assertTrue(oracle.run_query('db', 'query'))
  31. def test_show_dbs(self):
  32. '''
  33. Test for Show databases configuration from pillar. Filter by `*args`
  34. '''
  35. with patch.dict(oracle.__salt__, {'pillar.get':
  36. MagicMock(return_value='a')}):
  37. self.assertDictEqual(oracle.show_dbs('A', 'B'),
  38. {'A': 'a', 'B': 'a'})
  39. self.assertEqual(oracle.show_dbs(), 'a')
  40. def test_version(self):
  41. '''
  42. Test for Server Version (select banner from v$version)
  43. '''
  44. with patch.dict(oracle.__salt__, {'pillar.get':
  45. MagicMock(return_value='a')}):
  46. with patch.object(oracle, 'run_query', return_value='A'):
  47. self.assertDictEqual(oracle.version(), {})
  48. def test_client_version(self):
  49. '''
  50. Test for Oracle Client Version
  51. '''
  52. with patch.object(oracle, 'cx_Oracle',
  53. MagicMock(side_effect=MagicMock())):
  54. self.assertEqual(oracle.client_version(), '')
  55. def test_show_pillar(self):
  56. '''
  57. Test for Show Pillar segment oracle.*
  58. '''
  59. with patch.dict(oracle.__salt__, {'pillar.get':
  60. MagicMock(return_value='a')}):
  61. self.assertEqual(oracle.show_pillar('item'), 'a')
  62. def test_show_env(self):
  63. '''
  64. Test for Show Environment used by Oracle Client
  65. '''
  66. with patch.object(os, 'environ',
  67. return_value={'PATH': 'PATH',
  68. 'ORACLE_HOME': 'ORACLE_HOME',
  69. 'TNS_ADMIN': 'TNS_ADMIN',
  70. 'NLS_LANG': 'NLS_LANG'}):
  71. self.assertDictEqual(oracle.show_env(), {})