def test_delete_game_valid(self): # Test API DELETE method api/game valid_game = None test_id = -1 test_name = 'API TEST DELETE GAME' # Insert test user into database to get using the API valid_game = Game() valid_game.id = test_id valid_game.name = test_name try: db.session.add(valid_game) db.session.commit() db.session.close() except: db.session.rollback() # Delete the db instance through an API call response = requests.delete(self.game_url + '/' + str(test_id), headers=self.headers) self.assertEqual(response.status_code, 204) # Make sure db instance is no longer in db try: query = Game.query.get(test_id) self.assertisNone(query) db.session.close() except: db.session.rollback()
def test_get_single_game_valid(self): # Test API GET method api/game/(int:id) with valid id valid_game = None test_id = -1 test_name = 'API TEST GET GAME' # Insert test user into database to get using the API valid_game = Game() valid_game.id = test_id valid_game.name = test_name try: db.session.add(valid_game) db.session.commit() db.session.close() except: db.session.rollback() # Make sure API call gets and matches the test user just entered into the db above response = requests.get(self.game_url + '/' + str(test_id), headers=self.headers) self.assertEqual(response.status_code, 200) json_response = json.loads(response.text) self.assertEqual(json_response.get('id'), test_id) self.assertEqual(json_response.get('name'), test_name) # Delte the test user that was inserted earlier in this try: db.session.delete(valid_game) db.session.commit() db.session.close() except: db.session.rollback()
def test_update_game_valid(self): # Test API PUT method api/game valid_game = None test_id = -1 test_name = 'API TEST UPDATE GAME' # Insert test user into database to get using the API valid_game = Game() valid_game.id = test_id valid_game.name = test_name try: db.session.add(valid_game) db.session.commit() db.session.close() except: db.session.rollback() new_test_name = 'NEW API TEST UPDATE GAME' update = {'name': new_test_name} # Update the user through the API response = requests.put(self.game_url + '/' + str(test_id), data=json.dumps(update), headers=self.headers) # Make sure instance was updated self.assertEqual(response.status_code, 200) json_response = json.loads(response.text) self.assertEqual(json_response.get('name'), new_test_name) # Delete the test instance for cleanup try: updated_game_query = Game.query.filter_by(id=test_id) self.assertEqual( updated_game_query.count(), 1) # Make sure update did not create duplicate entry updated_game = updated_game_query.first() db.session.delete(updated_game) db.session.commit() db.session.close() except: db.session.rollback()
def test_get_game_search_match(self): # Test API GET method api/game?q=<searchjson> with a match valid_game = None test_id = -1 test_name = 'API TEST GET GAME THROUGH SEARCH' test_rating = 'Made Up Game Rating' # Insert test user into database to get using the API valid_game = Game() valid_game.id = test_id valid_game.name = test_name valid_game.rating = test_rating try: db.session.add(valid_game) db.session.commit() db.session.close() except: db.session.rollback() filters = [dict(name='rating', op='ilike', val='made up game rating')] params = dict(q=json.dumps(dict(filters=filters))) response = requests.get(self.game_url, params=params, headers=self.headers) json_response = json.loads(response.text) # Make sure API call searches and matches the test user just entered into the db above self.assertEqual(response.status_code, 200) self.assertEqual(json_response.get('num_results'), 1) self.assertEqual(json_response.get('objects')[0].get('id'), test_id) self.assertEqual( json_response.get('objects')[0].get('name'), test_name) self.assertEqual( json_response.get('objects')[0].get('rating'), test_rating) # Delte the test user that was inserted earlier in this try: db.session.delete(valid_game) db.session.commit() db.session.close() except: db.session.rollback()
def setUp(self): db.create_all() samplegame1 = Game(name="Test Game", platform = 1, genre = "Test Genre") db.session.add(samplegame1) db.session.commit() samplerating1 = Rating(name=samplegame1.name, rating=1, review="Test Review", username="******") db.session.add(samplerating1) db.session.commit()
1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.conf.urls import include, url from django.contrib import admin from django.views.generic import ListView from application.models import Game from datetime import datetime from django.contrib.auth.views import LoginView, LogoutView from application import views, forms from django.contrib.auth.decorators import login_required urlpatterns = [ url(r'^$', login_required(ListView.as_view( queryset=Game.get_available_games(), template_name='application/games.html'), login_url='/login/'), name='home'), url(r'^login/', LoginView.as_view(template_name='application/login.html', authentication_form=forms.AuthForm, extra_context={ 'title': 'Sign in', 'year': datetime.now().year }), name='login'), url(r'^register/', views.SignUp.as_view(), name='register'), url(r'^game/', include('application.urls')), url(r'^mygames/', views.mygames, name='mygames'), url(r'^statistic/', views.statistic, name='statistics'),
def lookup_user(parent_db, user_id): ''' parent_db: The parent object that the user is nested under user_id: The user to lookup This method will perform the necessary API calls to lookup a Twitch user and their associated game and returns their associated game's id. ''' global games_dict global users_dict if user_id not in users_dict: # users_set.add(user_id) user_db = User() user_db.id = user_id # Have to do another request to get description for user because not included in teams users response user_json = requests.get((channel_api_url + str(user_db.id)), headers=headers).json() user_db.name = user_json.get('display_name') user_db.description = user_json.get('description') if not user_db.description: user_db.description = 'This user has no bio.' # This is what Twitch displays if a user leaves this field empty. language_code = user_json.get('language') language = language_code if language_code: try: language_iso = iso639.to_name(language_code) if language_iso: language = language_iso except: pass user_db.language = language user_db.views = user_json.get('views') user_db.followers = user_json.get('followers') user_db.url = user_json.get('url') user_db.created = user_json.get('created_at') user_db.updated = user_json.get('updated_at') user_db.image_url = user_json.get('logo') if user_db.image_url == None: user_db.image_url = 'https://static-cdn.jtvnw.net/jtv_user_pictures/xarth/404_user_70x70.png' # This is Twitch's default user logo if isinstance(parent_db, Team): user_db.team_ids = [parent_db.id] elif isinstance(parent_db, Community): user_db.community_id = parent_db.id user_game_name = user_json.get('game') if user_game_name != None: # Get game information from 2nd API gb_request_url = gb_api_url + user_game_name + '&resources=game' game_response = requests.get(gb_request_url, headers={'user-agent': 'streamGlean'}) game_json = None if game_response: game_json = game_response.json() if game_json: game_json = game_json.get('results') if game_json: game_json = game_json[0] game_id = game_json.get('id') if game_id and game_id not in games_dict: user_db.game_id = game_id game_db = Game() game_db.id = game_id game_db.user_ids = [user_db.id] if isinstance(parent_db, Team): game_db.team_ids = [parent_db.id] elif isinstance(parent_db, Community): game_db.community_ids = [parent_db.id] game_db.name = game_json.get('name') game_db.description = game_json.get('deck') genres = [] giantbomb_game_api_url = 'http://www.giantbomb.com/api/game/' + str( game_db.id ) + '?api_key=0ac0037d403fff412c9e9ac9e60a23acc5b2e736&format=json' game_response = requests.get( giantbomb_game_api_url, headers={'user-agent': 'streamGlean'}) if game_response: game_response = game_response.json().get( 'results') genres_response = game_response.get('genres') if genres_response: for genre_dict in game_response.get( 'genres'): genres.append(genre_dict.get('name')) game_db.genres = genres platforms = [] game_platforms = game_json.get('platforms') if (game_platforms and isinstance(game_platforms, Iterable)): for platform_dict in game_platforms: platforms.append(platform_dict.get('name')) game_db.platforms = platforms game_db.release_date = game_json.get( 'original_release_date') rating = game_json.get('original_game_rating') if rating: for d in rating: if 'ESRB' in d.get('name'): actual_rating = d.get('name').replace( 'ESRB: ', '') game_db.rating = actual_rating game_image = game_json.get('image') if game_image: game_image_small_url = game_image.get( 'small_url') if game_image_small_url: game_db.image_url = game_image_small_url else: game_db.image_url = 'https://static-cdn.jtvnw.net/jtv_user_pictures/xarth/404_user_70x70.png' # This is Twitch's default logo games_dict[game_db.id] = game_db elif game_id: game_db = games_dict[game_id] if user_db.id not in game_db.user_ids: game_db.user_ids += [user_db.id] if isinstance(parent_db, Team): if game_db.team_ids: if parent_db.id not in game_db.team_ids: game_db.team_ids += [parent_db.id] elif isinstance(parent_db, Community): if game_db.community_ids: if parent_db.id not in game_db.community_ids: game_db.community_ids += [parent_db.id] users_dict[user_db.id] = user_db return user_db.game_id else: user_db = users_dict[user_id] if isinstance(parent_db, Team): if user_db.team_ids != None: user_db.team_ids += [parent_db.id] else: user_db.team_ids = [parent_db.id] elif isinstance(parent_db, Community): user_db.community_id = parent_db.id