<commit_before>import os
from unittest2 import TestCase
from dimagi.utils.django import cached_object
from dimagi.utils.django.cached_object import CachedObject, CachedImage, IMAGE_SIZE_ORDERING, OBJECT_ORIGINAL
import StringIO
import Image, ImageFont, ImageDraw


class FakeCache(object):
    def __init__(self):
        self.cache_dict = {}

    def get(self, key):
        return self.cache_dict.get(key, None)

    def keys(self, pattern):
        all_keys = self.cache_dict.keys()
        filtered = filter(lambda x: x.startswith(pattern[:-1]), all_keys)
        return filtered

    def set(self, key, value):
        self.cache_dict[key] = value


fake_cache = FakeCache()
cached_object.MOCK_REDIS_CACHE = fake_cache

class CachedObjectTests(TestCase):

    def SetUp(self):
        cached_object.MOCK_REDIS_CACHE = fake_cache

    def testBasicObjects(self):
        text = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
        filename = "something"
        buffer = StringIO.StringIO(text)

        obj = CachedObject(filename)

        self.assertFalse(obj.is_cached())
        metadata = {'content_type': 'text/plain'}
        obj.cache_put(buffer, metadata)

        self.assertTrue(obj.is_cached())
        cmeta, cstream = obj.get()
        self.assertEqual(cmeta['content_length'], len(text))
        self.assertEqual(cstream.read(), text)


    def _make_image(self, width=3001, height=2001):
        im = Image.new("RGB", (width, height), (0, 0, 0))
        buf = StringIO.StringIO()
        im.save(buf, "JPEG")
        buf.seek(0)
        return (im, buf)

    def testHugeImageObject(self):
        image, buffer = self._make_image()
        buffer.seek(0, os.SEEK_END)
        orig_size = buffer.tell()
        buffer.seek(0)

        cimage = CachedImage("test_huge")
        metadata = {'content_type': 'image/png'}
        cimage.cache_image(buffer, metadata)

        for size in IMAGE_SIZE_ORDERING:
            self.assertTrue(cimage.can_size(size))
<commit_msg>another jpg reference to remove<commit_after>import os
from unittest2 import TestCase
from dimagi.utils.django import cached_object
from dimagi.utils.django.cached_object import CachedObject, CachedImage, IMAGE_SIZE_ORDERING, OBJECT_ORIGINAL
import StringIO
import Image, ImageFont, ImageDraw


class FakeCache(object):
    def __init__(self):
        self.cache_dict = {}

    def get(self, key):
        return self.cache_dict.get(key, None)

    def keys(self, pattern):
        all_keys = self.cache_dict.keys()
        filtered = filter(lambda x: x.startswith(pattern[:-1]), all_keys)
        return filtered

    def set(self, key, value):
        self.cache_dict[key] = value


fake_cache = FakeCache()
cached_object.MOCK_REDIS_CACHE = fake_cache

class CachedObjectTests(TestCase):

    def SetUp(self):
        cached_object.MOCK_REDIS_CACHE = fake_cache

    def testBasicObjects(self):
        text = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
        filename = "something"
        buffer = StringIO.StringIO(text)

        obj = CachedObject(filename)

        self.assertFalse(obj.is_cached())
        metadata = {'content_type': 'text/plain'}
        obj.cache_put(buffer, metadata)

        self.assertTrue(obj.is_cached())
        cmeta, cstream = obj.get()
        self.assertEqual(cmeta['content_length'], len(text))
        self.assertEqual(cstream.read(), text)


    def _make_image(self, width=3001, height=2001):
        im = Image.new("RGB", (width, height), (0, 0, 0))
        buf = StringIO.StringIO()
        im.save(buf, "png")
        buf.seek(0)
        return (im, buf)

    def testHugeImageObject(self):
        image, buffer = self._make_image()
        buffer.seek(0, os.SEEK_END)
        orig_size = buffer.tell()
        buffer.seek(0)

        cimage = CachedImage("test_huge")
        metadata = {'content_type': 'image/png'}
        cimage.cache_image(buffer, metadata)

        for size in IMAGE_SIZE_ORDERING:
            self.assertTrue(cimage.can_size(size))

{'fraction_non_alphanumeric': 0.11014203837528033, 'fraction_numerical': 0.00797408422626464, 'mean_word_length': 3.026078234704112, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '7226589', 'n_tokens_mistral': 1280, 'n_tokens_neox': 1228, 'n_words': 258}