potiuk commented on a change in pull request #20848:
URL: https://github.com/apache/airflow/pull/20848#discussion_r790585893



##########
File path: dev/breeze/tests/test_cache.py
##########
@@ -0,0 +1,67 @@
+# 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 pathlib import Path
+from unittest import mock
+
+import pytest
+
+from airflow_breeze.cache import check_if_cache_exists, 
check_if_values_allowed, read_from_cache_file
+
+AIRFLOW_SOURCES = Path(__file__).parent.parent.parent.parent
+
+
+@pytest.mark.parametrize(
+    'parameter, value, result, exception',
+    [
+        ("backends", "mysql", (True, ['sqlite', 'mysql', 'postgres', 
'mssql']), None),
+        ("backends", "xxx", (False, ['sqlite', 'mysql', 'postgres', 'mssql']), 
None),
+        ("python_major_minor_version", "3.8", (True, ['3.6', '3.7', '3.8', 
'3.9']), None),
+        ("python_major_minor_version", "3.5", (False, ['3.6', '3.7', '3.8', 
'3.9']), None),
+        ("missing", "value", None, AttributeError),
+    ],
+)
+def test_allowed_values(parameter, value, result, exception):
+    if exception:
+        with pytest.raises(expected_exception=exception):
+            check_if_values_allowed(parameter, value)
+    else:
+        assert result == check_if_values_allowed(parameter, value)
+
+
+@mock.patch("airflow_breeze.cache.Path")
+def test_check_if_cache_exists(path):
+    check_if_cache_exists("test_param")
+    path.assert_called_once_with(AIRFLOW_SOURCES / ".build")
+
+
+@pytest.mark.parametrize(

Review comment:
       Very good question!
   
   Yuou usually do it with mocking. See 
https://github.com/apache/airflow/pull/20791/files#diff-3ad508267c15154c09aa339dc7224679bc9260a48b0fc9f5e3f288e6e74210a3R45
 
   
   Basically it's about replacing the real class or method with a "mock" 
variant of it.  
   In short you "patch" the real class that performs some operation with a 
mock. Then you can check if the right method of the mock has been called. 
   
   For example when you want to test if "delete_cache()" works, you can patch 
the os.remove method and check if it has been called:
   
   
https://stackoverflow.com/questions/61610719/mock-os-remove-in-python-with-unittest-mock
 (this example does not have "assert" to check if it has been called and you 
can add it as an exercise :)
   
   In general - I recommend to find and read and practice some tutorials about 
mocking for unitttests in Python  - first I found is here but there are plemty 
of them https://www.toptal.com/python/an-introduction-to-mocking-in-python
   
   This is an extremely useful technique and we use it a lot in Airflow for our 
unit tests.
   
   
   




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

To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to