aboutsummaryrefslogtreecommitdiff
blob: e6357705e942b98ac49b67deafefe0ad05e4104f (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""

from django.test import TestCase
from django.db import models
from django.test.client import RequestFactory
from django.template import Template, Context, TemplateSyntaxError
from django.http import Http404
from django.conf.urls import url, patterns
from django.http import HttpResponse
from django.core.urlresolvers import reverse
from operator import attrgetter
from . import views
from .templatetags import active_link, paginator
import re
import datetime

def test_view(request, *args, **kwargs):
    return HttpResponse('')

urlpatterns = patterns('',
    url(r'^test/$', test_view, name = 'test'),
    url(r'^test2/(?P<extra>extra)(?P<args>args)$', test_view, name = 'test2'),
)

class Tag(models.Model):
    name = models.CharField(unique = True, max_length = 40)

    def __unicode__(self):
        return self.name

class Messages(models.Model):
    title = models.CharField(max_length = 120)
    text = models.TextField()
    published = models.BooleanField(default = False)
    created_datetime = models.DateTimeField(auto_now_add = True)
    updated_datetime = models.DateTimeField(auto_now = True)
    tags = models.ManyToManyField(Tag, blank = True)

    def __unicode__(self):
        return self.title

class TestingViewMixin(object):
    
    def init_test_args(self, request, *args, **kwargs):
        self.request = request
        self.args = args
        self.kwargs = kwargs

    def get_queryset(self):
        return self.queryset

    def get_context_data(self, **kwargs):
        dct = kwargs.copy()
        return dct

class MessagesFilter(views.MultipleFilterListViewMixin, TestingViewMixin):
    queryset = Messages.objects.all()
    m2m_filter = set(['tags'])
    allowed_many = {'tags': 3 }
    boolean_filters = ('published',)
    allowed_filter = { 'title':'title',
                       'published' : 'published',
                       'tags' : 'tags__name' }

    allowed_order = { 'create': 'created_datetime',
                      'update': 'updated_datetime', 
                       None   : 'created_datetime' }

    def init_test(self, request):
        url_re = re.compile(r'^%s$' % self.get_url_part())
        m = url_re.match(request.path[1:])
        if m is None:
            raise Http404('Bad url')
        kwargs = m.groupdict()
        self.init_test_args(request, **kwargs)

    def __init__(self, request):
        self.init_test(request)


class SimpleTest(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        tags = []
        for tag in ['one', 'two', 'three', 'four', 'five', 'six', 'seven']:
            tag_obj = Tag.objects.create(name = tag)
            tags.append(tag_obj)

        m_obj = Messages.objects.create(title = 'First message',
                                        text = 'Message',
                                        published = True)
        m_obj.tags.add(*tags[:2])

        m_obj = Messages.objects.create(title = 'Second message',
                                        text = 'Message',
                                        published = False)
        m_obj.tags.add(*tags[2:4])

        mu_obj = Messages.objects.create(title = 'Update',
                                         text = 'Message',
                                         published = False)
        mu_obj.tags.add(*tags[4:])

        m_obj = Messages.objects.create(title = 'New',
                                        text = 'Message',
                                        published = True)
        m_obj.tags.add(tags[0], tags[6])

        m_obj = Messages.objects.create(title = 'title',
                                        text = 'Message',
                                        published = False)
        m_obj.tags.add(tags[3], tags[5])

        m_obj = Messages.objects.create(title = 'message',
                                        text = 'Message',
                                        published = True)
        m_obj.tags.add(tags[6])

        m_obj = Messages.objects.create(title = 'cats',
                                        text = 'Message',
                                        published = True)
        m_obj.tags.add(tags[5])
        m_obj = Messages.objects.create(title = 'dogs',
                                        text = 'Message',
                                        published = False)
        m_obj.tags.add(tags[5])

        # For changed updated_datetime
        nd = datetime.datetime.now() + datetime.timedelta(0, 5)
        Messages.objects.filter(pk = mu_obj.pk). \
            update(text = 'Updated', updated_datetime = nd)

    def test_basic_addition(self):
        self.assertEqual(1 + 1, 2)

    def test_filtering_view_url(self):
        url = MessagesFilter.get_url_part()
        self.assertEqual(url, '(?:published/(?P<published>yes|no)/)?' \
                              '(?:tags/(?P<tags>[^/]+)/)?' \
                              '(?:title/(?P<title>[^/]+)/)?' \
                              '(?:order/(?P<order>[a-z]*)/)?(?P<rev>rev/)?')

    def test_filtering_view_query_boolean(self):
        active = [
             "First message",
             "New",
             "message",
             "cats"
        ]
        inactive = [
             "Second message",
             "Update",
             "title",
             "dogs"
        ]
        request = self.factory.get('/published/yes/')
        self.assertQuerysetEqual(
            MessagesFilter(request).get_queryset(), 
            active, 
            attrgetter("title")
        )

        request = self.factory.get('/published/no/')
        self.assertQuerysetEqual(
            MessagesFilter(request).get_queryset(), 
            inactive, 
            attrgetter("title")
        )
        request = self.factory.get('/?published=yes')
        self.assertQuerysetEqual(
            MessagesFilter(request).get_queryset(), 
            active, 
            attrgetter("title")
        )

        request = self.factory.get('/?published=no')
        self.assertQuerysetEqual(
            MessagesFilter(request).get_queryset(), 
            inactive, 
            attrgetter("title")
        )

    def test_filtering_view_query_ordering(self):
        request = self.factory.get('/order/update/rev/')
        first = MessagesFilter(request).get_queryset()[0]
        self.assertEqual(first.title, "Update")

        request = self.factory.get('/order/test/')
        with self.assertRaises(Http404):
            MessagesFilter(request).get_queryset()

        request = self.factory.get('/order/newtest/rev/')
        with self.assertRaises(Http404):
            MessagesFilter(request).get_queryset()

        request = self.factory.get('/?order=update&rev=1')
        first = MessagesFilter(request).get_queryset()[0]
        self.assertEqual(first.title, "Update")

        request = self.factory.get('/?order=test')
        with self.assertRaises(Http404):
            MessagesFilter(request).get_queryset()

        request = self.factory.get('/?order=newtest&rev=1')
        with self.assertRaises(Http404):
            MessagesFilter(request).get_queryset()

    def test_filtering_view_query_search(self):
        def title_test(title):
            request = self.factory.get('/title/%s/' % title)
            obj = MessagesFilter(request).get_queryset()[0]
            self.assertEqual(obj.title, title)

        title_test("title")
        title_test("cats")
        title_test("dogs")
        title_test("New")

        def title_test2(title):
            request = self.factory.get('/?title=%s' % title)
            obj = MessagesFilter(request).get_queryset()[0]
            self.assertEqual(obj.title, title)

        title_test2("First message")
        title_test2("Second message")

        request = self.factory.get('/title/empty/')
        self.assertEqual(len(MessagesFilter(request).get_queryset()), 0)

    def test_filtering_view_query_m2m_search(self):
        first_tag = [
            "First message", 
            "New",
        ]
        last_tag = [
            "Update",
            "message"
        ]
        four_tag = [
            "Second message",
            "title"
        ]
        six_tag = [
            "Update",
            "title",
            "cats",
            "dogs"
        ]
        first_last_tag = first_tag + last_tag
        four_six_tag = list(set(six_tag + four_tag))
        first_four_last = list(set(first_last_tag + four_tag))

        def test_m2m_tags(link, args_cmp):
            request = self.factory.get(link)
            self.assertQuerysetEqual(
                MessagesFilter(request).get_queryset(), 
                args_cmp, 
                attrgetter("title"),
                ordered = False
            )

        test_m2m_tags('/tags/one/', first_tag)
        test_m2m_tags('/?tags=one', first_tag)

        test_m2m_tags('/tags/one,seven/', first_last_tag)
        test_m2m_tags('/?tags=one,seven', first_last_tag)

        test_m2m_tags('/tags/four,six/', four_six_tag)
        test_m2m_tags('/?tags=four,six', four_six_tag)

        test_m2m_tags('/tags/one,four,seven/', first_four_last)
        test_m2m_tags('/?tags=one,four,seven', first_four_last)

        # last tag should be rejected due the limit in allowed_many
        test_m2m_tags('/tags/one,four,seven,six/', first_four_last)
        test_m2m_tags('/?tags=one,four,seven,six', first_four_last)

    def test_filtering_view_query_complex(self):
        def test_complex(link, args_cmp):
            request = self.factory.get(link)
            self.assertQuerysetEqual(
                MessagesFilter(request).get_queryset(), 
                args_cmp, 
                attrgetter("title"),
                ordered = False
            )

        test_complex('/published/yes/tags/six/', ["cats"])
        test_complex('/?published=yes&tags=six', ["cats"])

        test_complex('/published/yes/tags/seven/title/message/', ["message"])
        test_complex('/?published=yes&tags=seven&title=message', ["message"])

class TemplateTagsTest(TestCase):
    urls = "generic.tests"
    def setUp(self):
        self.factory = RequestFactory()

    def test_basic_addition(self):
        self.assertEqual(1 + 1, 2)

    def test_settings(self):
        url = reverse('test')
        self.assertEqual(url, '/test/')

    def test_templatetag_fun1(self):
        request = self.factory.get('/test/')
        self.assertEqual(
            active_link.active_str({'request': request}, '/test/'),
            "active"
        )
        request = self.factory.get('/test/?query=val')
        self.assertEqual(
            active_link.active_str({'request': request}, '/test/'),
            "active"
        )
        request = self.factory.get('/test2/')
        self.assertEqual(
            active_link.active_str({'request': request}, '/test/'),
            ""
        )
        request = self.factory.get('/test/')
        self.assertEqual(
            active_link.active_str({'request': request}, '/test'),
            ""
        )

    def test_templatetag_fun2(self):
        fun2 = active_link.active_link
        request = self.factory.get('/test/')
        self.assertEqual(
            fun2({'request': request}, 'test', 'text'),
            '<li class="active"><a href="/test/">text</a><li>'
        )
        request = self.factory.get('/another_test/')
        self.assertEqual(
            fun2({'request': request}, 'test', 'text'),
            '<li><a href="/test/">text</a><li>'
        )
        
        request = self.factory.get('/another_test/')
        self.assertEqual(
            fun2({'request': request}, 'bad_test', 'text'),
            '<li><a href="#">text</a><li>'
        )

        request = self.factory.get('/test/')
        self.assertEqual(
            fun2({'request': request}, 'test', 'text', id = 'test_id'),
            '<li class="active"><a href="/test/" id="test_id">text</a><li>'
        )
        request = self.factory.get('/test2/extraargs')
        self.assertEqual(
            fun2({'request': request}, 'test2', 'text', 
                 id = 'test_id', extra = 'extra', args = 'args'),
            '<li class="active"><a href="/test2/extraargs" '\
            'id="test_id">text</a><li>'
        )

    def test_templatetag_active_str(self):
        request = self.factory.get('/test/')
        out = Template(
                "{% load active_link %}"
                "{% active_str '/test/' %}"
            ).render(Context({'request': request}))

        self.assertEqual(out, "active")

        request = self.factory.get('/test/?query=val')
        out = Template(
                "{% load active_link %}"
                "{% active_str '/test/' %}"
            ).render(Context({'request': request}))

        self.assertEqual(out, "active")

        request = self.factory.get('/test2/')
        out = Template(
                "{% load active_link %}"
                "{% active_str '/test/' %}"
            ).render(Context({'request': request}))

        self.assertEqual(out, "")