def get_pingouts_occur_range_date(pingout_uuid): if validate_uuid(pingout_uuid): pingout = collection.find_one({'uuid': pingout_uuid}) if pingout: initial = request.args.get('initial_date') final = request.args.get('final_date') try: initial = parser.parse(initial) final = parser.parse(final) except TypeError: return Response(status=400) try: query = filter_occurrences_ping_range_date( pingout_uuid, collection, initial, final) except KeyError: response = jsonify( error= "Pingout without pings received on this range of date!" ) response.status_code = 400 return response filename = '{}.csv'.format(pingout_uuid) from_json_to_csv(query, filename) url = url_for('download_file', pingout_uuid=pingout_uuid, filename=filename, _external=True) response = jsonify(message="File created with success!", url=url) response.status_code = 200 return response else: return Response(status=404)
def test_filter_occurrences_ping_range_date_error_date_type(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_occurrences_ping_range_date(uuid.hex, db_collection, initial, final) assert 'Invalid date type' in str(excinfo.value)
def get_pingouts_occur_range_date(pingout_uuid): if validate_uuid(pingout_uuid): pingout = collection.find_one({'uuid': pingout_uuid}) if pingout: initial = request.args.get('initial_date') final = request.args.get('final_date') try: initial = parser.parse(initial) final = parser.parse(final) except TypeError: return Response(status=400) query = filter_occurrences_ping_range_date( pingout_uuid, collection, initial, final) from_json_to_csv(query, "{}.csv".format(pingout_uuid)) return redirect('/{}/download'.format(pingout_uuid)) else: return Response(status=404)
def test_filter_occurrences_ping_range_date(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() final = datetime.datetime.today() test = filter_occurrences_ping_range_date(uuid.hex, db_collection, initial, final) assert type(test) == dict
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}
def test_number_of_occurrences_ping_range_date(pingout, db_collection, today): """ Filter number of occurrences of dates on pings list """ db_collection.update_one({'uuid': pingout}, {'$push': { 'pings': { 'count': 2, 'date': today } }}) db_collection.update_one({'uuid': pingout}, { '$push': { 'pings': { 'count': 3, 'date': datetime.datetime(2001, 8, 17, 0, 0) } } }) db_collection.update_one({'uuid': pingout}, { '$push': { 'pings': { 'count': 3, 'date': datetime.datetime(2018, 8, 17, 0, 0) } } }) db_collection.update_one({'uuid': pingout}, {'$push': { 'pings': { 'count': 2, 'date': today } }}) initial = datetime.datetime(2001, 1, 1, 0, 0) final = datetime.datetime(2018, 12, 20, 0, 0) pings = filter_occurrences_ping_range_date(pingout, db_collection, initial, final) assert pings[str(today.date())] == 2 assert pings[str(datetime.datetime(2001, 8, 17, 0, 0).date())] == 1 assert pings[str(datetime.datetime(2018, 8, 17, 0, 0).date())] == 1