<commit_before>from corehq.apps.accounting.tests.base_tests import BaseAccountingTest
from toggle.models import Toggle
from corehq import toggles
from corehq.apps.accounting import generator
from corehq.apps.accounting.models import (
    Subscription, BillingAccount, DefaultProductPlan, SoftwarePlanEdition,
)
from corehq.apps.users.models import (
    Permissions, UserRole, UserRolePresets, WebUser, CommCareUser,
)


class TestUserRoleSubscriptionChanges(BaseAccountingTest):
    min_subscription_length = 3

    def setUp(self):
        super(TestUserRoleSubscriptionChanges, self).setUp()
        self.domain = generator.arbitrary_domain()
        UserRole.init_domain_with_presets(self.domain.name)
        self.user_roles = UserRole.by_domain(self.domain.name)
        self.custom_role = UserRole.get_or_create_with_permissions(
            self.domain.name,
            Permissions(edit_apps=True, edit_web_users=True),
            "Custom Role"
        )
        self.custom_role.save()
        self.read_only_role = UserRole.get_read_only_role_by_domain(self.domain.name)

        self.admin_user = generator.arbitrary_web_user()
        self.admin_user.add_domain_membership(self.domain.name, is_admin=True)
        self.admin_user.save()

        self.web_users = []
        self.commcare_users = []
        for role in [self.custom_role] + self.user_roles:
            web_user = generator.arbitrary_web_user()
            web_user.add_domain_membership(self.domain.name, role_id=role.get_id)
            web_user.save()
            self.web_users.append(web_user)

            commcare_user = generator.arbitrary_commcare_user(
                domain=self.domain.name)
            commcare_user.set_role(self.domain.name, role.get_qualified_id())
            commcare_user.save()
            self.commcare_users.append(commcare_user)

        # toggle until release
        self.toggle = Toggle(
            slug=toggles.ACCOUNTING_PREVIEW.slug,
            enabled_users=[self.admin_user.username],
        )
        self.toggle.save()

        self.account = BillingAccount.get_or_create_account_by_domain(
            self.domain.name,created_by=self.admin_user.username)[0]<commit_msg>fix resource conflict due to Static Toggles<commit_after>from couchdbkit import ResourceConflict
from corehq.apps.accounting.tests.base_tests import BaseAccountingTest
from toggle.models import Toggle
from corehq import toggles
from corehq.apps.accounting import generator
from corehq.apps.accounting.models import (
    Subscription, BillingAccount, DefaultProductPlan, SoftwarePlanEdition,
)
from corehq.apps.users.models import (
    Permissions, UserRole, UserRolePresets, WebUser, CommCareUser,
)


class TestUserRoleSubscriptionChanges(BaseAccountingTest):
    min_subscription_length = 3

    def setUp(self):
        super(TestUserRoleSubscriptionChanges, self).setUp()
        self.domain = generator.arbitrary_domain()
        UserRole.init_domain_with_presets(self.domain.name)
        self.user_roles = UserRole.by_domain(self.domain.name)
        self.custom_role = UserRole.get_or_create_with_permissions(
            self.domain.name,
            Permissions(edit_apps=True, edit_web_users=True),
            "Custom Role"
        )
        self.custom_role.save()
        self.read_only_role = UserRole.get_read_only_role_by_domain(self.domain.name)

        self.admin_user = generator.arbitrary_web_user()
        self.admin_user.add_domain_membership(self.domain.name, is_admin=True)
        self.admin_user.save()

        self.web_users = []
        self.commcare_users = []
        for role in [self.custom_role] + self.user_roles:
            web_user = generator.arbitrary_web_user()
            web_user.add_domain_membership(self.domain.name, role_id=role.get_id)
            web_user.save()
            self.web_users.append(web_user)

            commcare_user = generator.arbitrary_commcare_user(
                domain=self.domain.name)
            commcare_user.set_role(self.domain.name, role.get_qualified_id())
            commcare_user.save()
            self.commcare_users.append(commcare_user)

        # toggle until release
        try:
            self.toggle = Toggle(
                slug=toggles.ACCOUNTING_PREVIEW.slug,
                enabled_users=[self.admin_user.username],
            )
            self.toggle.save()
        except ResourceConflict:
            self.toggle = Toggle.get(toggles.ACCOUNTING_PREVIEW.slug)
            self.toggle.enabled_users.append(self.admin_user.username)
            self.toggle.save()

        self.account = BillingAccount.get_or_create_account_by_domain(
            self.domain.name,created_by=self.admin_user.username)[0]
{'fraction_non_alphanumeric': 0.08520559559135227, 'fraction_numerical': 0.000847816871555744, 'mean_word_length': 2.9456521739130435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3399073', 'n_tokens_mistral': 1439, 'n_tokens_neox': 1414, 'n_words': 196}