From a2ccc9cc92f4576b562c35da4c5675f106819d44 Mon Sep 17 00:00:00 2001 From: Hanne Moa Date: Mon, 8 Jan 2024 10:32:46 +0100 Subject: [PATCH 1/4] Test _config_resource_walk --- tests/unittests/config_test.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 tests/unittests/config_test.py 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('/')) From 147887c0ee1731f43f341103f350be5b89e6eb13 Mon Sep 17 00:00:00 2001 From: Hanne Moa Date: Mon, 8 Jan 2024 11:42:42 +0100 Subject: [PATCH 2/4] Test ChangeScriptFinder._find_change_scripts --- tests/unittests/pgsync_test.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tests/unittests/pgsync_test.py diff --git a/tests/unittests/pgsync_test.py b/tests/unittests/pgsync_test.py new file mode 100644 index 0000000000..783b480abb --- /dev/null +++ b/tests/unittests/pgsync_test.py @@ -0,0 +1,15 @@ +from nav.pgsync import ChangeScriptFinder + + +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')) From a5efc3c307ee4426ef3e29de3fec6b1a7efc5bc5 Mon Sep 17 00:00:00 2001 From: Hanne Moa Date: Mon, 8 Jan 2024 11:52:54 +0100 Subject: [PATCH 3/4] Test sql file loading in Synchronizer --- python/nav/pgsync.py | 12 +++++++++--- tests/unittests/pgsync_test.py | 10 +++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) 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/pgsync_test.py b/tests/unittests/pgsync_test.py index 783b480abb..4c37e798bf 100644 --- a/tests/unittests/pgsync_test.py +++ b/tests/unittests/pgsync_test.py @@ -1,4 +1,4 @@ -from nav.pgsync import ChangeScriptFinder +from nav.pgsync import ChangeScriptFinder, Synchronizer from unittest import TestCase @@ -13,3 +13,11 @@ def test_init_should_read_sql_filenames_from_package_and_return_list_of_relative 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)) From e9203f20c806a0e1ae6bd4d7672d66dae0a7b776 Mon Sep 17 00:00:00 2001 From: Hanne Moa Date: Mon, 8 Jan 2024 12:07:35 +0100 Subject: [PATCH 4/4] Test that VERSION can be imported from buildconf --- tests/unittests/buildconf_test.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/unittests/buildconf_test.py 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')