<commit_before>import json

from frontend.models import (
    Practice,
    PracticeStatistics,
    Prescription,
    Presentation,
    ImportLog,
)
from matrixstore.tests.matrixstore_factory import (
    matrixstore_from_data_factory,
    patch_global_matrixstore,
)


def copy_fixtures_to_matrixstore(cls):
    """
    Decorator for TestCase classes which copies data from Postgres into an
    in-memory MatrixStore instance. This allows us to re-use database fixtures,
    and the tests designed to work with those fixtures, to test
    MatrixStore-powered code.
    """
    # These methods have been decorated with `@classmethod` so we need to use
    # `__func__` to get a reference to the original, undecorated method
    decorated_setUpClass = cls.setUpClass.__func__
    decorated_tearDownClass = cls.tearDownClass.__func__

    def setUpClass(inner_cls):
        decorated_setUpClass(inner_cls)
        matrixstore = matrixstore_from_postgres()
        stop_patching = patch_global_matrixstore(matrixstore)
        # Have to wrap this in a staticmethod decorator otherwise Python thinks
        # we're trying to create a new class method
        inner_cls._stop_patching = staticmethod(stop_patching)

    def tearDownClass(inner_cls):
        inner_cls._stop_patching()
        decorated_tearDownClass(inner_cls)

    cls.setUpClass = classmethod(setUpClass)
    cls.tearDownClass = classmethod(tearDownClass)
    return cls


def matrixstore_from_postgres():
    """
    Creates a MatrixStore SQLite database using data sourced from Postgres.
    This provides an easy way of using existing test fixtures with the
    MatrixStore.
    """
    latest_date = ImportLog.objects.latest("current_at").current_at
    end_date = str(latest_date)[:7]
    return matrixstore_from_data_factory(
        _DatabaseFixtures(), end_date=end_date, months=60
    )


class _DatabaseFixtures(object):
    """
    Presents the same attributes as a DataFactory instance but with data read
    from the database (where it was presumably loaded from test fixtures),
    rather than randomly generated.
    """

    def __init__(self):
        self.prescribing = [
            {<commit_msg>Fix MatrixStore test fixture importer<commit_after>import json

from frontend.models import (
    Practice,
    PracticeStatistics,
    Prescription,
    Presentation,
    ImportLog,
)
from matrixstore.tests.matrixstore_factory import (
    matrixstore_from_data_factory,
    patch_global_matrixstore,
)


def copy_fixtures_to_matrixstore(cls):
    """
    Decorator for TestCase classes which copies data from Postgres into an
    in-memory MatrixStore instance. This allows us to re-use database fixtures,
    and the tests designed to work with those fixtures, to test
    MatrixStore-powered code.
    """
    # These methods have been decorated with `@classmethod` so we need to use
    # `__func__` to get a reference to the original, undecorated method
    decorated_setUpClass = cls.setUpClass.__func__
    decorated_tearDownClass = cls.tearDownClass.__func__

    def setUpClass(inner_cls):
        decorated_setUpClass(inner_cls)
        matrixstore = matrixstore_from_postgres()
        stop_patching = patch_global_matrixstore(matrixstore)
        # Have to wrap this in a staticmethod decorator otherwise Python thinks
        # we're trying to create a new class method
        inner_cls._stop_patching = staticmethod(stop_patching)

    def tearDownClass(inner_cls):
        inner_cls._stop_patching()
        decorated_tearDownClass(inner_cls)

    cls.setUpClass = classmethod(setUpClass)
    cls.tearDownClass = classmethod(tearDownClass)
    return cls


def matrixstore_from_postgres():
    """
    Creates a MatrixStore SQLite database using data sourced from Postgres.
    This provides an easy way of using existing test fixtures with the
    MatrixStore.
    """
    latest_date = ImportLog.objects.latest_in_category("prescribing").current_at
    end_date = str(latest_date)[:7]
    return matrixstore_from_data_factory(
        _DatabaseFixtures(), end_date=end_date, months=60
    )


class _DatabaseFixtures(object):
    """
    Presents the same attributes as a DataFactory instance but with data read
    from the database (where it was presumably loaded from test fixtures),
    rather than randomly generated.
    """

    def __init__(self):
        self.prescribing = [
            {
{'fraction_non_alphanumeric': 0.06095064816920628, 'fraction_numerical': 0.001364566750056857, 'mean_word_length': 3.5480868665977248, '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': '4145319', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1221, 'n_words': 366}