def test_sync_empty(alter_xmlrpc):
    alter_xmlrpc(set())
    redis.delete(POLL_SIMPLE_THROTTLE)
    Package.query.delete()
    db.session.commit()

    assert '302 FOUND' == current_app.test_client().get('/pypi/sync').status
    assert [] == db.session.query(Package.name, Package.summary, Package.latest_version).all()
def test_sync_unhandled_exception():
    old_throttle = pypi.THROTTLE
    pypi.THROTTLE = 'nan'
    redis.delete(POLL_SIMPLE_THROTTLE)

    with pytest.raises(ValueError):
        current_app.test_client().get('/pypi/sync').status()

    pypi.THROTTLE = old_throttle
def test_sync_few(alter_xmlrpc):
    alter_xmlrpc([dict(name='packageB', summary='Test package.', version='3.0.0'), ])
    redis.delete(POLL_SIMPLE_THROTTLE)

    assert '302 FOUND' == current_app.test_client().get('/pypi/sync').status

    expected = [('packageB', 'Test package.', '3.0.0'), ]
    actual = db.session.query(Package.name, Package.summary, Package.latest_version).all()
    assert expected == actual
def test_sync_empty(alter_xmlrpc):
    alter_xmlrpc(set())
    redis.delete(POLL_SIMPLE_THROTTLE)
    Package.query.delete()
    db.session.commit()

    assert '302 FOUND' == current_app.test_client().get('/pypi/sync').status
    assert [] == db.session.query(Package.name, Package.summary,
                                  Package.latest_version).all()
def test_sync_unhandled_exception():
    old_throttle = pypi.THROTTLE
    pypi.THROTTLE = 'nan'
    redis.delete(POLL_SIMPLE_THROTTLE)

    with pytest.raises(ValueError):
        current_app.test_client().get('/pypi/sync').status()

    pypi.THROTTLE = old_throttle
def test_rate_limit():
    assert redis.exists(POLL_SIMPLE_THROTTLE)
    assert update_package_list() is None

    redis.delete(POLL_SIMPLE_THROTTLE)
    assert not redis.exists(POLL_SIMPLE_THROTTLE)
    assert list() == update_package_list()

    assert redis.exists(POLL_SIMPLE_THROTTLE)
    assert update_package_list() is None
def test_sync_few(alter_xmlrpc):
    alter_xmlrpc([
        dict(name='packageB', summary='Test package.', version='3.0.0'),
    ])
    redis.delete(POLL_SIMPLE_THROTTLE)

    assert '302 FOUND' == current_app.test_client().get('/pypi/sync').status

    expected = [
        ('packageB', 'Test package.', '3.0.0'),
    ]
    actual = db.session.query(Package.name, Package.summary,
                              Package.latest_version).all()
    assert expected == actual
def test_sorting(alter_xmlrpc, latest):
    value = [
        dict(name='packageA', summary='Test package.', version=('2.0.0-beta' if latest else '0.0.9')),
        dict(name='packageA', summary='Test package.', version='1.10.0'),
        dict(name='packageB', summary='Test package.', version='3.0.0'),
    ]
    alter_xmlrpc(value)
    redis.delete(POLL_SIMPLE_THROTTLE)
    assert sorted(['packageA', 'packageB']) == sorted(update_package_list())

    expected = [
        ('packageA', 'Test package.', '2.0.0-beta' if latest else '1.10.0'),
        ('packageB', 'Test package.', '3.0.0'),
    ]
    actual = db.session.query(Package.name, Package.summary, Package.latest_version).all()
    assert expected == actual
def test_sync_timeout():
    old_delay = pypi.update_package_list.delay
    pypi.update_package_list.delay = FakeDelay
    redis.delete(POLL_SIMPLE_THROTTLE)

    assert '302 FOUND' == current_app.test_client().get('/pypi/sync').status

    expected = [
        ('packageB', 'Test package.', '3.0.0'), ('packageB1', 'Test package.', '3.0.0'),
        ('packageB2', 'Test package.', '3.0.0'), ('packageB3', 'Test package.', '3.0.0'),
        ('packageB4', 'Test package.', '3.0.0'), ('packageB5', 'Test package.', '3.0.0'),
    ]
    actual = db.session.query(Package.name, Package.summary, Package.latest_version).all()
    assert sorted(expected) == sorted(actual)

    pypi.update_package_list.delay = old_delay
def test_sync_parallel(alter_xmlrpc):
    alter_xmlrpc([dict(name='packageD', summary='Test package.', version='3.0.0'), ])
    redis.delete(POLL_SIMPLE_THROTTLE)

    redis_key = CELERY_LOCK.format(task_name='pypi_portal.tasks.pypi.update_package_list')
    lock = redis.lock(redis_key, timeout=1)
    assert lock.acquire(blocking=False)

    assert '302 FOUND' == current_app.test_client().get('/pypi/sync').status

    expected = [('packageB', 'Test package.', '3.0.0'), ]
    actual = db.session.query(Package.name, Package.summary, Package.latest_version).all()
    assert expected == actual

    try:
        lock.release()
    except LockError:
        pass
def test_sync_timeout():
    old_delay = pypi.update_package_list.delay
    pypi.update_package_list.delay = FakeDelay
    redis.delete(POLL_SIMPLE_THROTTLE)

    assert '302 FOUND' == current_app.test_client().get('/pypi/sync').status

    expected = [
        ('packageB', 'Test package.', '3.0.0'),
        ('packageB1', 'Test package.', '3.0.0'),
        ('packageB2', 'Test package.', '3.0.0'),
        ('packageB3', 'Test package.', '3.0.0'),
        ('packageB4', 'Test package.', '3.0.0'),
        ('packageB5', 'Test package.', '3.0.0'),
    ]
    actual = db.session.query(Package.name, Package.summary,
                              Package.latest_version).all()
    assert sorted(expected) == sorted(actual)

    pypi.update_package_list.delay = old_delay
def test_sync_parallel(alter_xmlrpc):
    alter_xmlrpc([
        dict(name='packageD', summary='Test package.', version='3.0.0'),
    ])
    redis.delete(POLL_SIMPLE_THROTTLE)

    redis_key = CELERY_LOCK.format(
        task_name='pypi_portal.tasks.pypi.update_package_list')
    lock = redis.lock(redis_key, timeout=1)
    assert lock.acquire(blocking=False)

    assert '302 FOUND' == current_app.test_client().get('/pypi/sync').status

    expected = [
        ('packageB', 'Test package.', '3.0.0'),
    ]
    actual = db.session.query(Package.name, Package.summary,
                              Package.latest_version).all()
    assert expected == actual

    try:
        lock.release()
    except LockError:
        pass
def test_no_results(alter_xmlrpc):
    alter_xmlrpc(set())
    redis.delete(POLL_SIMPLE_THROTTLE)
    assert list() == update_package_list()