~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to wlmaps/tests/test_views.py

  • Committer: Holger Rapp
  • Date: 2012-03-17 16:22:06 UTC
  • Revision ID: sirver@gmx.de-20120317162206-fgttamk22qt1nytj
Let post count be calculated automatically instead of keeping track of it manually. Let's see how this affects performance

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
from django.test import TestCase as DjangoTest, Client
6
6
from django.core.urlresolvers import reverse
7
7
from django.contrib.auth.models import User
8
 
# NOCOMM: Not used, but should be replaced with python json because it gets removed in django 1.7
9
 
#from django.utils import simplejson as json
 
8
from django.utils import simplejson as json
10
9
from wlmaps.models import *
11
10
 
12
11
import os
23
22
        u = User.objects.create(username="root", email="root@root.com")
24
23
        u.set_password("root")
25
24
        u.save()
26
 
 
 
25
        
27
26
        self.client.login( username="root", password="root")
28
27
 
29
28
        self.user = u
37
36
class TestWLMaps_ValidUpload_ExceptCorrectResult(_LoginToSite):
38
37
    def runTest(self):
39
38
        url = reverse('wlmaps_upload')
40
 
        c = self.client.post(url, {'file': open(elven_forests,"rb"), 'test': True })
 
39
        k = self.client.post(url, {'mapfile': open(elven_forests,"rb"), 'test': True })
 
40
        rv = json.loads( k.content )
 
41
        self.assertEqual(rv["success_code"], 0)
 
42
        self.assertEqual(rv["map_id"], 1)
41
43
 
42
44
        o = Map.objects.get(pk=1)
43
45
        self.assertEqual(o.name, "Elven Forests")
45
47
class TestWLMaps_AnonUpload_ExceptRedirect(DjangoTest):
46
48
    def runTest(self):
47
49
        url = reverse('wlmaps_upload')
48
 
        k = self.client.post(url, {'file': open(elven_forests,"rb") })
 
50
        k = self.client.post(url, {'mapfile': open(elven_forests,"rb") })
49
51
        self.assertRedirects( k, reverse('django.contrib.auth.views.login') + '?next=%s' %url )
50
 
 
 
52
class TestWLMaps_UploadGet_ExceptNotAllowed(_LoginToSite):
 
53
    def runTest(self):
 
54
        k = self.client.get(reverse('wlmaps_upload'))
 
55
        self.assertEqual( k.status_code, 405 )
 
56
        self.assertEqual( k["allow"], 'post' )
 
57
    
51
58
 
52
59
# Invalid Uploading
53
60
class TestWLMaps_UploadWithoutMap_ExceptError(_LoginToSite):
54
61
    def runTest(self):
55
62
        url = reverse('wlmaps_upload')
56
63
        k = self.client.post(url, {'test': True })
57
 
        self.assertEqual(len(Map.objects.all()), 0)
 
64
        rv = json.loads( k.content )
 
65
        self.assertEqual(rv["success_code"], 1)
58
66
class TestWLMaps_UploadTwice_ExceptCorrectResult(_LoginToSite):
59
67
    def runTest(self):
60
68
        url = reverse('wlmaps_upload')
61
 
        self.client.post(url, {'file': open(elven_forests,"rb"), 'test': True })
62
 
        self.assertEqual(len(Map.objects.all()), 1)
63
 
 
64
 
        self.client.post(url, {'file': open(elven_forests,"rb"), 'test': True })
65
 
        self.assertEqual(len(Map.objects.all()), 1)
 
69
        k = self.client.post(url, {'mapfile': open(elven_forests,"rb"), 'test': True })
 
70
        rv = json.loads( k.content )
 
71
        self.assertEqual(rv["success_code"], 0)
 
72
        self.assertEqual(rv["map_id"], 1)
 
73
        
 
74
        k = self.client.post(url, {'mapfile': open(elven_forests,"rb"), 'test': True })
 
75
        rv = json.loads( k.content )
 
76
        self.assertEqual(rv["success_code"], 2)
66
77
class TestWLMaps_UploadWithInvalidMap_ExceptError(_LoginToSite):
67
78
    def runTest(self):
68
79
        url = reverse('wlmaps_upload')
