Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add init_memory_sqlite decorator #1657

Merged
merged 10 commits into from
Jun 24, 2024

Conversation

waketzheng
Copy link
Contributor

@waketzheng waketzheng commented Jun 17, 2024

Description

Almost every file in examples include the following two lines:

        await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
        await Tortoise.generate_schemas()

Motivation and Context

  • Proposal 1: Add a decorator to initial memory sqlite
  • Proposal 2: Add a optional argument into run_async to init Tortoise
  • Example1:
from tortoise import fields, models, run_async
from tortoise.contrib.test import init_memory_sqlite

class User(models.Model):
    id = fields.IntField(primary_key=True)
    name = fields.TextField()

@init_memory_sqlite
async def run():
    user = await User.create(name='J')
    print(user.name)

if __name__ == '__main__':
    run_async(run())
  • Example2:
# app/models.py
from tortoise import fields, models

class User(models.Model):
    id = fields.IntField(primary_key=True)
    name = fields.TextField()
# main.py
from tortoise import run_async
from tortoise.contrib.test import init_memory_sqlite

from app.models import User

@init_memory_sqlite(['app.models'])
async def run():
    user = await User.create(name='J')
    print(user.name)

if __name__ == '__main__':
    run_async(run())

How Has This Been Tested?

make ci

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@coveralls
Copy link

coveralls commented Jun 17, 2024

Pull Request Test Coverage Report for Build 9544850985

Details

  • 14 of 14 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 89.07%

Totals Coverage Status
Change from base Build 9543663341: 0.02%
Covered Lines: 5881
Relevant Lines: 6505

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jun 18, 2024

Pull Request Test Coverage Report for Build 9560727240

Details

  • 14 of 14 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 89.07%

Totals Coverage Status
Change from base Build 9543663341: 0.02%
Covered Lines: 5881
Relevant Lines: 6505

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jun 18, 2024

Pull Request Test Coverage Report for Build 9560904994

Details

  • 14 of 14 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 89.07%

Totals Coverage Status
Change from base Build 9543663341: 0.02%
Covered Lines: 5881
Relevant Lines: 6505

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jun 18, 2024

Pull Request Test Coverage Report for Build 9561639055

Details

  • 14 of 14 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 89.07%

Totals Coverage Status
Change from base Build 9543663341: 0.02%
Covered Lines: 5881
Relevant Lines: 6505

💛 - Coveralls

@waketzheng waketzheng changed the title Add init_memory_sqlite function Add init_memory_sqlite decorator Jun 18, 2024
@coveralls
Copy link

coveralls commented Jun 18, 2024

Pull Request Test Coverage Report for Build 9562578561

Details

  • 16 of 16 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 89.071%

Totals Coverage Status
Change from base Build 9543663341: 0.02%
Covered Lines: 5882
Relevant Lines: 6506

💛 - Coveralls

await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
await Tortoise.generate_schemas()

@init_memory_sqlite
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be we should make it configurable? To let it pass another modules value

Because if you allow only __main__ I am not sure it would be useful enough to make it part of contrib

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abondar I made a refactor to support models argument.

@coveralls
Copy link

coveralls commented Jun 24, 2024

Pull Request Test Coverage Report for Build 9645745307

Details

  • 32 of 33 (96.97%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.008%) to 89.094%

Changes Missing Coverage Covered Lines Changed/Added Lines %
tortoise/contrib/test/init.py 32 33 96.97%
Totals Coverage Status
Change from base Build 9642974390: 0.008%
Covered Lines: 5906
Relevant Lines: 6531

💛 - Coveralls

@abondar abondar merged commit 8848b20 into tortoise:develop Jun 24, 2024
7 checks passed
@waketzheng waketzheng deleted the feat-run-in-memory-sqlite branch June 24, 2024 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants