Exemplo n.º 1
0
def test_list_stale_entities(
    db_session, add_model, feed_1_1, route_1_1, route_1_2, route_1_3
):
    update_1 = add_model(models.FeedUpdate(feed=feed_1_1))
    update_2 = add_model(models.FeedUpdate(feed=feed_1_1))
    route_1_1.source = update_1
    route_1_2.source = update_2
    route_1_3.source = update_2
    db_session.flush()

    assert [route_1_1] == genericqueries.list_stale_entities(models.Route, update_2)
Exemplo n.º 2
0
def test_get_id_to_pk_map_by_feed_pk__no_matches(
    db_session, add_model, feed_1_1, feed_1_2,
):
    update_1 = add_model(models.FeedUpdate(feed=feed_1_1))
    update_2 = add_model(models.FeedUpdate(feed=feed_1_2))
    db_session.flush()

    expected = {}

    actual = genericqueries.get_id_to_pk_map_by_feed_pk(models.Route, feed_1_2.pk)

    assert expected == actual
Exemplo n.º 3
0
def test_delete_stale_entities(
    db_session, add_model, system_1, feed_1_1, route_1_1, route_1_2, route_1_3
):
    # This breaks the test and hence the method:
    # add_model(models.Trip(route=route_1_1))
    update_1 = add_model(models.FeedUpdate(feed=feed_1_1))
    update_2 = add_model(models.FeedUpdate(feed=feed_1_1))
    route_1_1.source = update_1
    route_1_2.source = update_2
    route_1_3.source = update_2
    db_session.flush()

    genericqueries.delete_stale_entities(models.Route, update_2)

    assert [route_1_2, route_1_3] == routequeries.list_in_system(system_1.id)
Exemplo n.º 4
0
def test_install(
    mock_systemdam,
    inline_unit_of_work,
    monkeypatch,
    feed_update_status,
    delete_called,
    final_system_status,
    delete_fail,
):

    _delete_feed = mock.MagicMock()
    if delete_fail:
        _delete_feed.side_effect = ValueError
    monkeypatch.setattr(systemservice, "_delete_feed", _delete_feed)
    monkeypatch.setattr(
        systemservice,
        "_install_system_configuration",
        lambda *args, **kwargs: ([FEED_ID_1], [FEED_ID_2]),
    )
    monkeypatch.setattr(client, "refresh_tasks", lambda: None)
    monkeypatch.setattr(
        updatemanager,
        "execute_feed_update",
        lambda *args, **kwargs:
        (models.FeedUpdate(status=feed_update_status), None),
    )
    monkeypatch.setattr(updatemanager, "create_feed_update", mock.MagicMock())

    systemservice.install(SYSTEM_ID, "adsf", {"key": "value"}, None)

    system = mock_systemdam.get_by_id(SYSTEM_ID)
    assert system.status is final_system_status

    if delete_called:
        _delete_feed.assert_called_once()
Exemplo n.º 5
0
def feed_2_1_update_1(add_model, feed_2_1):
    return add_model(
        models.FeedUpdate(
            feed=feed_2_1,
            status=models.FeedUpdate.Status.SUCCESS,
            completed_at=datetime.datetime(2011, 1, 1, 5, 0, 0),
        ))
Exemplo n.º 6
0
def scheduled_service_1(add_model, system_1, feed_1_1):
    feed_update = add_model(models.FeedUpdate(feed=feed_1_1))
    return add_model(
        models.ScheduledService(pk=811,
                                id="812",
                                system=system_1,
                                source=feed_update))
Exemplo n.º 7
0
def feed_1_1_update_3(add_model, feed_1_1):
    return add_model(
        models.FeedUpdate(
            pk=355,
            feed=feed_1_1,
            status=models.FeedUpdate.Status.FAILURE,
            completed_at=datetime.datetime(2011, 1, 1, 5, 0, 0),
        ))
Exemplo n.º 8
0
def feed_1_1_update_2(add_model, feed_1_1):
    return add_model(
        models.FeedUpdate(
            pk=353,
            feed=feed_1_1,
            status=models.FeedUpdate.Status.SUCCESS,
            completed_at=datetime.datetime(2011, 1, 1, 3, 0, 0),
            content_hash="BLAH",
        ))
Exemplo n.º 9
0
def test_list_updates_in_feed(monkeypatch, feed_1_model):
    update_1 = models.FeedUpdate(feed=feed_1_model)
    update_2 = models.FeedUpdate(feed=feed_1_model)

    monkeypatch.setattr(feedqueries, "get_in_system_by_id",
                        lambda *args: feed_1_model)
    monkeypatch.setattr(feedqueries, "list_updates_in_feed",
                        lambda *args: [update_1, update_2])

    expected = [
        views.FeedUpdate.from_model(update_1),
        views.FeedUpdate.from_model(update_2),
    ]

    actual = feedservice.list_updates_in_feed(feed_1_model.system.id,
                                              feed_1_model.id)

    assert expected == actual
