示例#1
0
def __save_update_timestamp(session: scoped_session,
                            type: CachedDataType,
                            id: str = "") -> None:
    """Stores the current time as the timestamp of the last update
    for a given CachedDataType and id pair"""
    ts = session.query(CacheTimestamp).filter(
        CacheTimestamp.data_type == type).filter(
            CacheTimestamp.data_id == id).one_or_none()
    if ts == None:
        session.add(CacheTimestamp(data_type=type, data_id=id))
    else:
        ts.update_time = datetime.utcnow()
    session.commit()
示例#2
0
def test_it_will_send_event_if_email_address_is_updated(mock_publisher: MagicMock, profile: Profile,
                                                        session: scoped_session,
                                                        commit: Callable[[], None]):
    event_publisher = send_update_events(publisher=mock_publisher)
    models_committed.connect(receiver=event_publisher)

    profile.add_email_address('*****@*****.**')
    session.add(profile)

    commit()

    assert mock_publisher.publish.call_count == 1
    assert mock_publisher.publish.call_args[0][0] == {'id': '12345678', 'type': 'profile'}
示例#3
0
def __cache_arrivals(session: scoped_session, naptan_id: str) -> None:
    """Fetches the arrivals for a single stop point from TFL and stores in the database"""
    logger = logging.getLogger(__name__)
    arrivals = fetch_arrivals(naptan_id)
    logger.info(f"Adding arrivals for '{naptan_id}' to database")
    for arrival in arrivals:
        db_arrival = session.query(Arrival).filter(
            Arrival.arrival_id == arrival.arrival_id).one_or_none()
        if db_arrival is not None:
            db_arrival.update_with(arrival)
        else:
            session.add(arrival)
    session.commit()
示例#4
0
def __cache_stop_point(session: scoped_session, naptan_id: str) -> None:
    """Fetches the data for a single stop point from TFL and stores in the database"""
    logger = logging.getLogger(__name__)
    stops = fetch_stops(naptan_id)
    for stop in stops:
        #logger.debug(f"Adding stop '{stop.name}', '{stop.indicator}' to database")
        # TODO This should do a proper update instead of remove-instert
        session.query(StopPoint).filter(
            StopPoint.naptan_id == stop.naptan_id).delete()
        __save_update_timestamp(session, CachedDataType.stop_point,
                                stop.naptan_id)
        session.add(stop)
    session.commit()
def test_it_ignores_other_models_being_committed(orcid_token: OrcidToken,
                                                 orcid_config: Dict[str, str],
                                                 mock_orcid_client: MagicMock,
                                                 session: scoped_session,
                                                 url_safe_serializer: URLSafeSerializer):
    webhook_maintainer = maintain_orcid_webhook(orcid_config, mock_orcid_client,
                                                url_safe_serializer)
    models_committed.connect(receiver=webhook_maintainer)

    session.add(orcid_token)
    session.commit()

    assert mock_orcid_client.set_webhook.call_count == 0
    assert mock_orcid_client.remove_webhook.call_count == 0
示例#6
0
def test_it_will_send_event_for_affiliation_insert(mock_publisher: MagicMock, profile: Profile,
                                                   session: scoped_session,
                                                   commit: Callable[[], None]) -> None:
    event_publisher = send_update_events(publisher=mock_publisher)
    models_committed.connect(receiver=event_publisher)

    affiliation = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017))

    profile.add_affiliation(affiliation)
    session.add(profile)

    commit()

    assert mock_publisher.publish.call_count == 1
    assert mock_publisher.publish.call_args[0][0] == {'id': '12345678', 'type': 'profile'}
示例#7
0
def test_exception_is_handled_by_catch_exception_decorator(mock_publisher: MagicMock,
                                                           profile: Profile,
                                                           session: scoped_session,
                                                           commit: Callable[[], None]) -> None:
    mock_publisher.publish.side_effect = Exception('Some Exception')

    event_publisher = send_update_events(publisher=mock_publisher)
    models_committed.connect(receiver=event_publisher)

    affiliation = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017))

    profile.add_affiliation(affiliation)
    session.add(profile)

    commit()

    assert mock_publisher.publish.call_count == 1
