# Database

These fixtures are related to Django database operation, including issued by ORM.

class pytest_djangoapp.fixtures.db.Queries

  Allows access to executed DB queries.

  clear(db_alias: str = None)

    Clear queries for the given or default DB.

    Parameters:

      db_alias – Database alias. Default is used if not given.

  clear_all()

    Clears all queries logged for all DBs.

  get_log(db_alias: str = None) → deque
    Parameters:

      db_alias –

  sql(db_alias: str = None, *, drop_auxiliary: bool = True) → List[str]

    Returns a list of queries executed using the given or default DB.

    Parameters:

      - db_alias – Database alias. Default is used if not given.

      - drop_auxiliary – Filter out auxiliary SQL like: * BEGIN * COMMIT * END

  time(db_alias: str = None) → Decimal

    Returns total time executing queries (in seconds) using the given or default DB.

    Parameters:

      db_alias – Database alias. Default is used if not given.

pytest_djangoapp.fixtures.db.db_queries(settings) → Queries

  Allows access to executed DB queries.

  Example:

      def test_db(db_queries):

          # Previous queries cleared at the beginning.
          assert len(db_queries) == 0

          ...  # Do some DB-related stuff.

          # Assert total queries on all DBs.
          assert len(db_queries) == 10

          # Default DBs SQLs with auxiliary commands filtered out by default.
          sqls = db_queries.sql()

          # Assert total execution time is less than a second.
          assert db_queries.time() < 1

          # Drop SQL gathered so far on default DB.
          db_queries.clear()

  Warning

  Requires Django 1.9+ to work.