Skip to content

Commit

Permalink
Merge pull request #285 from peopledoc/json-migrations
Browse files Browse the repository at this point in the history
FORM-13: Fix JSON migrations
  • Loading branch information
alexdashkov authored Nov 29, 2017
2 parents 1df4f95 + 4aba31f commit 94ac59e
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 1 deletion.
33 changes: 33 additions & 0 deletions demo/tests/fixtures/migration-form-data-expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"description": "Some description for the tests",
"fields": [
{
"accesses": [
{
"access_id": "first-access-group",
"level": "EDITABLE"
},
{
"access_id": "second-access-group",
"level": "EDITABLE"
},
{
"access_id": "third-access-group",
"level": "EDITABLE"
}
],
"defaults": [],
"help_text": "",
"items": [],
"label": "There is my label field label",
"multiple": false,
"placeholder": null,
"slug": "multiline-text",
"type_id": "paragraph",
"validations": []
}
],
"id": 0,
"label": "And my form label",
"presets": []
}
65 changes: 65 additions & 0 deletions demo/tests/fixtures/migration-form-data-input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"first-access-group": {
"description": "Some description for the tests",
"fields": [
{
"defaults": [],
"disabled": false,
"help_text": "",
"items": [],
"label": "There is my label field label",
"multiple": false,
"placeholder": null,
"required": false,
"slug": "multiline-text",
"type_id": "paragraph",
"validations": []
}
],
"id": 0,
"label": "And my form label",
"presets": []
},
"second-access-group": {
"description": "Some description for the tests",
"fields": [
{
"defaults": [],
"disabled": false,
"help_text": "",
"items": [],
"label": "There is my label field label",
"multiple": false,
"placeholder": null,
"required": false,
"slug": "multiline-text",
"type_id": "paragraph",
"validations": []
}
],
"id": 0,
"label": "And my form label",
"presets": []
},
"third-access-group": {
"description": "Some description for the tests",
"fields": [
{
"defaults": [],
"disabled": false,
"help_text": "",
"items": [],
"label": "There is my label field label",
"multiple": false,
"placeholder": null,
"required": false,
"slug": "multiline-text",
"type_id": "paragraph",
"validations": []
}
],
"id": 0,
"label": "And my form label",
"presets": []
}
}
60 changes: 60 additions & 0 deletions demo/tests/test_json_migration_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os
import json
from django.test import TestCase

from formidable.json_migrations.utils import merge_context_forms

TESTS_DIR = os.path.dirname(os.path.abspath(__file__))


class JSONMigrationUtilsTestCase(TestCase):
@classmethod
def setUpTestData(cls):
cls.expected_form = json.load(open(
os.path.join(
TESTS_DIR, 'fixtures', 'migration-form-data-expected.json'
)
))
cls.base_form = json.load(open(
os.path.join(
TESTS_DIR,
'fixtures',
'migration-form-data-input.json'
)
))

def get_base_form(self, version=None):
form = self.base_form.copy()
if version is not None:
form['version'] = version
return form

def get_expected_form(self, version=None):
form = self.expected_form.copy()
if version is not None:
form['version'] = version
return form

def test_merge_context_forms_without_version(self):
base_form = self.get_base_form()
expected_form = self.get_expected_form()
expected_form['fields'][0]['accesses'].sort(key=self.dict_sort_key)

result = merge_context_forms(base_form)
result['fields'][0]['accesses'].sort(key=self.dict_sort_key)

self.assertDictEqual(expected_form, result)

def test_merge_context_forms_with_version(self):
version = 2
base_form = self.get_base_form(version=version)
expected_form = self.get_expected_form(version=version)
expected_form['fields'][0]['accesses'].sort(key=self.dict_sort_key)

result = merge_context_forms(base_form)
result['fields'][0]['accesses'].sort(key=self.dict_sort_key)

self.assertDictEqual(expected_form, result)

def dict_sort_key(self, obj):
return obj['access_id']
2 changes: 1 addition & 1 deletion formidable/forms/field_builder_from_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_label(self):
return self.field['label']

def get_help_text(self):
return self.field['description']
return self.field.get('description', '')

def get_validations(self):
return self.field['validations']
Expand Down
7 changes: 7 additions & 0 deletions formidable/json_migrations/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def add_fields(ref_fields, new_fields):

def merge_context_forms(forms):
# forms: role => ContextForm
# remove version from context if exists
version = forms.pop('version', None)

roles = list(forms.keys())

if {'description', 'fields', 'label', 'id'}.issubset(roles):
Expand All @@ -67,4 +70,8 @@ def merge_context_forms(forms):
)
form['fields'] = fields

# set saved version if exists
if version is not None:
form['version'] = version

return form

0 comments on commit 94ac59e

Please sign in to comment.