Skip to content

test_init: Decrease assertion checks

To allow the debian build to pass [1]

Within the debian chroot (unstable), the current assertion fails because it's only an AssertionError without any associated message (contrary to tox/pytest in the venv).

Might be related to the pytest version which is too old within the chroot (4.6.11 vs 5.4.3, no idea really). When trying to use within the venv the same version I had other unrelated problems which prevented me to check further.

Running with python3.8 (using nix and another virtualenv), I do not reproduce either (<- everything is fine).

(Finally able to) reproduce within the chroot (+ modifying the swh_storage fixture to be in-memory storage, so i'm not side-tracked by other issues):

With packaged version:

(sid-amd64-sbuild)root@yavin4:/build/swh-loader-core-cCFgYS/swh-loader-core-0.5.9# python3 -m pytest swh/loader/tests/test_init.py -k test_check_snapshot_failure
============================================================================================================= test session starts =============================================================================================================
platform linux -- Python 3.8.4, pytest-4.6.11, py-1.8.1, pluggy-0.13.0
rootdir: /build/swh-loader-core-cCFgYS/swh-loader-core-0.5.9, inifile: pytest.ini
plugins: requests-mock-1.7.0, swh.core-0.1.2, celery-4.4.2, postgresql-2.2.0, mock-1.10.4, hypothesis-5.16.0
collected 11 items / 10 deselected / 1 selected

swh/loader/tests/test_init.py F                                                                                                                                                                                                         [100%]

================================================================================================================== FAILURES ===================================================================================================================
________________________________________________________________________________________________________ test_check_snapshot_failures _________________________________________________________________________________________________________

swh_storage = <swh.storage.in_memory.InMemoryStorage object at 0x7fed693d0760>

    def test_check_snapshot_failures(swh_storage):
        """Failure scenarios:

        0. snapshot parameter is not a snapshot
        1. snapshot id is correct but branches mismatched
        2. snapshot id is not correct, it's not found in the storage
        3. snapshot reference an alias which does not exist
        4. snapshot is found in storage, targeted revision does not exist
        5. snapshot is found in storage, targeted revision exists but the directory the
           revision targets does not exist
        6. snapshot is found in storage, target revision exists, targeted directory by the
           revision exist. Content targeted by the directory does not exist.
        7. snapshot is found in storage, targeted release does not exist

        """
        snap_id_hex = "2498dbf535f882bc7f9a18fb16c9ad27fda7bab7"
        snapshot = Snapshot(
            id=hash_to_bytes(snap_id_hex),
            branches={
                b"master": SnapshotBranch(
                    target=hash_to_bytes(hash_hex), target_type=TargetType.REVISION,
                ),
            },
        )

        s = swh_storage.snapshot_add([snapshot])
        assert s == {
            "snapshot:add": 1,
        }

        unexpected_snapshot = Snapshot(
            branches={
                b"tip": SnapshotBranch(  # wrong branch
                    target=hash_to_bytes(hash_hex), target_type=TargetType.RELEASE
                )
            },
        )

        # 0. not a Snapshot object, raise!
        with pytest.raises(AssertionError, match="variable 'snapshot' must be a snapshot"):
            check_snapshot(ORIGIN_VISIT, swh_storage)

        # 1. snapshot id is correct but branches mismatched
>       check_snapshot(attr.evolve(unexpected_snapshot, id=snapshot.id), swh_storage)

swh/loader/tests/test_init.py:404:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

snapshot = Snapshot(branches=<swh.model.collections.ImmutableDict object at 0x7fed69492ee0>, id=b"$\x98\xdb\xf55\xf8\x82\xbc\x7f\x9a\x18\xfb\x16\xc9\xad'\xfd\xa7\xba\xb7")
storage = <swh.storage.in_memory.InMemoryStorage object at 0x7fed693d0760>, allowed_empty = []

    def check_snapshot(
        snapshot: Snapshot,
        storage: StorageInterface,
        allowed_empty: Iterable[Tuple[TargetType, bytes]] = [],
    ):
        """Check that:
        - snapshot exists in the storage and match
        - each object reference up to the revision/release targets exists

        Args:
            snapshot: full snapshot to check for existence and consistency
            storage: storage to lookup information into
            allowed_empty: Iterable of branch we allow to be empty (some edge case loaders
              allows this case to happen, nixguix for example allows the branch evaluation"
              to target the nixpkgs git commit reference, which may not yet be resolvable at
              loading time)

        Returns:
            the snapshot stored in the storage for further test assertion if any is
            needed.

        """
        if not isinstance(snapshot, Snapshot):
            raise AssertionError(f"variable 'snapshot' must be a snapshot: {snapshot!r}")

        snapshot_dict = storage.snapshot_get(snapshot.id)
        if snapshot_dict is None:
            raise AssertionError(f"Snapshot {snapshot.id.hex()} is not found")

        snapshot_dict.pop("next_branch")
        actual_snaphot = Snapshot.from_dict(snapshot_dict)
        assert isinstance(actual_snaphot, Snapshot)

>       assert snapshot == actual_snaphot
E       AssertionError

swh/loader/tests/__init__.py:149: AssertionError
=================================================================================================== 1 failed, 10 deselected in 0.08 seconds ===================================================================================================

Fixing as this diff does:

(sid-amd64-sbuild)root@yavin4:/build/swh-loader-core-cCFgYS/swh-loader-core-0.5.9# emacs swh/loader/tests/test_init.py
(sid-amd64-sbuild)root@yavin4:/build/swh-loader-core-cCFgYS/swh-loader-core-0.5.9# python3 -m pytest swh/loader/tests/test_init.py -k test_check_snapshot_failure
============================================================================================================= test session starts =============================================================================================================
platform linux -- Python 3.8.4, pytest-4.6.11, py-1.8.1, pluggy-0.13.0
rootdir: /build/swh-loader-core-cCFgYS/swh-loader-core-0.5.9, inifile: pytest.ini
plugins: requests-mock-1.7.0, swh.core-0.1.2, celery-4.4.2, postgresql-2.2.0, mock-1.10.4, hypothesis-5.16.0
collected 11 items / 10 deselected / 1 selected

swh/loader/tests/test_init.py .                                                                                                                                                                                                         [100%]

=================================================================================================== 1 passed, 10 deselected in 0.05 seconds ===================================================================================================

Test Plan

tox


Migrated from D3552 (view on Phabricator)

Merge request reports