def mock_get_session(*args) -> UnifiedAlchemyMagicMock: """ return a session with a bare minimum database that should be good for most unit tests. """ prediction_model = PredictionModel( id=1, abbreviation='GDPS', projection='latlon.15x.15', name='Global Deterministic Prediction System') prediction_model_run = PredictionModelRunTimestamp( id=1, prediction_model_id=1, prediction_run_timestamp=time_utils.get_utc_now(), prediction_model=prediction_model, complete=True) session = UnifiedAlchemyMagicMock( data=[( [ mock.call.query(PredictionModel), mock.call.filter( PredictionModel.abbreviation == 'GDPS', PredictionModel.projection == 'latlon.15x.15') ], [prediction_model], ), ([mock.call.query(PredictionModelRunTimestamp)], [prediction_model_run])]) return session
def mock_session(monkeypatch): """ Mocked out sqlalchemy session object """ geom = ( "POLYGON ((-120.525 50.77500000000001, -120.375 50.77500000000001,-120.375 50.62500000000001," " -120.525 50.62500000000001, -120.525 50.77500000000001))") shape = shapely.wkt.loads(geom) gdps_url = ( 'https://dd.weather.gc.ca/model_gem_global/15km/grib2/lat_lon/00/000/' 'CMC_glb_TMP_TGL_2_latlon.15x.15_2021020300_P000.grib2') gdps_processed_model_run = ProcessedModelRunUrl(url=gdps_url) gdps_prediction_model = PredictionModel( id=1, abbreviation='GDPS', projection='latlon.15x.15', name='Global Deterministic Prediction System') gdps_prediction_model_run = PredictionModelRunTimestamp( id=1, prediction_model_id=1, prediction_run_timestamp=time_utils.get_utc_now(), prediction_model=gdps_prediction_model, complete=True) @contextmanager def mock_get_session_gdps_scope(*args) -> Generator[Session, None, None]: yield UnifiedAlchemyMagicMock( data=[( [ mock.call.query(PredictionModel), mock.call.filter( PredictionModel.abbreviation == 'GDPS', PredictionModel.projection == 'latlon.15x.15') ], [gdps_prediction_model], ), ([ mock.call.query(ProcessedModelRunUrl), mock.call.filter(ProcessedModelRunUrl.url == gdps_url) ], [gdps_processed_model_run]), ([mock.call.query(PredictionModelRunTimestamp)], [gdps_prediction_model_run]), ([mock.call.query(PredictionModelGridSubset)], [ PredictionModelGridSubset( id=1, prediction_model_id=gdps_prediction_model.id, geom=from_shape(shape)) ])]) def mock_get_gdps_prediction_model_run_timestamp_records(*args, **kwargs): return [(gdps_prediction_model_run, gdps_prediction_model)] monkeypatch.setattr(app.db.database, 'get_write_session_scope', mock_get_session_gdps_scope) monkeypatch.setattr(env_canada, 'get_prediction_model_run_timestamp_records', mock_get_gdps_prediction_model_run_timestamp_records)
def mock_session(monkeypatch): """ Mocked out sqlalchemy session object """ geom = ( "POLYGON ((-120.525 50.77500000000001, -120.375 50.77500000000001,-120.375 50.62500000000001," " -120.525 50.62500000000001, -120.525 50.77500000000001))") shape = shapely.wkt.loads(geom) rdps_url = ( 'https://dd.weather.gc.ca/model_gem_regional/10km/grib2/00/034/' 'CMC_reg_RH_TGL_2_ps10km_2020110500_P034.grib2') rdps_processed_model_run = ProcessedModelRunUrl(url=rdps_url) rdps_prediction_model = PredictionModel( id=2, abbreviation='RDPS', projection='ps10km', name='Regional Deterministic Prediction System') rdps_prediction_model_run = PredictionModelRunTimestamp( id=1, prediction_model_id=2, prediction_run_timestamp=time_utils.get_utc_now(), prediction_model=rdps_prediction_model, complete=True) @contextmanager def mock_get_session_rdps_scope(*args): yield UnifiedAlchemyMagicMock( data=[( [ mock.call.query(PredictionModel), mock.call.filter(PredictionModel.abbreviation == 'RDPS', PredictionModel.projection == 'ps10km') ], [rdps_prediction_model], ), ([ mock.call.query(ProcessedModelRunUrl), mock.call.filter(ProcessedModelRunUrl.url == rdps_url) ], [rdps_processed_model_run]), ([mock.call.query(PredictionModelRunTimestamp)], [rdps_prediction_model_run]), ([mock.call.query(PredictionModelGridSubset)], [ PredictionModelGridSubset( id=1, prediction_model_id=rdps_prediction_model.id, geom=from_shape(shape)) ])]) def mock_get_rdps_prediction_model_run_timestamp_records(*args, **kwargs): return [(rdps_prediction_model_run, rdps_prediction_model)] monkeypatch.setattr(app.db.database, 'get_write_session_scope', mock_get_session_rdps_scope) monkeypatch.setattr(env_canada, 'get_prediction_model_run_timestamp_records', mock_get_rdps_prediction_model_run_timestamp_records)
def mock_session(monkeypatch): """ Mocked out sqlalchemy session object """ geom = ( "POLYGON ((-120.525 50.77500000000001, -120.375 50.77500000000001,-120.375 50.62500000000001," " -120.525 50.62500000000001, -120.525 50.77500000000001))") shape = shapely.wkt.loads(geom) hrdps_url = 'https://dd.weather.gc.ca/model_hrdps/continental/grib2/00/007/' \ + 'CMC_hrdps_continental_TMP_TGL_2_ps2.5km_2020100700_P007-00.grib2' hrdps_processed_model_run = ProcessedModelRunUrl(url=hrdps_url) hrdps_prediction_model = PredictionModel( id=3, abbreviation='HRDPS', projection='ps2.5km', name='High Resolution Deterministic Prediction System') hrdps_prediction_model_run = PredictionModelRunTimestamp( id=1, prediction_model_id=3, prediction_run_timestamp=time_utils.get_utc_now(), prediction_model=hrdps_prediction_model, complete=True) @contextmanager def mock_get_session_hrdps_scope(*args): yield UnifiedAlchemyMagicMock( data=[( [ mock.call.query(PredictionModel), mock.call.filter(PredictionModel.abbreviation == 'HRDPS', PredictionModel.projection == 'ps2.5km') ], [hrdps_prediction_model], ), ([ mock.call.query(ProcessedModelRunUrl), mock.call.filter(ProcessedModelRunUrl == hrdps_url) ], [hrdps_processed_model_run]), ([mock.call.query(PredictionModelRunTimestamp)], [hrdps_prediction_model_run]), ([mock.call.query(PredictionModelGridSubset)], [ PredictionModelGridSubset( id=1, prediction_model_id=hrdps_prediction_model.id, geom=from_shape(shape)) ])]) def mock_get_hrdps_prediction_model_run_timestamp_records(*args, **kwargs): return [(hrdps_prediction_model_run, hrdps_prediction_model)] monkeypatch.setattr(app.db.database, 'get_write_session_scope', mock_get_session_hrdps_scope) monkeypatch.setattr(app.weather_models.env_canada, 'get_prediction_model_run_timestamp_records', mock_get_hrdps_prediction_model_run_timestamp_records)
def given_an_instance() -> machine_learning.StationMachineLearning: """ Bind the data variable """ # super weird bug? with pytest_bdd that hooked into isoformat on the coordinate and points fields. # tried forever to figure out why - and gave up in the end. removed coordinate and point from # feature file and just loading it in here. coordinate = _load_json_file(__file__, 'coordinate.json') points = _load_json_file(__file__, 'points.json') return machine_learning.StationMachineLearning( session=None, model=PredictionModel(id=1), grid=PredictionModelGridSubset(id=1), points=points, target_coordinate=coordinate, station_code=None, max_learn_date=datetime.now())
def mock_session(monkeypatch, data): """ Mocked out sqlalchemy session """ if data: dirname = os.path.dirname(os.path.realpath(__file__)) filename = os.path.join(dirname, data) with open(filename) as data_file: json_data = json.load(data_file) predictions = json_data['predictions'] geometry = json_data['geometry'] prediction_model = PredictionModel( id=1, name='name', abbreviation='abbrev', projection='projection') def mock_get_session(*args): mock_session = UnifiedAlchemyMagicMock() return mock_session def mock_get_most_recent_model_run(*args) -> PredictionModelRunTimestamp: timestamp = '2020-01-22T18:00:00+00:00' return PredictionModelRunTimestamp(id=1, prediction_model=prediction_model, prediction_run_timestamp=datetime.fromisoformat(timestamp)) def mock_get_model_run_predictions(*args): shape = shapely.wkt.loads(geometry) grid = PredictionModelGridSubset( id=1, prediction_model_id=prediction_model.id, prediction_model=prediction_model, geom=from_shape(shape) ) result = [] for prediction in predictions: prediction['prediction_timestamp'] = datetime.fromisoformat( prediction['prediction_timestamp']) result.append( (grid, ModelRunGridSubsetPrediction(**prediction))) return result monkeypatch.setattr(app.db.database, 'get_session', mock_get_session) monkeypatch.setattr(app.db.crud, 'get_most_recent_model_run', mock_get_most_recent_model_run) monkeypatch.setattr(app.db.crud, 'get_model_run_predictions', mock_get_model_run_predictions)
def mock_get_session(*args): url = ( 'https://dd.weather.gc.ca/model_gem_global/15km/grib2/lat_lon/00/000/' 'CMC_glb_TMP_TGL_2_latlon.15x.15_2021020300_P000.grib2') return UnifiedAlchemyMagicMock(data=[( [ mock.call.query(PredictionModel), mock.call.filter(PredictionModel.abbreviation == 'GDPS', PredictionModel.projection == 'latlon.15x.15') ], [PredictionModel(abbreviation='GDPS', projection='latlon.15x.15')], ), ([ mock.call.query( ProcessedModelRunUrl), mock.call.filter( ProcessedModelRunUrl.url == url) ], [ProcessedModelRunUrl()])])
def mock_get_session(*args): prediction_model = PredictionModel( id=1, abbreviation='GDPS', projection='latlon.15x.15', name='Global Deterministic Prediction System') geometry = ( "POLYGON ((-120.525 50.77500000000001, -120.375 50.77500000000001," "-120.375 50.62500000000001, -120.525 50.62500000000001, -120.525 50.77500000000001))" ) shape = shapely.wkt.loads(geometry) grid = PredictionModelGridSubset( id=1, prediction_model_id=prediction_model.id, prediction_model=prediction_model, geom=from_shape(shape)) date_1 = "2020-07-22T18:00:00+00:00" date_2 = "2020-07-22T20:00:00+00:00" data = [( [ mock.call.query(PredictionModelGridSubset, ModelRunGridSubsetPrediction, PredictionModel) ], [ # 3 on the same hour, testing interpolation and percentiles. [ grid, ModelRunGridSubsetPrediction( prediction_model_grid_subset_id=grid.id, prediction_timestamp=datetime.fromisoformat(date_1), tmp_tgl_2=[10, 11, 12, 13], rh_tgl_2=[20, 30, 40, 50]), prediction_model ], [ grid, ModelRunGridSubsetPrediction( prediction_model_grid_subset_id=grid.id, prediction_timestamp=datetime.fromisoformat(date_1), tmp_tgl_2=[11, 12, 13, 14], rh_tgl_2=[30, 40, 50, 60]), prediction_model ], [ grid, ModelRunGridSubsetPrediction( prediction_model_grid_subset_id=grid.id, prediction_timestamp=datetime.fromisoformat(date_1), tmp_tgl_2=[9, 10, 11, 12], rh_tgl_2=[20, 30, 40, 50]), prediction_model ], # 1 on the hour, testing it remains unchanged. [ grid, ModelRunGridSubsetPrediction( prediction_model_grid_subset_id=grid.id, prediction_timestamp=datetime.fromisoformat( "2020-07-22T19:00:00+00:00"), tmp_tgl_2=[9, 9, 9, 9], rh_tgl_2=[20, 20, 20, 20]), prediction_model ], # 3 on same hour, same values at each grid point for easy percentile testing. [ grid, ModelRunGridSubsetPrediction( prediction_model_grid_subset_id=grid.id, prediction_timestamp=datetime.fromisoformat(date_2), tmp_tgl_2=[9, 9, 9, 9], rh_tgl_2=[20, 20, 20, 20]), prediction_model ], [ grid, ModelRunGridSubsetPrediction( prediction_model_grid_subset_id=grid.id, prediction_timestamp=datetime.fromisoformat(date_2), tmp_tgl_2=[10, 10, 10, 10], rh_tgl_2=[21, 21, 21, 21]), prediction_model ], [ grid, ModelRunGridSubsetPrediction( prediction_model_grid_subset_id=grid.id, prediction_timestamp=datetime.fromisoformat(date_2), tmp_tgl_2=[11, 11, 11, 11], rh_tgl_2=[22, 22, 22, 22]), prediction_model ] ])] mock_session = UnifiedAlchemyMagicMock(data=data) return mock_session