def test_package_details_success():
    # Arrange
    from pypi_org.views.package_views import package_details
    from pypi_org.data.package import Package
    from pypi_org.data.releases import Release

    test_package = Package()
    test_package.id = 'sqlalchemy'
    test_package.description = "TDB"
    test_package.releases = [
        Release(created_date=datetime.datetime.now(),
                major_ver=1,
                minor_ver=2,
                build_ver=200),
        Release(created_date=datetime.datetime.now() -
                datetime.timedelta(days=10)),
    ]

    # Act
    with unittest.mock.patch(
            'pypi_org.services.package_service.get_package_by_id',
            return_value=test_package):
        with flask_app.test_request_context(path='/project/' +
                                            test_package.id):
            resp: Response = package_details(test_package.id)

    # Assert
    assert b'sqlalchemy 1.2.200' in resp.data
Example #2
0
def load_package(data: dict, user_lookup: Dict[str, User]):
    try:
        info = data.get('info', {})

        p = Package()
        p.id = data.get('package_name', '').strip()
        if not p.id:
            return

        p.author = info.get('author')
        p.author_email = info.get('author_email')

        releases = build_releases(p.id, data.get("releases", {}))

        if releases:
            p.created_date = releases[0].created_date

        maintainers_lookup = get_email_and_name_from_text(
            info.get('maintainer'), info.get('maintainer_email'))
        maintainers = []
        for email, name in maintainers_lookup.items():
            user = user_lookup.get(email)
            if not user:
                continue

            m = Maintainer()
            m.package_id = p.id
            m.user_id = user.id
            maintainers.append(m)

        p.summary = info.get('summary')
        p.description = info.get('description')

        p.home_page = info.get('home_page')
        p.docs_url = info.get('docs_url')
        p.package_url = info.get('package_url')

        p.author = info.get('author')
        p.author_email = info.get('author_email')
        p.license = detect_license(info.get('license'))

        session = db_session.create_session()
        session.add(p)
        session.add_all(releases)
        if maintainers:
            session.add_all(maintainers)
        session.commit()
        session.close()
    except OverflowError:
        # What the heck, people just putting fake data in here
        # Size is terabytes...
        pass
    except Exception:
        raise