From 624c1b75f820276b022d90c16a6f65c70d60b952 Mon Sep 17 00:00:00 2001 From: Luiz Guilherme Silva Date: Fri, 2 Nov 2018 11:06:00 -0300 Subject: [PATCH 1/3] Added filter tests for valid and invalid date on a instance of MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Luiz Guilherme Silva Co-authored-by: Amanda Muniz Co-Authored-by: João Saliba Co-Authored-by: Mateus de Oliveira Co-Authored-by: Lucas Siqueira --- tests/test_filters.py | 84 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 tests/test_filters.py diff --git a/tests/test_filters.py b/tests/test_filters.py new file mode 100644 index 0000000..5918fb0 --- /dev/null +++ b/tests/test_filters.py @@ -0,0 +1,84 @@ +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) + print(initial) + + final = datetime.datetime(2018, 11, 2, 14, 31, 1) + + + print(final) + + test = filter_occurrences_ping_range_date(uuid.hex, db_collection, initial, final) + assert test == {'2018-11-02': 1} From 44cc002c837b28b10cf6ad9e0ae21bdf29ca204a Mon Sep 17 00:00:00 2001 From: Lucas Siqueira Date: Fri, 2 Nov 2018 16:45:14 -0200 Subject: [PATCH 2/3] Create init tests. Signed-off-by: Lucas Siqueira Co-Authored-by: Amanda Muniz --- tests/test_app.py | 23 ++++++++++++++++++++++- tests/test_filters.py | 21 ++++++++------------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/tests/test_app.py b/tests/test_app.py index 1ceb416..0465b36 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,5 +1,26 @@ - 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') + assert response.status_code == 201 + +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 diff --git a/tests/test_filters.py b/tests/test_filters.py index 5918fb0..5d7271b 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -9,10 +9,10 @@ def test_filter_pings_range_of_dates(db_collection): 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()}] @@ -21,10 +21,10 @@ def test_filter_pings_range_of_dates_error(db_collection): 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) @@ -34,7 +34,7 @@ def test_filter_invalid_date_type(db_collection): 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) @@ -44,7 +44,7 @@ def test_filter_valid_date_type(db_collection): # 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)}] @@ -54,7 +54,7 @@ def test_filter_invalid_type_of_range_of_dates(db_collection): 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)]) @@ -69,16 +69,11 @@ def test_filter_valid_type_of_range_of_dates(db_collection): 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) - print(initial) - final = datetime.datetime(2018, 11, 2, 14, 31, 1) - - print(final) - test = filter_occurrences_ping_range_date(uuid.hex, db_collection, initial, final) assert test == {'2018-11-02': 1} From 76f1105aecf0b38c74ac3d430363ef238d28d1d3 Mon Sep 17 00:00:00 2001 From: Lucas Siqueira Date: Fri, 2 Nov 2018 17:12:54 -0200 Subject: [PATCH 3/3] Create test for invalid date range MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lucas Siqueira Co-Authored-by: João Saliba Co-Authored-by: Amanda Muniz --- tests/test_app.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_app.py b/tests/test_app.py index 0465b36..64eff7d 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,3 +1,6 @@ +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('/') @@ -5,7 +8,9 @@ def test_return_200_on_root(client): 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' @@ -22,5 +27,12 @@ def test_valid_pingout(client): 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