def test_branch_name_head_commit_digests_exist(self, diverse_repo):
        from hangar.records.heads import get_branch_names, get_branch_head_commit
        from hangar.records.parsing import commit_ref_db_key_from_raw_key
        from hangar.records.parsing import commit_parent_db_key_from_raw_key
        from hangar.records.parsing import commit_spec_db_key_from_raw_key
        from hangar.diagnostics.integrity import _verify_branch_integrity

        branch_names = get_branch_names(diverse_repo._env.branchenv)
        for bname in branch_names:
            bhead = get_branch_head_commit(diverse_repo._env.branchenv,
                                           branch_name=bname)
            with diverse_repo._env.refenv.begin(write=True) as txn:
                cmtRefKey = commit_ref_db_key_from_raw_key(bhead)
                cmtSpecKey = commit_spec_db_key_from_raw_key(bhead)
                cmtParentKey = commit_parent_db_key_from_raw_key(bhead)

                cmtRefVal = txn.get(cmtRefKey)
                cmtSpecVal = txn.get(cmtSpecKey)
                cmtParentVal = txn.get(cmtParentKey)

                txn.delete(cmtRefKey)
                txn.delete(cmtSpecKey)
                txn.delete(cmtParentKey)

            with pytest.raises(
                    RuntimeError,
                    match='Branch commit map compromised. Branch name'):
                _verify_branch_integrity(diverse_repo._env.branchenv,
                                         diverse_repo._env.refenv)

            with diverse_repo._env.refenv.begin(write=True) as txn:
                txn.put(cmtRefKey, cmtRefVal)
                txn.put(cmtSpecKey, cmtSpecVal)
                txn.put(cmtParentKey, cmtParentVal)
    def test_staging_head_branch_name_exists(self, diverse_repo):
        from hangar.records.heads import get_staging_branch_head
        from hangar.records.parsing import repo_branch_head_db_key_from_raw_key
        from hangar.diagnostics.integrity import _verify_branch_integrity

        bname = get_staging_branch_head(diverse_repo._env.branchenv)
        with diverse_repo._env.branchenv.begin(write=True) as txn:
            branchKey = repo_branch_head_db_key_from_raw_key(bname)
            txn.delete(branchKey)

        with pytest.raises(
                RuntimeError,
                match=
                'Brach commit map compromised. Staging head refers to branch name'
        ):
            _verify_branch_integrity(diverse_repo._env.branchenv,
                                     diverse_repo._env.refenv)
    def test_atleast_one_branch_exists(self, diverse_repo):
        from hangar.records.heads import get_branch_names
        from hangar.records.parsing import repo_branch_head_db_key_from_raw_key
        from hangar.diagnostics.integrity import _verify_branch_integrity

        branch_names = get_branch_names(diverse_repo._env.branchenv)
        with diverse_repo._env.branchenv.begin(write=True) as txn:
            for bname in branch_names:
                branchKey = repo_branch_head_db_key_from_raw_key(bname)
                txn.delete(branchKey)

        with pytest.raises(
                RuntimeError,
                match=
                'Branch map compromised. Repo must contain atleast one branch'
        ):
            _verify_branch_integrity(diverse_repo._env.branchenv,
                                     diverse_repo._env.refenv)