示例#8
0
def update_item(db_session: scoped_session, query_data: dict, trial_results: dict, mab: MAB) -> None or str:
    """
    Check results of MAB trial against data in db, update db with trial results if necessary,
    as well as the pickled mab.

    If the trial has ended, set the best preforming thumbnail to be `next`, set `active_trial`
    to False, and delete all options associated with the item.
    """

    # Re-pickle mab class test instance
    query_data["item"].mab = pickle.dumps(mab)

    # If there are more trials, set the next url
    if trial_results["new_trial"]:
        if trial_results["choice"] != query_data["item"].next:
            # item.id is already hashed with project, just need to add option_num
            next_id = hash_id(query_data["item"].id, trial_results["choice"])
            query_data["item"].option_num = trial_results["choice"]

            # Find the next image using the id we get from `trial_results`
            try:
                option = Option.query.get(next_id)
                query_data["item"].next = option.content
            except:
                return "500 - internal server error"

    # The trial is over, set `next` to the best preforming thumbnail and `active_trial` to False
    else:
        query_data["item"].next = trial_results["best"]
        query_data["item"].active_trial = False

        # Delete all options as they are no longer needed
        for i in range(1, query_data["item"].total_num + 1):
            # item.id is already hashed, just add option_num
            option_id = hash_id(query_data["item"].id, i)
            Option.query.filter_by(id=option_id).delete()

        query_data["item"].total_num = 0

    try:
        db_session.add(query_data["item"])
        db_session.commit()
        return "200 - trial successfully updated."
    except:
        return "500 - internal server error."
def test_it_sets_a_webhook_when_a_profile_is_inserted(profile: Profile,
                                                      orcid_config: Dict[str, str],
                                                      mock_orcid_client: MagicMock,
                                                      session: scoped_session,
                                                      url_safe_serializer: URLSafeSerializer,
                                                      commit: Callable[[], None]):
    webhook_maintainer = maintain_orcid_webhook(orcid_config, mock_orcid_client,
                                                url_safe_serializer)
    models_committed.connect(receiver=webhook_maintainer)

    session.add(profile)

    commit()

    assert mock_orcid_client.set_webhook.call_count == 1
    assert mock_orcid_client.set_webhook.call_args[0][0] == '0000-0002-1825-0097'
    assert mock_orcid_client.set_webhook.call_args[0][1] == 'http://localhost/orcid-webhook/{}' \
        .format(url_safe_serializer.dumps('0000-0002-1825-0097'))
def test_exception_is_handled_by_catch_exception_decorator(profile: Profile,
                                                           orcid_config: Dict[str, str],
                                                           mock_orcid_client: MagicMock,
                                                           session: scoped_session,
                                                           url_safe_serializer: URLSafeSerializer,
                                                           commit: Callable[[], None]):
    mock_orcid_client.remove_webhook.side_effect = Exception('Some Exception')

    session.add(profile)
    commit()

    webhook_maintainer = maintain_orcid_webhook(orcid_config, mock_orcid_client,
                                                url_safe_serializer)
    models_committed.connect(receiver=webhook_maintainer)

    session.delete(profile)
    commit()

    assert mock_orcid_client.remove_webhook.call_count == 1
示例#11
0
def get_arrivals(session: scoped_session, naptan_id: StopId) -> List[Arrival]:
    try:
        __refresh_arrivals(session, naptan_id)
    except exc.DataError as e:
        logger(f"Cannot refresh arrival info for {naptan_id}:", e)
        return []

    req = session.query(ArrivalRequest).filter(
        ArrivalRequest.naptan_id == naptan_id).one_or_none()
    if req == None:
        session.add(ArrivalRequest(naptan_id=naptan_id))
    else:
        req.request_time = datetime.utcnow()
    session.commit()

    return session.query(Arrival).\
        filter(Arrival.naptan_id == naptan_id).\
        filter(Arrival.ttl > datetime.utcnow()).\
        order_by(Arrival.expected).\
        limit(6).all()