# Path: pypi_portal/extensions.py
# LOG = getLogger(__name__)
# def _on_connect(dbapi_connection, **_):
# 
# Path: pypi_portal/models/helpers.py
# def count(column, value, glob=False):
#     """Counts number of rows with value in a column. This function is case-insensitive.
# 
#     Positional arguments:
#     column -- the SQLAlchemy column object to search in (e.g. Table.a_column).
#     value -- the value to search for, any string.
# 
#     Keyword arguments:
#     glob -- enable %globbing% search (default False).
# 
#     Returns:
#     Number of rows that match. Equivalent of SELECT count(*) FROM.
#     """
#     query = db.session.query(func.count('*'))
#     if glob:
#         query = query.filter(column.ilike(value))
#     else:
#         query = query.filter(func.lower(column) == value.lower())
#     return query.one()[0]
# 
# Path: pypi_portal/models/pypi.py
# class Package(Base):
#     """Holds all of the packages on PyPI."""
#     name = Column(String(255), unique=True, nullable=False)
#     summary = Column(Text, nullable=False)
#     latest_version = Column(String(128), nullable=False)


from pypi_portal.extensions import db
from pypi_portal.models.helpers import count
from pypi_portal.models.pypi import Package


def test_count():
    Package.query.delete()
    db.session.commit()

    assert 0 == count(Package.name, '')
    assert 0 == count(Package.name, '%')
    assert 0 == count(Package.name, '%', True)

    db.session.add(Package(name='packageA', summary='Test Package', latest_version='1.0.0'))
    db.session.commit()

    assert 0 == count(Package.name, '%')
    assert 1 == count(Package.name, '%', True)

    db.session.add(Package(name='packageB', summary='Test Package', latest_version='1.0.0'))
    db.session.commit()

    assert 0 == count(Package.name, 'package')
    assert 1 == count(Package.name, 'packagea')
    assert 0 == count(Package.name, 'package%')

# Path: pypi_portal/extensions.py
# LOG = getLogger(__name__)
# def _on_connect(dbapi_connection, **_):
# 
# Path: pypi_portal/models/helpers.py
# def count(column, value, glob=False):
#     """Counts number of rows with value in a column. This function is case-insensitive.
# 
#     Positional arguments:
#     column -- the SQLAlchemy column object to search in (e.g. Table.a_column).
#     value -- the value to search for, any string.
# 
#     Keyword arguments:
#     glob -- enable %globbing% search (default False).
# 
#     Returns:
#     Number of rows that match. Equivalent of SELECT count(*) FROM.
#     """
#     query = db.session.query(func.count('*'))
#     if glob:
#         query = query.filter(column.ilike(value))
#     else:
#         query = query.filter(func.lower(column) == value.lower())
#     return query.one()[0]
# 
# Path: pypi_portal/models/pypi.py
# class Package(Base):
#     """Holds all of the packages on PyPI."""
#     name = Column(String(255), unique=True, nullable=False)
#     summary = Column(Text, nullable=False)
#     latest_version = Column(String(128), nullable=False)


# Path: tests/models/test_helpers.py
from pypi_portal.extensions import db
from pypi_portal.models.helpers import count
from pypi_portal.models.pypi import Package



def test_count():
    Package.query.delete()
    db.session.commit()

    assert 0 == count(Package.name, '')
    assert 0 == count(Package.name, '%')
    assert 0 == count(Package.name, '%', True)

    db.session.add(Package(name='packageA', summary='Test Package', latest_version='1.0.0'))
    db.session.commit()

    assert 0 == count(Package.name, '%')
    assert 1 == count(Package.name, '%', True)

    db.session.add(Package(name='packageB', summary='Test Package', latest_version='1.0.0'))
    db.session.commit()

    assert 0 == count(Package.name, 'package')
    assert 1 == count(Package.name, 'packagea')
    assert 0 == count(Package.name, 'package%')

    assert 2 == count(Package.name, 'package%', True)