-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_models.py
117 lines (87 loc) · 3.84 KB
/
test_models.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
111
112
113
114
115
116
117
import datetime
import unittest
import logging
from google.appengine.ext import ndb
from google.appengine.ext.db import BadValueError
from google.appengine.ext import testbed
from google.appengine.datastore import datastore_stub_util
from models import Account, Torrent
class DatastoreTestCase(unittest.TestCase):
def setUp(self):
# First, create an instance of the Testbed class.
self.testbed = testbed.Testbed()
# Then activate the testbed, which prepares the service stubs for use.
self.testbed.activate()
# Create a consistency policy that will simulate the High Replication
# consistency model.
self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1)
# Initialize the datastore stub with this policy.
self.testbed.init_datastore_v3_stub(consistency_policy=self.policy)
self.testbed.init_memcache_stub()
# Clear ndb's in-context cache between tests.
ndb.get_context().clear_cache()
self._logger = logging.getLogger()
self._old_logging_level = self._logger.getEffectiveLevel()
self._logger.setLevel(logging.ERROR)
def tearDown(self):
self.testbed.deactivate()
self._logger.setLevel(self._old_logging_level)
class AccountTestCase(DatastoreTestCase):
def setUp(self):
super(AccountTestCase, self).setUp()
self.username = 'testuser'
self.password = 'testpassword'
self.userid = 123
def test_account_is_stored(self):
acc = Account(username=self.username, password=self.password, userid=self.userid)
key = acc.put()
stored = key.get()
self.assertEqual(stored.username, self.username)
self.assertEqual(stored.password, self.password)
self.assertEqual(stored.userid, self.userid)
def test_username_is_required(self):
acc = Account(password=self.password, userid=self.userid)
with self.assertRaises(BadValueError):
acc.put()
def test_password_is_required(self):
acc = Account(username=self.username, userid=self.userid)
with self.assertRaises(BadValueError):
acc.put()
def test_userid_is_required(self):
acc = Account(password=self.password, username=self.username)
with self.assertRaises(BadValueError):
acc.put()
def test_get_one_returns_entity(self):
acc = Account(username=self.username, password=self.password, userid=self.userid)
acc.put()
result = Account.get_one()
self.assertEqual(acc, result)
class TorrentTestCase(DatastoreTestCase):
def setUp(self):
super(TorrentTestCase, self).setUp()
self.title = 'Torrent title'
self.btih = '1234567890ABCDEF'
self.nbytes = 1234567890
self.dt = datetime.datetime.now()
def test_required_fields_are_required(self):
no_title = Torrent(btih=self.btih, nbytes=self.nbytes, dt=self.dt)
no_btih = Torrent(title=self.title, nbytes=self.nbytes, dt=self.dt)
no_nbytes = Torrent(title=self.title, btih=self.btih, dt=self.dt)
no_dt = Torrent(title=self.title, btih=self.btih, nbytes=self.nbytes)
with self.assertRaises(BadValueError):
no_title.put()
with self.assertRaises(BadValueError):
no_btih.put()
with self.assertRaises(BadValueError):
no_nbytes.put()
with self.assertRaises(BadValueError):
no_dt.put()
def test_get_latest_dt_returns_lstest(self):
now = datetime.datetime.now()
yesterday = now - datetime.timedelta(1)
t1 = Torrent(title=self.title, btih=self.btih, nbytes=self.nbytes, dt=yesterday)
t2 = Torrent(title=self.title, btih=self.btih, nbytes=self.nbytes, dt=now)
t1.put()
t2.put()
rv = Torrent.get_latest_dt()
self.assertEqual(rv, now)