<commit_before>from __future__ import absolute_import
from random import randint

from uuid import uuid4

from autofixture import AutoFixture
from django.test import TestCase
from rest_framework import serializers

from .. import tasks
from ..exceptions import ModelTaskError
from .models import SimpleModel, NonAutoPrimaryKeyModel, PartialUpdateModel


def get_model_dict(model):
    result = model.__dict__.copy()
    del result['_state']
    return result


class BaseTaskTests(TestCase):

    MODEL_SYMBOL = 'celery_rpc.tests.models:SimpleModel'

    def setUp(self):
        self.models = AutoFixture(SimpleModel).create(5)


class FilterTaskTests(BaseTaskTests):

    def testLimit(self):
        r = tasks.filter.delay(self.MODEL_SYMBOL)
        self.assertEquals(5, len(r.get()))

        r = tasks.filter.delay(self.MODEL_SYMBOL, limit=2)
        self.assertEquals(2, len(r.get()))

    def testOffset(self):
        r = tasks.filter.delay(self.MODEL_SYMBOL, offset=1)
        expected = get_model_dict(self.models[1])
        self.assertEquals(expected, r.get()[0])

    def testFilters(self):
        expected = get_model_dict(self.models[0])
        r = tasks.filter.delay(self.MODEL_SYMBOL,
                               filters={'pk': expected['id']})
        self.assertEquals(expected, r.get()[0])

    def testSerializerFields(self):
        expected = get_model_dict(self.models[0])
        field = expected.keys()[0]
        r = tasks.filter.delay(self.MODEL_SYMBOL,
                               filters={'pk': expected['id']},
                               fields=[field])
        self.assertEquals({field: expected[field]}, r.get()[0])

    def testOrdering(self):
        self.models[0].char = 'a'
        self.models[0].save()

        self.models[1].char = 'b'
        self.models[1].save()

        r = tasks.filter.delay(self.MODEL_SYMBOL,
                               filters={'char__in': ['a', 'b']},
                               order_by=['char'])
        self.assertEquals(['a', 'b'], [item['char'] for item in r.get()])

    def testRevertOrdering(self):
        self.models[0].char = 'a'
        self.models[0].save()

        self.models[1].char = 'b'
        self.models[1].save()

        r = tasks.filter.delay(self.MODEL_SYMBOL,
                               filters={'char__in': ['a', 'b']},
                               order_by='-char')
        self.assertEquals(['b', 'a'], [item['char'] for item in r.get()])


class SimpleTaskSerializer(serializers.ModelSerializer):
    """ Test serializer
    """
    class Meta:
        model = SimpleModel
        fields = ('id', )


class SingleObjectsDoesNotExistMixin(object):
    """ Checks behavior of tasks, which modify existing objects.
    """

    def testSingleObjectDoesNotExist(self):<commit_msg>rename testRevertOrdering  -> testReverseOrdering<commit_after>from __future__ import absolute_import
from random import randint

from uuid import uuid4

from autofixture import AutoFixture
from django.test import TestCase
from rest_framework import serializers

from .. import tasks
from ..exceptions import ModelTaskError
from .models import SimpleModel, NonAutoPrimaryKeyModel, PartialUpdateModel


def get_model_dict(model):
    result = model.__dict__.copy()
    del result['_state']
    return result


class BaseTaskTests(TestCase):

    MODEL_SYMBOL = 'celery_rpc.tests.models:SimpleModel'

    def setUp(self):
        self.models = AutoFixture(SimpleModel).create(5)


class FilterTaskTests(BaseTaskTests):

    def testLimit(self):
        r = tasks.filter.delay(self.MODEL_SYMBOL)
        self.assertEquals(5, len(r.get()))

        r = tasks.filter.delay(self.MODEL_SYMBOL, limit=2)
        self.assertEquals(2, len(r.get()))

    def testOffset(self):
        r = tasks.filter.delay(self.MODEL_SYMBOL, offset=1)
        expected = get_model_dict(self.models[1])
        self.assertEquals(expected, r.get()[0])

    def testFilters(self):
        expected = get_model_dict(self.models[0])
        r = tasks.filter.delay(self.MODEL_SYMBOL,
                               filters={'pk': expected['id']})
        self.assertEquals(expected, r.get()[0])

    def testSerializerFields(self):
        expected = get_model_dict(self.models[0])
        field = expected.keys()[0]
        r = tasks.filter.delay(self.MODEL_SYMBOL,
                               filters={'pk': expected['id']},
                               fields=[field])
        self.assertEquals({field: expected[field]}, r.get()[0])

    def testOrdering(self):
        self.models[0].char = 'a'
        self.models[0].save()

        self.models[1].char = 'b'
        self.models[1].save()

        r = tasks.filter.delay(self.MODEL_SYMBOL,
                               filters={'char__in': ['a', 'b']},
                               order_by=['char'])
        self.assertEquals(['a', 'b'], [item['char'] for item in r.get()])

    def testReverseOrdering(self):
        self.models[0].char = 'a'
        self.models[0].save()

        self.models[1].char = 'b'
        self.models[1].save()

        r = tasks.filter.delay(self.MODEL_SYMBOL,
                               filters={'char__in': ['a', 'b']},
                               order_by='-char')
        self.assertEquals(['b', 'a'], [item['char'] for item in r.get()])


class SimpleTaskSerializer(serializers.ModelSerializer):
    """ Test serializer
    """
    class Meta:
        model = SimpleModel
        fields = ('id', )


class SingleObjectsDoesNotExistMixin(object):
    """ Checks behavior of tasks, which modify existing objects.
    """

    def testSingleObjectDoesNotExist(self):
{'fraction_non_alphanumeric': 0.13233989710839097, 'fraction_numerical': 0.007450771687067589, 'mean_word_length': 2.5018633540372672, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '901597', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1643, 'n_words': 327}