Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fase 3 TBL 02 - Grupo 06 #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
from dateutil import parser
from pingout.__init__ import create_app

def test_return_200_on_root(client):
""" Return status code 200 on root url"""
response = client.get('/')
assert response.status_code == 200

def test_create_pingout(client):
response = client.post('create-pingout')
response2 = client.get('create-pingout')
assert response.status_code == 201
assert response2.status_code == 405

def test_bad_format_pingout(client):
uuid = '12345678aq234123ac12423a'
response = client.get(f'/{uuid}')
assert response.status_code == 400

def test_invalid_pingout(client):
uuid = '9e46643f534c4807adb2d7691188242a'
response = client.get(f'/{uuid}')
assert response.status_code == 404

def test_valid_pingout(client):
pingout = client.post('/create-pingout')
uuid = pingout.json['uuid']
client.post(f'/{uuid}/ping')
pingout = client.get(f'/{uuid}')
assert pingout.status_code == 200

def test_pingout_invalid_date_rage(client):
pingout = client.post('/create-pingout')
uuid = pingout.json['uuid']
initial_date = parser.parse('2018-11-02')
final_date = parser.parse('2018-11-01')
pingout = client.get(f'/{uuid}/filter/?initial_date={initial_date}&final_date={final_date}')
assert pingout.status_code == 404
79 changes: 79 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import pytest
import datetime
from dateutil import parser
from uuid import uuid4
from pingout.filters import filter_pings_range_of_dates, filter_pings_of_date, filter_occurrences_ping_range_date

def test_filter_pings_range_of_dates(db_collection):
uuid = uuid4()
date = datetime.datetime.today().replace(second=0,
microsecond=0)
db_collection.insert_one({'uuid': uuid.hex, 'pings': [{'count': 1, "date": date}]})

initial = parser.parse('-'.join([str(date.year), str(date.month), str(date.day)]))
final = parser.parse('-'.join([str(date.year), str(date.month), str(date.day)]))

test = filter_pings_range_of_dates(uuid.hex, db_collection, initial, final)
assert test == [{'count': 1, "date": date.date()}]

def test_filter_pings_range_of_dates_error(db_collection):
uuid = uuid4()
date = datetime.datetime.today().replace(second=0,
microsecond=0)
db_collection.insert_one({'uuid': uuid.hex, 'pings': [{'count': 1, "date": date}]})

initial = '-'.join([str(date.year), str(date.month), str(date.day)])
final = '-'.join([str(date.year), str(date.month), str(date.day)])

with pytest.raises(ValueError) as excinfo:
filter_pings_range_of_dates(uuid.hex, db_collection, initial, final)
assert 'Invalid date type' in str(excinfo.value)

def test_filter_invalid_date_type(db_collection):
uuid = uuid4()
date = 'invalid date'
# date = datetime.datetime.today().replace(second=0, microsecond=0)
db_collection.insert_one({'uuid': uuid.hex, 'pings': [{'count': 1, "date": date}]})

with pytest.raises(ValueError) as excinfo:
filter_pings_of_date(uuid.hex, db_collection, date)
assert 'Invalid date type' in str(excinfo.value)

def test_filter_valid_date_type(db_collection):
uuid = uuid4()
# date = datetime.datetime.today().replace(second=0, microsecond=0)
date = datetime.datetime(2018, 10, 15, 14, 2)
db_collection.insert_one({'uuid': uuid.hex, 'pings': [{'count': 1, "date": date}]})

result = filter_pings_of_date(uuid.hex, db_collection, date)

assert result == [{'count': 1, 'date': datetime.datetime(2018, 10, 15, 14, 2)}]

def test_filter_invalid_type_of_range_of_dates(db_collection):
uuid = uuid4()
date = 'invalid date'
# date = datetime.datetime.today().replace(second=0, microsecond=0)
db_collection.insert_one({'uuid': uuid.hex, 'pings': [{'count': 1, "date": date}]})

# initial = '-'.join([str(date.year), str(date.month), str(date.day)])
initial = 'invalid initial date'
# final = '-'.join([str(date.year), str(date.month), str(date.day)])
final = 'invalid final date'

with pytest.raises(ValueError) as excinfo:
filter_occurrences_ping_range_date(uuid.hex, db_collection, initial, final)
assert 'Invalid date type' in str(excinfo.value)

def test_filter_valid_type_of_range_of_dates(db_collection):
uuid = uuid4()
date = datetime.datetime.today().replace(second=0,
microsecond=0)
db_collection.insert_one({'uuid': uuid.hex, 'pings': [{'count': 1, "date": date}]})

# initial = datetime.datetime.today().replace(second=0,
# microsecond=0)
initial = datetime.datetime(2018, 11, 2, 11, 31, 1)
final = datetime.datetime(2018, 11, 2, 14, 31, 1)

test = filter_occurrences_ping_range_date(uuid.hex, db_collection, initial, final)
assert test == {'2018-11-02': 1}