test_sdb.py 1.7 KB

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