Changeset View
Changeset View
Standalone View
Standalone View
swh/provenance/tests/test_cli.py
# Copyright (C) 2021 The Software Heritage developers | # Copyright (C) 2021 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from typing import Set | |||||
from click.testing import CliRunner | from click.testing import CliRunner | ||||
import psycopg2 | |||||
import pytest | import pytest | ||||
from swh.core.cli import swh as swhmain | from swh.core.cli import swh as swhmain | ||||
import swh.core.cli.db # noqa ; ensure cli is loaded | import swh.core.cli.db # noqa ; ensure cli is loaded | ||||
from swh.core.db import BaseDb | from swh.core.db import BaseDb | ||||
import swh.provenance.cli # noqa ; ensure cli is loaded | import swh.provenance.cli # noqa ; ensure cli is loaded | ||||
def test_cli_swh_db_help(): | def test_cli_swh_db_help() -> None: | ||||
# swhmain.add_command(provenance_cli) | # swhmain.add_command(provenance_cli) | ||||
result = CliRunner().invoke(swhmain, ["provenance", "-h"]) | result = CliRunner().invoke(swhmain, ["provenance", "-h"]) | ||||
assert result.exit_code == 0 | assert result.exit_code == 0 | ||||
assert "Commands:" in result.output | assert "Commands:" in result.output | ||||
commands = result.output.split("Commands:")[1] | commands = result.output.split("Commands:")[1] | ||||
for command in ( | for command in ( | ||||
"find-all", | "find-all", | ||||
"find-first", | "find-first", | ||||
Show All 18 Lines | TABLES = { | ||||
"revision_in_origin", | "revision_in_origin", | ||||
} | } | ||||
@pytest.mark.parametrize( | @pytest.mark.parametrize( | ||||
"flavor, dbtables", (("with-path", TABLES | {"location"}), ("without-path", TABLES)) | "flavor, dbtables", (("with-path", TABLES | {"location"}), ("without-path", TABLES)) | ||||
) | ) | ||||
def test_cli_db_create_and_init_db_with_flavor( | def test_cli_db_create_and_init_db_with_flavor( | ||||
monkeypatch, postgresql, flavor, dbtables | monkeypatch, # TODO: add proper type annotation | ||||
): | postgresql: psycopg2.extensions.connection, | ||||
flavor: str, | |||||
dbtables: Set[str], | |||||
) -> None: | |||||
"""Test that 'swh db init provenance' works with flavors | """Test that 'swh db init provenance' works with flavors | ||||
for both with-path and without-path flavors""" | for both with-path and without-path flavors""" | ||||
dbname = f"{flavor}-db" | dbname = f"{flavor}-db" | ||||
# DB creation using 'swh db create' | # DB creation using 'swh db create' | ||||
db_params = postgresql.get_dsn_parameters() | db_params = postgresql.get_dsn_parameters() | ||||
Show All 21 Lines | with cnx.cursor() as cur: | ||||
"select table_name from information_schema.tables " | "select table_name from information_schema.tables " | ||||
"where table_schema = 'public' " | "where table_schema = 'public' " | ||||
f"and table_catalog = '{dbname}'" | f"and table_catalog = '{dbname}'" | ||||
) | ) | ||||
tables = set(x for (x,) in cur.fetchall()) | tables = set(x for (x,) in cur.fetchall()) | ||||
assert tables == dbtables | assert tables == dbtables | ||||
def test_cli_init_db_default_flavor(postgresql): | def test_cli_init_db_default_flavor(postgresql: psycopg2.extensions.connection) -> None: | ||||
"Test that 'swh db init provenance' defaults to a with-path flavored DB" | "Test that 'swh db init provenance' defaults to a with-path flavored DB" | ||||
dbname = postgresql.dsn | dbname = postgresql.dsn | ||||
result = CliRunner().invoke(swhmain, ["db", "init", "-d", dbname, "provenance"]) | result = CliRunner().invoke(swhmain, ["db", "init", "-d", dbname, "provenance"]) | ||||
assert result.exit_code == 0, result.output | assert result.exit_code == 0, result.output | ||||
with postgresql.cursor() as cur: | with postgresql.cursor() as cur: | ||||
cur.execute("select swh_get_dbflavor()") | cur.execute("select swh_get_dbflavor()") | ||||
assert cur.fetchone() == ("with-path",) | assert cur.fetchone() == ("with-path",) |