Skip to content

Commit

Permalink
CircPoliy: add unique default policy validation
Browse files Browse the repository at this point in the history
Closes rero#2079.

Co-Authored-by: Renaud Michotte <renaud.michotte@gmail.com>
  • Loading branch information
zannkukai committed Jun 25, 2021
1 parent 13e5d6f commit 8e2bcc4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
14 changes: 14 additions & 0 deletions rero_ils/modules/circ_policies/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ def extended_validation(self, **kwargs):
from ..item_types.api import ItemType
from ..patron_types.api import PatronType

# Only one default policy by organisation
if self.get('is_default', False):
if CircPolicy.get_default_circ_policy(self.organisation_pid):
return 'CircPolicy: already a default policy for this org'

for library in self.get('libraries', []):
library_pid = extracted_data_from_ref(library)
if not Library.get_record_by_pid(library_pid):
Expand Down Expand Up @@ -147,6 +152,15 @@ def extended_validation(self, **kwargs):

return True

@classmethod
def create(cls, data, id_=None, delete_pid=False,
dbcommit=True, reindex=True, pidcheck=True, **kwargs):
"""Create a new circulation policy record."""
# default behavior is to reindex the record. Needed to check that there
# is only one default policy by organisation
return super().create(
data, id_, delete_pid, dbcommit, reindex, **kwargs)

@classmethod
def exist_name_and_organisation_pid(cls, name, organisation_pid):
"""Check if the policy name is unique on organisation.
Expand Down
2 changes: 0 additions & 2 deletions tests/api/circ_policies/test_circ_policies_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ def test_circ_policies_post_put_delete(client, org_martigny,

# Check that the returned policy matches the given data
circ_policy_default_martigny_data['pid'] = '1'

assert data['metadata'] == circ_policy_default_martigny_data

res = client.get(item_url)
assert res.status_code == 200
data = get_json(res)
Expand Down
17 changes: 14 additions & 3 deletions tests/ui/circ_policies/test_circ_policies_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def test_no_default_policy(app):


def test_circ_policy_create(circ_policy_martigny_data_tmp,
circ_policy_short_martigny_data,
org_martigny,
lib_martigny, lib_saxon,
patron_type_children_martigny,
Expand All @@ -56,9 +57,9 @@ def test_circ_policy_create(circ_policy_martigny_data_tmp,
assert fetched_pid.pid_value == '1'
assert fetched_pid.pid_type == 'cipo'

circ_policy = deepcopy(circ_policy_martigny_data_tmp)
del circ_policy['$schema']
cipo = CircPolicy.create(circ_policy, delete_pid=True)
circ_policy_data = deepcopy(circ_policy_short_martigny_data)
del circ_policy_data['$schema']
cipo = CircPolicy.create(circ_policy_data, delete_pid=True)
assert cipo.get('$schema')
assert cipo.get('pid') == '2'

Expand Down Expand Up @@ -92,6 +93,16 @@ def test_circ_policy_create(circ_policy_martigny_data_tmp,
with pytest.raises(ValidationError):
cipo = CircPolicy.create(cipo_data, delete_pid=False)

# TEST #2 : create a second defaut policy
# The first created policy (pid=1) is the default policy.
# Creation of a second default policy should raise a ValidationError
default_cipo = CircPolicy.get_record_by_pid('1')
assert default_cipo.get('is_default')
with pytest.raises(ValidationError) as excinfo:
CircPolicy.create(circ_policy_martigny_data_tmp, delete_pid=True)
assert 'CircPolicy: already a default policy for this org' \
in str(excinfo.value)


def test_circ_policy_exist_name_and_organisation_pid(
circ_policy_default_martigny):
Expand Down

0 comments on commit 8e2bcc4

Please sign in to comment.