Skip to content

Commit 94ac59e

Browse files
authored
Merge pull request #285 from peopledoc/json-migrations
FORM-13: Fix JSON migrations
2 parents 1df4f95 + 4aba31f commit 94ac59e

File tree

5 files changed

+166
-1
lines changed

5 files changed

+166
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"description": "Some description for the tests",
3+
"fields": [
4+
{
5+
"accesses": [
6+
{
7+
"access_id": "first-access-group",
8+
"level": "EDITABLE"
9+
},
10+
{
11+
"access_id": "second-access-group",
12+
"level": "EDITABLE"
13+
},
14+
{
15+
"access_id": "third-access-group",
16+
"level": "EDITABLE"
17+
}
18+
],
19+
"defaults": [],
20+
"help_text": "",
21+
"items": [],
22+
"label": "There is my label field label",
23+
"multiple": false,
24+
"placeholder": null,
25+
"slug": "multiline-text",
26+
"type_id": "paragraph",
27+
"validations": []
28+
}
29+
],
30+
"id": 0,
31+
"label": "And my form label",
32+
"presets": []
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"first-access-group": {
3+
"description": "Some description for the tests",
4+
"fields": [
5+
{
6+
"defaults": [],
7+
"disabled": false,
8+
"help_text": "",
9+
"items": [],
10+
"label": "There is my label field label",
11+
"multiple": false,
12+
"placeholder": null,
13+
"required": false,
14+
"slug": "multiline-text",
15+
"type_id": "paragraph",
16+
"validations": []
17+
}
18+
],
19+
"id": 0,
20+
"label": "And my form label",
21+
"presets": []
22+
},
23+
"second-access-group": {
24+
"description": "Some description for the tests",
25+
"fields": [
26+
{
27+
"defaults": [],
28+
"disabled": false,
29+
"help_text": "",
30+
"items": [],
31+
"label": "There is my label field label",
32+
"multiple": false,
33+
"placeholder": null,
34+
"required": false,
35+
"slug": "multiline-text",
36+
"type_id": "paragraph",
37+
"validations": []
38+
}
39+
],
40+
"id": 0,
41+
"label": "And my form label",
42+
"presets": []
43+
},
44+
"third-access-group": {
45+
"description": "Some description for the tests",
46+
"fields": [
47+
{
48+
"defaults": [],
49+
"disabled": false,
50+
"help_text": "",
51+
"items": [],
52+
"label": "There is my label field label",
53+
"multiple": false,
54+
"placeholder": null,
55+
"required": false,
56+
"slug": "multiline-text",
57+
"type_id": "paragraph",
58+
"validations": []
59+
}
60+
],
61+
"id": 0,
62+
"label": "And my form label",
63+
"presets": []
64+
}
65+
}
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import os
2+
import json
3+
from django.test import TestCase
4+
5+
from formidable.json_migrations.utils import merge_context_forms
6+
7+
TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
8+
9+
10+
class JSONMigrationUtilsTestCase(TestCase):
11+
@classmethod
12+
def setUpTestData(cls):
13+
cls.expected_form = json.load(open(
14+
os.path.join(
15+
TESTS_DIR, 'fixtures', 'migration-form-data-expected.json'
16+
)
17+
))
18+
cls.base_form = json.load(open(
19+
os.path.join(
20+
TESTS_DIR,
21+
'fixtures',
22+
'migration-form-data-input.json'
23+
)
24+
))
25+
26+
def get_base_form(self, version=None):
27+
form = self.base_form.copy()
28+
if version is not None:
29+
form['version'] = version
30+
return form
31+
32+
def get_expected_form(self, version=None):
33+
form = self.expected_form.copy()
34+
if version is not None:
35+
form['version'] = version
36+
return form
37+
38+
def test_merge_context_forms_without_version(self):
39+
base_form = self.get_base_form()
40+
expected_form = self.get_expected_form()
41+
expected_form['fields'][0]['accesses'].sort(key=self.dict_sort_key)
42+
43+
result = merge_context_forms(base_form)
44+
result['fields'][0]['accesses'].sort(key=self.dict_sort_key)
45+
46+
self.assertDictEqual(expected_form, result)
47+
48+
def test_merge_context_forms_with_version(self):
49+
version = 2
50+
base_form = self.get_base_form(version=version)
51+
expected_form = self.get_expected_form(version=version)
52+
expected_form['fields'][0]['accesses'].sort(key=self.dict_sort_key)
53+
54+
result = merge_context_forms(base_form)
55+
result['fields'][0]['accesses'].sort(key=self.dict_sort_key)
56+
57+
self.assertDictEqual(expected_form, result)
58+
59+
def dict_sort_key(self, obj):
60+
return obj['access_id']

formidable/forms/field_builder_from_schema.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def get_label(self):
3939
return self.field['label']
4040

4141
def get_help_text(self):
42-
return self.field['description']
42+
return self.field.get('description', '')
4343

4444
def get_validations(self):
4545
return self.field['validations']

formidable/json_migrations/utils.py

+7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def add_fields(ref_fields, new_fields):
4141

4242
def merge_context_forms(forms):
4343
# forms: role => ContextForm
44+
# remove version from context if exists
45+
version = forms.pop('version', None)
46+
4447
roles = list(forms.keys())
4548

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

73+
# set saved version if exists
74+
if version is not None:
75+
form['version'] = version
76+
7077
return form

0 commit comments

Comments
 (0)