Package: mockito-calliari
MotivationWhen employing mocks to substitute actual databases in unit tests, a common challenge arises wherein the tests may not accurately reflect the data in the database, thereby overlooking specific and unique scenarios.To address this issue, Mockito strives to standardize the data being mocked.StructureImagine that are 2 SQL tables, with the following structures:table1ColumnTypeidintegernametextmenustexttable2ColumnTypeidintegernametextdescriptiontextBased on them, it is necessary to create a dictionary, where the keys are the names of the tables, and the values are a list of dictionaries, where these dictionary keys would be the table columns and the values are examples of data to be returned by Mockito.In the example below, the dictionary will containtable1andtable2, where thetable1will have 2 data examples and thetable2only will have 1.data_file.pyDEFAULT_BASES = {
    "table1": [
        {"id": 1, "name": "Guest", "menus": "menu_1"},
        {"id": 2, "name": "Some One", "menus": "menu_2"},
    ],
    "table2": [
        {"id": 1, "name": "OtherName", "Description": "Super important data"},
    ]
}Method one()Basic utilization:The function below returns theidandnamefromtable1:file.pydef return_table_data(id):
    table_data = Table1.query.filter(
        Table1.id == id
    ).with_entities(
        Table1.id,
        Table1.name
    ).first()
    
    return table_dataThe function test, using the methodone()fromMockitowill be something like this:from mockito.mockito import Mockito

from data_file import DEFAULT_BASES
from file import return_table_data


@mock_patch('file.Table1')
class TestReturnData():
def test_retorn_data(self, mock_table1):
    mock_table1.query.filter.return_value.with_entities.return_value.first.return_value = (
        Mockito(
            data_dict={"table1": ["id", "name"]},
            data_bases=DEFAULT_BASES,
        ).one()
    )
    
    response = return_table_data(id = 1)
    print(response)
    print(response.id)
    print(response.name)
    print(response.to_dict())Outputs:<MoMock>
1
Guest
{"id": 1, "name": "Guest"}The object return from theMockitois calledMoMock, which is aMockwith a method calledto_dict()Column with alias:To assign the aliaspotatoto thenamecolumn within the function, provide the list["name", "potato"]to Mockito. Note that theidcolumn will also be included in the result, but it won't be given an alias:from mockito.mockito import Mockito

from data_file import DEFAULT_BASES
from file import return_table_data


@mock_patch('file.Table1')
class TestReturnData():
def test_retorn_data(self, mock_table1):
    mock_table1.query.filter.return_value.with_entities.return_value.first.return_value = (
        Mockito(
            data_dict={"table1": ["id", ["name", "potato"]]},
            data_bases=DEFAULT_BASES,
        ).one()
    )
    
    response = return_table_data(id = 1)
    print(response)
    print(response.id)
    print(response.potato)
    print(response.to_dict())Outputs:<MoMock>
1
Guest
{"id": 1, "potato": "Guest"}Non-existent column:If a column is passed toMockitothat does not exist inDEFAULT_BASESfor the table in question, it will have the valueinvalid columnin its place, in the example below thefoocolumn was chosen and it is not present in theDEFAULT_BASESdictionary for thetable1table:from mockito.mockito import Mockito

from data_file import DEFAULT_BASES
from file import return_table_data


@mock_patch('file.Table1')
class TestReturnData():
def test_retorn_data(self, mock_table1):
    mock_table1.query.filter.return_value.with_entities.return_value.first.return_value = (
        Mockito(
            data_dict={"table1": ["id", "foo"]},
            data_bases=DEFAULT_BASES,
        ).one()
    )
    
    response = return_table_data(id = 1)
    print(response)
    print(response.id)
    print(response.foo)
    print(response.to_dict())Outputs:<MoMock>
1
invalid column
{"id": 1, "foo": "invalid column"}JoinsTo retrieve data from multiple tables, you can achieve this by providing the table name along with its respective columns toMockito. In the example below, it will return theidandnamecolumns fromtable1, as well as thedescriptioncolumn from thetable2:file2.pydef return_table_data(id):
    table_data = Table1.query.filter(
        Table1.id == id
    ).join(
        Table2, "some valid condition here"
    ).with_entities(
        Table1.id,
        Table1.name
    ).first()
    
    return table_dataTest_file.pyfrom mockito.mockito import Mockito

