<commit_before># coding: utf-8

from __future__ import unicode_literals

from django.core.exceptions import ImproperlyConfigured
from django.core.files.storage import FileSystemStorage
from django.test import TestCase
from django.test.utils import override_settings

from .storages import get_storage


class FooStorage(object):
    pass


class StoragesTest(TestCase):

    @override_settings(GALLERY_FOO_STORAGE='gallery.test_storages.FooStorage')
    def test_get_storage(self):
        foo_storage = get_storage('foo')
        self.assertIsInstance(foo_storage, FooStorage)

    @override_settings(GALLERY_FOO_STORAGE='gallery.test_storages.FooStorage')
    def test_get_storage_caching(self):
        foo_storage_1 = get_storage('foo')
        foo_storage_2 = get_storage('foo')
        self.assertIs(foo_storage_1, foo_storage_2)

    @override_settings(GALLERY_FOO_DIR='/path/to/foo')
    def test_get_storage_legacy(self):
        foo_storage = get_storage('foo')
        self.assertIsInstance(foo_storage, FileSystemStorage)
        self.assertEqual(foo_storage.location, '/path/to/foo')

    def test_get_storage_unconfigured(self):
        with self.assertRaises(ImproperlyConfigured):
            get_storage('foo')
<commit_msg>Implement an in-memory storage class for tests.<commit_after># coding: utf-8

from __future__ import unicode_literals

import io

from django.core.exceptions import ImproperlyConfigured
from django.core.files.storage import FileSystemStorage, Storage
from django.test import TestCase
from django.test.utils import override_settings

from .storages import get_storage


class MemoryStorage(Storage):
    """
    Limited implementation of an in-memory file storage.

    This class does the bare minimum for tests to pass. It doesn't implement
    accessed/created/modified_time.

    """
    def __init__(self, *args, **kwargs):
        super(MemoryStorage, self).__init__(*args, **kwargs)
        self.files = {}

    def _open(self, name, mode='rb'):
        assert mode == 'rb'
        return io.BytesIO(self.files[name])

    def _save(self, name, content):
        content = content.read()
        assert isinstance(content, bytes)
        self.files[name] = content
        return name

    def delete(self, name):
        self.files.pop(name, None)

    def exists(self, name):
        return name in self.files

    def listdir(self, name):
        dirs, files = [], []
        for filename in sorted(self.files):
            filename = filename[len(name):]
            if '/' in key:
                dirs.append(filename.partition('/')[0])
            else:
                files.append(filename)
        return dirs, files

    def size(self, name):
        return len(self.files[name])


class StoragesTest(TestCase):

    @override_settings(GALLERY_FOO_STORAGE='gallery.test_storages.MemoryStorage')
    def test_get_storage(self):
        foo_storage = get_storage('foo')
        self.assertIsInstance(foo_storage, MemoryStorage)

    @override_settings(GALLERY_FOO_STORAGE='gallery.test_storages.MemoryStorage')
    def test_get_storage_caching(self):
        foo_storage_1 = get_storage('foo')
        foo_storage_2 = get_storage('foo')
        self.assertIs(foo_storage_1, foo_storage_2)

    @override_settings(GALLERY_FOO_DIR='/path/to/foo')
    def test_get_storage_legacy(self):
        foo_storage = get_storage('foo')
        self.assertIsInstance(foo_storage, FileSystemStorage)
        self.assertEqual(foo_storage.location, '/path/to/foo')

    def test_get_storage_unconfigured(self):
        with self.assertRaises(ImproperlyConfigured):
            get_storage('foo')

{'fraction_non_alphanumeric': 0.0968363136176066, 'fraction_numerical': 0.003026134800550206, 'mean_word_length': 3.5223880597014925, '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': '5736637', 'n_tokens_mistral': 1119, 'n_tokens_neox': 1086, 'n_words': 220}