Skip to content

Commit

Permalink
Add get_set_prefix_for_value_copy function with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenklar committed Jan 17, 2025
1 parent 5f623b4 commit 6f7e57f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
21 changes: 19 additions & 2 deletions rdmo/projects/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from rdmo.core.tests.utils import compute_checksum

from ..filters import ProjectFilter
from ..models import Project
from ..utils import copy_project, set_context_querystring_with_filter_and_page
from ..models import Project, Value
from ..utils import compute_set_prefix_from_set_value, copy_project, set_context_querystring_with_filter_and_page

GET_queries = [
'page=2&title=project',
Expand All @@ -17,6 +17,18 @@
''
]

SET_VALUES = [
({'set_prefix': '' , 'set_index': 1}, {'set_prefix': '0'}, '1'),
({'set_prefix': '' , 'set_index': 1}, {'set_prefix': '0|0'}, '1|0'),
({'set_prefix': '' , 'set_index': 1}, {'set_prefix': '0|0|0'}, '1|0|0'),
({'set_prefix': '' , 'set_index': 2}, {'set_prefix': '0'}, '2'),
({'set_prefix': '' , 'set_index': 2}, {'set_prefix': '0|0'}, '2|0'),
({'set_prefix': '' , 'set_index': 2}, {'set_prefix': '0|0|0'}, '2|0|0'),
({'set_prefix': '0' , 'set_index': 1}, {'set_prefix': '0|0'}, '0|1'),
({'set_prefix': '0' , 'set_index': 1}, {'set_prefix': '0|0|0'}, '0|1|0'),
({'set_prefix': '0|0', 'set_index': 1}, {'set_prefix': '0|0|0'}, '0|0|1'),
]

@pytest.mark.parametrize('GET_query', GET_queries)
def test_set_context_querystring_with_filter_and_page(GET_query):
querydict = QueryDict(GET_query)
Expand Down Expand Up @@ -128,3 +140,8 @@ def test_copy_project(db, files):
compute_checksum(value.file.open('rb').read())
else:
assert not value.file


@pytest.mark.parametrize('set_value, value, result', SET_VALUES)
def test_compute_set_prefix_from_set_value(set_value, value, result):
assert compute_set_prefix_from_set_value(Value(**set_value), Value(**value)) == result
8 changes: 8 additions & 0 deletions rdmo/projects/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,11 @@ def get_upload_accept():
else:
return None
return ','.join(accept)


def compute_set_prefix_from_set_value(set_value, value):
set_prefix_length = len(set_value.set_prefix.split('|')) if set_value.set_prefix else 0
return '|'.join([
str(set_value.set_index) if (index == set_prefix_length) else value
for index, value in enumerate(value.set_prefix.split('|'))
])
14 changes: 8 additions & 6 deletions rdmo/projects/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@
)
from .serializers.v1.overview import CatalogSerializer, ProjectOverviewSerializer
from .serializers.v1.page import PageSerializer
from .utils import check_conditions, copy_project, get_upload_accept, send_invite_email
from .utils import (
check_conditions,
compute_set_prefix_from_set_value,
copy_project,
get_upload_accept,
send_invite_email,
)


class ProjectPagination(PageNumberPagination):
Expand Down Expand Up @@ -527,16 +533,12 @@ def copy_set(self, request, parent_lookup_project, pk=None):

# create new values for the new set
values = []
set_prefix_length = len(set_value.set_prefix.split('|')) if set_value.set_prefix else 0
for value in currentValues:
value.id = None
if value.set_prefix == set_value.set_prefix:
value.set_index = set_value.set_index
else:
value.set_prefix = '|'.join([
str(set_value.set_index) if (index == set_prefix_length) else value
for index, value in enumerate(value.set_prefix.split('|'))
])
value.set_prefix = compute_set_prefix_from_set_value(set_value, value)
values.append(value)

# bulk create the new values
Expand Down

0 comments on commit 6f7e57f

Please sign in to comment.