Exemplo n.º 10
0
def test_get_id_to_pk_map_by_feed_pk(
    db_session, add_model, feed_1_1, feed_1_2, route_1_1, route_1_2, route_1_3
):
    update_1 = add_model(models.FeedUpdate(feed=feed_1_1))
    update_2 = add_model(models.FeedUpdate(feed=feed_1_2))
    route_1_1.source = update_1
    route_1_2.source = update_2
    route_1_3.source = update_2
    db_session.flush()

    expected = {
        route_1_2.id: route_1_2.pk,
        route_1_3.id: route_1_3.pk,
    }

    actual = genericqueries.get_id_to_pk_map_by_feed_pk(models.Route, feed_1_2.pk)

    assert expected == actual
Exemplo n.º 11
0
def _create_feed_update_helper(system_id, feed_id,
                               update_type) -> typing.Optional[int]:
    feed = feedqueries.get_in_system_by_id(system_id, feed_id)
    if feed is None:
        return None
    feed_update = models.FeedUpdate()
    feed_update.update_type = update_type
    feed_update.status = feed_update.Status.SCHEDULED
    feed_update.feed = feed
    dbconnection.get_session().add(feed_update)
    dbconnection.get_session().flush()
    return feed_update.pk
Exemplo n.º 12
0
def test_execute_feed_update(
    monkeypatch,
    inline_unit_of_work,
    custom_parser,
    feed_content,
    previous_content,
    expected_status,
    expected_explanation,
):
    system = models.System(id=SYSTEM_ID)
    feed = models.Feed(id=FEED_ID,
                       system=system,
                       custom_parser=custom_parser,
                       url=URL,
                       headers="{}")
    feed_update = models.FeedUpdate(feed=feed)

    response = mock.MagicMock()
    if feed_content is None:
        response.raise_for_status.side_effect = requests.exceptions.RequestException(
        )
    else:
        response.content = feed_content

    def get(*args, **kwargs):
        return response

    monkeypatch.setattr(requests, "get", get)

    def get_update_by_pk(feed_update_pk):
        return feed_update

    def get_last_successful_update(*args, **kwargs):
        if previous_content is None:
            return None
        m = hashlib.md5()
        m.update(previous_content)
        return m.hexdigest()

    monkeypatch.setattr(feedqueries, "get_update_by_pk", get_update_by_pk)
    monkeypatch.setattr(feedqueries, "get_last_successful_update_hash",
                        get_last_successful_update)
    monkeypatch.setattr(import_, "run_import", lambda: (0, 0, 0))

    feed_update, _ = updatemanager.execute_feed_update(1)

    assert feed_update.status == expected_status
    assert feed_update.result == expected_explanation
Exemplo n.º 13
0
def test_execute_feed_update__success_or_sync_error(inline_unit_of_work,
                                                    monkeypatch, sync_error,
                                                    expected_status,
                                                    expected_explanation):

    system = models.System(id=SYSTEM_ID)
    feed = models.Feed(id=FEED_ID,
                       system=system,
                       custom_parser="custom_parser",
                       url=URL,
                       headers="{}")
    feed_update = models.FeedUpdate(feed=feed)

    response = mock.MagicMock()
    response.content = b"a"
    monkeypatch.setattr(requests, "get", lambda *args, **kwargs: response)

    monkeypatch.setattr(feedqueries, "get_update_by_pk",
                        lambda *args: feed_update)
    monkeypatch.setattr(feedqueries, "get_last_successful_update_hash",
                        lambda *args: None)

    class Parser(parse.TransiterParser):
        def load_content(self, content: bytes):
            pass

    monkeypatch.setattr(updatemanager, "_get_parser", lambda *args: Parser())

    def sync_func(feed_update_pk, entities):
        if sync_error:
            raise ValueError
        return importdriver.ImportStats()

    monkeypatch.setattr(import_, "run_import", sync_func)

    feed_update, _ = updatemanager.execute_feed_update(1)

    assert feed_update.status == expected_status
    assert feed_update.result == expected_explanation
Exemplo n.º 14
0
def other_feed_update(add_model, feed_2):
    return add_model(models.FeedUpdate(feed=feed_2))
Exemplo n.º 15
0
def current_update(add_model, feed):
    return add_model(models.FeedUpdate(feed=feed))
Exemplo n.º 16
0
def previous_update(add_model, feed):
    return add_model(models.FeedUpdate(feed=feed))