]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Create function optional_set.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 15 Mar 2022 21:12:53 +0000 (22:12 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 15 Mar 2022 21:12:53 +0000 (22:12 +0100)
tests/test_lib_sledrun_wikitext_to_json.py
wrpylib/lib_sledrun_wikitext_to_json.py

index afbc25d2c76283d8c2d3c4da1bc2c9211d45f809..72c7c316292c62b25963fb47a853ce7e2a2e08ca 100644 (file)
@@ -2,7 +2,21 @@ import unittest
 
 from mwparserfromhell import parse
 
-from wrpylib.lib_sledrun_wikitext_to_json import get_sledrun_description
+from wrpylib.lib_sledrun_wikitext_to_json import get_sledrun_description, optional_set
+
+
+class TestOptionalSet(unittest.TestCase):
+    def test_optional_set_string(self):
+        h = {'a': 3}
+        optional_set(h, 'b', 5)
+        expected = {'a': 3, 'b': 5}
+        self.assertDictEqual(expected, h)
+
+    def test_optional_set_none(self):
+        h = {'a': 3}
+        expected = h.copy()
+        optional_set(h, 'b', None)
+        self.assertDictEqual(expected, h)
 
 
 class TestGetSledrunDescription(unittest.TestCase):
index 541551e415cb89ba77351f6f1fe6e41ab06b5021..4fffb55b3252d6c75ac1274b501814fad8994e77 100644 (file)
@@ -5,6 +5,12 @@ from mwparserfromhell.nodes import Template, Text, Tag
 from mwparserfromhell.wikicode import Wikicode
 
 
+def optional_set(haystack: dict, key: str, value: Optional[Any]):
+    """Sets the key to value in haystack only if value is not None"""
+    if value is not None:
+        haystack[key] = value
+
+
 def get_sledrun_description(sledrun: Wikicode) -> Optional[str]:
     """Get description from sledrun"""
     for v in sledrun.get_sections(levels=[2], matches='Allgemeines', include_headings=False):