forked from luwo1701/Party-Queue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models_test.py
executable file
·203 lines (171 loc) · 7.14 KB
/
models_test.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""
Unit-Testing file for models.py
"""
import sys
sys.path.insert(1, '/home/user/Downloads/google_appengine')
sys.path.insert(1, '/home/user/Downloads/google_appengine/lib/yaml/lib')
sys.path.insert(1, '/home/user/workspaces/Party-Queue/lib')
import unittest
from models import Song, Playlist, Account
from google.appengine.ext import testbed
from google.appengine.ext import ndb
from google.appengine.ext import testbed
from google.appengine.api.datastore_errors import BadValueError
class AccountRequest:
""" Helper Class to simulate an Account request """
def __init__(self, username, email):
self.username = username
self.email = email
class PlaylistRequest:
""" Helper Class to simulate a Playlist request """
def __init__(self, pid, spotify_id, name):
self.pid = pid
self.spotify_id = spotify_id
self.name = name
class TestModelsTestCase(unittest.TestCase):
def setUp(self):
# Setting up testbed with the method stubs uses an in memory datastore
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_datastore_v3_stub()
self.testbed.init_memcache_stub()
# Clear ndb's in-context cache between tests.
# This prevents data from leaking between tests.
# Alternatively, you could disable caching by
# using ndb.get_context().set_cache_policy(False)
ndb.get_context().clear_cache()
def tearDown(self):
self.testbed.deactivate()
def test_songs_initiliazation(self):
# Test that it fails if we don't put in req'd values
try:
Song().put()
# this is hacky, but for some reason self.assertRaises wasn't
# properly catching the exception
self.assertEqual(1,2)
except BadValueError:
pass
try:
Song(spotify_id='sldkjfldkjflkd').put()
self.assertEqual(1,2)
except BadValueError:
pass
try:
Song(name='song_name').put()
self.assertEqual(1,2)
except BadValueError:
pass
# Song(spotify_id='sldkjfldkjflkd', name='songname').put()
# self.assertEqual(1, len(Song.query().fetch(2)))
def test_Playlist_initialization(self):
owner=Account(username='Mr. Magoo', email='[email protected]').put()
try:
Playlist().put()
self.assertEqual(1,2)
except BadValueError:
pass
try:
Playlist(owner=owner).put()
self.assertEqual(1,2)
except BadValueError:
pass
try:
Playlist(name='atlejljd').put()
self.assertEqual(1,2)
except BadValueError:
pass
Playlist(owner=owner, name='MM\'s awesome playlist').put()
self.assertEqual(1, len(Playlist.query().fetch(2)))
def test_Account_initialization(self):
try:
Account().put()
self.assertEqual(1,2)
except BadValueError:
pass
try:
Account(username='Captain Blackbeard').put()
self.assertEqual(1,2)
except BadValueError:
pass
try:
Account(email='[email protected]').put()
self.assertEqual(1,2)
except BadValueError:
pass
# test contructor
Account(username='Captain BlackBeard', email='[email protected]').put()
self.assertEqual(1, len(Account.query().fetch(2)))
# test add new user
request = AccountRequest('First Mate', '[email protected]')
Account.add_new_user(request)
self.assertEqual(2, len(Account.query().fetch(3)))
def test_account(self):
user = Account(username='Captain BlackBeard', email='[email protected]')
user.put()
self.assertEqual(1, len(Account.query().fetch(2)))
# test find by username
found_user = Account.find_by_username('Captain BlackBeard')
self.assertEqual(user, found_user, 'Failed to find user based on username')
# test update email
Account.update_email(user.key.id(), '[email protected]')
self.assertEqual('[email protected]',
Account.find_by_username('Captain BlackBeard').email,
'Failed to update email')
# test find by id
found_user = Account.find_by_id(user.key.id())
self.assertEqual(user, found_user, 'Failed to find user based on id')
def test_playlist(self):
owner=Account(username='Mr. Magoo', email='[email protected]').put()
pl = []
pl.append(Playlist(owner=owner, name='MM\'s awesome playlist'))
pl[0].put()
self.assertEqual(1, len(Playlist.query().fetch(2)))
# test find by id
found = Playlist.find_by_id(pl[0].key.id())
self.assertEqual(pl[0], found, 'Failed to find playlist based on id')
# test add new playlist
pl.append(Playlist.add_new_playlist(owner,
'A great playlist'))
self.assertEqual(2, len(Playlist.query().fetch(3)))
# test find by owner
pl.append(Playlist(owner=owner, name='MM\'s other awesome playlist'))
pl[2].put()
self.assertEqual(3, len(Playlist.query().fetch(4)))
pl_sorted = sorted(pl, key=lambda Playlist: Playlist.name)
found_pl = Playlist.find_by_owner(owner).fetch(3)
for i in range(len(found_pl)):
self.assertEqual(pl_sorted[i], found_pl[i])
# test add song
request = PlaylistRequest(pl_sorted[0].key.id(),
'1892300kdkeo',
'Space Oddity')
Playlist.add_song(request)
self.assertEqual(1, len(pl_sorted[0].songs), 'add_song failed to add song')
self.assertEqual('1892300kdkeo', pl_sorted[0].songs[0].spotify_id)
self.assertEqual('Space Oddity', pl_sorted[0].songs[0].name)
self.assertEqual(0, pl_sorted[0].songs[0].vote_count)
# Upvote a song
new_request = PlaylistRequest(pl_sorted[0].key.id(),
'asdlfjsadlfkj',
'What A Wonderful World')
Playlist.add_song(new_request)
songs = Song.find_by_playlist(pl_sorted[0].key)
self.assertEqual(2, len(songs.fetch(4)), 'Failed to find songs by playlist id')
song_entity = songs.get()
Song.upvote(song_entity.key.id())
self.assertEqual(1, song_entity.vote_count, song_entity)
songs_on_pl = pl_sorted[0].songs
self.assertEqual(1, songs_on_pl[0].vote_count, songs_on_pl)
song_entity = songs.fetch(2)[1]
Song.upvote(song_entity.key.id())
Song.upvote(song_entity.key.id())
songs = Song.find_by_playlist(pl_sorted[0].key).fetch(2)
self.assertEqual('What A Wonderful World', songs[0].name, song_entity)
self.assertEqual(2, songs[0].vote_count, song_entity)
# downvote
self.assertEqual(2, song_entity.vote_count)
Song.downvote(song_entity.key.id())
self.assertEqual(1, song_entity.vote_count)
# Main: Run Test Cases
if __name__ == '__main__':
unittest.main()