~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to wlimages/tests.py

  • Committer: Holger Rapp
  • Date: 2009-03-01 20:47:48 UTC
  • Revision ID: sirver@kallisto.local-20090301204748-h3ouqkp8zhv10ydq
First (working) commit of wlimages app, the app that will handle image uploading and managing. Uploading works now; images can only be uploaded once (with the same filename). Much stil missing. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python -tt
 
2
 
 
3
#!/usr/bin/env python -tt
 
4
# encoding: utf-8
 
5
#
 
6
# File: utests/test_wl_markdown.py
 
7
#
 
8
# Created by Holger Rapp on 2009-02-28.
 
9
# Copyright (c) 2009 HolgerRapp@gmx.net. All rights reserved.
 
10
#
 
11
# Last Modified: $Date$
 
12
#
 
13
 
 
14
# Since we want to include something from one path up, 
 
15
# we append the parent path to sys.path
 
16
import sys; sys.path.append("..")
 
17
 
 
18
import Image as PIL 
 
19
from cStringIO import StringIO
 
20
 
 
21
from django.test import TestCase
 
22
from django.contrib.contenttypes.models import ContentType 
 
23
from django.contrib.auth.models import User 
 
24
from django.core.files.uploadedfile import SimpleUploadedFile
 
25
from django.test.client import Client
 
26
from django.core.urlresolvers import reverse
 
27
from django.db import IntegrityError
 
28
 
 
29
from models import Image
 
30
from forms import UploadImageForm
 
31
 
 
32
import appsettings
 
33
 
 
34
from views import upload
 
35
 
 
36
class _TestUploadingBase(TestCase):
 
37
    @staticmethod
 
38
    def _make_new_uploaded_image(name,type="bmp"):
 
39
        sio = StringIO()
 
40
        i = PIL.new("RGB",(4,4))
 
41
 
 
42
        i.save(sio,type)
 
43
        
 
44
        return SimpleUploadedFile(name,sio.read(),content_type="image/%s" % type)
 
45
 
 
46
    def setUp(self):
 
47
        # We need some dummy objects
 
48
        # User
 
49
        self.u = User.objects.create(username="paul")
 
50
        # A Content type
 
51
        self.ct = ContentType.objects.create( app_label="test",model="TestModel")
 
52
 
 
53
        self.t1 = self._make_new_uploaded_image("test.png");
 
54
        self.t2 = self._make_new_uploaded_image("test.png");
 
55
        self.o1 = self._make_new_uploaded_image("othername.png");
 
56
       
 
57
        self.c = Client()
 
58
 
 
59
 
 
60
###########################################################################
 
61
#                  MODEL TESTS (need database, are slow)                  #
 
62
###########################################################################
 
63
class TestImages_TestModelAdding_ExceptCorrectResult(_TestUploadingBase):
 
64
    def runTest(self):
 
65
        self.assertFalse( Image.objects.has_image("test"))
 
66
        u = Image.objects.create( user=self.u, content_type=self.ct, object_id=1,
 
67
                name="test", revision=1)
 
68
        self.assertEqual( Image.objects.get( name="test",revision=1), u )
 
69
        self.assertTrue( Image.objects.has_image("test"))
 
70
class TestImages_TestModelAddingTwiceTheSameNameAndRevision_ExceptRaises(_TestUploadingBase):
 
71
    def runTest(self):
 
72
        u = Image.objects.create( user=self.u, content_type=self.ct, object_id=1,
 
73
                name="test", revision=1)
 
74
        self.assertRaises( Image.AlreadyExisting, Image.objects.create, **{"user":self.u, "content_type": self.ct, "object_id":1,
 
75
                                                                  "name": "test", "revision" :1 })
 
76
class TestImages_TestModelAddingTwiceTheSameNameDifferentRevision_ExceptRaises(_TestUploadingBase):
 
77
    def runTest(self):
 
78
        u = Image.objects.create( user=self.u, content_type=self.ct, object_id=1,
 
79
                name="test", revision=1)
 
80
        u = Image.objects.create( user=self.u, content_type=self.ct, object_id=1,
 
81
                name="test", revision=2)
 
82
        self.assertEqual(Image.objects.filter(name="test").count(),2)
 
83
 
 
84
###############
 
85
# Other Tests #
 
86
###############
 
87
# This test is not of much use
 
88
# class TestImages_TestUploadForm_ExceptCorrectResult(_TestUploadingBase):
 
89
#     def runTest(self):
 
90
#         form = UploadImageForm()
 
91
#         self.assertEqual( form.is_valid(), False )
 
92
 
 
93
if __name__ == '__main__':
 
94
    unittest.main()
 
95
    # k = TestWlMarkdown_WikiWordsInLink_ExceptCorrectResult()
 
96
    # unittest.TextTestRunner().run(k)
 
97