Define data in YAML format and load it into a relational database using SQLAlchemy✨
Features
- The YAML root contains a sequence of
mapper names, such as- Userand- Profile. - These names should be ordered based on relationship dependencies.
- Each mapper name should contain a sequence of
instances. - An
instanceis a mapping of attributes to values. - The attributes are derived from the mapper's
__init__()method, which typically maps attributes to columns. - A special field called
__key__can be used to identify instances in relationship references, such asProfile.user. - It is crucial to note that any
__key__must be globally unique. - In a
to-one relationships, data can be nested directly in the parent data definition. - References can access attributes using a dot notation, such as
xyz.profile. To-many relationshipscan be added as a list of references.
Example
This module expose a single function load(ModelBase, session, fixture_text, loader=None).
ModelBaseis SQLAlchemy declarative base.- Session is SQLAlchemy session.
fixture_textis a string contain theYAMLfixtures
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session
from sql_fixture import fixture
BaseModel = declarative_base()
class User(BaseModel):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
username = Column(String(150), nullable=False, unique=True)
email = Column(String(254), unique=True)
def main():
engine = create_engine('sqlite://')
BaseModel.metadata.create_all(engine)
connection = engine.connect()
session = Session(bind=connection)
fixtures = """
- User:
- username: xyz
email: xyz@example.com
- username: abc
email: abc@example.com
"""
fixture.load(BaseModel, session, fixtures)
print('\n'.join(u.username for u in session.query(User).all()))
if __name__ == '__main__':
main()
Note: the fixture.load() function performs a session.commit().
The
load()function yields afixture.Storeinstance. With this instance, you can utilize theget()method by providing a key argument to obtain a reference to the object that was added to the database. This approach is convenient for accessing attributes that were produced by the database.
data = fixture.load(BaseModel, session, fixtures)
my_obj = data.get('my_key')
print(f"The id of my_obj is {my_obj.id}")
Warning : The default YAML loading method employs yaml.FullLoader. However, this method is insecure when used to load untrusted input. To address this, it is feasible to replace the default loader with a different one by specifying the loader parameter within the load() function.
Installation
You can add sql-fixture in a few easy steps. First of all, install the dependency:
$ pip install sql_fixture
---> 100%
Successfully installed sql_fixture
Development 🚧
Setup environment 📦
You should create a virtual environment and activate it:
python -m venv venv/
source venv/bin/activate
And then install the development dependencies:
# Install dependencies
pip install -e .[test,lint]
Run tests 🌝
You can run all the tests with:
bash scripts/test.sh
Note: You can also generate a coverage report with:
bash scripts/test_html.sh
Format the code 🍂
Execute the following command to apply pre-commit formatting:
bash scripts/format.sh
Execute the following command to apply mypy type checking:
bash scripts/lint.sh
License
This project is licensed under the terms of the MIT license.
Metadata
Release files for sql-fixture 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sql_fixture-0.1.0.tar.gz | 14.5 kB | Without provenance | Details for sql_fixture-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sql_fixture-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for sql_fixture-0.1.0-py3-none-any.whl |
Total release size: 23.2 kB
Release files / sql_fixture-0.1.0.tar.gz
| Download URL | sql_fixture-0.1.0.tar.gz |
|---|---|
| Size | 14.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b2adea964f56d22ad7a16aa04d178a18739b33beca3f94aae0f4249374638074
|
|
BLAKE2b-256 checksum How to use checksums |
0840685ef3fef41ad91bcfbaace51689566d2edc8c4a13292d015bc00b27b3c1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.11.2
|
Release files / sql_fixture-0.1.0-py3-none-any.whl
| Download URL | sql_fixture-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ca568f3425bfb1952a74848229f162826432c5e258589da6f74c8bcde9661c47
|
|
BLAKE2b-256 checksum How to use checksums |
aa1391fe80db443527dce47be6d7ad4ebc1e03221dc6952c271313775fc6323f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.11.2
|
Security for sql-fixture 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.