-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #223 from fga-eps-mds/develop
Develop
- Loading branch information
Showing
21 changed files
with
572 additions
and
368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
POSTGRES_USER=gamesbi | ||
POSTGRES_PASSWORD=gamesbi123 | ||
POSTGRES_DB=gamesbi | ||
POSTGRES_HOST=db | ||
PGDATA=/tmp/pgdata | ||
MB_USERNAME=[email protected] | ||
MB_PASSWORD=gamesbi123 | ||
DB_NAME=gamesbi | ||
PORT=8000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,5 +86,4 @@ target/ | |
__pycache__/ | ||
local_settings.py | ||
|
||
.env | ||
db.sqlite3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,139 @@ | ||
from django.test import TestCase | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
from CrossData.importdata.models import * | ||
|
||
# Create your tests here. | ||
class EndpointPOSTTestCase(APITestCase): | ||
|
||
def setUp(self): | ||
|
||
self.url = reverse("games_view") | ||
|
||
self.data = [ | ||
{ | ||
"name": "Jogo Teste", | ||
"languages": "Ingles", | ||
"genre": "Ação", | ||
"count_videos": 10, | ||
"count_views": 11, | ||
"count_likes": 20, | ||
"count_dislikes": 110, | ||
"count_comments": 10, | ||
"positive_reviews_steam": 10, | ||
"negative_reviews_steam": 11, | ||
"owners": 10, | ||
"average_forever": 20, | ||
"average_2weeks": 40, | ||
"price": 50, | ||
"total_views": 70, | ||
"streams":[ | ||
{ | ||
"language": "Portugues", | ||
"started_at": "2018-09-30", | ||
"type": "Live", | ||
"viewer_count": 10 | ||
} | ||
] | ||
|
||
} | ||
] | ||
|
||
self.data_2 = [ | ||
{ | ||
"languages": "Ingles", | ||
"genre": "Tiro", | ||
"count_videos": 14, | ||
"count_views": 19, | ||
"count_likes": 30, | ||
"count_dislikes": 5, | ||
"count_comments": 2, | ||
"positive_reviews_steam": 15, | ||
"negative_reviews_steam": 12, | ||
"owners": 9, | ||
"average_forever": 15, | ||
"average_2weeks": 30, | ||
"price": 60, | ||
"total_views": 80, | ||
"streams":[ | ||
{ | ||
"language": "Portugues", | ||
"started_at": "2018-09-30", | ||
"type": "Live", | ||
"viewer_count": 12 | ||
} | ||
] | ||
} | ||
] | ||
|
||
|
||
def tearDown(self): | ||
|
||
Game.objects.all().delete() | ||
|
||
def test_status_code(self): | ||
|
||
response = self.client.post(self.url, self.data, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
|
||
def test_status_code_not_created(self): | ||
|
||
response = self.client.post(self.url, self.data_2, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
|
||
def test_game_data_persistence(self): | ||
|
||
response = self.client.post(self.url, self.data, format='json') | ||
self.assertEqual(Game.objects.all().count(), 1) | ||
|
||
def test_game_data_not_persistence(self): | ||
|
||
response = self.client.post(self.url, self.data_2, format='json') | ||
self.assertEqual(Game.objects.all().count(), 0) | ||
|
||
def test_steam_data_persistence(self): | ||
|
||
response = self.client.post(self.url, self.data, format='json') | ||
self.assertEqual(InfoSteam.objects.all().count(), 1) | ||
for info in InfoSteam.objects.all(): | ||
self.assertEqual(info.game.name, self.data[0]['name']) | ||
|
||
def test_steam_data_not_persistence(self): | ||
|
||
response = self.client.post(self.url, self.data_2, format='json') | ||
self.assertEqual(InfoSteam.objects.all().count(), 0) | ||
|
||
def test_youtube_data_persistence(self): | ||
|
||
response = self.client.post(self.url, self.data, format='json') | ||
self.assertEqual(InfoYoutube.objects.all().count(), 1) | ||
for info in InfoYoutube.objects.all(): | ||
self.assertEqual(info.game.name, self.data[0]['name']) | ||
|
||
def test_youtube_data_not_persistence(self): | ||
|
||
response = self.client.post(self.url, self.data_2, format='json') | ||
self.assertEqual(InfoYoutube.objects.all().count(), 0) | ||
|
||
def test_twitch_data_persistence(self): | ||
|
||
response = self.client.post(self.url, self.data, format='json') | ||
self.assertEqual(InfoTwitch.objects.all().count(), 1) | ||
for info in InfoTwitch.objects.all(): | ||
self.assertEqual(info.game.name, self.data[0]['name']) | ||
|
||
def test_twitch_data_not_persistence(self): | ||
|
||
response = self.client.post(self.url, self.data_2, format='json') | ||
self.assertEqual(InfoTwitch.objects.all().count(), 0) | ||
|
||
def test_stream_data_persistence(self): | ||
|
||
response = self.client.post(self.url, self.data, format='json') | ||
self.assertEqual(TwitchStream.objects.all().count(), 1) | ||
for info in TwitchStream.objects.all(): | ||
self.assertEqual(info.game.name, self.data[0]['name']) | ||
|
||
def test_stream_data_not_persistence(self): | ||
|
||
response = self.client.post(self.url, self.data_2, format='json') | ||
self.assertEqual(TwitchStream.objects.all().count(), 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.urls import include, path | ||
from .views import GamesView | ||
urlpatterns = [ | ||
path('', GamesView.as_view(), name="games_view"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,155 @@ | ||
from django.shortcuts import render | ||
import requests | ||
|
||
# Create your views here. | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
from rest_framework.views import APIView | ||
from CrossData.importdata.models import * | ||
from CrossData.importdata.serializers import * | ||
|
||
|
||
class GamesView(APIView): | ||
|
||
''' | ||
Endpoint for receiving | ||
data and persisting it on database | ||
''' | ||
|
||
def get(self, request, format=None): | ||
game_name = request.GET.get('name') | ||
serializer = GameNameSerializer(Game.objects.filter(name__istartswith=game_name), many=True) | ||
return Response(serializer.data) | ||
|
||
|
||
def post(self, request, format=None): | ||
|
||
game_list = request.data | ||
|
||
if self.check_request_data(game_list): | ||
|
||
for game_data in game_list: | ||
|
||
new_game = self.save_game(game_data) | ||
|
||
info_youtube = self.save_info_youtube(game_data, new_game) | ||
info_steam = self.save_info_steam(game_data, new_game) | ||
info_twitch = self.save_info_twitch(game_data, new_game) | ||
streams = self.save_streams(game_data, new_game) | ||
|
||
return Response( | ||
data={"mensagem":"Dados salvos com sucesso"}, | ||
status=status.HTTP_201_CREATED | ||
) | ||
|
||
return Response( | ||
data={"mensagem":"Erro ao Salvar dados. Alguns atributos podem estar faltando"}, | ||
status=status.HTTP_400_BAD_REQUEST | ||
) | ||
|
||
def check_request_data(self, data): | ||
|
||
attrs_list = [ | ||
"name", "languages", "genre", | ||
"count_videos", "count_views", "count_likes", | ||
"count_dislikes", "count_comments", "positive_reviews_steam", | ||
"negative_reviews_steam", "owners", "average_forever", | ||
"average_2weeks", "price", "total_views", "streams" | ||
] | ||
|
||
for record in data: | ||
for attr in attrs_list: | ||
if attr not in list(record.keys()): | ||
return False | ||
return True | ||
|
||
def save_game(self, game_data): | ||
|
||
try: | ||
Game.objects.get(name=game_data['name']) | ||
print("jogo já cadastrado") | ||
return Game.objects.get(name=game_data['name']) | ||
|
||
except Game.DoesNotExist: | ||
|
||
new_game = Game( | ||
name=game_data['name'], | ||
languages_game=game_data['languages'], | ||
genres=game_data['genre'], | ||
) | ||
|
||
new_game.save() | ||
return new_game | ||
|
||
def save_info_youtube(self, game_data, game): | ||
|
||
new_info_youtube = InfoYoutube( | ||
game=game, | ||
count_videos=game_data['count_videos'], | ||
count_views=game_data['count_views'], | ||
count_likes=game_data['count_likes'], | ||
count_dislikes=game_data['count_dislikes'], | ||
count_comments=game_data['count_comments'], | ||
|
||
) | ||
|
||
try: | ||
new_info_youtube.save() | ||
return new_info_youtube | ||
|
||
except ValueError: | ||
return False | ||
|
||
def save_info_steam(self, game_data, game): | ||
|
||
new_info_steam = InfoSteam( | ||
game=game, | ||
positive_reviews_steam=game_data['positive_reviews_steam'], | ||
negative_reviews_steam=game_data['negative_reviews_steam'], | ||
owners=game_data['owners'], | ||
average_forever=game_data['average_forever'], | ||
average_2weeks=game_data['average_2weeks'], | ||
price=game_data['price'], | ||
) | ||
|
||
try: | ||
new_info_steam.save() | ||
return new_info_steam | ||
|
||
except ValueError: | ||
return False | ||
|
||
def save_info_twitch(self, game_data, game): | ||
|
||
new_info_twitch = InfoTwitch( | ||
game=game, | ||
viewer_count=game_data['total_views'], | ||
) | ||
|
||
try: | ||
new_info_twitch.save() | ||
return new_info_twitch | ||
|
||
except ValueError: | ||
return False | ||
|
||
def save_streams(self, game_data, game): | ||
|
||
saved_streams = [] | ||
|
||
for stream_data in game_data['streams']: | ||
|
||
new_twitch_stream = TwitchStream( | ||
game=game, | ||
language_stream=stream_data['language'], | ||
started_at=stream_data['started_at'], | ||
type=stream_data['type'], | ||
stream_view_count=stream_data['viewer_count'] | ||
) | ||
|
||
try: | ||
new_twitch_stream.save() | ||
saved_streams.append(new_twitch_stream) | ||
|
||
except ValueError: | ||
return False | ||
|
||
return saved_streams |
Oops, something went wrong.