diff --git a/MANIFEST.in b/MANIFEST.in --- a/MANIFEST.in +++ b/MANIFEST.in @@ -3,5 +3,6 @@ include requirements*.txt include version.txt recursive-include swh py.typed +recursive-include swh/core/db/tests/data/ * recursive-include swh/core/tests/data/ * recursive-include swh/core/tests/fixture/data/ * diff --git a/swh/core/db/tests/data/cli/1-schema.sql b/swh/core/db/tests/data/cli/30-schema.sql rename from swh/core/db/tests/data/cli/1-schema.sql rename to swh/core/db/tests/data/cli/30-schema.sql diff --git a/swh/core/db/tests/data/cli/3-func.sql b/swh/core/db/tests/data/cli/40-funcs.sql rename from swh/core/db/tests/data/cli/3-func.sql rename to swh/core/db/tests/data/cli/40-funcs.sql diff --git a/swh/core/db/tests/data/cli/4-data.sql b/swh/core/db/tests/data/cli/50-data.sql rename from swh/core/db/tests/data/cli/4-data.sql rename to swh/core/db/tests/data/cli/50-data.sql diff --git a/swh/core/db/tests/test_cli.py b/swh/core/db/tests/test_cli.py --- a/swh/core/db/tests/test_cli.py +++ b/swh/core/db/tests/test_cli.py @@ -62,12 +62,29 @@ def mock_package_sql(mocker, datadir): """This bypasses the module manipulation to only returns the data test files. + For a given module `test.mod`, look for sql files in the directory `data/mod/*.sql`. + + Typical usage:: + + def test_xxx(cli_runner, mock_package_sql): + conninfo = craft_conninfo(test_db, "new-db") + module_name = "test.cli" + # the command below will use sql scripts from swh/core/db/tests/data/cli/*.sql + cli_runner.invoke(swhdb, ["init", module_name, "--dbname", conninfo]) """ + from swh.core.db.db_utils import get_sql_for_package from swh.core.utils import numfile_sortkey as sortkey - mock_sql_files = mocker.patch("swh.core.db.db_utils.get_sql_for_package") - sql_files = sorted(glob.glob(path.join(datadir, "cli", "*.sql")), key=sortkey) - mock_sql_files.return_value = sql_files + def get_sql_for_package_mock(modname): + if modname.startswith("test."): + sqldir = modname.split(".", 1)[1] + return sorted(glob.glob(path.join(datadir, sqldir, "*.sql")), key=sortkey) + return get_sql_for_package(modname) + + mock_sql_files = mocker.patch( + "swh.core.db.db_utils.get_sql_for_package", get_sql_for_package_mock + ) + return mock_sql_files @@ -107,7 +124,7 @@ """Create a db then initializing it should be ok """ - module_name = "something" + module_name = "test.cli" conninfo = craft_conninfo(test_db, "new-db") # This creates the db and installs the necessary admin extensions @@ -133,7 +150,7 @@ """Init command on an inexisting db cannot work """ - module_name = "anything" # it's mocked here + module_name = "test.cli" # it's mocked here conninfo = craft_conninfo(test_db, "inexisting-db") result = cli_runner.invoke(swhdb, ["init", module_name, "--dbname", conninfo]) @@ -149,7 +166,7 @@ In this test, the schema needs privileged extension to work. """ - module_name = "anything" # it's mocked here + module_name = "test.cli" # it's mocked here conninfo = craft_conninfo(test_db) result = cli_runner.invoke(swhdb, ["init", module_name, "--dbname", conninfo]) @@ -164,7 +181,7 @@ """Init commands with carefully crafted libpq conninfo works """ - module_name = "anything" # it's mocked here + module_name = "test.cli" # it's mocked here conninfo = craft_conninfo(test_db) result = cli_runner.invoke(swhdb, ["init-admin", module_name, "--dbname", conninfo]) @@ -185,7 +202,7 @@ """Init commands with standard environment variables works """ - module_name = "anything" # it's mocked here + module_name = "test.cli" # it's mocked here cli_runner, db_params = swh_db_cli result = cli_runner.invoke( swhdb, ["init-admin", module_name, "--dbname", db_params["dbname"]] @@ -209,7 +226,7 @@ """Multiple runs of the init commands are idempotent """ - module_name = "anything" # mocked + module_name = "test.cli" # mocked cli_runner, db_params = swh_db_cli result = cli_runner.invoke(