<commit_before>#-*- coding: utf-8 -*-
from django.core.files.base import ContentFile
from django.core.files.storage import get_storage_class
from health_check.backends.base import BaseHealthCheckBackend, ServiceUnavailable
import random
import datetime
import platform
py_majversion, py_minversion, py_revversion = platform.python_version_tuple()
if py_majversion == '2':
    basestring = basestring
else:
    basestring = (str,bytes)

class StorageHealthCheck(BaseHealthCheckBackend):
    """
    Tests the status of a StorageBakcend. Can be extended to test any storage backend by subclassing:

        class MyStorageHealthCheck(StorageHealthCheck):
            storage = 'some.other.StorageBackend'
        plugin_dir.register(MyStorageHealthCheck)

    storage must be either a string pointing to a storage class (e.g 'django.core.files.storage.FileSystemStorage') or
    a Storage instance.
    """
    storage = None

    def get_storage(self):
        if isinstance(self.storage, basestring):
            return get_storage_class(self.storage)()
        else:
            return self.storage

    def get_file_name(self):
        return 'health_check_storage_test/test-%s-%s.txt' % (datetime.datetime.now(), random.randint(10000,99999))

    def get_file_content(self):
        return b'this is the healthtest file content'

    def check_status(self):
        try:
            # write the file to the storage backend
            storage = self.get_storage()
            file_name = self.get_file_name()
            file_content = self.get_file_content()

            # save the file
            file_name = storage.save(file_name, ContentFile(content=file_content))
            # read the file and compare
            f = storage.open(file_name)
            if not storage.exists(file_name):
                raise ServiceUnavailable("File does not exist")

            read_file_contents = f.read()
            if not read_file_contents.decode("utf-8")  == file_content.decode("utf-8") :
                return ServiceUnavailable("File content doesn't match")
            # delete the file and make sure it is gone
            storage.delete(file_name)
            if storage.exists(file_name):
                return ServiceUnavailable("File was not deleted")
            return True
        except Exception as exc:
            return ServiceUnavailable("Unknown exception")<commit_msg>Defautl storage test was returning exceptions instead of raising them<commit_after>#-*- coding: utf-8 -*-
from django.core.files.base import ContentFile
from django.core.files.storage import get_storage_class
from health_check.backends.base import BaseHealthCheckBackend, ServiceUnavailable
import random
import datetime
import platform
py_majversion, py_minversion, py_revversion = platform.python_version_tuple()
if py_majversion == '2':
    basestring = basestring
else:
    basestring = (str,bytes)

class StorageHealthCheck(BaseHealthCheckBackend):
    """
    Tests the status of a StorageBakcend. Can be extended to test any storage backend by subclassing:

        class MyStorageHealthCheck(StorageHealthCheck):
            storage = 'some.other.StorageBackend'
        plugin_dir.register(MyStorageHealthCheck)

    storage must be either a string pointing to a storage class (e.g 'django.core.files.storage.FileSystemStorage') or
    a Storage instance.
    """
    storage = None

    def get_storage(self):
        if isinstance(self.storage, basestring):
            return get_storage_class(self.storage)()
        else:
            return self.storage

    def get_file_name(self):
        return 'health_check_storage_test/test-%s-%s.txt' % (datetime.datetime.now(), random.randint(10000,99999))

    def get_file_content(self):
        return b'this is the healthtest file content'

    def check_status(self):
        try:
            # write the file to the storage backend
            storage = self.get_storage()
            file_name = self.get_file_name()
            file_content = self.get_file_content()

            # save the file
            file_name = storage.save(file_name, ContentFile(content=file_content))
            # read the file and compare
            f = storage.open(file_name)
            if not storage.exists(file_name):
                raise ServiceUnavailable("File does not exist")

            read_file_contents = f.read()
            if not read_file_contents.decode("utf-8")  == file_content.decode("utf-8") :
                raise ServiceUnavailable("File content doesn't match")
            # delete the file and make sure it is gone
            storage.delete(file_name)
            if storage.exists(file_name):
                raise ServiceUnavailable("File was not deleted")

            return True
        except ServiceUnavailable as service_unavailable_exc:
            raise service_unavailable_exc
        except Exception as exc:
            raise ServiceUnavailable("Unknown exception")
{'fraction_non_alphanumeric': 0.07855411954765751, 'fraction_numerical': 0.005654281098546042, 'mean_word_length': 2.755117513267627, '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': '4199913', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1296, 'n_words': 364}