I have some method in a Python module db_access called read_all_rows that takes 2 strings as parameters and returns a list:
def read_all_rows(table_name='', mode=''):
return [] # just an example
I can mock this method non-conditionally using pytest like:
mocker.patch('db_access.read_all_rows', return_value=['testRow1', 'testRow2'])
But I want to mock its return value in pytest depending on table_name and mode parameters, so that it would return different values of parameters and combinations of them. And to make this as simple as possible.
The pseudocode of what I want:
when(db_access.read_all_rows).called_with('table_name1', any_string()).then_return(['testRow1'])
when(db_access.read_all_rows).called_with('table_name2' 'mode1').then_return(['testRow2', 'tableRow3'])
when(db_access.read_all_rows).called_with('table_name2' 'mode2').then_return(['testRow2', 'tableRow3'])
You can see that the 1st call is mocked for "any_string" placeholder.
I know that it can be achieved with side_effect like
def mock_read_all_rows:
...
mocker.patch('db_access.read_all_rows', side_effect=mock_read_all_rows)
but it is not very convenient because you need to add extra function which makes the code cumbersome. Even with lambda it is not so convenient because you would need to handle all conditions manually.
How this could be solved in a more short and readable way (ideally in a single line of code for each mock condition)?
P.S. In Java Mockito it is can be easily acheived with single line of code for each condition like
when(dbAccess.readAllRows(eq("tableName1"), any())).thenReturn(List.of(value1, value2));
...
but can I do this with Python's pytest mocker.patch?