Skip to content

Commit

Permalink
Merge pull request #223 from fga-eps-mds/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
BRZangado authored Oct 28, 2018
2 parents 7860125 + a786820 commit 941d7b9
Show file tree
Hide file tree
Showing 21 changed files with 572 additions and 368 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
9 changes: 9 additions & 0 deletions .env
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,4 @@ target/
__pycache__/
local_settings.py

.env
db.sqlite3
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,21 @@ script:
after_success:
- docker-compose run web coveralls

deploy:
- provider: heroku
skip_cleanup: true
api_key:
secure: ${HEROKU_KEY}
app: gbi-crossdata-staging
on:
repo: fga-eps-mds/2018.2-GamesBI
branch: develop

- provider: heroku
skip_cleanup: true
api_key:
secure: ${HEROKU_KEY}
app: gbi-crossdata-production
on:
repo: fga-eps-mds/2018.2-GamesBI
branch: master
140 changes: 138 additions & 2 deletions CrossData/API/tests.py
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)
5 changes: 5 additions & 0 deletions CrossData/API/urls.py
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"),
]
156 changes: 154 additions & 2 deletions CrossData/API/views.py
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
Loading

0 comments on commit 941d7b9

Please sign in to comment.