<commit_before>from __future__ import absolute_import

from datetime import datetime, timedelta
from uuid import uuid4

import pytest

from sentry.constants import ObjectStatus
from sentry.exceptions import DeleteAborted
from sentry.models import (
    ApiApplication, ApiApplicationStatus, ApiGrant, ApiToken, Commit, CommitAuthor, Environment,
    EnvironmentProject, Event, EventMapping, EventTag, Group, GroupAssignee, GroupHash, GroupMeta,
    GroupRedirect, GroupResolution, GroupStatus, GroupTagKey, GroupTagValue, Organization,
    OrganizationStatus, Project, ProjectStatus, Release, ReleaseCommit, ReleaseEnvironment,
    Repository, TagKey, TagValue, Team, TeamStatus
)
from sentry.tasks.deletion import (
    delete_api_application, delete_group, delete_organization, delete_project, delete_repository,
    delete_tag_key, delete_team, generic_delete, revoke_api_tokens
)
from sentry.testutils import TestCase


class DeleteOrganizationTest(TestCase):
    def test_simple(self):
        org = self.create_organization(
            name='test',
            status=OrganizationStatus.PENDING_DELETION,
        )
        self.create_team(organization=org, name='test1')
        self.create_team(organization=org, name='test2')
        release = Release.objects.create(version='a' * 32, organization_id=org.id)
        repo = Repository.objects.create(
            organization_id=org.id,
            name=org.name,
            provider='dummy',
        )
        commit_author = CommitAuthor.objects.create(
            organization_id=org.id,
            name='foo',
            email='<EMAIL>',
        )
        commit = Commit.objects.create(
            repository_id=repo.id,
            organization_id=org.id,
            author=commit_author,
            key='a' * 40,
        )
        ReleaseCommit.objects.create(
            organization_id=org.id,
            release=release,
            commit=commit,
            order=0,
        )

        env = Environment.objects.create(organization_id=org.id, project_id=4, name='foo')
        release_env = ReleaseEnvironment.objects.create(
            organization_id=org.id, project_id=4, release_id=release.id, environment_id=env.id
        )

        with self.tasks():
            delete_organization(object_id=org.id)<commit_msg>test(deletion): Add test for repo deletion when org is deleted<commit_after>from __future__ import absolute_import

from datetime import datetime, timedelta
from mock import patch
from uuid import uuid4

import pytest

from sentry.constants import ObjectStatus
from sentry.exceptions import DeleteAborted
from sentry.models import (
    ApiApplication, ApiApplicationStatus, ApiGrant, ApiToken, Commit, CommitAuthor, Environment,
    EnvironmentProject, Event, EventMapping, EventTag, Group, GroupAssignee, GroupHash, GroupMeta,
    GroupRedirect, GroupResolution, GroupStatus, GroupTagKey, GroupTagValue, Organization,
    OrganizationStatus, Project, ProjectStatus, Release, ReleaseCommit, ReleaseEnvironment,
    Repository, TagKey, TagValue, Team, TeamStatus
)
from sentry.plugins.providers.dummy.repository import DummyRepositoryProvider
from sentry.tasks.deletion import (
    delete_api_application, delete_group, delete_organization, delete_project, delete_repository,
    delete_tag_key, delete_team, generic_delete, revoke_api_tokens
)
from sentry.testutils import TestCase


class DeleteOrganizationTest(TestCase):
    def test_simple(self):
        org = self.create_organization(
            name='test',
            status=OrganizationStatus.PENDING_DELETION,
        )
        user = self.create_user()
        self.create_team(organization=org, name='test1')
        self.create_team(organization=org, name='test2')
        release = Release.objects.create(version='a' * 32, organization_id=org.id)
        repo = Repository.objects.create(
            organization_id=org.id,
            name=org.name,
            provider='dummy',
        )
        commit_author = CommitAuthor.objects.create(
            organization_id=org.id,
            name='foo',
            email='<EMAIL>',
        )
        commit = Commit.objects.create(
            repository_id=repo.id,
            organization_id=org.id,
            author=commit_author,
            key='a' * 40,
        )
        ReleaseCommit.objects.create(
            organization_id=org.id,
            release=release,
            commit=commit,
            order=0,
        )

        env = Environment.objects.create(organization_id=org.id, project_id=4, name='foo')
        release_env = ReleaseEnvironment.objects.create(
            organization_id=org.id, project_id=4, release_id=release.id, environment_id=env.id
        )

        with self.tasks():
            with patch.object(DummyRepositoryProvider, 'delete_repository') as mock_delete_repo:
                delete_organization(object_id=org.id, actor_id=user.id)
                mock_delete_repo.assert_called_once()  # NOQA
{'fraction_non_alphanumeric': 0.08475941211999195, 'fraction_numerical': 0.004026575397624321, 'mean_word_length': 3.0687960687960687, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5508032', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1345, 'n_words': 276}