Skip to content

Commit

Permalink
Refactoring of unit tests helper
Browse files Browse the repository at this point in the history
  • Loading branch information
DraTeots committed Jun 27, 2024
1 parent 8c3fdaa commit d9675a5
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 31 deletions.
1 change: 1 addition & 0 deletions python/ccdb/sql/mysql_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ def init_mysql_database(engine, tables_dict=None):
except SQLAlchemyError as e:
print(f"Error processing table '{table_name}': {str(e)}")


def generate_mysql_sql_file(tables_dict, file_path='mysql_setup.sql'):
"""
Generate a MySQL .sql file with drop, create, and insert commands
Expand Down
10 changes: 6 additions & 4 deletions python/tests/helper.py → python/ccdb/testing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# There are a number of tools and configs for unit testing which is convenient to have in ccdb package.
# The unit tests and integration tests are located outside of the package

import inspect
import os
import shutil
Expand Down Expand Up @@ -33,9 +36,11 @@
# SQLite connection string for tests
sqlite_test_file_path = os.path.join(os.getcwd(), 'test.sqlite')


# SQLite connection string for tests
sqlite_test_connection_str = "sqlite:///" + os.path.join(ccdb_path, 'sql', 'ccdb.sqlite')


def recreate_mysql_db(connection_str):
""" Erases the test database and put it to default conditions """

Expand All @@ -50,10 +55,7 @@ def recreate_mysql_db(connection_str):
cli.process_command_line("db init --init-i-am-sure")





def recreate_mysql_db2(connection_string='mysql+pymysql://ccdb_user@localhost/ccdb_test'):
def recreate_mysql_db2(connection_string):
from sqlalchemy import create_engine, text
from sqlalchemy.exc import SQLAlchemyError

Expand Down
15 changes: 11 additions & 4 deletions python/run_unit_tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import glob
import unittest
import inspect
import os
import tests
from ccdb import testing as ccdb_testing

test_names = [
'unit_test_authentication',
Expand Down Expand Up @@ -33,7 +31,16 @@ def run_unit_tests():


if __name__ == "__main__":
script_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

if ccdb_testing.ENV_TEST_MYSQL not in os.environ:
print(f"(!)WARNING(!) TESTS Mysql Connection string is not set in {ccdb_testing.ENV_TEST_MYSQL}")
print(f"Using default: {ccdb_testing.mysql_test_connection_str}")

if ccdb_testing.ENV_TEST_SQLITE not in os.environ:
print(f"(!)WARNING(!) TESTS SQLite Connection string is not set in {ccdb_testing.ENV_TEST_SQLITE}")
print(f"Using default: {ccdb_testing.sqlite_test_file_path}")

script_dir = os.path.dirname(os.path.abspath(__file__))
unit_test_dir = os.path.join(script_dir, "tests")
os.chdir(unit_test_dir)
run_unit_tests()
1 change: 0 additions & 1 deletion python/tests/__init__.py

This file was deleted.

6 changes: 2 additions & 4 deletions python/tests/integ_test_cli_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
import ccdb.path_utils
import ccdb.cmd.themes
from ccdb.cmd.cli_manager import CliManager, CCDB_EXCEPTIONS_THROW
try:
from . import helper
except:
import helper
from ccdb import testing as helper


logger = logging.getLogger("ccdb")

Expand Down
9 changes: 4 additions & 5 deletions python/tests/integ_test_provider_mysql.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from ccdb.errors import DatabaseStructureError
from ccdb.errors import AnonymousUserForbiddenError
from ccdb import testing as helper
import unittest
import ccdb

# Have to import it this way as tests may be considered a package or not depending on context
try:
from . import provider_fixture
from . import helper
except:
import provider_fixture
import helper

import unittest
import ccdb


class MySQLAlchemyProviderTest(provider_fixture.AlchemyProviderTest):
Expand Down
5 changes: 3 additions & 2 deletions python/tests/integ_test_provider_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

from ccdb import get_ccdb_home_path
from ccdb.errors import DatabaseStructureError
from ccdb import testing as helper

# Have to import it this way as tests may be considered a package or not depending on context
try:
from . import provider_fixture
from . import helper
except:
import provider_fixture
import helper


# noinspection PyPep8Naming
Expand Down
7 changes: 1 addition & 6 deletions python/tests/provider_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@

from ccdb import AlchemyProvider
from ccdb.path_utils import ParseRequestResult

try:
from . import helper
except ImportError:
import helper

from ccdb import testing as helper

class AlchemyProviderTest(unittest.TestCase):
ccdb_path = get_ccdb_home_path()
Expand Down
6 changes: 1 addition & 5 deletions python/tests/unit_test_dump_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
import ccdb.path_utils
import ccdb.cmd.themes
from ccdb.cmd.cli_manager import CliManager, CCDB_EXCEPTIONS_THROW

try:
from . import helper
except ImportError:
import helper
from ccdb import testing as helper

logger = logging.getLogger("ccdb")

Expand Down

0 comments on commit d9675a5

Please sign in to comment.