Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test pkg_resource usage #2799

Merged
merged 4 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions python/nav/pgsync.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
hmpf marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
@@ -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))
Loading