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_lib.py
Go to the documentation of this file.
1 from __future__ import unicode_literals
2 import datetime
3 from wowf.lib.auth import Auth
4 from wowf.lib.pagination import Pager
5 from wowf.lib.utils import (
6 Storage, calculate_bmi, parse_timedelta, to_system_timezone,
7 to_user_timezone, years_ago)
8 from wowf.tests import BaseUnitTestCase
9
10
11 class AuthTestCase(BaseUnitTestCase):
12
13 def test_hash_password(self):
14 original = 'password'
15 hashed = Auth.hash_password(original)
16 self.assertNotEqual(original, hashed)
17 self.assertEqual(hashed, Auth.hash_password(original, hashed))
18
19
20 class UtilsTestCase(BaseUnitTestCase):
21
22 def test_years_ago(self):
23 today = datetime.date.today()
24 start = datetime.date(year=1980, month=1, day=1)
25 self.assertEqual(years_ago(start), today.year - start.year)
26
27 def test_calculate_bmi(self):
28 weight = 45.3592
29 height = 1.524
30 self.assertEqual(calculate_bmi(weight, height), 19.5)
31
32 def test_pager(self):
33 self.assertEqual(Pager(1, 10).offset, 0)
34 self.assertEqual(Pager(2, 10).offset, 10)
35 self.assertEqual(Pager(1, None).offset, 0)
36 self.assertEqual(Pager(None, 10).offset, 0)
37 self.assertEqual(Pager(None, None).offset, 0)
38
39 def test_to_timezone(self):
40 timezone = 'America/New_York'
41 original_dt = datetime.datetime(2012, 1, 1)
42 offset = datetime.timedelta(hours=5) # Standard time, not DST
43 user_dt = to_user_timezone(original_dt, timezone)
44 self.assertEqual(original_dt.strftime('%s'), (user_dt + offset).strftime('%s'))
45
46 def test_from_timezone(self):
47 timezone = 'America/New_York'
48 original_dt = datetime.datetime(2012, 1, 1)
49 offset = datetime.timedelta(hours=5) # Standard time, not DST
50 system_dt = to_system_timezone(original_dt, timezone)
51 self.assertEqual(original_dt.strftime('%s'), (system_dt - offset).strftime('%s'))
52
53 def test_parse_timedelta(self):
54 td = parse_timedelta(datetime.timedelta(days=7, hours=12, minutes=30))
55 self.assertEqual(td.days, 7)
56 self.assertEqual(td.hours, 12)
57 self.assertEqual(td.minutes, 30)
58 td = parse_timedelta(datetime.timedelta(hours=36))
59 self.assertEqual(td.days, 1)
60 self.assertEqual(td.hours, 12)
61 self.assertEqual(td.minutes, 0)
62
63 def test_storage(self):
64 bar = 'bar'
65 storage = Storage(foo=bar)
66 self.assertEqual(storage['foo'], bar)
67 self.assertEqual(storage['foo'], storage.foo)
68 self.assertRaises(AttributeError, storage.__getattr__, 'doesntexist')
69