<commit_before>from datetime import datetime, date, time, timedelta

from django.test import TestCase

from speeches.models import Section, Speech
from instances.models import Instance


class ParentInstanceMismatchError(Exception):
    pass


def create_sections( subsections, parent=None, instance=None):
    """
    Create a hierachy of sections and speeches. Very useful for setting up test data as needed.

    Note - used in external packages (pombola.south_africa), alter with care.

    create_sections([
        {
            'title': "Top level section",
            'subsections': [
                {   'title': "Nested section",
                    'subsections': [
                        {   'title': "Section with speeches",
                            'speeches': [ 4, date(2013, 3, 25), time(9, 0) ],
                        },
                        {   'title': "<NAME>",
                            'speeches': [ 2, date(2013, 3, 25), time(12, 0) ],
                        },
                    ]
                },
                {
                    'title': "Another nested section (but completely empty)",
                    'subsections': []
                },
            ]
        },
    ])
    """

    # If given an instance and a parent check that they are compatible - we don't
    # want to end up with a hierarchy that spans instances and this would probably
    # mean that there is some confusion upstream that we should highlight.
    if instance and parent and instance != parent.instance:
        raise ParentInstanceMismatchError("The instance and parent.instance do not match")

    # If we have a parent then use the instance from that.
    if parent:
        instance = parent.instance

    # If we don't have an instance (which also means we had no parent) then create
    # one - this is very convenient in the test scripts.
    if not instance:
        instance, _ = Instance.objects.get_or_create(label='create-sections-instance')

    for subsection in subsections:
        s = Section.objects.create( instance=instance, title=subsection['title'], parent=parent )
        if 'subsections' in subsection:
            create_sections(subsection['subsections'], parent=s)
        if 'speeches' in subsection:
            num, d, t = subsection['speeches']
            for i in range(0, num):
                Speech.objects.create(
                    instance = instance,
                    section = s,
                    text = 'rhubarb rhubarb',
                    start_date = d,
                    start_time = t,
                )
                if t:
                    t = (datetime.combine(date.today(), t) + timedelta(minutes=10)).time()


class CeateSectionsTests(TestCase):
    def test_parent_instance_mismatch(self):
        foo_instance = Instance.objects.create(label="foo")
        bar_instance = Instance.objects.create(label="bar")<commit_msg>Let the create_sections test helper create private speeches<commit_after>from datetime import datetime, date, time, timedelta

from django.test import TestCase

from speeches.models import Section, Speech
from instances.models import Instance


class ParentInstanceMismatchError(Exception):
    pass


def create_sections( subsections, parent=None, instance=None):
    """
    Create a hierachy of sections and speeches. Very useful for setting up test data as needed.

    Note - used in external packages (pombola.south_africa), alter with care.

    create_sections([
        {
            'title': "Top level section",
            'subsections': [
                {   'title': "Nested section",
                    'subsections': [
                        {   'title': "Section with speeches",
                            'speeches': [ 4, date(2013, 3, 25), time(9, 0) ],
                        },
                        {   'title': "<NAME>",
                            'speeches': [ 2, date(2013, 3, 25), time(12, 0) ],
                        },
                    ]
                },
                {
                    'title': "Another nested section (but completely empty)",
                    'subsections': []
                },
            ]
        },
    ])
    """

    # If given an instance and a parent check that they are compatible - we don't
    # want to end up with a hierarchy that spans instances and this would probably
    # mean that there is some confusion upstream that we should highlight.
    if instance and parent and instance != parent.instance:
        raise ParentInstanceMismatchError("The instance and parent.instance do not match")

    # If we have a parent then use the instance from that.
    if parent:
        instance = parent.instance

    # If we don't have an instance (which also means we had no parent) then create
    # one - this is very convenient in the test scripts.
    if not instance:
        instance, _ = Instance.objects.get_or_create(label='create-sections-instance')

    for subsection in subsections:
        s = Section.objects.create( instance=instance, title=subsection['title'], parent=parent )
        if 'subsections' in subsection:
            create_sections(subsection['subsections'], parent=s)
        if 'speeches' in subsection:
            # If there's a 4th element in speeches, that's a boolean
            # indicating whether the speeches should be public:
            speeches_details = subsection['speeches']
            if len(speeches_details) >= 4:
                public = speeches_details[3]
            else:
                public = True
            num, d, t = speeches_details[:3]
            for i in range(0, num):
                Speech.objects.create(
                    instance = instance,
                    section = s,
                    text = 'rhubarb rhubarb',
                    start_date = d,
                    start_time = t,
                    public = public,
                )
                if t:
                    t = (datetime.combine(date.today(), t) + timedelta(minutes=10)).time()


class CeateSectionsTests(TestCase):
    def test_parent_instance_mismatch(self):
        foo_instance = Instance.objects.create(label="foo")
        bar_instance = Instance.objects.create(label="bar")
{'fraction_non_alphanumeric': 0.0891025641025641, 'fraction_numerical': 0.008333333333333333, 'mean_word_length': 1.8188798554652212, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '891557', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1500, 'n_words': 521}