# Path: jacquard/directory/base.py
# class Directory(metaclass=abc.ABCMeta):
#     def __init__(self, **kwargs):
#     def from_configuration(cls, config, options):
#     def lookup(self, user_id):
# 
# Path: jacquard/directory/django.py
# class DjangoDirectory(Directory):
#     """Django user directory."""
# 
#     query = """
#     SELECT
#         auth_user.id,
#         auth_user.date_joined,
#         auth_user.is_superuser
#     FROM
#         auth_user
#     """
# 
#     def __init__(self, url):
#         """Initialise with SQLAlchemy connection URL."""
#         LOGGER.debug("Opening SQL connection to: %r", url)
#         self.engine = sqlalchemy.create_engine(url)
#         LOGGER.debug("(opened)")
# 
#     def describe_user(self, row):
#         """
#         Describe a user from a single row of results.
# 
#         This is intended to be overridden in subclasses which have provided a
#         custom `query` in order to add more tags, or use a different notion of
#         join date.
#         """
#         tags = []
# 
#         if row.is_superuser:
#             tags.append("superuser")
# 
#         return UserEntry(id=row.id, join_date=row.date_joined, tags=tuple(tags))
# 
#     @functools.lru_cache(maxsize=1024)
#     def lookup(self, user_id):
#         """
#         Look up user by ID.
# 
#         This makes a single DB query (based on the `query` attribute with an
#         added WHERE clause), with a small LRU cache applied on top.
#         """
#         query = self.query + " WHERE id = :user"
# 
#         try:
#             user_id = int(user_id)
#         except ValueError:
#             LOGGER.debug("Invalid ID")
#             return None
# 
#         LOGGER.debug("Lookup user %s", user_id)
#         result = self.engine.execute(sqlalchemy.sql.text(query), user=user_id)
# 
#         try:
#             row = next(iter(result))
#         except StopIteration:
#             LOGGER.debug("not found")
#             return None
# 
#         LOGGER.debug("Got row: %s", row)
#         return self.describe_user(row)


import unittest.mock
import pytest
    import sqlalchemy
from jacquard.directory.base import UserEntry
from jacquard.directory.django import DjangoDirectory
        id INTEGER NOT NULL PRIMARY KEY,
        date_joined DATETIME NOT NULL,
        is_superuser BOOLEAN NOT NULL
    )
    """
    )

    test_database.execute(
        """
    INSERT INTO auth_user(id, date_joined, is_superuser) VALUES
        (1, date('now'), 1),
        (2, date('now'), 0),
        (3, date('now'), 0)
    """
    )


@pytest.mark.skipif(sqlalchemy is None, reason="sqlalchemy not installed")
@unittest.mock.patch("sqlalchemy.create_engine", lambda *args: test_database)
def test_get_extant_user():
    directory = DjangoDirectory("")

    user_one = directory.lookup("1")

    assert list(user_one.tags) == ["superuser"]


@pytest.mark.skipif(sqlalchemy is None, reason="sqlalchemy not installed")
@unittest.mock.patch("sqlalchemy.create_engine", lambda *args: test_database)
def test_get_extant_non_superuser():

# Path: jacquard/directory/base.py
# class Directory(metaclass=abc.ABCMeta):
#     def __init__(self, **kwargs):
#     def from_configuration(cls, config, options):
#     def lookup(self, user_id):
# 
# Path: jacquard/directory/django.py
# class DjangoDirectory(Directory):
#     """Django user directory."""
# 
#     query = """
#     SELECT
#         auth_user.id,
#         auth_user.date_joined,
#         auth_user.is_superuser
#     FROM
#         auth_user
#     """
# 
#     def __init__(self, url):
#         """Initialise with SQLAlchemy connection URL."""
#         LOGGER.debug("Opening SQL connection to: %r", url)
#         self.engine = sqlalchemy.create_engine(url)
#         LOGGER.debug("(opened)")
# 
#     def describe_user(self, row):
#         """
#         Describe a user from a single row of results.
# 
#         This is intended to be overridden in subclasses which have provided a
#         custom `query` in order to add more tags, or use a different notion of
#         join date.
#         """
#         tags = []
# 
#         if row.is_superuser:
#             tags.append("superuser")
# 
#         return UserEntry(id=row.id, join_date=row.date_joined, tags=tuple(tags))
# 
#     @functools.lru_cache(maxsize=1024)
#     def lookup(self, user_id):
#         """
#         Look up user by ID.
# 
#         This makes a single DB query (based on the `query` attribute with an
#         added WHERE clause), with a small LRU cache applied on top.
#         """
#         query = self.query + " WHERE id = :user"
# 
#         try:
#             user_id = int(user_id)
#         except ValueError:
#             LOGGER.debug("Invalid ID")
#             return None
# 
#         LOGGER.debug("Lookup user %s", user_id)
#         result = self.engine.execute(sqlalchemy.sql.text(query), user=user_id)
# 
#         try:
#             row = next(iter(result))
#         except StopIteration:
#             LOGGER.debug("not found")
#             return None
# 
#         LOGGER.debug("Got row: %s", row)
#         return self.describe_user(row)


# Path: jacquard/directory/tests/test_django.py
import unittest.mock
import pytest
    import sqlalchemy
from jacquard.directory.base import UserEntry
from jacquard.directory.django import DjangoDirectory

        id INTEGER NOT NULL PRIMARY KEY,
        date_joined DATETIME NOT NULL,
        is_superuser BOOLEAN NOT NULL
    )
    """
    )

    test_database.execute(
        """
    INSERT INTO auth_user(id, date_joined, is_superuser) VALUES
        (1, date('now'), 1),
        (2, date('now'), 0),
        (3, date('now'), 0)
    """
    )


@pytest.mark.skipif(sqlalchemy is None, reason="sqlalchemy not installed")
@unittest.mock.patch("sqlalchemy.create_engine", lambda *args: test_database)
def test_get_extant_user():
    directory = DjangoDirectory("")

    user_one = directory.lookup("1")

    assert list(user_one.tags) == ["superuser"]


@pytest.mark.skipif(sqlalchemy is None, reason="sqlalchemy not installed")
@unittest.mock.patch("sqlalchemy.create_engine", lambda *args: test_database)
def test_get_extant_non_superuser():

    directory = DjangoDirectory("")