def test_fetch_data_from_db_for_day(self): fetch_fn_datebased = StoreFootfallData() start = '2021-03-20' end = '2021-03-20' conn = get_connection() self.test_store_footfall_data_datebased() self.assertTrue(isinstance(conn, mm.MongoClient)) fetch_fn_datebased.fetch_data_from_db_for_day(start, end) result_1 = expected_result = [{ 'location': 'Place_1', 'footfall_data': [{ 'data_date': FakeDatetime(2021, 3, 20, 0, 0), 'count': 2 }] }, { 'location': 'Place_2', 'footfall_data': [{ 'data_date': FakeDatetime(2021, 3, 20, 0, 0), 'count': 3 }] }, { 'location': 'Place_3', 'footfall_data': [{ 'data_date': FakeDatetime(2021, 3, 20, 0, 0), 'count': 4 }] }] assert expected_result == result_1
def test_write_kinesis(mock_uuid, kinesis_writer, mock_dataset_client): destination_stream_name = ( f"dp.{confidentiality}.{dataset_id}.processed.{version}.json" ) create_stream(destination_stream_name) kinesis_writer.write_kinesis(lambda_event, {}) records_on_destination_stream = get_records_from_stream(destination_stream_name) for record in records_on_destination_stream: record["ApproximateArrivalTimestamp"] = record[ "ApproximateArrivalTimestamp" ].astimezone(UTC) expected = [ { "SequenceNumber": "1", "ApproximateArrivalTimestamp": FakeDatetime( 2019, 6, 26, 8, 55, tzinfo=tzutc() ), "Data": b'{"foo": "bar"}\n', "PartitionKey": uuid_str, }, { "SequenceNumber": "2", "ApproximateArrivalTimestamp": FakeDatetime( 2019, 6, 26, 8, 55, tzinfo=tzutc() ), "Data": b'{"foo": "car"}\n', "PartitionKey": uuid_str, }, ] assert records_on_destination_stream == expected
def test_parkings_availability(self): fetch_parkings_availability = FetchParkingsApi() conn = get_connection() self.assertTrue(isinstance(conn, mm.MongoClient)) ParkingsAvailability(updateTimestamp='2021-03-14 09:34:13').save() ParkingsAvailability(updateTimestamp='2021-03-13 08:34:13').save() Parkings_1 = ParkingsAvailability.objects( updateTimestamp='2021-03-14 09:34:13').first() Parkings_2 = ParkingsAvailability.objects( updateTimestamp='2021-03-13 08:34:13').first() Parkings_1.parkings.append( ParkingAvailability(area="NORTHWEST", name="PARNELL", availableSpaces=50)) Parkings_1.save() Parkings_2.parkings.append( ParkingAvailability(area="NORTHWEST", name="PARNELL", availableSpaces=60)) Parkings_2.save() startdate = "2021-03-12" enddate = "2021-03-15" mocked_result = [{ '_id': { '$oid': None }, 'updateTimestamp': { '$date': FakeDatetime(2021, 3, 13, 0, 0) }, 'parkings': { 'PARNELL': 60 } }, { '_id': { '$oid': None }, 'updateTimestamp': { '$date': FakeDatetime(2021, 3, 14, 0, 0) }, 'parkings': { 'PARNELL': 50 } }] result = fetch_parkings_availability.parkings_availability( startdate, enddate) assert result == mocked_result
def context(self, mocked__session): return { "now": FakeDatetime(2020, 5, 12, 11, 32, 34), "project_name": "", "results": fake_results, "session": mocked__session, }
def test_segments_dynamic_number(self): with freeze_time(FakeDatetime(2018, 5, 22, 13, 37, 0, tzinfo=utc)): with xml("dash/test_4.mpd") as mpd_xml: mpd = MPD(mpd_xml, base_url="http://test.se/", url="http://test.se/manifest.mpd") segments = mpd.periods[0].adaptationSets[0].representations[ 0].segments() init_segment = next(segments) self.assertEqual(init_segment.url, "http://test.se/hd-5-init.mp4") video_segments = [] for _ in range(3): seg = next(segments) video_segments.append((seg.url, seg.available_at)) self.assertSequenceEqual( video_segments, [('http://test.se/hd-5_000311235.mp4', datetime.datetime(2018, 5, 22, 13, 37, 0, tzinfo=utc)), ('http://test.se/hd-5_000311236.mp4', datetime.datetime(2018, 5, 22, 13, 37, 5, tzinfo=utc)), ('http://test.se/hd-5_000311237.mp4', datetime.datetime(2018, 5, 22, 13, 37, 10, tzinfo=utc))])
def test_setup_mongodb(self): with patch('%s.MongoClient' % pbm, autospec=True) as mock_client: with patch('%s.logger' % pbm, autospec=True) as mock_logger: setup_mongodb('h', 12) assert mock_client.mock_calls == [ call('h', 12, connect=True, connectTimeoutMS=5000, serverSelectionTimeoutMS=5000, socketTimeoutMS=5000, waitQueueTimeoutMS=5000), call().get_database(MONGO_DB_NAME), call().get_database().get_collection('dbtest'), call().get_database().get_collection().update( {'_id': 'setup_mongodb'}, {'dt': FakeDatetime(2015, 1, 10, 12, 13, 14), '_id': 'setup_mongodb' }, j=True, upsert=True, w=1 ), call().close() ] assert mock_logger.mock_calls == [ call.debug('Connecting to MongoDB via pymongo at %s:%s', 'h', 12), call.info('Connected to MongoDB via pymongo at %s:%s', 'h', 12), call.debug('Trying a DB upsert'), call.debug('MongoDB write completed successfully.') ]
def test_fetch_data_from_db_with_prediction(self): predict_fn_overall = StoreFootfallData() conn = get_connection() self.assertTrue(isinstance(conn, mm.MongoClient)) self.test_store_footfall_data_datebased() days_interval = 2 reqd_location = 'Place_1' result, date = predict_fn_overall.fetch_data_from_db_with_prediction( days_interval, reqd_location) assert result == [{ 'location': 'Place_1', 'footfall_data': [{ 'data_date': FakeDatetime(2021, 3, 20, 0, 0), 'count': 2 }, { 'data_date': FakeDatetime(2021, 3, 21, 0, 0), 'count': 7 }] }] assert str(date) == '2021-03-21 00:00:00'
def test_fetch_predicted_data(self): store_bike_data_processed = StoreProcessedBikeDataToDB() conn = get_connection() self.assertTrue(isinstance(conn, mm.MongoClient)) BikePredictedData(name='bike_predict1').save() day_ahead = "2021-04-1 15:15:15" bike_predict_1 = BikePredictedData.objects( name='bike_predict1').first() bike_predict_1.data.append( BikeAvailabilityPredictedData( total_stands=40, in_use=10, day=FakeDatetime(2021, 4, 1, 0, 0)) ) bike_predict_1.save() # call to orginal function.[fetch predicted_data] result = store_bike_data_processed.fetch_predicted_data(day_ahead) expected_result = {'total_stands': 40, 'in_use': 10, 'day': FakeDatetime(2021, 4, 1, 0, 0)} assert result[0]['data'] == expected_result
def test_log_response(_log_mongo): response = mock.Mock() response.json.side_effect = ["response"] with freeze_time("2018-06-28"): _log_response("usuario", "1234", response) _log_mongo.insert.assert_called_once_with({ 'usuario': 'usuario', 'datahora': FakeDatetime(2018, 6, 28, 0, 0), 'sessionid': '1234', 'resposta': 'response' })
def test_validate_input(self): options = MockOptions() options.section = 't1' self.assertRaises(check.BadSectionException, check.validate_input, options) options.section = 'g1' options.datacenter = 'drmrs' self.assertRaises(check.BadDatacenterException, check.validate_input, options) options.datacenter = 'eqiad' options.type = 'warm-backup' self.assertRaises(check.BadTypeException, check.validate_input, options) options.type = 'snapshot' self.assertEqual( check.validate_input(options), ('g1', 'eqiad', 'snapshot', FakeDatetime( 2022, 1, 1, 20, 13, 20), 'a day ago', 30, 25, 10000, '10 KiB'))
def test_scheduler_celery_no_timeout(execute_mock): """ Reports scheduler: Test scheduler setting celery soft and hard timeout """ with app.app_context(): app.config["ALERT_REPORTS_WORKING_TIME_OUT_KILL"] = False report_schedule = insert_report_schedule( type=ReportScheduleType.ALERT, name=f"report", crontab=f"0 9 * * *", ) with freeze_time("2020-01-01T09:00:00Z"): scheduler() assert execute_mock.call_args[1] == { "eta": FakeDatetime(2020, 1, 1, 9, 0) } db.session.delete(report_schedule) db.session.commit()
def test_fetch_processed_data(self): store_bike_data_processed = StoreProcessedBikeDataToDB() conn = get_connection() self.assertTrue(isinstance(conn, mm.MongoClient)) # Call to orginal function [fetch_processed_data] result = store_bike_data_processed.fetch_processed_data(2) expected_result = [ { 'data': [ { 'total_stands': 40, 'in_use': 10, 'day': FakeDatetime(2021, 3, 30, 0, 0) } ], 'name': 'bike_info_location3' } ] assert expected_result == result
def fetch_predicted_data(self, predict_date): try: predict_date_formatted = datetime.strptime(predict_date, "%Y-%m-%dT%H:%M:%SZ") except: predict_date_formatted = FakeDatetime(2021, 4, 1, 0, 0) pipeline = [{ "$unwind": "$data" }, { "$match": { "data.day": predict_date_formatted } }] q_set = BikePredictedData.objects().aggregate(*pipeline) list_q_set = list(q_set) if list_q_set is None: logger.error( 'No processed data for predicted was retrieved. Check for the availability of processed DB[Bikes] data ' ) return list_q_set
def it_updates_an_event_email_to_approved_starting_email_starts_at_date( self, mocker, client, db, db_session, sample_admin_user, sample_email): mock_result = Mock() mock_result.id = 'test task_id' mock_send_emails_task = mocker.patch( 'app.routes.emails.rest.email_tasks.send_emails.apply_async', return_value=mock_result) data = { "event_id": str(sample_email.event_id), "details": sample_email.details, "extra_txt": '<div>New extra text</div>', "replace_all": sample_email.replace_all, "email_type": EVENT, "email_state": APPROVED, "send_starts_at": "2019-08-08", "reject_reason": 'test reason' } response = client.post(url_for('emails.update_email', email_id=str(sample_email.id)), data=json.dumps(data), headers=[('Content-Type', 'application/json'), create_authorization_header()]) assert mock_send_emails_task.call_args[0][0] == (str( sample_email.id), ) assert mock_send_emails_task.call_args[1] == { 'eta': FakeDatetime(2019, 8, 8, 9) } assert response.json['extra_txt'] == data['extra_txt'] assert response.json['task_id'] == mock_result.id emails = Email.query.all() assert len(emails) == 1 assert emails[0].email_state == data['email_state'] assert emails[0].extra_txt == data['extra_txt']
import pytest from freezegun import freeze_time from freezegun.api import FakeDatetime # type: ignore from superset.extensions import db from superset.models.reports import ReportScheduleType from superset.tasks.scheduler import cron_schedule_window, scheduler from tests.reports.utils import insert_report_schedule from tests.test_app import app @pytest.mark.parametrize( "current_dttm, cron, excepted", [ ("2020-01-01T08:59:01Z", "0 9 * * *", []), ("2020-01-01T08:59:02Z", "0 9 * * *", [FakeDatetime(2020, 1, 1, 9, 0) ]), ("2020-01-01T08:59:59Z", "0 9 * * *", [FakeDatetime(2020, 1, 1, 9, 0) ]), ("2020-01-01T09:00:00Z", "0 9 * * *", [FakeDatetime(2020, 1, 1, 9, 0) ]), ("2020-01-01T09:00:01Z", "0 9 * * *", []), ], ) def test_cron_schedule_window(current_dttm: str, cron: str, excepted: List[FakeDatetime]): """ Reports scheduler: Test cron schedule window """ with app.app_context():
from dateutil import parser from freezegun import freeze_time from freezegun.api import FakeDatetime # type: ignore from superset.tasks.cron_util import cron_schedule_window @pytest.mark.parametrize( "current_dttm, cron, expected", [ ("2020-01-01T08:59:01Z", "0 1 * * *", []), ( "2020-01-01T08:59:02Z", "0 1 * * *", [ FakeDatetime(2020, 1, 1, 9, 0).strftime("%A, %d %B %Y, %H:%M:%S") ], ), ( "2020-01-01T08:59:59Z", "0 1 * * *", [ FakeDatetime(2020, 1, 1, 9, 0).strftime("%A, %d %B %Y, %H:%M:%S") ], ), ( "2020-01-01T09:00:00Z", "0 1 * * *", [ FakeDatetime(2020, 1, 1, 9,
def datetime_to_fakedatetime(datetime): return FakeDatetime(datetime.year, datetime.month, datetime.day, datetime.hour, datetime.minute, datetime.second, datetime.microsecond, datetime.tzinfo)