diff --git a/swh/storage/storage.py b/swh/storage/storage.py --- a/swh/storage/storage.py +++ b/swh/storage/storage.py @@ -1234,7 +1234,8 @@ def metadata_fetcher_add( self, fetchers: Iterable[MetadataFetcher], db=None, cur=None ) -> None: - for (i, fetcher) in enumerate(fetchers): + count = 0 + for fetcher in fetchers: if fetcher.metadata is None: raise StorageArgumentException( "MetadataFetcher.metadata may not be None in metadata_fetcher_add." @@ -1242,7 +1243,8 @@ db.metadata_fetcher_add( fetcher.name, fetcher.version, dict(fetcher.metadata), cur=cur ) - send_metric("metadata_fetcher:add", count=i + 1, method_name="metadata_fetcher") + count += 1 + send_metric("metadata_fetcher:add", count=count, method_name="metadata_fetcher") @timed @db_transaction(statement_timeout=500) @@ -1259,7 +1261,8 @@ def metadata_authority_add( self, authorities: Iterable[MetadataAuthority], db=None, cur=None ) -> None: - for (i, authority) in enumerate(authorities): + count = 0 + for authority in authorities: if authority.metadata is None: raise StorageArgumentException( "MetadataAuthority.metadata may not be None in " @@ -1268,8 +1271,9 @@ db.metadata_authority_add( authority.type.value, authority.url, dict(authority.metadata), cur=cur ) + count += 1 send_metric( - "metadata_authority:add", count=i + 1, method_name="metadata_authority" + "metadata_authority:add", count=count, method_name="metadata_authority" ) @timed diff --git a/swh/storage/tests/test_storage.py b/swh/storage/tests/test_storage.py --- a/swh/storage/tests/test_storage.py +++ b/swh/storage/tests/test_storage.py @@ -3153,6 +3153,13 @@ res = swh_storage.metadata_fetcher_get(fetcher.name, fetcher.version) assert res == fetcher + def test_metadata_fetcher_add_zero(self, swh_storage, sample_data): + fetcher = sample_data.metadata_fetcher + actual_fetcher = swh_storage.metadata_fetcher_get(fetcher.name, fetcher.version) + assert actual_fetcher is None # does not exist + + swh_storage.metadata_fetcher_add([]) + def test_metadata_authority_add_get(self, swh_storage, sample_data): authority = sample_data.metadata_authority @@ -3166,6 +3173,16 @@ res = swh_storage.metadata_authority_get(authority.type, authority.url) assert res == authority + def test_metadata_authority_add_zero(self, swh_storage, sample_data): + authority = sample_data.metadata_authority + + actual_authority = swh_storage.metadata_authority_get( + authority.type, authority.url + ) + assert actual_authority is None # does not exist + + swh_storage.metadata_authority_add([]) + def test_content_metadata_add(self, swh_storage, sample_data): content = sample_data.content fetcher = sample_data.metadata_fetcher