Skip to content

Commit

Permalink
Add load Django fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
quiqueporta committed Jun 11, 2020
1 parent 8771ea9 commit 3f683d9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,38 @@ with description("Mamba with Django") as self:
with after.each:
rollback_django_transactions(self.transactions)
```

## How to load Django fixtures

You can use the method `load_fixtures` to load [Django fixtures](https://docs.djangoproject.com/en/3.0/ref/django-admin/#what-s-a-fixture).


```python
from expects import (
equal,
expect
)

from myapp.mamba_runner import (
load_fixtures,
start_django_transactions,
rollback_django_transactions
)

from django.contrib.auth.models import Group


with description("Mamba with Django") as self:

with before.each:
self.transactions = start_django_transactions()
load_fixtures(['group.json'])

with context("Fixtures"):

with it("can retrieve loaded fixtures"):
expect(Group.objects.all().count()).to(equal(1))

with after.each:
rollback_django_transactions(self.transactions)
```
6 changes: 6 additions & 0 deletions mamba_runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse

from django.core.management import call_command
from django.db import transaction, connections
from django.test.utils import (
setup_databases,
Expand Down Expand Up @@ -154,3 +155,8 @@ def rollback_django_transactions(transactions):
for alias, db_transaction in transactions.items():
transaction.set_rollback(True, using=alias)
db_transaction.__exit__(None, None, None)


def load_fixtures(fixtures):
for alias in connections:
call_command('loaddata', *fixtures, **{'verbosity': 0, 'database': alias})

0 comments on commit 3f683d9

Please sign in to comment.