populate_db: Fix data for "Favorite editor" custom field.

The field_data sent from client while creating a select
type field is a dict with a number as key.

In development database the field data for "Favorite editor"
field was of different form where the option label was used
as key in the dict.

This commit fixes it to be of the same as it is when creating
a field from web-app. As a result, we also need to update
the tests and this commit also update field_data for other
select-type fields.
This commit is contained in:
Sahil Batra 2022-06-30 22:49:21 +05:30 committed by Tim Abbott
parent 2b30c3c37c
commit 690420ffa0
4 changed files with 34 additions and 34 deletions

View File

@ -1624,7 +1624,7 @@ paths:
"name": "Favorite editor",
"type": 3,
"hint": "",
"field_data": '{"vim":{"text":"Vim","order":"1"},"emacs":{"text":"Emacs","order":"2"}}',
"field_data": '{"0":{"text":"Vim","order":"1"},"1":{"text":"Emacs","order":"2"}}',
"order": 4,
},
{
@ -6243,7 +6243,7 @@ paths:
"is_guest": false,
"profile_data":
{
"4": {"value": "vim"},
"4": {"value": "0"},
"2":
{
"value": "I am:\n* The prince of Denmark\n* Nephew to the usurping Claudius",
@ -7753,7 +7753,7 @@ paths:
"name": "Favorite editor",
"type": 3,
"hint": "",
"field_data": '{"vim":{"text":"Vim","order":"1"},"emacs":{"text":"Emacs","order":"2"}}',
"field_data": '{"0":{"text":"Vim","order":"1"},"1":{"text":"Emacs","order":"2"}}',
"order": 4,
},
{
@ -8482,7 +8482,7 @@ paths:
"is_guest": false,
"profile_data":
{
"4": {"value": "vim"},
"4": {"value": "0"},
"2":
{
"value": "I am:\n* The prince of Denmark\n* Nephew to the usurping Claudius",
@ -8572,7 +8572,7 @@ paths:
"is_guest": false,
"profile_data":
{
"4": {"value": "vim"},
"4": {"value": "0"},
"2":
{
"value": "I am:\n* The prince of Denmark\n* Nephew to the usurping Claudius",
@ -8666,7 +8666,7 @@ paths:
items:
type: object
example:
[{"id": 4, "value": "vim"}, {"id": 5, "value": "1909-04-05"}]
[{"id": 4, "value": "0"}, {"id": 5, "value": "1909-04-05"}]
required: false
responses:

View File

@ -106,8 +106,8 @@ class CreateCustomProfileFieldTest(CustomProfileFieldTestCase):
data["field_data"] = orjson.dumps(
{
"python": {"text": "Python"},
"java": {"text": "Java"},
"0": {"text": "Python"},
"1": {"text": "Java"},
}
).decode()
result = self.client_post("/json/realm/profile_fields", info=data)
@ -115,8 +115,8 @@ class CreateCustomProfileFieldTest(CustomProfileFieldTestCase):
data["field_data"] = orjson.dumps(
{
"python": {"text": "Python", "order": ""},
"java": {"text": "Java", "order": "2"},
"0": {"text": "Python", "order": ""},
"1": {"text": "Java", "order": "2"},
}
).decode()
result = self.client_post("/json/realm/profile_fields", info=data)
@ -125,7 +125,7 @@ class CreateCustomProfileFieldTest(CustomProfileFieldTestCase):
data["field_data"] = orjson.dumps(
{
"": {"text": "Python", "order": "1"},
"java": {"text": "Java", "order": "2"},
"1": {"text": "Java", "order": "2"},
}
).decode()
result = self.client_post("/json/realm/profile_fields", info=data)
@ -133,8 +133,8 @@ class CreateCustomProfileFieldTest(CustomProfileFieldTestCase):
data["field_data"] = orjson.dumps(
{
"python": {"text": "Python", "order": 1},
"java": {"text": "Java", "order": "2"},
"0": {"text": "Python", "order": 1},
"1": {"text": "Java", "order": "2"},
}
).decode()
result = self.client_post("/json/realm/profile_fields", info=data)
@ -146,8 +146,8 @@ class CreateCustomProfileFieldTest(CustomProfileFieldTestCase):
data["field_data"] = orjson.dumps(
{
"python": {"text": "Duplicate", "order": "1"},
"java": {"text": "Duplicate", "order": "2"},
"0": {"text": "Duplicate", "order": "1"},
"1": {"text": "Duplicate", "order": "2"},
}
).decode()
result = self.client_post("/json/realm/profile_fields", info=data)
@ -155,8 +155,8 @@ class CreateCustomProfileFieldTest(CustomProfileFieldTestCase):
data["field_data"] = orjson.dumps(
{
"python": {"text": "Python", "order": "1"},
"java": {"text": "Java", "order": "2"},
"0": {"text": "Python", "order": "1"},
"1": {"text": "Java", "order": "2"},
}
).decode()
result = self.client_post("/json/realm/profile_fields", info=data)
@ -497,8 +497,8 @@ class UpdateCustomProfileFieldTest(CustomProfileFieldTestCase):
field_data = orjson.dumps(
{
"vim": "Vim",
"emacs": {"order": "2", "text": "Emacs"},
"0": "Vim",
"1": {"order": "2", "text": "Emacs"},
}
).decode()
result = self.client_patch(
@ -509,9 +509,9 @@ class UpdateCustomProfileFieldTest(CustomProfileFieldTestCase):
field_data = orjson.dumps(
{
"vim": {"order": "1", "text": "Vim"},
"emacs": {"order": "2", "text": "Emacs"},
"notepad": {"order": "3", "text": "Notepad"},
"0": {"order": "1", "text": "Vim"},
"1": {"order": "2", "text": "Emacs"},
"2": {"order": "3", "text": "Notepad"},
}
).decode()
result = self.client_patch(
@ -594,7 +594,7 @@ class UpdateCustomProfileFieldTest(CustomProfileFieldTestCase):
("Phone number", "*short* text data"),
("Biography", "~~short~~ **long** text data"),
("Favorite food", "long short text data"),
("Favorite editor", "vim"),
("Favorite editor", "0"),
("Birthday", "1909-03-05"),
("Favorite website", "https://zulip.com"),
("Mentor", [self.example_user("cordelia").id]),
@ -667,7 +667,7 @@ class UpdateCustomProfileFieldTest(CustomProfileFieldTestCase):
data = [
{
"id": field.id,
"value": "emacs",
"value": "1",
}
]
@ -732,23 +732,23 @@ class UpdateCustomProfileFieldTest(CustomProfileFieldTestCase):
realm = get_realm("zulip")
field = CustomProfileField.objects.get(name="Favorite editor", realm=realm)
self.assertTrue(
CustomProfileFieldValue.objects.filter(field_id=field.id, value="vim").exists()
CustomProfileFieldValue.objects.filter(field_id=field.id, value="0").exists()
)
self.assertTrue(
CustomProfileFieldValue.objects.filter(field_id=field.id, value="emacs").exists()
CustomProfileFieldValue.objects.filter(field_id=field.id, value="1").exists()
)
new_options = {"emacs": {"text": "Emacs", "order": "1"}}
new_options = {"1": {"text": "Emacs", "order": "1"}}
result = self.client_patch(
f"/json/realm/profile_fields/{field.id}",
info={"name": "Favorite editor", "field_data": orjson.dumps(new_options).decode()},
)
self.assert_json_success(result)
self.assertFalse(
CustomProfileFieldValue.objects.filter(field_id=field.id, value="vim").exists()
CustomProfileFieldValue.objects.filter(field_id=field.id, value="0").exists()
)
self.assertTrue(
CustomProfileFieldValue.objects.filter(field_id=field.id, value="emacs").exists()
CustomProfileFieldValue.objects.filter(field_id=field.id, value="1").exists()
)

View File

@ -608,7 +608,7 @@ class PermissionTest(ZulipTestCase):
"Phone number": "short text data",
"Biography": "long text data",
"Favorite food": "short text data",
"Favorite editor": "vim",
"Favorite editor": "0",
"Birthday": "1909-03-05",
"Favorite website": "https://zulip.com",
"Mentor": [cordelia.id],

View File

@ -726,8 +726,8 @@ class Command(BaseCommand):
hint="Or drink, if you'd prefer",
)
field_data: ProfileFieldData = {
"vim": {"text": "Vim", "order": "1"},
"emacs": {"text": "Emacs", "order": "2"},
"0": {"text": "Vim", "order": "1"},
"1": {"text": "Emacs", "order": "2"},
}
favorite_editor = try_add_realm_custom_profile_field(
zulip_realm, "Favorite editor", CustomProfileField.SELECT, field_data=field_data
@ -754,7 +754,7 @@ class Command(BaseCommand):
{"id": phone_number.id, "value": "+1-234-567-8901"},
{"id": biography.id, "value": "Betrayer of Othello."},
{"id": favorite_food.id, "value": "Apples"},
{"id": favorite_editor.id, "value": "emacs"},
{"id": favorite_editor.id, "value": "1"},
{"id": birthday.id, "value": "2000-01-01"},
{"id": favorite_website.id, "value": "https://zulip.readthedocs.io/en/latest/"},
{"id": mentor.id, "value": [hamlet.id]},
@ -770,7 +770,7 @@ class Command(BaseCommand):
"value": "I am:\n* The prince of Denmark\n* Nephew to the usurping Claudius",
},
{"id": favorite_food.id, "value": "Dark chocolate"},
{"id": favorite_editor.id, "value": "vim"},
{"id": favorite_editor.id, "value": "0"},
{"id": birthday.id, "value": "1900-01-01"},
{"id": favorite_website.id, "value": "https://blog.zulig.org"},
{"id": mentor.id, "value": [iago.id]},