1
|
|
|
from random import choice |
2
|
|
|
|
3
|
|
|
from django.conf import settings |
4
|
|
|
from django.contrib.auth import get_user_model |
5
|
|
|
from django.db import connection |
6
|
|
|
from django.utils.six import text_type |
7
|
|
|
|
8
|
|
|
from actstream.signals import action |
9
|
|
|
from actstream.models import model_stream |
10
|
|
|
from actstream.tests.base import ActivityBaseTestCase |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class ZombieTest(ActivityBaseTestCase): |
14
|
|
|
human = 10 |
15
|
|
|
zombie = 1 |
16
|
|
|
|
17
|
|
|
def setUp(self): |
18
|
|
|
self.User = get_user_model() |
19
|
|
|
super(ZombieTest, self).setUp() |
20
|
|
|
settings.DEBUG = True |
21
|
|
|
|
22
|
|
|
player_generator = lambda n, count: [self.User.objects.create( |
23
|
|
|
username='%s%d' % (n, i)) for i in range(count)] |
24
|
|
|
|
25
|
|
|
self.humans = player_generator('human', self.human) |
26
|
|
|
self.zombies = player_generator('zombie', self.zombie) |
27
|
|
|
|
28
|
|
|
self.zombie_apocalypse() |
29
|
|
|
|
30
|
|
|
def tearDown(self): |
31
|
|
|
settings.DEBUG = False |
32
|
|
|
super(ZombieTest, self).tearDown() |
33
|
|
|
|
34
|
|
|
def zombie_apocalypse(self): |
35
|
|
|
humans = self.humans[:] |
36
|
|
|
zombies = self.zombies[:] |
37
|
|
|
while humans: |
38
|
|
|
for z in self.zombies: |
39
|
|
|
victim = choice(humans) |
40
|
|
|
humans.remove(victim) |
41
|
|
|
zombies.append(victim) |
42
|
|
|
action.send(z, verb='killed', target=victim) |
43
|
|
|
if not humans: |
44
|
|
|
break |
45
|
|
|
|
46
|
|
|
def check_query_count(self, queryset): |
47
|
|
|
ci = len(connection.queries) |
48
|
|
|
|
49
|
|
|
result = list([map(text_type, (x.actor, x.target, x.action_object)) |
50
|
|
|
for x in queryset]) |
51
|
|
|
self.assertTrue(len(connection.queries) - ci <= 4, |
52
|
|
|
'Too many queries, got %d expected no more than 4' % |
53
|
|
|
len(connection.queries)) |
54
|
|
|
return result |
55
|
|
|
|
56
|
|
|
def test_query_count(self): |
57
|
|
|
queryset = model_stream(self.User) |
58
|
|
|
result = self.check_query_count(queryset) |
59
|
|
|
self.assertEqual(len(result), 10) |
60
|
|
|
|
61
|
|
|
def test_query_count_sliced(self): |
62
|
|
|
queryset = model_stream(self.User)[:5] |
63
|
|
|
result = self.check_query_count(queryset) |
64
|
|
|
self.assertEqual(len(result), 5) |
65
|
|
|
|