Repository: ddionrails/ddionrails

Issue:
data.Variable.get_categories() throws KeyError when "labels_de" is not set
### Subject of the issue
data.Variable.get_categories() is used in the variable detail view.
If a variable has no "labels_de" in its categories, this method throws a KeyError.

Propsed fix in `variable.py
`
```python

    def get_categories(self) -> List:
        if self.categories:
            categories = []
            for index, _ in enumerate(self.categories["values"]):
                category = dict(
                    value=self.categories["values"][index],
                    label=self.categories["labels"][index],
                    frequency=self.categories["frequencies"][index],
                    valid=(not self.categories["missings"][index]),
                )
                if "labels_de" in self.categories:
                    category["label_de"] = self.categories["labels_de"][index]
                categories.append(category)
            return categories
        else:
            return []

```


Discussion:

Resolving patch:
diff --git a/ddionrails/data/models/variable.py b/ddionrails/data/models/variable.py
--- a/ddionrails/data/models/variable.py
+++ b/ddionrails/data/models/variable.py
@@ -149,16 +149,16 @@ def html_description_long(self):
     def get_categories(self) -> List:
         if self.categories:
             categories = []
-            for index in range(len(self.categories["values"])):
-                categories.append(
-                    dict(
-                        value=self.categories["values"][index],
-                        label=self.categories["labels"][index],
-                        label_de=self.categories["labels_de"][index],
-                        frequency=self.categories["frequencies"][index],
-                        valid=(not self.categories["missings"][index]),
-                    )
+            for index, _ in enumerate(self.categories["values"]):
+                category = dict(
+                    value=self.categories["values"][index],
+                    label=self.categories["labels"][index],
+                    frequency=self.categories["frequencies"][index],
+                    valid=(not self.categories["missings"][index]),
                 )
+                if "labels_de" in self.categories:
+                    category["label_de"] = self.categories["labels_de"][index]
+                categories.append(category)
             return categories
         else:
             return []


Test patch:
diff --git a/tests/data/test_models.py b/tests/data/test_models.py
--- a/tests/data/test_models.py
+++ b/tests/data/test_models.py
@@ -94,6 +94,18 @@ def test_get_categories_method(self, variable):
         }
         assert expected == result[0]
 
+    def test_get_categories_method_without_labels_de(self, variable):
+        variable.categories.pop("labels_de")
+        variable.save()
+        result = variable.get_categories()
+        expected = {
+            "value": "-6",
+            "label": "[-6] Version of questionnaire with modified filtering",
+            "frequency": 1,
+            "valid": False,
+        }
+        assert expected == result[0]
+
     def test_is_categorical_method(self, variable):
         variable.categories = [dict(label="some-category")]
         variable.save()