Name | mockito-test JSON |
Version |
0.3.2
JSON |
| download |
home_page | |
Summary | Package to help standardization of data used in mocks. |
upload_time | 2024-03-07 02:12:57 |
maintainer | |
docs_url | None |
author | |
requires_python | >=3.7 |
license | |
keywords |
mock
tests
unittest
|
VCS |
|
bugtrack_url |
|
requirements |
mock
parameterized
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Motivation
When 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.
# Structure
Imagine that are 2 SQL tables, with the following structures:
table1
> Column |Type
>--------|------------
> id| integer
> name| text
> menus| text
table2
> Column |Type
>--------|------------
> id| integer
> name| text
> description| text
Based 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 contain `table1` and `table2`, where the `table1` will have 2 data examples and the `table2` only will have 1.
_**data_file.py**_
DEFAULT_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 the `id` and `name` from `table1`:
_**file.py**_
def return_table_data(id):
table_data = Table1.query.filter(
Table1.id == id
).with_entities(
Table1.id,
Table1.name
).first()
return table_data
The function test, using the method `one()` from `Mockito` will 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 the `Mockito` is called `MoMock`, which is a `Mock` with a method called `to_dict()`
## Column with alias:
To assign the alias `potato` to the `name` column within the function, provide the list `["name", "potato"]` to Mockito. Note that the `id` column 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 to `Mockito` that does not exist in `DEFAULT_BASES` for the table in question, it will have the value `invalid column` in its place, in the example below the `foo` column was chosen and it is not present in the `DEFAULT_BASES` dictionary for the `table1` table:
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"}
## Joins
To retrieve data from multiple tables, you can achieve this by providing the table name along with its respective columns to `Mockito`. In the example below, it will return the `id` and `name` columns from `table1`, as well as the `description` column from the `table2`:
_**file2.py**_
def 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_data
_**Test_file.py**_
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,
).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 to `one()` but will return a list of all combinations in the `DEFAULT_TABLES` variable.
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:
The `all_combinations` method has the `return_dicts` parameter (which by default is equal to `False`) and if changed to `True`, it will return a list of dictionaries instead of `MoMock` objects.
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"}]
## Joins
Just like in the case of the `one()` method, you can pass the table name and its columns to `Mockito`, the difference is that `Mockito` will combine all possible values. In the example below, table1 has 2 possible values, and table2 has only 1, thus `all_combinations` returns 2 results, being a combination of the first value from `table1` with the only value from `table2`, and the second value from the `table1` with a single value from the `table2`.
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 how `MoMock` elements would be constructed:
> Values |First value from table2
>--------|------------
> First value from table1 | First MoMock
> Second value from table1 | Second MoMock
If `table2` also had 2 values, the `all_combinations` method would return a list with 4 combinations, with the structure:
> Values |First value from table2 | Second value from table2
>--------|------------|------------
> First value from table1 | First MoMock | Third MoMock
> Second value from table1 | Second MoMock | Fourth MoMock
Raw data
{
"_id": null,
"home_page": "",
"name": "mockito-test",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "mock,tests,unittest",
"author": "",
"author_email": "Luiz Antonio Calliari Filho <luizcalliari.filho@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/95/67/8d7cbc85a50a7f8db8a03e7af75eef52ee8fbc9511b497d43651d0af9294/mockito-test-0.3.2.tar.gz",
"platform": null,
"description": "# Motivation\nWhen 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.\n\nTo address this issue, Mockito strives to standardize the data being mocked.\n\n# Structure\nImagine that are 2 SQL tables, with the following structures:\n\ntable1\n> Column |Type\n>--------|------------\n> id| integer\n> name| text\n> menus| text\n\ntable2\n> Column |Type\n>--------|------------\n> id| integer\n> name| text\n> description| text\n\nBased 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.\n\nIn the example below, the dictionary will contain `table1` and `table2`, where the `table1` will have 2 data examples and the `table2` only will have 1.\n\n_**data_file.py**_\n\n DEFAULT_BASES = {\n \"table1\": [\n {\"id\": 1, \"name\": \"Guest\", \"menus\": \"menu_1\"},\n {\"id\": 2, \"name\": \"Some One\", \"menus\": \"menu_2\"},\n ],\n \"table2\": [\n {\"id\": 1, \"name\": \"OtherName\", \"Description\": \"Super important data\"},\n ]\n }\n\n# Method one()\n## Basic utilization:\nThe function below returns the `id` and `name` from `table1`:\n\n_**file.py**_\n\n def return_table_data(id):\n table_data = Table1.query.filter(\n Table1.id == id\n ).with_entities(\n Table1.id,\n Table1.name\n ).first()\n \n return table_data\n\nThe function test, using the method `one()` from `Mockito` will be something like this:\n\n from mockito.mockito import Mockito\n\n from data_file import DEFAULT_BASES\n from file import return_table_data\n \n \n @mock_patch('file.Table1')\n class TestReturnData():\n def test_retorn_data(self, mock_table1):\n mock_table1.query.filter.return_value.with_entities.return_value.first.return_value = (\n Mockito(\n data_dict={\"table1\": [\"id\", \"name\"]},\n data_bases=DEFAULT_BASES,\n ).one()\n )\n \n response = return_table_data(id = 1)\n print(response)\n print(response.id)\n print(response.name)\n print(response.to_dict())\n\nOutputs:\n\n <MoMock>\n 1\n Guest\n {\"id\": 1, \"name\": \"Guest\"}\n\nThe object return from the `Mockito` is called `MoMock`, which is a `Mock` with a method called `to_dict()`\n\n## Column with alias:\nTo assign the alias `potato` to the `name` column within the function, provide the list `[\"name\", \"potato\"]` to Mockito. Note that the `id` column will also be included in the result, but it won't be given an alias:\n\n from mockito.mockito import Mockito\n\n from data_file import DEFAULT_BASES\n from file import return_table_data\n \n \n @mock_patch('file.Table1')\n class TestReturnData():\n def test_retorn_data(self, mock_table1):\n mock_table1.query.filter.return_value.with_entities.return_value.first.return_value = (\n Mockito(\n data_dict={\"table1\": [\"id\", [\"name\", \"potato\"]]},\n data_bases=DEFAULT_BASES,\n ).one()\n )\n \n response = return_table_data(id = 1)\n print(response)\n print(response.id)\n print(response.potato)\n print(response.to_dict())\n\nOutputs:\n\n <MoMock>\n 1\n Guest\n {\"id\": 1, \"potato\": \"Guest\"}\n\n## Non-existent column:\nIf a column is passed to `Mockito` that does not exist in `DEFAULT_BASES` for the table in question, it will have the value `invalid column` in its place, in the example below the `foo` column was chosen and it is not present in the `DEFAULT_BASES` dictionary for the `table1` table:\n\n from mockito.mockito import Mockito\n\n from data_file import DEFAULT_BASES\n from file import return_table_data\n \n \n @mock_patch('file.Table1')\n class TestReturnData():\n def test_retorn_data(self, mock_table1):\n mock_table1.query.filter.return_value.with_entities.return_value.first.return_value = (\n Mockito(\n data_dict={\"table1\": [\"id\", \"foo\"]},\n data_bases=DEFAULT_BASES,\n ).one()\n )\n \n response = return_table_data(id = 1)\n print(response)\n print(response.id)\n print(response.foo)\n print(response.to_dict())\n\nOutputs:\n\n <MoMock>\n 1\n invalid column\n {\"id\": 1, \"foo\": \"invalid column\"}\n\n## Joins\nTo retrieve data from multiple tables, you can achieve this by providing the table name along with its respective columns to `Mockito`. In the example below, it will return the `id` and `name` columns from `table1`, as well as the `description` column from the `table2`:\n_**file2.py**_\n\n def return_table_data(id):\n table_data = Table1.query.filter(\n Table1.id == id\n ).join(\n Table2, \"some valid condition here\"\n ).with_entities(\n Table1.id,\n Table1.name\n ).first()\n \n return table_data\n\n_**Test_file.py**_\n\n from mockito.mockito import Mockito\n\n from data_file import DEFAULT_BASES\n from file2 import return_table_data\n \n \n @mock_patch('file2.Table1')\n class TestReturnData():\n def test_retorn_data(self, mock_table1):\n mock_table1.query.filter.return_value.join.return_value.with_entities.return_value.first.return_value = (\n Mockito(\n data_dict={\n \"table1\": [\"id\", \"name\"],\n \"table2\": [\"description\"],\n },\n data_bases=DEFAULT_BASES,\n ).one()\n )\n \n response = return_table_data(id = 1)\n print(response)\n print(response.id)\n print(response.name)\n print(response.description)\n print(response.to_dict())\n\nOutputs:\n\n <MoMock>\n 1\n Guest\n Super important data\n {\"id\": 1, \"name\": \"Guest\": \"description\": \"Super important data\"}\n\n# Method all_combinations()\n## Basic utilization:\nThis method is similar to `one()` but will return a list of all combinations in the `DEFAULT_TABLES` variable.\n\n from mockito.mockito import Mockito\n\n from data_file import DEFAULT_BASES\n from file import return_table_data\n \n \n @mock_patch('file.Table1')\n class TestReturnData():\n def test_retorn_data(self, mock_table1):\n mock_table1.query.filter.return_value.with_entities.return_value.first.return_value = (\n Mockito(\n data_dict={\"table1\": [\"id\", \"name\"]},\n data_bases=DEFAULT_BASES,\n ).all_combinations()\n )\n \n response = return_table_data(id = 1)\n print(response)\n print(response[0].id)\n print(response[0].name)\n print(response[0].to_dict())\n print(response[1].id)\n print(response[1].name)\n print(response[1].to_dict())\n\nOutputs:\n\n [<MoMock>, <MoMock>]\n 1\n Guest\n {\"id\": 1, \"name\": \"Guest\"}\n 2\n Some One\n {\"id\": 2, \"name\": \"Some One\"}\n\n## Return list of dictionaries:\nThe `all_combinations` method has the `return_dicts` parameter (which by default is equal to `False`) and if changed to `True`, it will return a list of dictionaries instead of `MoMock` objects.\n\n from mockito.mockito import Mockito\n\n from data_file import DEFAULT_BASES\n from file import return_table_data\n \n \n @mock_patch('file.Table1')\n class TestReturnData():\n def test_retorn_data(self, mock_table1):\n mock_table1.query.filter.return_value.with_entities.return_value.first.return_value = (\n Mockito(\n data_dict={\"table1\": [\"id\", \"name\"]},\n data_bases=DEFAULT_BASES,\n ).all_combinations(return_dicts=True)\n )\n \n response = return_table_data(id = 1)\n print(response)\n\n\nOutputs:\n\n [{\"id\": 1, \"name\": \"Guest\"}, {\"id\": 2, \"name\": \"Some One\"}]\n\n## Joins\nJust like in the case of the `one()` method, you can pass the table name and its columns to `Mockito`, the difference is that `Mockito` will combine all possible values. In the example below, table1 has 2 possible values, and table2 has only 1, thus `all_combinations` returns 2 results, being a combination of the first value from `table1` with the only value from `table2`, and the second value from the `table1` with a single value from the `table2`.\n\n from mockito.mockito import Mockito\n\n from data_file import DEFAULT_BASES\n from file2 import return_table_data\n \n \n @mock_patch('file2.Table1')\n class TestReturnData():\n def test_retorn_data(self, mock_table1):\n mock_table1.query.filter.return_value.join.return_value.with_entities.return_value.first.return_value = (\n Mockito(\n data_dict={\n \"table1\": [\"id\", \"name\"],\n \"table2\": [\"description\"],\n },\n data_bases=DEFAULT_BASES,\n ).all_combinations()\n )\n \n response = return_table_data(id = 1)\n print(response)\n print(response[0].id)\n print(response[0].name)\n print(response[0].description)\n print(response[0].to_dict())\n print(response[1].id)\n print(response[1].name)\n print(response[1].description)\n print(response[1].to_dict())\n\nOutputs:\n\n [<MoMock>, <MoMock>]\n 1\n Guest\n Super important data\n {\"id\": 1, \"name\": \"Guest\", \"description\": \"Super important data\"}\n 2\n Some One\n Super important data\n {\"id\": 2, \"name\": \"Some One\", \"description\": \"Super important data\"}\n\nThe table below is a representation of how `MoMock` elements would be constructed:\n\n> Values |First value from table2\n>--------|------------\n> First value from table1 | First MoMock\n> Second value from table1 | Second MoMock\n\nIf `table2` also had 2 values, the `all_combinations` method would return a list with 4 combinations, with the structure:\n\n> Values |First value from table2 | Second value from table2\n>--------|------------|------------\n> First value from table1 | First MoMock | Third MoMock\n> Second value from table1 | Second MoMock | Fourth MoMock\n",
"bugtrack_url": null,
"license": "",
"summary": "Package to help standardization of data used in mocks.",
"version": "0.3.2",
"project_urls": {
"Bug Tracker": "https://github.com/luizcalliari/mockito-test/issues",
"Documentation": "https://github.com/luizcalliari/mockito-test/wiki",
"Homepage": "https://github.com/luizcalliari/mockito-test"
},
"split_keywords": [
"mock",
"tests",
"unittest"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "e8936f18df766eefc278076b768e9c091b54ea7d811e2daf9ab0d34b332c2c35",
"md5": "9af66c7da314254a2c4f71dcc5f17230",
"sha256": "276855da3e846ec918d806e0e16f8be243ee83391283cd99aacf64cf0fcb1109"
},
"downloads": -1,
"filename": "mockito_test-0.3.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9af66c7da314254a2c4f71dcc5f17230",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 5480,
"upload_time": "2024-03-07T02:12:55",
"upload_time_iso_8601": "2024-03-07T02:12:55.907681Z",
"url": "https://files.pythonhosted.org/packages/e8/93/6f18df766eefc278076b768e9c091b54ea7d811e2daf9ab0d34b332c2c35/mockito_test-0.3.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "95678d7cbc85a50a7f8db8a03e7af75eef52ee8fbc9511b497d43651d0af9294",
"md5": "9b153f34c33567a5d1cef49540a82c17",
"sha256": "916368bde558f0ab136b309dbbd18a833622358cdd04b9e08556610f92b73bb8"
},
"downloads": -1,
"filename": "mockito-test-0.3.2.tar.gz",
"has_sig": false,
"md5_digest": "9b153f34c33567a5d1cef49540a82c17",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 6108,
"upload_time": "2024-03-07T02:12:57",
"upload_time_iso_8601": "2024-03-07T02:12:57.471090Z",
"url": "https://files.pythonhosted.org/packages/95/67/8d7cbc85a50a7f8db8a03e7af75eef52ee8fbc9511b497d43651d0af9294/mockito-test-0.3.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-03-07 02:12:57",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "luizcalliari",
"github_project": "mockito-test",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "mock",
"specs": [
[
"==",
"5.1.0"
]
]
},
{
"name": "parameterized",
"specs": [
[
"==",
"0.9.0"
]
]
}
],
"lcname": "mockito-test"
}