tests: add test on migration from 0019 to 0022 (#85549)
gitea/barbacompta/pipeline/head This commit looks good
Details
gitea/barbacompta/pipeline/head This commit looks good
Details
This commit is contained in:
parent
3f2648532d
commit
ea330151e9
|
@ -0,0 +1,58 @@
|
|||
# barbacompta - invoicing for dummies
|
||||
# Copyright (C) Entr'ouvert
|
||||
|
||||
import pytest
|
||||
from django.core.management import call_command
|
||||
from django.db import connection
|
||||
from django.db.migrations.executor import MigrationExecutor
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def migration(request, db):
|
||||
# see https://gist.github.com/asfaltboy/b3e6f9b5d95af8ba2cc46f2ba6eae5e2
|
||||
"""
|
||||
This fixture returns a helper object to test Django data migrations.
|
||||
The fixture returns an object with two methods;
|
||||
- `before` to initialize db to the state before the migration under test
|
||||
- `after` to execute the migration and bring db to the state after the migration
|
||||
The methods return `old_apps` and `new_apps` respectively; these can
|
||||
be to initiate the ORM models as in the migrations themselves.
|
||||
For example:
|
||||
def test_foo_set_to_bar(migration):
|
||||
old_apps = migration.before([('my_app', '0001_inital')])
|
||||
Foo = old_apps.get_model('my_app', 'foo')
|
||||
Foo.objects.create(bar=False)
|
||||
assert Foo.objects.count() == 1
|
||||
assert Foo.objects.filter(bar=False).count() == Foo.objects.count()
|
||||
# executing migration
|
||||
new_apps = migration.apply([('my_app', '0002_set_foo_bar')])
|
||||
Foo = new_apps.get_model('my_app', 'foo')
|
||||
|
||||
assert Foo.objects.filter(bar=False).count() == 0
|
||||
assert Foo.objects.filter(bar=True).count() == Foo.objects.count()
|
||||
Based on: https://gist.github.com/blueyed/4fb0a807104551f103e6
|
||||
"""
|
||||
|
||||
with connection.cursor() as cur:
|
||||
cur.execute('SET CONSTRAINTS ALL IMMEDIATE')
|
||||
|
||||
class Migrator:
|
||||
def before(self, targets, at_end=True):
|
||||
"""Specify app and starting migration names as in:
|
||||
before([('app', '0001_before')]) => app/migrations/0001_before.py
|
||||
"""
|
||||
executor = MigrationExecutor(connection)
|
||||
executor.migrate(targets)
|
||||
executor.loader.build_graph()
|
||||
return executor._create_project_state(with_applied_migrations=True).apps
|
||||
|
||||
def apply(self, targets):
|
||||
"""Migrate forwards to the "targets" migration"""
|
||||
executor = MigrationExecutor(connection)
|
||||
executor.migrate(targets)
|
||||
executor.loader.build_graph()
|
||||
return executor._create_project_state(with_applied_migrations=True).apps
|
||||
|
||||
yield Migrator()
|
||||
|
||||
call_command('migrate', verbosity=0)
|
|
@ -0,0 +1,39 @@
|
|||
# barbacompta - invoicing for dummies
|
||||
# Copyright (C) Entr'ouvert
|
||||
|
||||
import datetime
|
||||
|
||||
|
||||
def test_migration(migration):
|
||||
old_apps = migration.before([('eo_facture', '0020_facture_accounting_year')])
|
||||
User = old_apps.get_model('auth', 'User')
|
||||
Client = old_apps.get_model('eo_facture', 'Client')
|
||||
Facture = old_apps.get_model('eo_facture', 'Facture')
|
||||
|
||||
user = User.objects.create(username='user')
|
||||
|
||||
client = Client.objects.create(nom='client')
|
||||
facture1 = Facture.objects.create(
|
||||
proforma=False,
|
||||
client=client,
|
||||
emission=datetime.date(2020, 1, 1),
|
||||
echeance=datetime.date(2020, 1, 16),
|
||||
creator=user,
|
||||
)
|
||||
|
||||
facture2 = Facture.objects.create(
|
||||
proforma=False,
|
||||
client=client,
|
||||
emission=datetime.date(2020, 1, 1),
|
||||
echeance=datetime.date(2020, 1, 16),
|
||||
account_on_previous_period=True,
|
||||
creator=user,
|
||||
)
|
||||
|
||||
new_apps = migration.apply([('eo_facture', '0022_auto_20240111_1328')])
|
||||
|
||||
Facture = new_apps.get_model('eo_facture', 'Facture')
|
||||
facture1 = Facture.objects.get(pk=facture1.pk)
|
||||
facture2 = Facture.objects.get(pk=facture2.pk)
|
||||
assert facture1.accounting_year == 2020
|
||||
assert facture2.accounting_year == 2019
|
Loading…
Reference in New Issue