<commit_before>import pytest
import json
import requests
from collections import OrderedDict
from tag_release import (
    openedx_release_repos, get_ref_for_repos, commit_ref_info,
    create_ref_for_repos, remove_ref_for_repos, override_repo_refs
)

pytestmark = pytest.mark.usefixtures("common_mocks")


expected_repos = {
    'edx/edx-platform': {
        'openedx-release': {
            'ref': 'release',
            'requirements': 'requirements/edx/github.txt',
        }
    },
    'edx/configuration': {
        'openedx-release': {
            'ref': 'master',
        }
    },
    'edx/XBlock': {
        'openedx-release': {
            'parent-repo': 'edx/edx-platform',
        }
    },
}

expected_commits = {
    'edx/edx-platform': {
        'committer': {'name': '<NAME>'},
        'author': {'name': '<NAME>'},
        'sha': 'deadbeef12345',
        'ref_type': 'branch',
        'ref': 'release',
        'message': 'commit message for edx-platform release commit',
    },
    'edx/configuration': {
        'committer': {'name': 'Dev 2'},
        'author': {'name': '<NAME>'},
        'sha': '12345deadbeef',
        'ref_type': 'branch',
        'ref': 'master',
        'message': 'commit message for configuration master commit',
    },
    'edx/XBlock': {
        'committer': {'name': '<NAME>'},
        'author': {'name': '<NAME>'},
        'sha': '1a2b3c4d5e6f',
        'ref_type': 'tag',
        'ref': '0.4.4',
        'message': 'commit message for refs/tags/0.4.4',
    }
}


def test_get_repos(session):
    repos = openedx_release_repos(session)
    assert repos == expected_repos


def test_get_ref_for_repos(session):
    result = get_ref_for_repos(expected_repos, "tag-exists-some-repos", session)
    expected_result = {
        'edx/edx-platform': {
            'author': {'name': '<PASSWORD> 6'},
            'committer': {'name': '<NAME>'},
            'message': 'commit message for refs/tags/tag-exists-some-repos',
            'ref': 'refs/tags/tag-exists-some-repos',
            'ref_type': 'tag',
            'sha': '65656565',
        }
    }
    assert result == expected_result


def test_get_ref_for_repos_not_exist(session):
    result = get_ref_for_repos(expected_repos, "tag-exists-no-repos", session)
    assert result == {}


def test_commit_ref_info(session):
    result = commit_ref_info(expected_repos, session)
    assert result == expected_commits


def test_overrides_none():
    result = override_repo_refs(expected_repos)
    assert expected_repos == expected_repos


def test_overrides_global_ref():
    result = override_repo_refs(expected_repos, override_ref="abcdef")
    assert result["edx/edx-platform"]["openedx-release"]["ref"] == "abcdef"
    assert result["edx/configuration"]["openedx-release"]["ref"] == "abcdef"
    assert result["edx/XBlock"]["openedx-release"]["ref"] == "abcdef"
    assert "parent-repo" not in result["edx/XBlock"]["openedx-release"]


def test_overrides_dict():
    overrides = {
        "edx/edx-platform": "xyz",
        "edx/configuration": "refs/branch/no-way",
        "edx/does-not-exist": "does-not-matter",
    }
    result = override_repo_refs(expected_repos, overrides=overrides)
    assert result["edx/edx-platform"]["openedx-release"]["ref"] == "xyz"
    assert result["edx/configuration"]["openedx-release"]["ref"] == "refs/branch/no-way"
    assert result["edx/XBlock"]["openedx-release"]["parent-repo"] == "edx/edx-platform"
    assert "ref" not in result["edx/XBlock"]["openedx-release"]


