Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/webapp/utils/test.py
blob: c2faac7c6d6b8ac6e9729d24fc264afae61c8daf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# pylint: disable=C0111
import StringIO
import Image
import shutil
import glob
import os

from django.conf import settings
from django.test import TestCase
from django.core.files.uploadedfile import InMemoryUploadedFile

from utils.mongo_connection import connect, disconnect


disconnect()


class MongoTestCase(TestCase):  # pylint: disable=R0904

    def __init__(self, methodName='runtest'):
        db_name = 'test_%s' % settings.MONGO_SETTINGS['NAME']
        self.connection = connect(
            db_name,
            settings.MONGO_SETTINGS['ALIAS'],
            host=settings.MONGO_SETTINGS['HOST'],
            port=settings.MONGO_SETTINGS['PORT'],
        )
        self.db = self.connection[db_name]
        super(MongoTestCase, self).__init__(methodName)

    def _post_teardown(self):
        for collection_name in self.db.collection_names():
            if collection_name != 'system.indexes':
                self.db.drop_collection(collection_name)

        super(MongoTestCase, self)._post_teardown()


def mock_text_file():
    io = StringIO.StringIO()
    io.write('foo')
    text_file = InMemoryUploadedFile(io, None, 'foo.txt', 'text', io.len, None)
    text_file.seek(0)
    return text_file


def mock_image_file(format="jpeg", size=(200, 200)):
    io = StringIO.StringIO()
    color = (255, 0, 0, 0)
    image = Image.new("RGBA", size, color)
    image.save(io, format=format.upper())
    io.seek(0)
    return io


def mock_in_memory_image(name='foo.jpg', format="jpeg", size=(200, 200)):
    io = mock_image_file(format, size)
    content_type = "Image/%s" % format.lower()
    image_in_memory = InMemoryUploadedFile(
        io, None, name, content_type, io.len, None
    )
    image_in_memory.seek(0)
    return image_in_memory


def remove_option_images_dir():
    shutil.rmtree(settings.IMAGE_OPTIONS_ROOT, ignore_errors=True)


def empty_results_dir():
    results_dir_contents = glob.glob(settings.RESULT_BCK_ROOT + "/*")
    for i in results_dir_contents:
        shutil.rmtree(i, ignore_errors=True)


def create_results_dir():
    dir_ = settings.RESULT_BCK_ROOT
    try:
        os.mkdir(dir_)
    except OSError:
        pass