diff --git a/conftest.py b/conftest.py new file mode 100644 --- /dev/null +++ b/conftest.py @@ -0,0 +1 @@ +pytest_plugins = ["swh.storage.pytest_plugin"] \ No newline at end of file diff --git a/swh/clearlydefined/mapping_utils.py b/swh/clearlydefined/mapping_utils.py --- a/swh/clearlydefined/mapping_utils.py +++ b/swh/clearlydefined/mapping_utils.py @@ -7,18 +7,15 @@ import swh.model.hashutil -def map_sha1_with_swhid(sha1, database, user, password, host, port): +def map_sha1_with_swhid(sha1, dsn): if not sha1: return None - read_connection = psycopg2.connect( - database=database, user=user, password=password, host=host, port=port - ) + read_connection = psycopg2.connect(dsn=dsn) cur = read_connection.cursor() sha1 = swh.model.hashutil.hash_to_hex(sha1) sha1 = "\\x{sha1}".format(sha1=sha1) - cur.execute("SELECT sha1_git from content WHERE sha1=%s;", (sha1)) + cur.execute("SELECT sha1_git FROM content where sha1= %s;", (sha1,)) sha1_git = cur.fetchall() - swh_id = None if len(sha1_git) == 0: return None sha1_git = swh.model.hashutil.hash_to_hex(sha1_git[0][0]) diff --git a/swh/clearlydefined/tests/test_mapping.py b/swh/clearlydefined/tests/test_mapping.py new file mode 100644 --- /dev/null +++ b/swh/clearlydefined/tests/test_mapping.py @@ -0,0 +1,22 @@ +from swh.clearlydefined.mapping_utils import map_sha1_with_swhid +import psycopg2 + + +def test_mapping(swh_storage_backend_config): + dsn = swh_storage_backend_config["db"] + read_connection = psycopg2.connect(dsn=dsn) + cur = read_connection.cursor() + sha1_byte = "\\xa6628c8a4fbc08c29b8472e2222975e5b9918131" + sha1_git_byte = "\\xe103b11cbfecbc116dacbb1f9ab2a02176092a32" + sha256_byte = "\\x6ac599151a7aaa8ca5d38dc5bb61b49193a3cadc1ed33de5a57e4d1ecc53c846" + blake2s256_byte = "\\xc509b320abede3580bb1de75a0efa09ba7416db9c8de845a4e1b4317c0b8a8d9" + length = 717 + cur.execute( + "INSERT INTO content (sha1, sha1_git, sha256, blake2s256, length) VALUES (%s,%s,%s,%s,%s);", + (sha1_byte, sha1_git_byte, sha256_byte, blake2s256_byte, length), + ) + read_connection.commit() + sha1 = "a6628c8a4fbc08c29b8472e2222975e5b9918131" + assert "swh:1:cnt:e103b11cbfecbc116dacbb1f9ab2a02176092a32" == map_sha1_with_swhid( + sha1=sha1, dsn=dsn + )