1
0

test_sdb.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: :email:`Vernon Cole <vernondcole@gmail.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.runtests import RUNTIME_VARS
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.unit import TestCase, skipIf
  12. from tests.support.mock import (
  13. NO_MOCK,
  14. NO_MOCK_REASON
  15. )
  16. # Import Salt Libs
  17. import salt.utils.sdb as sdb
  18. TEMP_DATABASE_FILE = os.path.join(RUNTIME_VARS.TMP, 'test_sdb.sqlite')
  19. SDB_OPTS = {
  20. 'extension_modules': '',
  21. 'optimization_order': [0, 1, 2],
  22. 'test_sdb_data': {
  23. 'driver': 'sqlite3',
  24. 'database': TEMP_DATABASE_FILE,
  25. 'table': 'sdb',
  26. 'create_table': True
  27. }
  28. }
  29. @skipIf(NO_MOCK, NO_MOCK_REASON)
  30. class SdbTestCase(TestCase, LoaderModuleMockMixin):
  31. '''
  32. Test cases for salt.modules.sdb
  33. '''
  34. @classmethod
  35. def tearDownClass(cls):
  36. try:
  37. os.unlink(TEMP_DATABASE_FILE)
  38. except OSError:
  39. pass
  40. def setup_loader_modules(self):
  41. return {sdb: {}}
  42. # test with SQLite database key not presest
  43. def test_sqlite_get_not_found(self):
  44. what = sdb.sdb_get(
  45. 'sdb://test_sdb_data/thisKeyDoesNotExist', SDB_OPTS)
  46. self.assertEqual(what, None)
  47. # test with SQLite database write and read
  48. def test_sqlite_get_found(self):
  49. expected = {b'name': b'testone', b'number': 46}
  50. sdb.sdb_set('sdb://test_sdb_data/test1', expected, SDB_OPTS)
  51. resp = sdb.sdb_get('sdb://test_sdb_data/test1', SDB_OPTS)
  52. self.assertEqual(resp, expected)