Beispiel #1
0
 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
Beispiel #2
0
 def _mark_model_run_interpolated(self,
                                  model_run: PredictionModelRunTimestamp):
     """ Having completely processed a model run, we can mark it has having been interpolated.
     """
     model_run.interpolated = True
     logger.info('marking %s as interpolated', model_run)
     self.session.add(model_run)
     self.session.commit()
Beispiel #3
0
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)
Beispiel #4
0
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)
Beispiel #5
0
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)
Beispiel #6
0
def create_prediction_run(
    session: Session, prediction_model_id: int,
    prediction_run_timestamp: datetime.datetime
) -> PredictionModelRunTimestamp:
    """ Create a model prediction run for a particular model.
    """
    prediction_run = PredictionModelRunTimestamp(
        prediction_model_id=prediction_model_id,
        prediction_run_timestamp=prediction_run_timestamp)
    session.add(prediction_run)
    session.commit()
    return prediction_run
Beispiel #7
0
 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))