Workout With Friends Stay fit with a little motivation

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Properties
test_models.py
Go to the documentation of this file.
1 from __future__ import unicode_literals
2 import datetime
3 from wowf.models import Buddy, Challenge, Notification, SpeedChallenge, User
4 from wowf.models.pivot_tables import UserChallenge
5 from wowf.tests import BaseUnitTestCase
6
7
8 class BaseModelTestCase(BaseUnitTestCase):
9
10 def setup_db(self):
11 User.__table__.insert().execute([
12 dict(id=1, username='MarkSmith', email='mark@gmail.com', password='password',
13 gender='M', dob=datetime.datetime(year=1980, month=1, day=1),
14 weight=80, height=1.65),
15 dict(id=2, username='LeslieJames', email='leslie@yahoo.com', password='password',
16 gender='F', dob=datetime.datetime(year=1985, month=7, day=7),
17 weight=60, height=1.6),
18 dict(id=3, username='JustinJones', email='justin@msn.com', password='password',
19 gender='M', dob=datetime.datetime(year=1990, month=5, day=5),
20 weight=70, height=1.7),
21 ])
22 Buddy.__table__.insert().execute([
23 dict(user_id=1, buddy_id=2),
24 dict(user_id=3, buddy_id=1),
25 ])
26 Challenge.__table__.insert().execute([
27 dict(id=1, user_id=1, challenge_type_id=1),
28 dict(id=2, user_id=3, challenge_type_id=1),
29 ])
30 SpeedChallenge.__table__.insert().execute([
31 dict(id=1, distance=100),
32 dict(id=2, distance=100),
33 ])
34 UserChallenge.__table__.insert().execute([
35 dict(user_id=1, challenge_id=1, is_accepted=True),
36 dict(user_id=2, challenge_id=1, is_accepted=None),
37 dict(user_id=3, challenge_id=2, is_accepted=True),
38 dict(user_id=1, challenge_id=2, is_accepted=False),
39 ])
40
41
42 class UserModelTestCase(BaseModelTestCase):
43
44 def test_create_user(self):
45 data = dict(
46 username='MaryJane', email='maryj@gmail.com', password='mary90210',
47 gender='M', dob=datetime.datetime(year=1989, month=7, day=23),
48 weight=81.6466, height=1.778)
49 user = User.create(**data)
50 self.assertIsInstance(user, User)
51 self.assertEqual(user.email, data['email'])
52 self.assertNotEqual(user.password, data['password'])
53 self.assertTrue(user.is_active)
54 self.assertIsNotNone(user.id)
55 self.assertIsNotNone(user.created_at)
56 self.assertIsNotNone(user.last_active_at)
57
58 def test_get_by_id(self):
59 id = 1
60 user = User.get_by_id(id)
61 self.assertIsInstance(user, User)
62 self.assertEqual(user.id, id)
63 id = 999
64 user = User.get_by_id(id)
65 self.assertIsNone(user)
66
67 def test_get_by_email(self):
68 email = 'mark@gmail.com'
69 user = User.get_by_email(email)
70 self.assertIsInstance(user, User)
71 self.assertEqual(user.email, email)
72 user = User.get_by_email('doesnotexist@email.com')
73 self.assertIsNone(user)
74
75 def test_is_buddy(self):
76 user = User.get_by_id(2)
77 buddy = User.get_by_id(1)
78 self.assertTrue(user.is_buddy(buddy))
79 stranger = User.get_by_id(3)
80 self.assertFalse(user.is_buddy(stranger))
81
82 def test_add_buddy(self):
83 user = User.get_by_id(2)
84 stranger = User.get_by_id(3)
85 self.assertFalse(user.is_buddy(stranger))
86 user.add_buddy(stranger)
87 self.assertTrue(user.is_buddy(stranger))
88
89 def test_remove_buddy(self):
90 user = User.get_by_id(1)
91 buddy = User.get_by_id(2)
92 self.assertTrue(user.is_buddy(buddy))
93 user.remove_buddy(buddy)
94 self.assertFalse(user.is_buddy(buddy))
95
96 def test_get_buddies(self):
97 user = User.get_by_id(1)
98 expected_user_ids = (2, 3)
99 buddies = user.get_buddies()
100 self.assertEqual(len(buddies), len(expected_user_ids))
101 for user in buddies:
102 self.assertIsInstance(user, User)
103 self.assertIn(user.id, expected_user_ids)
104
105 def test_get_challenges(self):
106 user = User.get_by_id(1)
107 expected_challenge_ids = (1, 2)
108 challenges = user.get_challenges()
109 self.assertEqual(len(challenges), len(expected_challenge_ids))
110 for challenge in challenges:
111 self.assertIsInstance(challenge, Challenge)
112 self.assertIn(challenge.id, expected_challenge_ids)
113
114 def test_in_challenge(self):
115 user = User.get_by_id(1)
116 challenge = Challenge.get_by_id(1)
117 self.assertTrue(user.in_challenge(challenge))
118 user = User.get_by_id(3)
119 self.assertFalse(user.in_challenge(challenge))
120
121 def test_owns_challenge(self):
122 user = User.get_by_id(1)
123 challenge = Challenge.get_by_id(1)
124 self.assertTrue(user.owns_challenge(challenge))
125 user = User.get_by_id(2)
126 self.assertFalse(user.owns_challenge(challenge))
127
128 def test_accept_challenge(self):
129 creator = User.get_by_id(1)
130 competitor = User.get_by_id(2)
131 challenge = Challenge.get_by_id(1)
132 self.assertFalse(competitor.accepted_challenge(challenge))
133 self.assertEqual(creator.count_unconfirmed_notifications(), 0)
134 competitor.accept_challenge(challenge)
135 self.assertTrue(competitor.accepted_challenge(challenge))
136 self.assertEqual(creator.count_unconfirmed_notifications(), 1)
137 for notification in creator.get_all_notifications():
138 self.assertIsInstance(notification, Notification)
139 self.assertIsInstance(notification.user, User)
140 self.assertIsNotNone(notification.user.id)
141 self.assertIsInstance(notification.challenge, Challenge)
142 self.assertIsNotNone(notification.challenge.id)
143
144 def test_deny_challenge(self):
145 creator = User.get_by_id(1)
146 competitor = User.get_by_id(2)
147 challenge = Challenge.get_by_id(1)
148 self.assertFalse(competitor.denied_challenge(challenge))
149 self.assertEqual(creator.count_unconfirmed_notifications(), 0)
150 competitor.deny_challenge(challenge)
151 self.assertTrue(competitor.denied_challenge(challenge))
152 self.assertEqual(creator.count_unconfirmed_notifications(), 1)
153
154 def test_create_speed_challenge(self):
155 creator = User.get_by_id(1)
156 competitor = User.get_by_id(2)
157 distance = 400
158 self.assertEqual(competitor.count_unconfirmed_notifications(), 0)
159 creator.create_speed_challenge(competitor, distance)
160 self.assertEqual(competitor.count_unconfirmed_notifications(), 1)
161
162
163 class SpeedChallengeModelTestCase(BaseModelTestCase):
164
165 def test_create_challenge(self):
166 user = User.get_by_id(1)
167 distance = 100 # meters
168 challenge = SpeedChallenge.create(user, distance)
169 self.assertIsInstance(challenge, SpeedChallenge)
170 self.assertEqual(challenge.distance, distance)
171 self.assertEqual(challenge.creator, user)
172