<commit_before>from django.conf import settings
from django.test import TestCase
from nose.tools import assert_in

from corehq.motech.const import BASIC_AUTH
from corehq.motech.models import ConnectionSettings

from ..models import FormRepeater, get_all_repeater_types
from ..repeater_generators import FormRepeaterXMLPayloadGenerator

DOMAIN = "greasy-spoon"


def test_get_all_repeater_types():
    types = get_all_repeater_types()
    for cls in settings.REPEATER_CLASSES:
        name = cls.split('.')[-1]
        assert_in(name, types)


class RepeaterConnectionSettingsTests(TestCase):

    def setUp(self):
        self.rep = FormRepeater(
            domain=DOMAIN,
            url="https://spam.example.com/api/",
            auth_type=BASIC_AUTH,
            username="terry",
            password="<PASSWORD>!",
            notify_addresses_str="<EMAIL>",
            format=FormRepeaterXMLPayloadGenerator.format_name,
        )

    def tearDown(self):
        if self.rep.connection_settings_id:
            ConnectionSettings.objects.filter(
                pk=self.rep.connection_settings_id
            ).delete()
        self.rep.delete()

    def test_create_connection_settings(self):
        self.assertIsNone(self.rep.connection_settings_id)
        conn = self.rep.connection_settings

        self.assertIsNotNone(self.rep.connection_settings_id)
        self.assertEqual(conn.name, self.rep.url)
        self.assertEqual(self.rep.plaintext_password, conn.plaintext_password)
        # rep.password was saved decrypted; conn.password is not:
        self.assertNotEqual(self.rep.password, conn.password)<commit_msg>⚖ Test repeat record ordering<commit_after>from django.conf import settings
from django.test import TestCase

from nose.tools import assert_in

from corehq.motech.const import BASIC_AUTH
from corehq.motech.models import ConnectionSettings

from ..models import FormRepeater, SQLRepeaterStub, get_all_repeater_types
from ..repeater_generators import FormRepeaterXMLPayloadGenerator


def test_get_all_repeater_types():
    types = get_all_repeater_types()
    for cls in settings.REPEATER_CLASSES:
        name = cls.split('.')[-1]
        assert_in(name, types)


class RepeaterConnectionSettingsTests(TestCase):

    def setUp(self):
        self.rep = FormRepeater(
            domain="greasy-spoon",
            url="https://spam.example.com/api/",
            auth_type=BASIC_AUTH,
            username="terry",
            password="<PASSWORD>!",
            notify_addresses_str="<EMAIL>",
            format=FormRepeaterXMLPayloadGenerator.format_name,
        )

    def tearDown(self):
        if self.rep.connection_settings_id:
            ConnectionSettings.objects.filter(
                pk=self.rep.connection_settings_id
            ).delete()
        self.rep.delete()

    def test_create_connection_settings(self):
        self.assertIsNone(self.rep.connection_settings_id)
        conn = self.rep.connection_settings

        self.assertIsNotNone(self.rep.connection_settings_id)
        self.assertEqual(conn.name, self.rep.url)
        self.assertEqual(self.rep.plaintext_password, conn.plaintext_password)
        # rep.password was saved decrypted; conn.password is not:
        self.assertNotEqual(self.rep.password, conn.password)


class TestSQLRepeatRecordOrdering(TestCase):

    def setUp(self):
        self.couch_repeater = FormRepeater(
            domain='eden',
            url='https://spam.example.com/api/',
        )
        self.couch_repeater.save()
        self.sql_repeater = SQLRepeaterStub.objects.create(
            domain='eden',
            couch_id=self.couch_repeater.get_id,
        )
        self.sql_repeater.repeat_records.create(
            domain=self.sql_repeater.domain,
            payload_id='eve',
            registered_at='1970-02-01',
        )

    def tearDown(self):
        self.sql_repeater.delete()
        self.couch_repeater.delete()

    def test_earlier_record_created_later(self):
        self.sql_repeater.repeat_records.create(
            domain=self.sql_repeater.domain,
            payload_id='lilith',
            # If Unix time starts on 1970-01-01, then I guess 1970-01-06
            # is Unix <NAME>, the sixth day of Creation, the day
            # [Lilith][1] and Adam were created from clay.
            # [1] https://en.wikipedia.org/wiki/Lilith
            registered_at='1970-01-06',
        )
        repeat_records = self.sql_repeater.repeat_records.all()
        self.assertEqual(repeat_records[0].payload_id, 'lilith')
        self.assertEqual(repeat_records[1].payload_id, 'eve')

    def test_later_record_created_later(self):
        self.sql_repeater.repeat_records.create(
            domain=self.sql_repeater.domain,
            payload_id='cain',
            registered_at='1995-01-06',
        )
        repeat_records = self.sql_repeater.repeat_records.all()
        self.assertEqual(repeat_records[0].payload_id, 'eve')
        self.assertEqual(repeat_records[1].payload_id, 'cain')
{'fraction_non_alphanumeric': 0.09685759745425616, 'fraction_numerical': 0.00954653937947494, 'mean_word_length': 2.7585949177877427, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 4, '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': '3860403', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1512, 'n_words': 242}