Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
submarcos committed May 14, 2024
1 parent 016609d commit bd9408b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 6 deletions.
3 changes: 0 additions & 3 deletions georiviere/contribution/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ def __init__(self, *args, **kwargs):
self.fields["customization"] = JSONFormField(
schema=schema, required=False, label=_("Customization")
)
# self.fields["value_type"].choices = [
# (self.instance.value_type, self.instance.get_value_type_display())
# ]

class Meta:
model = models.CustomContributionTypeField
Expand Down
29 changes: 28 additions & 1 deletion georiviere/contribution/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,38 @@ class Meta:

label = Sequence(lambda n: f"Custom contribution type field {n}")
internal_identifier = Sequence(lambda n: f"custom_contribution_type_field_{n}")
value_type = models.CustomContributionTypeField.FieldTypeChoices.STRING
required = False
custom_type = SubFactory(CustomContributionTypeFactory)


class CustomContributionTypeStringFieldFactory(CustomContributionTypeFieldFactory):
value_type = models.CustomContributionTypeField.FieldTypeChoices.STRING


class CustomContributionTypeTextFieldFactory(CustomContributionTypeFieldFactory):
value_type = models.CustomContributionTypeField.FieldTypeChoices.TEXT


class CustomContributionTypeIntegerFieldFactory(CustomContributionTypeFieldFactory):
value_type = models.CustomContributionTypeField.FieldTypeChoices.INTEGER


class CustomContributionTypeBooleanFieldFactory(CustomContributionTypeFieldFactory):
value_type = models.CustomContributionTypeField.FieldTypeChoices.BOOLEAN


class CustomContributionTypeFloatFieldFactory(CustomContributionTypeFieldFactory):
value_type = models.CustomContributionTypeField.FieldTypeChoices.FLOAT


class CustomContributionTypeDateFieldFactory(CustomContributionTypeFieldFactory):
value_type = models.CustomContributionTypeField.FieldTypeChoices.DATE


class CustomContributionTypeDatetimeFieldFactory(CustomContributionTypeFieldFactory):
value_type = models.CustomContributionTypeField.FieldTypeChoices.DATETIME


class CustomContributionFactory(django.DjangoModelFactory):
class Meta:
model = models.CustomContribution
Expand Down
66 changes: 64 additions & 2 deletions georiviere/contribution/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@
from georiviere.tests.factories import UserAllPermsFactory

from ...observations.tests.factories import StationFactory
from ..forms import ContributionForm, CustomContributionForm
from ..forms import (
ContributionForm,
CustomContributionFieldForm,
CustomContributionFieldInlineForm,
CustomContributionForm,
)
from . import factories
from .factories import CustomContributionFactory, CustomContributionTypeFactory
from .factories import (
CustomContributionFactory,
CustomContributionTypeBooleanFieldFactory,
CustomContributionTypeDateFieldFactory,
CustomContributionTypeDatetimeFieldFactory,
CustomContributionTypeFactory,
CustomContributionTypeFieldFactory,
CustomContributionTypeFloatFieldFactory,
CustomContributionTypeIntegerFieldFactory,
)


class ContributionFormTestCase(TestCase):
Expand Down Expand Up @@ -65,3 +79,51 @@ def test_data_field_schema_is_correct_when_instance_exists(self):
form.fields["data"].widget.schema,
self.contribution.custom_type.get_json_schema_form(),
)


class CustomContributionFieldInlineFormTest(TestCase):
def setUp(self):
self.field = CustomContributionTypeFieldFactory()

def test_value_type_field_disabled_when_instance_exists(self):
form = CustomContributionFieldInlineForm(instance=self.field)
self.assertTrue(form.fields["value_type"].disabled)

def test_value_type_field_help_text_when_instance_exists(self):
form = CustomContributionFieldInlineForm(instance=self.field)
self.assertEqual(
form.fields["value_type"].help_text,
"You can't change value type after creation. Delete and/or create another one.",
)


class CustomContributionFieldFormTest(TestCase):
def test_schema_string(self):
field = CustomContributionTypeFieldFactory(value_type="string")
form = CustomContributionFieldForm(instance=field)
self.assertIsInstance(form.fields["customization"], JSONFormField)

def test_schema_integer(self):
field = CustomContributionTypeIntegerFieldFactory()
form = CustomContributionFieldForm(instance=field)
self.assertIsInstance(form.fields["customization"], JSONFormField)

def test_schema_number(self):
field = CustomContributionTypeFloatFieldFactory()
form = CustomContributionFieldForm(instance=field)
self.assertIsInstance(form.fields["customization"], JSONFormField)

def test_schema_boolean(self):
field = CustomContributionTypeBooleanFieldFactory()
form = CustomContributionFieldForm(instance=field)
self.assertIsInstance(form.fields["customization"], JSONFormField)

def test_schema_date(self):
field = CustomContributionTypeDateFieldFactory()
form = CustomContributionFieldForm(instance=field)
self.assertIsInstance(form.fields["customization"], JSONFormField)

def test_schema_datetime(self):
field = CustomContributionTypeDatetimeFieldFactory()
form = CustomContributionFieldForm(instance=field)
self.assertIsInstance(form.fields["customization"], JSONFormField)

0 comments on commit bd9408b

Please sign in to comment.