def test_overrides_global_ref_and_dict():
    override_ref = "fakie-mcfakerson"
    overrides = {
        "edx/edx-platform": "xyz",
        "edx/does-not-exist": "does-not-matter",
    }
    result = override_repo_refs(<commit_msg>Surely this is what db meant<commit_after>import pytest
import json
import requests
from collections import OrderedDict
from tag_release import (
    openedx_release_repos, get_ref_for_repos, commit_ref_info,
    create_ref_for_repos, remove_ref_for_repos, override_repo_refs
)

pytestmark = pytest.mark.usefixtures("common_mocks")


expected_repos = {
    'edx/edx-platform': {
        'openedx-release': {
            'ref': 'release',
            'requirements': 'requirements/edx/github.txt',
        }
    },
    'edx/configuration': {
        'openedx-release': {
            'ref': 'master',
        }
    },
    'edx/XBlock': {
        'openedx-release': {
            'parent-repo': 'edx/edx-platform',
        }
    },
}

expected_commits = {
    'edx/edx-platform': {
        'committer': {'name': 'Dev 1'},
        'author': {'name': 'Dev 1'},
        'sha': 'deadbeef12345',
        'ref_type': 'branch',
        'ref': 'release',
        'message': 'commit message for edx-platform release commit',
    },
    'edx/configuration': {
        'committer': {'name': 'Dev 2'},
        'author': {'name': 'Dev 2'},
        'sha': '12345deadbeef',
        'ref_type': 'branch',
        'ref': 'master',
        'message': 'commit message for configuration master commit',
    },
    'edx/XBlock': {
        'committer': {'name': 'Dev 1'},
        'author': {'name': 'Dev 1'},
        'sha': '1a2b3c4d5e6f',
        'ref_type': 'tag',
        'ref': '0.4.4',
        'message': 'commit message for refs/tags/0.4.4',
    }
}


def test_get_repos(session):
    repos = openedx_release_repos(session)
    assert repos == expected_repos


def test_get_ref_for_repos(session):
    result = get_ref_for_repos(expected_repos, "tag-exists-some-repos", session)
    expected_result = {
        'edx/edx-platform': {
            'author': {'name': '<NAME>'},
            'committer': {'name': '<PASSWORD> 6'},
            'message': 'commit message for refs/tags/tag-exists-some-repos',
            'ref': 'refs/tags/tag-exists-some-repos',
            'ref_type': 'tag',
            'sha': '65656565',
        }
    }
    assert result == expected_result


def test_get_ref_for_repos_not_exist(session):
    result = get_ref_for_repos(expected_repos, "tag-exists-no-repos", session)
    assert result == {}


def test_commit_ref_info(session):
    result = commit_ref_info(expected_repos, session)
    assert result == expected_commits


def test_overrides_none():
    result = override_repo_refs(expected_repos)
    assert result == expected_repos


def test_overrides_global_ref():
    result = override_repo_refs(expected_repos, override_ref="abcdef")
    assert result["edx/edx-platform"]["openedx-release"]["ref"] == "abcdef"
    assert result["edx/configuration"]["openedx-release"]["ref"] == "abcdef"
    assert result["edx/XBlock"]["openedx-release"]["ref"] == "abcdef"
    assert "parent-repo" not in result["edx/XBlock"]["openedx-release"]


def test_overrides_dict():
    overrides = {
        "edx/edx-platform": "xyz",
        "edx/configuration": "refs/branch/no-way",
        "edx/does-not-exist": "does-not-matter",
    }
    result = override_repo_refs(expected_repos, overrides=overrides)
    assert result["edx/edx-platform"]["openedx-release"]["ref"] == "xyz"
    assert result["edx/configuration"]["openedx-release"]["ref"] == "refs/branch/no-way"
    assert result["edx/XBlock"]["openedx-release"]["parent-repo"] == "edx/edx-platform"
    assert "ref" not in result["edx/XBlock"]["openedx-release"]


def test_overrides_global_ref_and_dict():
    override_ref = "fakie-mcfakerson"
    overrides = {
        "edx/edx-platform": "xyz",
        "edx/does-not-exist": "does-not-matter",
    }
    result = override_repo_refs(
{'fraction_non_alphanumeric': 0.16390374331550803, 'fraction_numerical': 0.009224598930481283, 'mean_word_length': 3.0724006532389767, 'pattern_counts': {'":': 10, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1521131', 'n_tokens_mistral': 2607, 'n_tokens_neox': 2533, 'n_words': 431}