<commit_before>import copy
import datetime
import json
import os

import kombu
import pytest
import responses
from _pytest.monkeypatch import MonkeyPatch
from django.conf import settings
from requests import Request
from requests_hawk import HawkAuth
from webtest.app import TestApp

from treeherder.client.thclient import TreeherderClient
from treeherder.config.wsgi import application
from treeherder.etl.jobs import store_job_data
from treeherder.etl.push import store_push_data
from treeherder.model.models import (JobNote,
                                     Push,
                                     TextLogErrorMetadata)


def pytest_addoption(parser):
    parser.addoption(
        "--runslow",
        action="store_true",
        help="run slow tests",
    )
    parser.addoption(
        "--runselenium",
        action="store_true",
        help="run selenium tests",
    )


def pytest_runtest_setup(item):
    """
    Per-test setup.
    - Add an option to run those tests marked as 'slow'
    - Provide cache isolation incrementing the cache key prefix
    """

    if 'slow' in item.keywords and not item.config.getoption("--runslow"):
        pytest.skip("need --runslow option to run")

    if 'selenium' in item.keywords and not item.config.getoption("--runselenium"):
        pytest.skip("need --runselenium option to run selenium tests")

    increment_cache_key_prefix()


def increment_cache_key_prefix():
    """Increment a cache prefix to effectively clear the cache."""
    from django.core.cache import cache
    cache.key_prefix = ""
    prefix_counter_cache_key = "treeherder-tests-key-prefix-counter"
    try:
        key_prefix_counter = cache.incr(prefix_counter_cache_key)
    except ValueError:
        key_prefix_counter = 0
        cache.set(prefix_counter_cache_key, key_prefix_counter)
    cache.key_prefix = "t{0}".format(key_prefix_counter)<commit_msg>Bug 1393762 - Stop incrementing cache key prefix in unit tests (#2734)<commit_after>import copy
import datetime
import json
import os

import kombu
import pytest
import responses
from _pytest.monkeypatch import MonkeyPatch
from django.conf import settings
from requests import Request
from requests_hawk import HawkAuth
from webtest.app import TestApp

from treeherder.client.thclient import TreeherderClient
from treeherder.config.wsgi import application
from treeherder.etl.jobs import store_job_data
from treeherder.etl.push import store_push_data
from treeherder.model.models import (JobNote,
                                     Push,
                                     TextLogErrorMetadata)


def pytest_addoption(parser):
    parser.addoption(
        "--runslow",
        action="store_true",
        help="run slow tests",
    )
    parser.addoption(
        "--runselenium",
        action="store_true",
        help="run selenium tests",
    )


def pytest_runtest_setup(item):
    """
    Per-test setup.
    - Add an option to run those tests marked as 'slow'
    - Clear the django cache between runs
    """

    if 'slow' in item.keywords and not item.config.getoption("--runslow"):
        pytest.skip("need --runslow option to run")

    if 'selenium' in item.keywords and not item.config.getoption("--runselenium"):
        pytest.skip("need --runselenium option to run selenium tests")

    from django.core.cache import cache
    cache.clear()
{'fraction_non_alphanumeric': 0.08035714285714286, 'fraction_numerical': 0.003869047619047619, 'mean_word_length': 3.0059594755661503, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3991196', 'n_tokens_mistral': 999, 'n_tokens_neox': 957, 'n_words': 278}