Skip to content

Commit

Permalink
fixtures: reset sequence to correct value after loading records
Browse files Browse the repository at this point in the history
* Closes #563

Co-Authored-by: Aly Badr <aly.badr@rero.ch>
  • Loading branch information
Aly Badr committed Oct 16, 2019
1 parent f9d91d2 commit f87f1b2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
13 changes: 11 additions & 2 deletions rero_ils/modules/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from flask.cli import with_appcontext
from flask_security.confirmable import confirm_user
from invenio_accounts.cli import commit, users
from invenio_db import db
from invenio_pidstore.models import PersistentIdentifier
from invenio_records.api import Record
from invenio_records_rest.utils import obj_or_import_string
Expand All @@ -47,11 +48,19 @@
from .items.cli import create_items, reindex_items
from .loans.cli import create_loans
from .patrons.cli import import_users
from .providers import append_pids_to_table

_datastore = LocalProxy(lambda: current_app.extensions['security'].datastore)


def append_fixtures_new_identifiers(table, pids):
"""Insert pids into the indentifier table and update its sequence."""
for pid in pids:
data = table(recid=pid)
db.session.add(data)
db.session.commit()
table._set_sequence(int(max(pids))+1)


@click.group()
def fixtures():
"""Fixtures management commands."""
Expand Down Expand Up @@ -240,7 +249,7 @@ def create(infile, pid_type, schema, verbose, dbcommit, reindex, append):
if append:
pids = record_class.get_all_pids()
table = record_class.provider.identifier
append_pids_to_table(table, pids)
append_fixtures_new_identifiers(table, pids)


fixtures.add_command(create)
Expand Down
9 changes: 0 additions & 9 deletions rero_ils/modules/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,11 @@

from __future__ import absolute_import, print_function

from invenio_db import db
from invenio_pidstore.errors import PIDDoesNotExistError
from invenio_pidstore.models import PIDStatus
from invenio_pidstore.providers.base import BaseProvider


def append_pids_to_table(table, pids):
"""Insert pids into an indentifier table."""
for pid in pids:
data = table(recid=pid)
db.session.add(data)
db.session.commit()


class Provider(BaseProvider):
"""CircPolicy identifier provider.
Expand Down
9 changes: 8 additions & 1 deletion tests/ui/organisations/test_organisations_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_organisation_libararies(org_martigny, lib_martigny):
assert list(org_martigny.get_libraries()) == [lib_martigny]


def test_organisation_create(app, db, org_martigny_data):
def test_organisation_create(app, db, org_martigny_data, org_sion_data):
"""Test organisation creation."""
org = Organisation.create(org_martigny_data, delete_pid=True)
assert org == org_martigny_data
Expand All @@ -44,3 +44,10 @@ def test_organisation_create(app, db, org_martigny_data):
fetched_pid = fetcher(org.id, org)
assert fetched_pid.pid_value == '1'
assert fetched_pid.pid_type == 'org'

org = Organisation.create(
org_sion_data, delete_pid=True, dbcommit=True, reindex=True)
assert org.get('pid') == '2'

identifier = Organisation.provider.identifier
assert identifier.next() == identifier.max() == 3

0 comments on commit f87f1b2

Please sign in to comment.