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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
#!/usr/bin/python -tt
#!/usr/bin/env python -tt
# encoding: utf-8
#
# File: utests/test_wl_markdown.py
#
# Created by Holger Rapp on 2009-02-28.
# Copyright (c) 2009 HolgerRapp@gmx.net. All rights reserved.
#
# Last Modified: $Date$
#
# Since we want to include something from one path up,
# we append the parent path to sys.path
import sys
sys.path.append('..')
import PIL
from cStringIO import StringIO
from django.test import TestCase
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test.client import Client
from django.urls import reverse
from django.db import IntegrityError
from models import Image
from forms import UploadImageForm
from views import upload
class _TestUploadingBase(TestCase):
@staticmethod
def _make_new_uploaded_image(name, type='bmp'):
sio = StringIO()
i = PIL.Image.new('RGB', (4, 4))
i.save(sio, type)
return SimpleUploadedFile(name, sio.read(), content_type='image/%s' % type)
def setUp(self):
# We need some dummy objects
# User
self.u = User.objects.create(username='paul')
# A Content type
self.ct = ContentType.objects.create(
app_label='test', model='TestModel')
self.t1 = self._make_new_uploaded_image('test.png')
self.t2 = self._make_new_uploaded_image('test.png')
self.o1 = self._make_new_uploaded_image('othername.png')
self.c = Client()
###########################################################################
# MODEL TESTS (need database, are slow) #
###########################################################################
class TestImages_TestModelAdding_ExceptCorrectResult(_TestUploadingBase):
def runTest(self):
self.assertFalse(Image.objects.has_image('test'))
u = Image.objects.create(user=self.u, content_type=self.ct, object_id=1,
name='test', revision=1)
self.assertEqual(Image.objects.get(name='test', revision=1), u)
self.assertTrue(Image.objects.has_image('test'))
class TestImages_TestModelAddingTwiceTheSameNameAndRevision_ExceptRaises(_TestUploadingBase):
def runTest(self):
u = Image.objects.create(user=self.u, content_type=self.ct, object_id=1,
name='test', revision=1)
self.assertRaises(Image.AlreadyExisting, Image.objects.create, **{'user': self.u, 'content_type': self.ct, 'object_id': 1,
'name': 'test', 'revision': 1})
class TestImages_TestModelAddingTwiceTheSameNameDifferentRevision_ExceptRaises(_TestUploadingBase):
def runTest(self):
u = Image.objects.create(user=self.u, content_type=self.ct, object_id=1,
name='test', revision=1)
u = Image.objects.create(user=self.u, content_type=self.ct, object_id=1,
name='test', revision=2)
self.assertEqual(Image.objects.filter(name='test').count(), 2)
###############
# Other Tests #
###############
# This test is not of much use
# class TestImages_TestUploadForm_ExceptCorrectResult(_TestUploadingBase):
# def runTest(self):
# form = UploadImageForm()
# self.assertEqual( form.is_valid(), False )
if __name__ == '__main__':
unittest.main()
# k = TestWlMarkdown_WikiWordsInLink_ExceptCorrectResult()
# unittest.TextTestRunner().run(k)
|