diff --git a/tests/test_app.py b/tests/test_app.py index 1ceb416..64eff7d 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -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 diff --git a/tests/test_filters.py b/tests/test_filters.py new file mode 100644 index 0000000..5d7271b --- /dev/null +++ b/tests/test_filters.py @@ -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}