From 67ac2c59f5abb9c6e180b9bf3587bcea1fae98b7 Mon Sep 17 00:00:00 2001
From: Geert Kapteijns <ghkapteijns@gmail.com>
Date: Fri, 16 Dec 2016 22:32:12 +0100
Subject: [PATCH] Extract model_form_data to common/helpers

This function helps to create form data from models.
See example given.
---
 common/__init__.py         |  0
 common/helpers/__init__.py | 30 ++++++++++++++++++++++++++++++
 theses/test_forms.py       | 22 +++++++++-------------
 3 files changed, 39 insertions(+), 13 deletions(-)
 create mode 100644 common/__init__.py
 create mode 100644 common/helpers/__init__.py

diff --git a/common/__init__.py b/common/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/common/helpers/__init__.py b/common/helpers/__init__.py
new file mode 100644
index 000000000..a29cff832
--- /dev/null
+++ b/common/helpers/__init__.py
@@ -0,0 +1,30 @@
+def model_form_data(model, form_class):
+    '''
+    Returns a dict that can be used to instantiate a form object.
+    It fills in the model's data, but filters out fields that are not on the form.
+    Example:
+
+    class Car(models.Model):
+        brand = CharField(max_length = 50)
+        fuel_tank_size = FloatField()
+        # more fields
+
+    class CreateCarForm(forms.ModelForm):
+        fields = ['brand']
+
+    my_car = Car(brand='Nissan', fuel_tank_size=60)
+
+    model_form_data(my_car, CreateCarForm)
+    # returns {'brand': 'Nissan'}
+
+    Note that the returned dict does not have a field 'fuel_tank_size', because it is not
+    on the form.
+    '''
+
+    model_data = model.__dict__
+    form_fields = list(form_class().fields.keys())
+    return filter_keys(model_data, form_fields)
+
+
+def filter_keys(dictionary, keys_to_keep):
+    return {key: dictionary[key] for key in keys_to_keep}
diff --git a/theses/test_forms.py b/theses/test_forms.py
index ef68a20a6..7a009ff9e 100644
--- a/theses/test_forms.py
+++ b/theses/test_forms.py
@@ -5,27 +5,23 @@ from django.test import TestCase
 from .factories import ThesisLinkFactory
 from .forms import RequestThesisLinkForm
 
-
-def valid_form_data(thesis_link):
-    thesis_link_data = thesis_link.__dict__
-    form_fields = list(RequestThesisLinkForm().fields.keys())
-    return filter_keys(thesis_link_data, form_fields)
-
-
-def filter_keys(dictionary, keys_to_keep):
-    return {key: dictionary[key] for key in keys_to_keep}
+from common.helpers import model_form_data
 
 
 class TestRequestThesisLink(TestCase):
     fixtures = ['permissions', 'groups']
 
+    def setUp(self):
+        self.valid_form_data = model_form_data(ThesisLinkFactory(), RequestThesisLinkForm)
+
     def test_valid_data_is_valid(self):
-        thesis_data = valid_form_data(ThesisLinkFactory())
-        form = RequestThesisLinkForm(thesis_data)
+        form_data = self.valid_form_data
+        form = RequestThesisLinkForm(self.valid_form_data)
         self.assertTrue(form.is_valid())
 
     def test_empty_domain_is_invalid(self):
-        thesis_data = valid_form_data(ThesisLinkFactory(domain=''))
-        form = RequestThesisLinkForm(thesis_data)
+        form_data = self.valid_form_data
+        form_data['domain'] = ''
+        form = RequestThesisLinkForm(form_data)
         form.is_valid()
         self.assertEqual(form.errors['domain'], ['This field is required.'])
-- 
GitLab