diff --git a/python/nav/pgsync.py b/python/nav/pgsync.py index 0d05eedfd9..6b8386b969 100755 --- a/python/nav/pgsync.py +++ b/python/nav/pgsync.py @@ -281,13 +281,16 @@ class Synchronizer(object): (None, 'indexes.sql'), ] - def __init__(self, resource_module, apply_out_of_order_changes=False): + def __init__(self, resource_module, apply_out_of_order_changes=False, config=None): self.resource_module = resource_module self.connection = None self.cursor = None - self.connect_options = ConnectionParameters.from_config() self.apply_out_of_order_changes = apply_out_of_order_changes self.finder = ChangeScriptFinder(self.resource_module) + if config: + self.connect_options = config + else: + self.connect_options = ConnectionParameters.from_config() def connect(self): """Connects the synchronizer to the NAV configured database.""" @@ -532,7 +535,7 @@ def execute_sql_file(self, filename): Terminates the process if there are errors. """ - sql = resource_string(self.resource_module, filename) + sql = self._read_sql_file(filename) print_color("%-20s " % (filename + ":"), COLOR_CYAN, newline=False) try: self.cursor.execute(sql) @@ -542,6 +545,9 @@ def execute_sql_file(self, filename): else: print_color("OK", COLOR_GREEN) + def _read_sql_file(self, filename): + return resource_string(self.resource_module, filename) + class ChangeScriptFinder(list): """Handles locating change scripts""" diff --git a/tests/unittests/buildconf_test.py b/tests/unittests/buildconf_test.py new file mode 100644 index 0000000000..264b3c27f5 --- /dev/null +++ b/tests/unittests/buildconf_test.py @@ -0,0 +1,9 @@ +from unittest import TestCase + + +class TestBuildconf(TestCase): + def test_VERSION_can_be_imported(self): + try: + from nav.buildconf import VERSION + except ImportError: + self.fail('VERSION could not be imported') diff --git a/tests/unittests/config_test.py b/tests/unittests/config_test.py new file mode 100644 index 0000000000..2d6875557d --- /dev/null +++ b/tests/unittests/config_test.py @@ -0,0 +1,16 @@ +from nav.config import _config_resource_walk + + +from unittest import TestCase + + +class TestConfigResourceWalk(TestCase): + def test_should_read_relative_paths_as_strings_from_nav_package_and_return_a_long_list_of_strings( + self, + ): + # result should be many, many relative paths as strings + result = tuple(_config_resource_walk()) # generator + self.assertTrue(len(result) > 20) + for relpath in result: + self.assertIsInstance(relpath, str) + self.assertFalse(relpath.startswith('/')) diff --git a/tests/unittests/pgsync_test.py b/tests/unittests/pgsync_test.py new file mode 100644 index 0000000000..4c37e798bf --- /dev/null +++ b/tests/unittests/pgsync_test.py @@ -0,0 +1,23 @@ +from nav.pgsync import ChangeScriptFinder, Synchronizer + + +from unittest import TestCase + + +class TestChangeScriptFinder(TestCase): + def test_init_should_read_sql_filenames_from_package_and_return_list_of_relative_filenames( + self, + ): + csf = ChangeScriptFinder('nav.models') + self.assertTrue(len(csf) > 40) + for sql_filename in csf: + self.assertTrue(sql_filename.startswith('sql/')) + self.assertTrue(sql_filename.endswith('.sql')) + + +class TestSynchronizer(TestCase): + def test_should_read_sql_file_from_package_and_return_bytes(self): + syncer = Synchronizer('nav.models', config=True) + filename = "sql/baseline/manage.sql" + sql = syncer._read_sql_file(filename) + self.assertIn("CREATE TABLE org", str(sql))