You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@fluo.apache.org by GitBox <gi...@apache.org> on 2020/01/06 19:36:27 UTC

[GitHub] [fluo-muchos] keith-turner commented on a change in pull request #296: added unit tests for decorator and validators

keith-turner commented on a change in pull request #296: added unit tests for decorator and validators
URL: https://github.com/apache/fluo-muchos/pull/296#discussion_r363448568
 
 

 ##########
 File path: lib/tests/test_decorators.py
 ##########
 @@ -0,0 +1,114 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from unittest import TestCase
+
+from muchos.config import decorators
+
+
+class DecoratedThing(object):
+    @property
+    @decorators.ansible_host_var
+    def host_var1(self):
+        return 'host_var1'
+
+    @property
+    @decorators.ansible_host_var(name='named_host_var2')
+    def host_var2(self):
+        return 'named_host_var2'
+
+    @property
+    @decorators.ansible_play_var
+    def play_var1(self):
+        return 'play_var1'
+
+    @property
+    @decorators.ansible_play_var(name='named_play_var2')
+    def play_var2(self):
+        return 'named_play_var2'
+
+    @property
+    @decorators.ansible_extra_var
+    def extra_var1(self):
+        return 'extra_var1'
+
+    @property
+    @decorators.ansible_extra_var(name='named_extra_var2')
+    def extra_var2(self):
+        return 'named_extra_var2'
+
+    @property
+    @decorators.default('default_val')
+    def default_val(self):
+        return None
+
+    @property
+    @decorators.required
+    def required_val(self):
+        return 'required_val'
+
+    @property
+    @decorators.required
+    def missing_required_val(self):
+        return None
+
+
+class DecoratorTests(TestCase):
+    def _flatten_dict(d):
+        return {(k, v) for k, v in d.items()}
+
+    def test_decorators(self):
+        thing = DecoratedThing()
+
+        all_host_vars = decorators.get_ansible_vars('host')
+        all_play_vars = decorators.get_ansible_vars('play')
+        all_extra_vars = decorators.get_ansible_vars('extra')
+
+        expected_host_vars = [
+            decorators._ansible_var('host_var1', 'DecoratedThing', 'host_var1'),
+            decorators._ansible_var('named_host_var2', 'DecoratedThing', 'host_var2')
+        ]
+
+        expected_play_vars = [
+            decorators._ansible_var('play_var1', 'DecoratedThing', 'play_var1'),
+            decorators._ansible_var('named_play_var2', 'DecoratedThing', 'play_var2')
+        ]
+
+        expected_extra_vars = [
+            decorators._ansible_var('extra_var1', 'DecoratedThing', 'extra_var1'),
+            decorators._ansible_var('named_extra_var2', 'DecoratedThing', 'extra_var2')
+        ]
+
+        self.assertEquals(
+            set([str(v) for v in expected_host_vars]) - set([str(v) for v in all_host_vars]),
 
 Review comment:
   The case I was thinking of was a really buggy change like the following to decorators.py.   This change add stuff where its wanted and also where its not wanted.  Would any of the unit test fail for this change?
   
   ```python
   def ansible_var_decorator(var_type, name):
       def _decorator(func):
           ansible_var = _ansible_var(
               var_name=name if isinstance(name, str) else func.__name__,
               class_name=func.__qualname__.split('.')[0],
               property_name=func.__name__)
           _ansible_vars['host'].append(ansible_var)
           _ansible_vars['play'].append(ansible_var)
           _ansible_vars['extra'].append(ansible_var)
           return func
   
       if callable(name):
           return _decorator(name)
       return _decorator
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services