<commit_before>import sys
from datetime import datetime, timedelta
from dateutil.parser import parse
from django.conf import settings
from pillow_retry.models import PillowError, Stub
from django.test import TestCase
from pillow_retry.tasks import process_pillow_retry
from pillowtop.couchdb import CachedCouchDB
from pillowtop.listener import BasicPillow


def get_ex_tb(message, ex_class=None):
    ex_class = ex_class if ex_class else ExceptionA
    try:
        raise ex_class(message)
    except Exception as e:
        return e, sys.exc_info()[2]


def create_error(change, message='message', attempts=0, pillow=None, ex_class=None):
    error = PillowError.get_or_create(change, pillow or FakePillow())
    for n in range(0, attempts):
        error.add_attempt(*get_ex_tb(message, ex_class=ex_class))
    return error


class PillowRetryTestCase(TestCase):
    def setUp(self):
        PillowError.objects.all().delete()

    def test_id(self):
        id = '12345'
        change_dict = {'id': id, 'seq': 54321, 'changes': [{'_rev': 'abc123'}]}
        error = create_error(change_dict)
        self.assertEqual(error.doc_id, id)
        self.assertEqual(error.pillow, 'pillow_retry.tests.FakePillow')
        self.assertEqual(error.change_dict, change_dict)


    def test_attempts(self):
        message = 'ex message'
        error = create_error({'id': '123'}, message=message, attempts=1)
        self.assertEqual(error.total_attempts, 1)
        self.assertEqual(error.current_attempt, 1)
        self.assertTrue(message in error.error_traceback)
        self.assertEqual(error.error_type, 'pillow_retry.tests.ExceptionA')<commit_msg>force tests to use Change objects<commit_after>import sys
from datetime import datetime, timedelta
from dateutil.parser import parse
from django.conf import settings
from pillow_retry.models import PillowError, Stub
from django.test import TestCase
from pillow_retry.tasks import process_pillow_retry
from pillowtop.couchdb import CachedCouchDB
from pillowtop.feed.couch import change_from_couch_row
from pillowtop.feed.interface import Change
from pillowtop.listener import BasicPillow


def get_ex_tb(message, ex_class=None):
    ex_class = ex_class if ex_class else ExceptionA
    try:
        raise ex_class(message)
    except Exception as e:
        return e, sys.exc_info()[2]


def create_error(change, message='message', attempts=0, pillow=None, ex_class=None):
    if not isinstance(change, Change):
        change = change_from_couch_row(change)
    error = PillowError.get_or_create(change, pillow or FakePillow())
    for n in range(0, attempts):
        error.add_attempt(*get_ex_tb(message, ex_class=ex_class))
    return error


class PillowRetryTestCase(TestCase):
    def setUp(self):
        PillowError.objects.all().delete()

    def test_id(self):
        id = '12345'
        change_dict = {'id': id, 'seq': 54321, 'changes': [{'_rev': 'abc123'}]}
        error = create_error(change_dict)
        self.assertEqual(error.doc_id, id)
        self.assertEqual(error.pillow, 'pillow_retry.tests.FakePillow')
        self.assertEqual(error.change_dict, change_dict)


    def test_attempts(self):
        message = 'ex message'
        error = create_error({'id': '123'}, message=message, attempts=1)
        self.assertEqual(error.total_attempts, 1)
        self.assertEqual(error.current_attempt, 1)
        self.assertTrue(message in error.error_traceback)
        self.assertEqual(error.error_type, 'pillow_retry.tests.ExceptionA')
{'fraction_non_alphanumeric': 0.0984870111333143, 'fraction_numerical': 0.012560662289466172, 'mean_word_length': 4.041726618705036, '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': '4587062', 'n_tokens_mistral': 1121, 'n_tokens_neox': 1080, 'n_words': 256}