Skip to content

Commit

Permalink
Merge pull request #832 from rdmorganiser/refactor_values
Browse files Browse the repository at this point in the history
Refactor values
  • Loading branch information
jochenklar authored Nov 24, 2023
2 parents 971fa7f + b2ee634 commit c737247
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 8 deletions.
26 changes: 18 additions & 8 deletions rdmo/projects/models/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def as_dict(self):
'collection_index': self.collection_index,
'value_type': self.value_type,
'unit': self.unit,
'text': self.text,
'option_uri': self.option_uri,
'option_text': self.option_text,
'option_additional_input': self.option_additional_input,
'external_id': self.external_id,
'value': self.value,
'value_and_unit': self.value_and_unit,
Expand Down Expand Up @@ -161,18 +165,14 @@ def value(self):
else:
return self.text
else:
return None
return ''

@property
def value_and_unit(self):
value = self.value

if value is None:
return ''
elif self.unit:
return f'{value} {self.unit}'
if self.unit:
return f'{self.value} {self.unit}'
else:
return value
return self.value

@property
def is_true(self):
Expand Down Expand Up @@ -254,6 +254,16 @@ def option_uri(self):
if self.option is not None:
return self.option.uri

@property
def option_text(self):
if self.option is not None:
return self.option.text

@property
def option_additional_input(self):
if self.option is not None:
return self.option.additional_input

def copy_file(self, file_name, file_content):
# copies a file field from a different value over to this value
# this is tricky, because we need to trick django_cleanup to not delete the original file
Expand Down
74 changes: 74 additions & 0 deletions rdmo/projects/tests/test_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import datetime

from ..models import Value


def mocked_trans(self, field):
return self.text_lang1


def test_value_text(db):
value = Value.objects.get(id=1)
assert value.value == value.text
assert value.value_and_unit == value.text
assert value.option_text is None
assert value.option_additional_input is None


def test_value_textarea(db):
value = Value.objects.get(id=2)
assert value.value == value.text
assert value.value_and_unit == value.text
assert value.option_text is None
assert value.option_additional_input is None


def test_value_bool(db):
value = Value.objects.get(id=3)
assert value.value == 'Yes'
assert value.value_and_unit == 'Yes'
assert value.option_text is None
assert value.option_additional_input is None


def test_value_radio(db, mocker):
mocker.patch('rdmo.options.models.Option.trans', mocked_trans)

value = Value.objects.get(id=4)
assert value.value == 'Other: Lorem ipsum'
assert value.value_and_unit == 'Other: Lorem ipsum'
assert value.option_text == 'Other'
assert value.option_additional_input is True


def test_value_select(db, mocker):
value = Value.objects.get(id=5)
mocker.patch('rdmo.options.models.Option.trans', mocked_trans)
assert value.value == 'One'
assert value.value_and_unit == 'One'
assert value.option_text == 'One'
assert value.option_additional_input is False


def test_value_range(db):
value = Value.objects.get(id=6)
assert value.value == value.text
assert value.value_and_unit == value.text
assert value.option_text is None
assert value.option_additional_input is None


def test_value_datetime(db):
value = Value.objects.get(id=7)
assert value.value == datetime.date(2018, 1, 1)
assert value.value_and_unit == datetime.date(2018, 1, 1)
assert value.option_text is None
assert value.option_additional_input is None


def test_value_file(db):
value = Value.objects.get(id=238)
assert value.value == 'rdmo-logo.svg'
assert value.value_and_unit == 'rdmo-logo.svg'
assert value.option_text is None
assert value.option_additional_input is None

0 comments on commit c737247

Please sign in to comment.