You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/04/24 07:05:27 UTC

[GitHub] [airflow] voltaiq-austin-chou opened a new pull request #8535: [Airflow-8439] Refactor test_variable_command.py

voltaiq-austin-chou opened a new pull request #8535:
URL: https://github.com/apache/airflow/pull/8535


   Refactored CLI variable command tests
   
   ---
   Make sure to mark the boxes below before creating PR: [x]
   
   - [ X ] Description above provides context of the change
   - [ X ] Unit tests coverage for changes (not needed for documentation changes)
   - [ X ] Target Github ISSUE in description if exists
   - [ X ] Commits follow "[How to write a good git commit message](http://chris.beams.io/posts/git-commit/)"
   - [ X ] Relevant documentation is updated including usage instructions.
   - [ X ] I will engage committers as explained in [Contribution Workflow Example](https://github.com/apache/airflow/blob/master/CONTRIBUTING.rst#contribution-workflow-example).
   
   ---
   In case of fundamental code change, Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvements+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in [UPDATING.md](https://github.com/apache/airflow/blob/master/UPDATING.md).
   Read the [Pull Request Guidelines](https://github.com/apache/airflow/blob/master/CONTRIBUTING.rst#pull-request-guidelines) for more information.
   


----------------------------------------------------------------
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



[GitHub] [airflow] seanxwzhang commented on a change in pull request #8535: [Airflow-8439] Refactor test_variable_command.py

Posted by GitBox <gi...@apache.org>.
seanxwzhang commented on a change in pull request #8535:
URL: https://github.com/apache/airflow/pull/8535#discussion_r419210541



##########
File path: tests/cli/commands/test_variable_command.py
##########
@@ -31,58 +34,38 @@ def setUpClass(cls):
         cls.dagbag = models.DagBag(include_examples=True)
         cls.parser = cli_parser.get_parser()
 
-    def test_variables(self):
-        # Checks if all subcommands are properly received
+    def setUp(self):
+        clear_db_variables()
+
+    def tearDown(self):
+        clear_db_variables()
+
+    def test_variables_set(self):
+        """Test variable_set command"""
+        variable_command.variables_set(self.parser.parse_args([
+            'variables', 'set', 'foo', 'bar']))
+        self.assertIsNotNone(Variable.get("foo"))
+        self.assertRaises(KeyError, Variable.get, "foo1")
+
+    @unittest.mock.patch('sys.stdout', new_callable=io.StringIO)
+    def test_variables_get(self, mock_stdout):
+        """"Test variable_get command"""
+        # Test conventional get call
         variable_command.variables_set(self.parser.parse_args([
             'variables', 'set', 'foo', '{"foo":"bar"}']))
         variable_command.variables_get(self.parser.parse_args([
             'variables', 'get', 'foo']))
         variable_command.variables_get(self.parser.parse_args([
             'variables', 'get', 'baz', '--default', 'bar']))
-        variable_command.variables_list(self.parser.parse_args([
-            'variables', 'list']))
-        variable_command.variables_delete(self.parser.parse_args([
-            'variables', 'delete', 'bar']))
-        variable_command.variables_import(self.parser.parse_args([
-            'variables', 'import', os.devnull]))
-        variable_command.variables_export(self.parser.parse_args([
-            'variables', 'export', os.devnull]))
-
-        variable_command.variables_set(self.parser.parse_args([
-            'variables', 'set', 'bar', 'original']))
-        # First export
-        variable_command.variables_export(self.parser.parse_args([
-            'variables', 'export', 'variables1.json']))
-
-        first_exp = open('variables1.json', 'r')
-
-        variable_command.variables_set(self.parser.parse_args([
-            'variables', 'set', 'bar', 'updated']))
-        variable_command.variables_set(self.parser.parse_args([
-            'variables', 'set', 'foo', '{"foo":"oops"}']))
-        variable_command.variables_delete(self.parser.parse_args([
-            'variables', 'delete', 'foo']))
-        # First import
-        variable_command.variables_import(self.parser.parse_args([
-            'variables', 'import', 'variables1.json']))
-
-        self.assertEqual('original', Variable.get('bar'))
-        self.assertEqual('{\n  "foo": "bar"\n}', Variable.get('foo'))
-        # Second export
-        variable_command.variables_export(self.parser.parse_args([
-            'variables', 'export', 'variables2.json']))
+        self.assertEqual(mock_stdout.getvalue(), 'bar\n')

Review comment:
       It seems that this assertion is failing on master. Perhaps you meant
   ```
   self.assertEqual(mock_stdout.getvalue(), '{"foo":"bar"}\nbar\n')
   ```
   ?




----------------------------------------------------------------
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



[GitHub] [airflow] mik-laj commented on a change in pull request #8535: [Airflow-8439] Refactor test_variable_command.py

Posted by GitBox <gi...@apache.org>.
mik-laj commented on a change in pull request #8535:
URL: https://github.com/apache/airflow/pull/8535#discussion_r415204731



##########
File path: tests/cli/commands/test_variable_command.py
##########
@@ -124,10 +82,78 @@ def test_variables(self):
         self.assertEqual(False, Variable.get('false', deserialize_json=True))
         self.assertEqual(None, Variable.get('null', deserialize_json=True))
 
-        os.remove('variables1.json')
-        os.remove('variables2.json')
-        os.remove('variables3.json')
+        os.remove('variables_types.json')
 
-    def test_get_missing_variable(self):
+    @unittest.mock.patch('sys.stdout', new_callable=io.StringIO)
+    def test_variables_get(self, mock_stdout):
+        """"Test variable_get command"""
+        # Test conventional get call
+        variable_command.variables_set(self.parser.parse_args([
+            'variables', 'set', 'foo', '{"foo":"bar"}']))
+        variable_command.variables_get(self.parser.parse_args([
+            'variables', 'get', 'foo']))
+        self.assertEqual(mock_stdout.getvalue(), '{"foo":"bar"}\n')
         with self.assertRaises(SystemExit):
-            variable_command.variables_get(self.parser.parse_args(['variables', 'get', 'no-existing-VAR']))
+            variable_command.variables_get(self.parser.parse_args(['variables', 'get', 'no-existing-VAR'])

Review comment:
       ```suggestion
               variable_command.variables_get(self.parser.parse_args(['variables', 'get', 'no-existing-VAR']))
   ```




----------------------------------------------------------------
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



[GitHub] [airflow] mik-laj commented on a change in pull request #8535: [Airflow-8439] Refactor test_variable_command.py

Posted by GitBox <gi...@apache.org>.
mik-laj commented on a change in pull request #8535:
URL: https://github.com/apache/airflow/pull/8535#discussion_r415205019



##########
File path: tests/cli/commands/test_variable_command.py
##########
@@ -31,58 +32,15 @@ def setUpClass(cls):
         cls.dagbag = models.DagBag(include_examples=True)
         cls.parser = cli_parser.get_parser()
 

Review comment:
       ```suggestion
   
       def setUp(self):
           clear_db_variables()
   
       def tearDown(self):
           clear_db_variables()
   
   ```
   This will allow us to maintain isolation between tests and reduce side effects.




----------------------------------------------------------------
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



[GitHub] [airflow] mik-laj commented on a change in pull request #8535: [Airflow-8439] Refactor test_variable_command.py

Posted by GitBox <gi...@apache.org>.
mik-laj commented on a change in pull request #8535:
URL: https://github.com/apache/airflow/pull/8535#discussion_r419217277



##########
File path: tests/cli/commands/test_variable_command.py
##########
@@ -31,58 +34,38 @@ def setUpClass(cls):
         cls.dagbag = models.DagBag(include_examples=True)
         cls.parser = cli_parser.get_parser()
 
-    def test_variables(self):
-        # Checks if all subcommands are properly received
+    def setUp(self):
+        clear_db_variables()
+
+    def tearDown(self):
+        clear_db_variables()
+
+    def test_variables_set(self):
+        """Test variable_set command"""
+        variable_command.variables_set(self.parser.parse_args([
+            'variables', 'set', 'foo', 'bar']))
+        self.assertIsNotNone(Variable.get("foo"))
+        self.assertRaises(KeyError, Variable.get, "foo1")
+
+    @unittest.mock.patch('sys.stdout', new_callable=io.StringIO)
+    def test_variables_get(self, mock_stdout):
+        """"Test variable_get command"""
+        # Test conventional get call
         variable_command.variables_set(self.parser.parse_args([
             'variables', 'set', 'foo', '{"foo":"bar"}']))
         variable_command.variables_get(self.parser.parse_args([
             'variables', 'get', 'foo']))
         variable_command.variables_get(self.parser.parse_args([
             'variables', 'get', 'baz', '--default', 'bar']))
-        variable_command.variables_list(self.parser.parse_args([
-            'variables', 'list']))
-        variable_command.variables_delete(self.parser.parse_args([
-            'variables', 'delete', 'bar']))
-        variable_command.variables_import(self.parser.parse_args([
-            'variables', 'import', os.devnull]))
-        variable_command.variables_export(self.parser.parse_args([
-            'variables', 'export', os.devnull]))
-
-        variable_command.variables_set(self.parser.parse_args([
-            'variables', 'set', 'bar', 'original']))
-        # First export
-        variable_command.variables_export(self.parser.parse_args([
-            'variables', 'export', 'variables1.json']))
-
-        first_exp = open('variables1.json', 'r')
-
-        variable_command.variables_set(self.parser.parse_args([
-            'variables', 'set', 'bar', 'updated']))
-        variable_command.variables_set(self.parser.parse_args([
-            'variables', 'set', 'foo', '{"foo":"oops"}']))
-        variable_command.variables_delete(self.parser.parse_args([
-            'variables', 'delete', 'foo']))
-        # First import
-        variable_command.variables_import(self.parser.parse_args([
-            'variables', 'import', 'variables1.json']))
-
-        self.assertEqual('original', Variable.get('bar'))
-        self.assertEqual('{\n  "foo": "bar"\n}', Variable.get('foo'))
-        # Second export
-        variable_command.variables_export(self.parser.parse_args([
-            'variables', 'export', 'variables2.json']))
+        self.assertEqual(mock_stdout.getvalue(), 'bar\n')

Review comment:
       This assertion failing on master, because this PR introduced unrelated change: https://github.com/apache/airflow/pull/8640/files
   
   Here is my fix: https://github.com/apache/airflow/pull/8698
   Can you do the review?




----------------------------------------------------------------
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



[GitHub] [airflow] boring-cyborg[bot] commented on pull request #8535: [Airflow-8439] Refactor test_variable_command.py

Posted by GitBox <gi...@apache.org>.
boring-cyborg[bot] commented on pull request #8535:
URL: https://github.com/apache/airflow/pull/8535#issuecomment-618841719


   Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/airflow/blob/master/CONTRIBUTING.rst)
   Here are some useful points:
   - Pay attention to the quality of your code (flake8, pylint and type annotations). Our [pre-commits]( https://github.com/apache/airflow/blob/master/STATIC_CODE_CHECKS.rst#prerequisites-for-pre-commit-hooks) will help you with that.
   - In case of a new feature add useful documentation (in docstrings or in `docs/` directory). Adding a new operator? Check this short [guide](https://github.com/apache/airflow/blob/master/docs/howto/custom-operator.rst) Consider adding an example DAG that shows how users should use it.
   - Consider using [Breeze environment](https://github.com/apache/airflow/blob/master/BREEZE.rst) for testing locally, itโ€™s a heavy docker but it ships with a working Airflow and a lot of integrations.
   - Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
   - Be sure to read the [Airflow Coding style]( https://github.com/apache/airflow/blob/master/CONTRIBUTING.rst#coding-style-and-best-practices).
   Apache Airflow is a community-driven project and together we are making it better ๐Ÿš€.
   In case of doubts contact the developers at:
   Mailing List: dev@airflow.apache.org
   Slack: https://apache-airflow-slack.herokuapp.com/
   


----------------------------------------------------------------
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



[GitHub] [airflow] boring-cyborg[bot] commented on pull request #8535: [Airflow-8439] Refactor test_variable_command.py

Posted by GitBox <gi...@apache.org>.
boring-cyborg[bot] commented on pull request #8535:
URL: https://github.com/apache/airflow/pull/8535#issuecomment-623146753


   Awesome work, congrats on your first merged pull request!
   


----------------------------------------------------------------
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



[GitHub] [airflow] mik-laj commented on a change in pull request #8535: [Airflow-8439] Refactor test_variable_command.py

Posted by GitBox <gi...@apache.org>.
mik-laj commented on a change in pull request #8535:
URL: https://github.com/apache/airflow/pull/8535#discussion_r415205547



##########
File path: tests/cli/commands/test_variable_command.py
##########
@@ -124,10 +82,78 @@ def test_variables(self):
         self.assertEqual(False, Variable.get('false', deserialize_json=True))
         self.assertEqual(None, Variable.get('null', deserialize_json=True))
 
-        os.remove('variables1.json')
-        os.remove('variables2.json')
-        os.remove('variables3.json')
+        os.remove('variables_types.json')
 
-    def test_get_missing_variable(self):
+    @unittest.mock.patch('sys.stdout', new_callable=io.StringIO)
+    def test_variables_get(self, mock_stdout):
+        """"Test variable_get command"""
+        # Test conventional get call
+        variable_command.variables_set(self.parser.parse_args([
+            'variables', 'set', 'foo', '{"foo":"bar"}']))
+        variable_command.variables_get(self.parser.parse_args([
+            'variables', 'get', 'foo']))
+        self.assertEqual(mock_stdout.getvalue(), '{"foo":"bar"}\n')
         with self.assertRaises(SystemExit):
-            variable_command.variables_get(self.parser.parse_args(['variables', 'get', 'no-existing-VAR']))
+            variable_command.variables_get(self.parser.parse_args(['variables', 'get', 'no-existing-VAR'])
+
+        # Test default functionality for get call
+        variable_command.variables_get(self.parser.parse_args([
+            'variables', 'get', 'baz', '--default', 'bar']))
+        self.assertEqual(mock_stdout.getvalue(), '{"foo":"bar"}\nbar\n')
+
+    def test_variables_list(self):
+        """Test variable_list command"""
+        # Test command is received
+        variable_command.variables_list(self.parser.parse_args([
+            'variables', 'list']))
+
+    def test_variables_delete(self):
+        """Test variable_delete command"""
+        variable_command.variables_set(self.parser.parse_args([
+            'variables', 'set', 'foo', 'bar']))
+        variable_command.variables_delete(self.parser.parse_args([
+            'variables', 'delete', 'foo']))
+        self.assertRaises(KeyError, Variable.get, "foo")
+
+    def test_variables_import(self):
+        """Test variables_import command"""
+        variable_command.variables_import(self.parser.parse_args([
+            'variables', 'import', os.devnull]))
+
+    def test_variables_export(self):
+        """Test variables_export command"""
+        variable_command.variables_export(self.parser.parse_args([
+            'variables', 'export', os.devnull]))
+
+    def test_variables_isolation(self):
+        """Test isolation of variables"""
+        variable_command.variables_set(self.parser.parse_args([
+            'variables', 'set', 'bar', 'original']))
+        variable_command.variables_export(self.parser.parse_args([
+            'variables', 'export', 'variables1.json']))
+
+        first_exp = open('variables1.json', 'r')

Review comment:
       Can you use NamedTemporaryFile? This deletes the file even if the test fails.




----------------------------------------------------------------
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



[GitHub] [airflow] mik-laj commented on pull request #8535: [Airflow-8439] Refactor test_variable_command.py

Posted by GitBox <gi...@apache.org>.
mik-laj commented on pull request #8535:
URL: https://github.com/apache/airflow/pull/8535#issuecomment-620468036


   We do squash before merging. It's simple, because it's part of Github UI. 


----------------------------------------------------------------
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



[GitHub] [airflow] voltaiq-austin-chou commented on pull request #8535: [Airflow-8439] Refactor test_variable_command.py

Posted by GitBox <gi...@apache.org>.
voltaiq-austin-chou commented on pull request #8535:
URL: https://github.com/apache/airflow/pull/8535#issuecomment-620424416


   @mik-laj will implement thanks for your suggestions! Quick question though, do we have to squash our commits as part of the merge?


----------------------------------------------------------------
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



[GitHub] [airflow] mik-laj commented on pull request #8535: [Airflow-8439] Refactor test_variable_command.py

Posted by GitBox <gi...@apache.org>.
mik-laj commented on pull request #8535:
URL: https://github.com/apache/airflow/pull/8535#issuecomment-619476570


   Can you aadd some tests that check stdout?
   Examplle
   ```
           with contextlib.redirect_stdout(io.StringIO()) as temp_stdout:
               config_command.show_config(self.parser.parse_args(['config']))
           self.assertIn('[core]', temp_stdout.getvalue())
   
   ```


----------------------------------------------------------------
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