69
 
        self.client.post(url, {'file': open(__file__,"rb"), 'test': True })
70
 
        self.assertEqual(len(Map.objects.all()), 0)
71
 
 
72
 
# Viewing
 
80
        k = self.client.post(url, {'mapfile': open(__file__,"rb"), 'test': True })
 
81
        rv = json.loads( k.content )
 
82
        self.assertEqual(rv["success_code"], 3)
 
83
       
 
84
# Viewing 
73
85
class TestWLMapsViews_Viewing(DjangoTest):
74
86
    def setUp(self):
75
87
        self.user = User.objects.create(username="testuser")
76
88
        self.user.save()
77
 
 
 
89
        
78
90
        # Add maps
79
91
        nm = Map.objects.create(
80
92
                        name = "Map",
82
94
                        w = 128,
83
95
                        h = 64,
84
96
                        nr_players = 4,
85
 
                        descr = "a good map to play with",
 
97
                        descr = "a good map to play with", 
86
98
                        minimap = "/wlmaps/minimaps/Map.png",
87
99
                        world_name = "blackland",
88
100
 
97
109
                        w = 128,
98
110
                        h = 64,
99
111
                        nr_players = 4,
100
 
                        descr = "a good map to play with",
 
112
                        descr = "a good map to play with", 
101
113
                        minimap = "/wlmaps/minimaps/Map with long slug.png",
102
114
                        world_name = "blackland",
103
115
 
106
118
        )
107
119
        nm.save()
108
120
        self.map1 = nm
109
 
 
 
121
    
110
122
    def test_ViewingValidMap_ExceptCorrectResult(self):
111
123
        c = self.client.get(reverse("wlmaps_view",args=("map-with-a-long-slug",)))
112
124
        self.assertEqual(c.status_code, 200 )
116
128
    def test_ViewingNonExistingMap_Except404(self):
117
129
        c = self.client.get(reverse("wlmaps_view",args=("a-map-that-doesnt-exist",)))
118
130
        self.assertEqual(c.status_code, 404 )
119
 
 
120
 
 
121
 
############
122
 
#  RATING  #
123
 
############
 
131
    
 
132
##########
 
133
# RATING #
 
134
##########
124
135
class TestWLMapsViews_Rating(_LoginToSite):
125
136
    def setUp(self):
126
137
        _LoginToSite.setUp(self)
132
143
                        w = 128,
133
144
                        h = 64,
134
145
                        nr_players = 4,
135
 
                        descr = "a good map to play with",
 
146
                        descr = "a good map to play with", 
136
147
                        minimap = "/wlmaps/minimaps/Map.png",
137
148
                        world_name = "blackland",
138
149
 
147
158
            reverse("wlmaps_rate",args=("a-map-that-doesnt-exist",)),
148
159
            { "vote": 10 } )
149
160
        self.assertEqual(c.status_code, 404 )
150
 
 
 
161
    
151
162
    def test_RatingGet_Except405(self):
152
163
        c = self.client.get(
153
164
            reverse("wlmaps_rate",args=("map",)),
154
165
            { "vote": 10 } )
155
166
        self.assertEqual(c.status_code, 405 )
156
 
 
 
167
    
157
168
    def test_RatingInvalidValue_Except400(self):
158
169
        c = self.client.post(
159
170
            reverse("wlmaps_rate",args=("map",)),
160
171
            { "vote": 11 } )
161
172
        self.assertEqual(c.status_code, 400 )
162
 
 
 
173
    
163
174
    def test_RatingNonIntegerValue_Except400(self):
164
175
        c = self.client.post(
165
176
            reverse("wlmaps_rate",args=("map",)),
166
177
            { "vote": "shubidu" } )
167
178
        self.assertEqual(c.status_code, 400 )
168
 
 
 
179
    
169
180
    def test_RatingExistingMap_ExceptCorrectResult(self):
170
181
        c = self.client.post(
171
182
            reverse("wlmaps_rate",args=("map",)),
179
190
 
180
191
        self.assertEqual(m.rating.votes, 1)
181
192
        self.assertEqual(m.rating.score, 7)
182
 
 
 
193
    
183
194
 
184
195
 
185
196