potiuk commented on pull request #21956:
URL: https://github.com/apache/airflow/pull/21956#issuecomment-1073213869


   > I could understand that you are mocking the return of those functions but 
I couldn't understand how it's further used in the code. If possible could you 
tell me or direct me to some relevant place to learn about it
   
   Sure - this is the key - lambda (in this case) is really an anonymous 
function:
   
   This 
   ```
    read_from_cache_mock.side_effect = lambda param_name: 
cached_values.get(param_name) 
   ```
   
   Is equivalent of this (just written in a shorter way):
   
   ```
    def read_from_cache_mocked_function(param_name):
         return cached_values.get(param_name) 
   
    read_from_cache_mock.side_effect = read_from_cache_mocked_function
   ```
   
   The "side-effect" really points to the function that will be called when the 
"read_from_cache_mock" funciton will be actually called. This allows to 
implement some logic in your test "mocks" - they become a bit smarter this way. 
Usually (in simple cases) your mocks can be told to return specific value:
   
   ```
   read_from_cache_mock.return_value = x
   ```
   
   Also you can tell it to return different values if called multiple times by 
assigning an iterable to return value:
   
   ```
   read_from_cache_mock.return_value = [return_when_called_first_time, 
return_when_called_second_time, return_when_called_third_time]
   ```
   
   And `side_effect` is the "smartest"  way - you just provide your own 
(usually very simple) implementation of a function that can return different 
return values - for example based on the parameters that are passed. 
   
   In the case above, The "read_from_cache_mock" and 
"check_cache_and_write_mock" values are really very simple implementation tha 
cache the values in memory and replace the more "file-based" cache. 
   
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to