Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor values #832

Merged
merged 3 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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