Skip to main content

Create test fixtures against an SQL database

Project description

Create database entries for your test scripts.

Example usage

First, set up a SQLite database. This should work with almost any SQL database. We’re using sqlite3 because it is built in.

import sqlite3  # or any db-api compatible database

conn = sqlite3.connect(":memory:")
conn.execute(
    """
    CREATE TABLE users (id INTEGER PRIMARY KEY, name text, active int)
    """
)
conn.execute(
    """
    CREATE TABLE groups (id INTEGER PRIMARY KEY, name text)
    """
)
conn.execute(
    """
    CREATE TABLE group_members (user_id INT, group_id INT)
    """
)

Now import sqlfixtures and create a sqlfixtures.SQLFixture object:

import sqlfixtures
fix = sqlfixtures.SQLFixture(conn)

Inserting data from dicts is the simplest way to start using sqlfixtures:

# Insert rows
with fix.insert("users", {"name": "Angus"}) as user:
    assert user.name == "Angus"
    print("Inserted user", user)

This outputs:

Inserted user {'id': 1, 'name': 'Angus', 'active': None}

Updating works in a similar way:

with fix.insert("users", {"name": "Angus"}) as user:
    assert user.name == "Angus"
    with fix.update("users", {"name": "Alice"}, where={"id": user.id}) as user:
        assert user.name == "Alice"
        print(user)

Outputting:

{'id': 1, 'name': 'Alice', 'active': None}

The declarative API allows more expressivity:

angus = sqlfixtures.Insertable("users", name="angus", active=1)
with sqlfixtures.apply_to_fixture(fix, [angus]) as users:
    assert users[0].name == "Angus"
    print(user)
[{'id': 1, 'name': 'Angus', 'active': 1}]

Once an insertable object has been created it can be copied and customized just by calling it:

User = sqlfixtures.Insertable("users", active=1)
angus = User(name="Angus")
alice = User(name="Alice")
with sqlfixtures.apply_to_fixture(fix, [angus, alice]) as users:
    assert users[0].name == "Angus"
    assert users[1].name == "Alice"
    print(users)
[{'id': 1, 'name': 'Angus', 'active': 1}, {'id': 2, 'name': 'Alice', 'active': 1}]

It can be more convenient to pass the list of Insertables as a dict:

with sqlfixtures.apply_to_fixture(fix, {"angus": angus, "alice": alice}) as users:
    print(users)
{
    "angus": {'id': 1, 'name': 'Angus', 'active': 1},
    "alice": {'id': 2, 'name': 'Alice', 'active': 1}
}

The declarative API allows you to reference columns that are populated by the database, for example using an auto-increment id field as a foreign key in another table:

administrators = sqlfixtures.Insertable("groups", name="administrators")
alice_is_admin = sqlfixtures.Insertable(
    "group_members", user_id=alice.id, group_id=administrators.id
)
with sqlfixtures.apply_to_fixture(fix, [alice, administrators, alice_is_admin]) as rows:
    print(rows)
[
    {'id': 1, 'name': 'Alice', 'active': 1},
    {'id': 1, 'name': 'administrators'},
    {'user_id': 1, 'group_id': 1}
]

Values can also be set from callable objects:

names = iter(["alice", "bob", "carol"])
User = sqlfixtures.Insertable("users", active=1, name=lambda: next(names))
with sqlfixtures.apply_to_fixture(fix, [User(), User(), User()]) as users:
    assert users[0].name == "alice"
    assert users[1].name == "bob"
    assert users[2].name == "carol"
    print(users)
[
    {'id': 1, 'name': 'alice', 'active': 1},
    {'id': 2, 'name': 'bob', 'active': 1},
    {'id': 3, 'name': 'carol', 'active': 1}
]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlfixtures-0.0.2.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlfixtures-0.0.2-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file sqlfixtures-0.0.2.tar.gz.

File metadata

  • Download URL: sqlfixtures-0.0.2.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for sqlfixtures-0.0.2.tar.gz
Algorithm Hash digest
SHA256 3c2d51b4a1260291279f7d7d4d5996e3e1ebdb6676aa8ff92629eacc0d8c4480
MD5 2b708a1300ca64bb7e69f328c168b353
BLAKE2b-256 d1af9928fd7f43ea4e6e97a0504160ae6948210f51b6fffa7bc39237cff846bb

See more details on using hashes here.

File details

Details for the file sqlfixtures-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: sqlfixtures-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for sqlfixtures-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 107a02ca8ed4a105f20269f398af82e07bb772074a49cb923fbc1ce4d2fa4543
MD5 8767474b023fefcc452fdf6933230f4f
BLAKE2b-256 441d86f1c6fe3d1689f1f08e5515e74959a05eb178ecaf58ae1d049c82e02f77

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page