from data_file import DEFAULT_BASES
from file2 import return_table_data


@mock_patch('file2.Table1')
class TestReturnData():
def test_retorn_data(self, mock_table1):
    mock_table1.query.filter.return_value.join.return_value.with_entities.return_value.first.return_value = (
        Mockito(
            data_dict={
                "table1": ["id", "name"],
                "table2": ["description"],
            },
            data_bases=DEFAULT_BASES,
        ).one()
    )
    
    response = return_table_data(id = 1)
    print(response)
    print(response.id)
    print(response.name)
    print(response.description)
    print(response.to_dict())Outputs:<MoMock>
1
Guest
Super important data
{"id": 1, "name": "Guest": "description": "Super important data"}Method all_combinations()Basic utilization:This method is similar toone()but will return a list of all combinations in theDEFAULT_TABLESvariable.from mockito.mockito import Mockito

from data_file import DEFAULT_BASES
from file import return_table_data


@mock_patch('file.Table1')
class TestReturnData():
def test_retorn_data(self, mock_table1):
    mock_table1.query.filter.return_value.with_entities.return_value.first.return_value = (
        Mockito(
            data_dict={"table1": ["id", "name"]},
            data_bases=DEFAULT_BASES,
        ).all_combinations()
    )
    
    response = return_table_data(id = 1)
    print(response)
    print(response[0].id)
    print(response[0].name)
    print(response[0].to_dict())
    print(response[1].id)
    print(response[1].name)
    print(response[1].to_dict())Outputs:[<MoMock>, <MoMock>]
1
Guest
{"id": 1, "name": "Guest"}
2
Some One
{"id": 2, "name": "Some One"}Return list of dictionaries:Theall_combinationsmethod has thereturn_dictsparameter (which by default is equal toFalse) and if changed toTrue, it will return a list of dictionaries instead ofMoMockobjects.from mockito.mockito import Mockito

from data_file import DEFAULT_BASES
from file import return_table_data


@mock_patch('file.Table1')
class TestReturnData():
def test_retorn_data(self, mock_table1):
    mock_table1.query.filter.return_value.with_entities.return_value.first.return_value = (
        Mockito(
            data_dict={"table1": ["id", "name"]},
            data_bases=DEFAULT_BASES,
        ).all_combinations(return_dicts=True)
    )
    
    response = return_table_data(id = 1)
    print(response)Outputs:[{"id": 1, "name": "Guest"}, {"id": 2, "name": "Some One"}]JoinsJust like in the case of theone()method, you can pass the table name and its columns toMockito, the difference is thatMockitowill combine all possible values. In the example below, table1 has 2 possible values, and table2 has only 1, thusall_combinationsreturns 2 results, being a combination of the first value fromtable1with the only value fromtable2, and the second value from thetable1with a single value from thetable2.from mockito.mockito import Mockito

from data_file import DEFAULT_BASES
from file2 import return_table_data


@mock_patch('file2.Table1')
class TestReturnData():
def test_retorn_data(self, mock_table1):
    mock_table1.query.filter.return_value.join.return_value.with_entities.return_value.first.return_value = (
        Mockito(
            data_dict={
                "table1": ["id", "name"],
                "table2": ["description"],
            },
            data_bases=DEFAULT_BASES,
        ).all_combinations()
    )
    
    response = return_table_data(id = 1)
    print(response)
    print(response[0].id)
    print(response[0].name)
    print(response[0].description)
    print(response[0].to_dict())
    print(response[1].id)
    print(response[1].name)
    print(response[1].description)
    print(response[1].to_dict())Outputs:[<MoMock>, <MoMock>]
1
Guest
Super important data
{"id": 1, "name": "Guest", "description": "Super important data"}
2
Some One
Super important data
{"id": 2, "name": "Some One", "description": "Super important data"}The table below is a representation of howMoMockelements would be constructed:ValuesFirst value from table2First value from table1First MoMockSecond value from table1Second MoMockIftable2also had 2 values, theall_combinationsmethod would return a list with 4 combinations, with the structure:ValuesFirst value from table2Second value from table2First value from table1First MoMockThird MoMockSecond value from table1Second MoMockFourth MoMock