From 18357ac59df7e772a0e3cb778daeed329610994e Mon Sep 17 00:00:00 2001 From: Vernon Cole Date: Fri, 29 Sep 2017 13:58:13 -0600 Subject: [PATCH 1/5] provide a unit test for the sdb utility --- tests/unit/utils/test_sdb.py | 65 ++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 tests/unit/utils/test_sdb.py diff --git a/tests/unit/utils/test_sdb.py b/tests/unit/utils/test_sdb.py new file mode 100644 index 00000000000..90b3922663d --- /dev/null +++ b/tests/unit/utils/test_sdb.py @@ -0,0 +1,65 @@ +# -*- coding: utf-8 -*- +''' + :codeauthor: :email:`Vernon Cole ` +''' + +# Import Python Libs +from __future__ import absolute_import +import os + +# Import Salt Testing Libs +from tests.support.mixins import LoaderModuleMockMixin +from tests.support.unit import TestCase, skipIf +from tests.support.mock import ( + NO_MOCK, + NO_MOCK_REASON +) + +# Import Salt Libs +import salt.utils.sdb as sdb +import salt.exceptions + +TEMP_DATABASE_FILE = '/tmp/salt-tests-tmpdir/test_sdb.sqlite' + +SDB_OPTS = { + 'extension_modules': '', + 'test_sdb_data': { + 'driver': 'sqlite3', + 'database': TEMP_DATABASE_FILE, + 'table': 'sdb', + 'create_table': True + } + } + +@skipIf(NO_MOCK, NO_MOCK_REASON) +class SdbTestCase(TestCase, LoaderModuleMockMixin): + ''' + Test cases for salt.modules.sdb + ''' + + @classmethod + def tearDownClass(cls): + try: + os.unlink(TEMP_DATABASE_FILE) + except: + pass + + def setup_loader_modules(self): + return {sdb: {}} + + # test with SQLite database key not presest + + def test_sqlite_get_not_found(self): + what = sdb.sdb_get( + 'sdb://test_sdb_data/thisKeyDoesNotExist', SDB_OPTS) + self.assertEqual(what, None, 'what is "{!r}"'.format(what)) + + # test with SQLite database write and read + + def test_sqlite_get_found(self): + expected = dict(name='testone', + number=46, + ) + sdb.sdb_set('sdb://test_sdb_data/test1', expected, SDB_OPTS) + resp = sdb.sdb_get('sdb://test_sdb_data/test1', SDB_OPTS) + self.assertEqual(resp, expected) From 22792e56cc4f06e92b7973c4819ac4d2534153a2 Mon Sep 17 00:00:00 2001 From: Vernon Cole Date: Mon, 2 Oct 2017 09:48:27 -0600 Subject: [PATCH 2/5] lint, style, and Windows fixes --- tests/unit/utils/test_sdb.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/unit/utils/test_sdb.py b/tests/unit/utils/test_sdb.py index 90b3922663d..0bf4a1a915e 100644 --- a/tests/unit/utils/test_sdb.py +++ b/tests/unit/utils/test_sdb.py @@ -8,6 +8,7 @@ from __future__ import absolute_import import os # Import Salt Testing Libs +from tests.support.paths import TMP from tests.support.mixins import LoaderModuleMockMixin from tests.support.unit import TestCase, skipIf from tests.support.mock import ( @@ -17,9 +18,8 @@ from tests.support.mock import ( # Import Salt Libs import salt.utils.sdb as sdb -import salt.exceptions -TEMP_DATABASE_FILE = '/tmp/salt-tests-tmpdir/test_sdb.sqlite' +TEMP_DATABASE_FILE = os.path.join(TMP, 'test_sdb.sqlite') SDB_OPTS = { 'extension_modules': '', @@ -57,9 +57,7 @@ class SdbTestCase(TestCase, LoaderModuleMockMixin): # test with SQLite database write and read def test_sqlite_get_found(self): - expected = dict(name='testone', - number=46, - ) + expected = {b'name': b'testone', b'number': 46} sdb.sdb_set('sdb://test_sdb_data/test1', expected, SDB_OPTS) resp = sdb.sdb_get('sdb://test_sdb_data/test1', SDB_OPTS) self.assertEqual(resp, expected) From 6c20e146c3a310699fa76933310a3e06ebbaa6ed Mon Sep 17 00:00:00 2001 From: garethgreenaway Date: Mon, 2 Oct 2017 10:10:53 -0700 Subject: [PATCH 3/5] Lint Fixes Lint Fixes --- tests/unit/utils/test_sdb.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/unit/utils/test_sdb.py b/tests/unit/utils/test_sdb.py index 0bf4a1a915e..c9724caca05 100644 --- a/tests/unit/utils/test_sdb.py +++ b/tests/unit/utils/test_sdb.py @@ -31,6 +31,7 @@ SDB_OPTS = { } } + @skipIf(NO_MOCK, NO_MOCK_REASON) class SdbTestCase(TestCase, LoaderModuleMockMixin): ''' @@ -41,7 +42,7 @@ class SdbTestCase(TestCase, LoaderModuleMockMixin): def tearDownClass(cls): try: os.unlink(TEMP_DATABASE_FILE) - except: + except OSError: pass def setup_loader_modules(self): @@ -52,7 +53,7 @@ class SdbTestCase(TestCase, LoaderModuleMockMixin): def test_sqlite_get_not_found(self): what = sdb.sdb_get( 'sdb://test_sdb_data/thisKeyDoesNotExist', SDB_OPTS) - self.assertEqual(what, None, 'what is "{!r}"'.format(what)) + self.assertEqual(what, None, 'what is "{0!r}"'.format(what)) # test with SQLite database write and read From 2bccf228baba98ef3a29b132ba94ac3c4cb9900d Mon Sep 17 00:00:00 2001 From: Vernon Cole Date: Mon, 2 Oct 2017 12:18:11 -0600 Subject: [PATCH 4/5] fix code-lint complaint --- tests/unit/utils/test_sdb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/utils/test_sdb.py b/tests/unit/utils/test_sdb.py index c9724caca05..91216d4c442 100644 --- a/tests/unit/utils/test_sdb.py +++ b/tests/unit/utils/test_sdb.py @@ -53,7 +53,7 @@ class SdbTestCase(TestCase, LoaderModuleMockMixin): def test_sqlite_get_not_found(self): what = sdb.sdb_get( 'sdb://test_sdb_data/thisKeyDoesNotExist', SDB_OPTS) - self.assertEqual(what, None, 'what is "{0!r}"'.format(what)) + self.assertEqual(what, None) # test with SQLite database write and read From f96740d278f3707c06410795851b1faf6b4df225 Mon Sep 17 00:00:00 2001 From: Vernon Cole Date: Mon, 2 Oct 2017 21:07:27 -0600 Subject: [PATCH 5/5] use recommended source for TMP directory --- tests/unit/utils/test_sdb.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/utils/test_sdb.py b/tests/unit/utils/test_sdb.py index 91216d4c442..0550758107d 100644 --- a/tests/unit/utils/test_sdb.py +++ b/tests/unit/utils/test_sdb.py @@ -8,7 +8,7 @@ from __future__ import absolute_import import os # Import Salt Testing Libs -from tests.support.paths import TMP +from tests.support.runtests import RUNTIME_VARS from tests.support.mixins import LoaderModuleMockMixin from tests.support.unit import TestCase, skipIf from tests.support.mock import ( @@ -19,7 +19,7 @@ from tests.support.mock import ( # Import Salt Libs import salt.utils.sdb as sdb -TEMP_DATABASE_FILE = os.path.join(TMP, 'test_sdb.sqlite') +TEMP_DATABASE_FILE = os.path.join(RUNTIME_VARS.TMP, 'test_sdb.sqlite') SDB_OPTS = { 'extension_modules': '',