<commit_before>import os, time
from django.core.files.base import File
from django.db.models.fields.files import FileDescriptor
from django.db.utils import ProgrammingError
from django.test import TestCase

from smartfields.models import SmartfieldsModelMixin

from sample_app.models import ProcessorTestingModel, FilesTestingModel, ImageTestingModel, \
    DependencyTestingModel
from sample_app.utils import add_base, remove_folder_content


class ProcessingTestCase(TestCase):

    def test_model_setup(self):
        instance = ProcessorTestingModel()
        self.assertIsInstance(instance, SmartfieldsModelMixin)
        self.assertIsNotNone(getattr(instance, '_smartfields_managers', None))

    def test_individual_field_processing(self):
        instance = ProcessorTestingModel(field_1='foo bar')
        instance.smartfields_process(field_names=['field_3'])
        self.assertEqual(instance.field_2, "")
        self.assertEqual(instance.field_3, "foo-bar")
        self.assertEqual(instance.field_4, "foo-bar")
        # also tests SlugProcessor
        instance.smartfields_process(field_names=['field_1'])
        self.assertEqual(instance.field_1, "Foo_Bar")
        self.assertEqual(instance.field_2, "foo-bar")
        self.assertEqual(instance.field_3, "foo-bar")

    def test_processing_order(self):
        instance = ProcessorTestingModel(field_1='foo bar')
        instance.smartfields_process(field_names=['field_1', 'field_3'])
        self.assertEqual(instance.field_1, "Foo_Bar")
        self.assertEqual(instance.field_2, "foo-bar")
        self.assertEqual(instance.field_3, "foo_bar")
        instance = ProcessorTestingModel(field_1='foo bar')
        instance.smartfields_process(field_names=['field_3', 'field_1'])
        self.assertEqual(instance.field_1, "Foo_Bar")
        self.assertEqual(instance.field_2, "foo-bar")
        self.assertEqual(instance.field_3, "foo-bar") # different then above: _

    def test_file_field(self):
        instance = FilesTestingModel.objects.create()
        # test default static
        self.assertEqual(instance.field_1.url, "/static/defaults/foo.txt")
        self.assertEqual(instance.field_2_foo.url, "/static/defaults/foo.txt")
        self.assertEqual(instance.bar.url, "/static/defaults/bar.txt")
        # test assignment of file
        file = open(add_base("media/foo.txt"), 'w')
        file.write("foo bar")
        file.close()
        instance.field_1 = "foo.txt"
        instance.save()
        self.assertEqual(instance.field_1.url, "/media/foo.txt")
        # test deletion of file together with instance<commit_msg>made tests compatible with PY2 and PY3<commit_after>import os, time
from django.core.files.base import File
from django.db.models.fields.files import FileDescriptor
from django.db.utils import ProgrammingError
from django.test import TestCase
from django.utils import six

from smartfields.models import SmartfieldsModelMixin

from sample_app.models import ProcessorTestingModel, FilesTestingModel, ImageTestingModel, \
    DependencyTestingModel
from sample_app.utils import add_base, remove_folder_content


class ProcessingTestCase(TestCase):

    def test_model_setup(self):
        instance = ProcessorTestingModel()
        self.assertIsInstance(instance, SmartfieldsModelMixin)
        self.assertIsNotNone(getattr(instance, '_smartfields_managers', None))

    def test_individual_field_processing(self):
        instance = ProcessorTestingModel(field_1=six.text_type('foo bar'))
        instance.smartfields_process(field_names=['field_3'])
        self.assertEqual(instance.field_2, "")
        self.assertEqual(instance.field_3, "foo-bar")
        self.assertEqual(instance.field_4, "foo-bar")
        # also tests SlugProcessor
        instance.smartfields_process(field_names=['field_1'])
        self.assertEqual(instance.field_1, "Foo_Bar")
        self.assertEqual(instance.field_2, "foo-bar")
        self.assertEqual(instance.field_3, "foo-bar")

    def test_processing_order(self):
        instance = ProcessorTestingModel(field_1=six.text_type('foo bar'))
        instance.smartfields_process(field_names=['field_1', 'field_3'])
        self.assertEqual(instance.field_1, "Foo_Bar")
        self.assertEqual(instance.field_2, "foo-bar")
        self.assertEqual(instance.field_3, "foo_bar")
        instance = ProcessorTestingModel(field_1=six.text_type('foo bar'))
        instance.smartfields_process(field_names=['field_3', 'field_1'])
        self.assertEqual(instance.field_1, "Foo_Bar")
        self.assertEqual(instance.field_2, "foo-bar")
        self.assertEqual(instance.field_3, "foo-bar") # different then above: _

    def test_file_field(self):
        instance = FilesTestingModel.objects.create()
        # test default static
        self.assertEqual(instance.field_1.url, "/static/defaults/foo.txt")
        self.assertEqual(instance.field_2_foo.url, "/static/defaults/foo.txt")
        self.assertEqual(instance.bar.url, "/static/defaults/bar.txt")
        # test assignment of file
        file = open(add_base("media/foo.txt"), 'w')
        file.write("foo bar")
        file.close()
        instance.field_1 = "foo.txt"
        instance.save()
        self.assertEqual(instance.field_1.url, "/media/foo.txt")
        # test deletion of file together with instance
{'fraction_non_alphanumeric': 0.10954263128176171, 'fraction_numerical': 0.009787314135140222, 'mean_word_length': 3.8003613369467026, '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': '6414343', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1509, 'n_words': 267}