-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
111 lines (89 loc) · 4.5 KB
/
tests.py
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
import unittest
import fudge
from djangomock import FakeDjangoQuerySet, stub
class TestFakeDjangoQueryset(unittest.TestCase):
def setUp(self):
self.objects = FakeDjangoQuerySet()
def test_all(self):
self.objects = FakeDjangoQuerySet([1,2,3])
self.assertEquals(3, self.objects.all().count())
def test_filter_equals(self):
person1 = stub('person1').has_attr(age = 12)
person2 = stub('person2').has_attr(age = 13)
person3 = stub('person3').has_attr(age = 12)
self.objects = FakeDjangoQuerySet([person1, person2, person3,])
self.assertEquals(1, self.objects.filter(age = 13).count())
self.assertEquals(2, self.objects.filter(age = 12).count())
def test_multiple_filter(self):
person1 = stub('person1').has_attr(age = 12, sex='male')
person2 = stub('person2').has_attr(age = 13, sex='male')
person3 = stub('person2').has_attr(age = 13, sex='female')
person4 = stub('person3').has_attr(age = 12, sex='female')
self.objects = FakeDjangoQuerySet([person1, person2, person3, person4])
self.assertEquals(1, self.objects.filter(age = 13, sex='male').count())
def test_filter_gt(self):
person1 = stub('person1').has_attr(age = 12)
person2 = stub('person2').has_attr(age = 13)
person3 = stub('person3').has_attr(age = 13)
self.objects = FakeDjangoQuerySet([person1, person2, person3,])
self.assertEquals(2, self.objects.filter(age__gt = 12).count())
def test_filter_lt(self):
person1 = stub('person1').has_attr(age = 12)
person2 = stub('person2').has_attr(age = 13)
person3 = stub('person3').has_attr(age = 13)
self.objects = FakeDjangoQuerySet([person1, person2, person3,])
self.assertEquals(1, self.objects.filter(age__lt = 13).count())
def test_order_by(self):
person1 = stub('person1').has_attr(age = 14)
person2 = stub('person2').has_attr(age = 13)
person3 = stub('person3').has_attr(age = 12)
self.objects = FakeDjangoQuerySet([person1, person2, person3,])
self.assertEquals(12, self.objects.order_by('age')[0].age)
self.assertEquals(13, self.objects.order_by('age')[1].age)
def test_order_by_desc(self):
person1 = stub('person1').has_attr(age = 14)
person2 = stub('person2').has_attr(age = 18)
person3 = stub('person3').has_attr(age = 10)
self.objects = FakeDjangoQuerySet([person1, person2, person3,])
self.assertEquals(18, self.objects.order_by('-age')[0].age)
self.assertEquals(14, self.objects.order_by('-age')[1].age)
def test_it_can_load_fixtures(self):
queryset = FakeDjangoQuerySet(fixtures='fixtures.json', model='auth.permission')
self.assertEquals(3, queryset.count())
def test_add_from_dict(self):
queryset = FakeDjangoQuerySet()
queryset.add_from_dict('User', {'username': 'herp', 'password':'h4$h', 'is_staff': True})
self.assertEquals(1, queryset[0].id)
def test_add_from_dict_should_add_autoincrementing_primary_key(self):
queryset = FakeDjangoQuerySet()
queryset.add_from_dict('User', {'username': 'herp', 'password':'h4$h', 'is_staff': True})
queryset.add_from_dict('User', {'username': 'herp', 'password':'h4$h', 'is_staff': True})
self.assertEquals(2, queryset[1].id)
queryset.add_from_dict('User', {'username': 'herp', 'password':'h4$h', 'is_staff': True}, pk=30)
self.assertEquals(30, queryset[2].id)
queryset.add_from_dict('User', {'username': 'herp', 'password':'h4$h', 'is_staff': True})
self.assertEquals(31, queryset[3].id)
import fakemodels as models
class TestFakeModels(unittest.TestCase):
def setUp(self):
class User(models.Model):
name = models.CharField(max_length=30)
self.User = User
def test_should_create_autofield(self):
self.assertTrue(type(self.User._meta.fields[0]) == models.AutoField)
def test_should_create_manager(self):
self.assertEquals(0, self.User.objects.count())
def test_save_should_create(self):
user = self.User()
user.name = 'me'
user.save()
self.assertEquals(1, self.User.objects.count())
self.assertEquals(1, user.id)
def test_save_two_times_should_add_only_one(self):
user = self.User()
user.name = 'me'
user.save()
user.save()
self.assertEquals(1, self.User.objects.count())
if __name__=='__main__':
unittest.main()