-
-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add init_memory_sqlite decorator (#1657)
* Add init_memory_sqlite function * Rename and fixing codacy issues * fixing codacy issue * fixing codacy issues * Use mock to test memory sqlite init decorator * Add AsyncFunc type for init_memory_sqlite * Support custom models * Update changelog * fix ci error
- Loading branch information
1 parent
b87f485
commit 8848b20
Showing
5 changed files
with
165 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import subprocess # nosec | ||
from unittest.mock import AsyncMock, patch | ||
|
||
from tortoise.contrib import test | ||
from tortoise.contrib.test import init_memory_sqlite | ||
|
||
|
||
class TestDecorator(test.TestCase): | ||
@test.requireCapability(dialect="sqlite") | ||
async def test_script_with_init_memory_sqlite(self) -> None: | ||
r = subprocess.run(["python", "examples/basic.py"], capture_output=True) # nosec | ||
output = r.stdout.decode() | ||
s = "[{'id': 1, 'name': 'Updated name'}, {'id': 2, 'name': 'Test 2'}]" | ||
self.assertIn(s, output) | ||
|
||
@test.requireCapability(dialect="sqlite") | ||
@patch("tortoise.Tortoise.init") | ||
@patch("tortoise.Tortoise.generate_schemas") | ||
async def test_init_memory_sqlite( | ||
self, | ||
mocked_generate: AsyncMock, | ||
mocked_init: AsyncMock, | ||
) -> None: | ||
@init_memory_sqlite | ||
async def run(): | ||
return "foo" | ||
|
||
res = await run() | ||
self.assertEqual(res, "foo") | ||
mocked_init.assert_awaited_once() | ||
mocked_init.assert_called_once_with( | ||
db_url="sqlite://:memory:", modules={"models": ["__main__"]} | ||
) | ||
mocked_generate.assert_awaited_once() | ||
|
||
@test.requireCapability(dialect="sqlite") | ||
@patch("tortoise.Tortoise.init") | ||
@patch("tortoise.Tortoise.generate_schemas") | ||
async def test_init_memory_sqlite_with_models( | ||
self, | ||
mocked_generate: AsyncMock, | ||
mocked_init: AsyncMock, | ||
) -> None: | ||
@init_memory_sqlite(["app.models"]) | ||
async def run(): | ||
return "foo" | ||
|
||
res = await run() | ||
self.assertEqual(res, "foo") | ||
mocked_init.assert_awaited_once() | ||
mocked_init.assert_called_once_with( | ||
db_url="sqlite://:memory:", modules={"models": ["app.models"]} | ||
) | ||
mocked_generate.assert_awaited_once() | ||
|
||
@test.requireCapability(dialect="sqlite") | ||
@patch("tortoise.Tortoise.init") | ||
@patch("tortoise.Tortoise.generate_schemas") | ||
async def test_init_memory_sqlite_model_str( | ||
self, | ||
mocked_generate: AsyncMock, | ||
mocked_init: AsyncMock, | ||
) -> None: | ||
@init_memory_sqlite("app.models") | ||
async def run(): | ||
return "foo" | ||
|
||
res = await run() | ||
self.assertEqual(res, "foo") | ||
mocked_init.assert_awaited_once() | ||
mocked_init.assert_called_once_with( | ||
db_url="sqlite://:memory:", modules={"models": ["app.models"]} | ||
) | ||
mocked_generate.assert_awaited_once() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters