Skip to content

Commit

Permalink
Merge pull request #2799 from hmpf/test-pkg-resource-usage
Browse files Browse the repository at this point in the history
Test pkg_resource usage
  • Loading branch information
hmpf committed Feb 20, 2024
2 parents d5497c8 + e9203f2 commit d96a876
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
12 changes: 9 additions & 3 deletions python/nav/pgsync.py
Expand Up @@ -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."""
Expand Down Expand Up @@ -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)
Expand All @@ -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"""
Expand Down
9 changes: 9 additions & 0 deletions 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')
16 changes: 16 additions & 0 deletions 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('/'))
23 changes: 23 additions & 0 deletions 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))

0 comments on commit d96a876

Please sign in to comment.