Init storage.

This commit is contained in:
Giacomo Leidi 2021-05-02 16:51:54 +02:00
parent d78ab809a7
commit ab69908a66
7 changed files with 89 additions and 0 deletions

View File

View File

@ -0,0 +1,24 @@
from pathlib import Path
from tortoise import Tortoise
class MobotsDB:
def __init__(self, path: Path):
self.path = path
async def setup(self):
await Tortoise.init(
db_url=f"sqlite:///{self.path}",
modules={"models": ["mobilizon_bots.event.model"]},
)
if not self.is_init():
# Generate the schema
await Tortoise.generate_schemas()
def is_init(self) -> bool:
# TODO: Check if DB is openable/"queriable"
return self.path.exists() and (not self.path.is_dir())
@staticmethod
async def tear_down():
await Tortoise.close_connections()

View File

@ -0,0 +1,29 @@
from tortoise.models import Model
from tortoise import fields
class Event(Model):
id = fields.UUID(pk=True)
name = fields.TextField()
# tournament = fields.ForeignKeyField('models.Tournament', related_name='events')
# participants = fields.ManyToManyField('models.Team', related_name='events', through='event_team')
# modified = fields.DatetimeField(auto_now=True)
# prize = fields.DecimalField(max_digits=10, decimal_places=2, null=True)
def __str__(self):
return self.name
class Meta:
table = "event"
class SocialNetwork(Model):
id = fields.IntField(pk=True)
name = fields.TextField()
def __str__(self):
return self.name
class Meta:
table = "social_network"

View File

@ -6,6 +6,9 @@ authors = ["Simone Robutti <simone.robutti@protonmail.com>"]
[tool.poetry.dependencies]
python = "^3.9"
dynaconf = "^3.1.4"
tortoise-orm = "^0.16.21"
aiosqlite = "^0.17.0"
[tool.poetry.dev-dependencies]
pytest = "^5.2"

14
tests/configtest.py Normal file
View File

@ -0,0 +1,14 @@
import pytest
from dynaconf import settings
from tortoise.contrib.test import finalizer, initializer
@pytest.fixture(scope="session", autouse=True)
def initialize_tests(request):
settings.configure(FORCE_ENV_FOR_DYNACONF="testing")
initializer(
["tests.test_models"],
db_url=f"sqlite:///{settings.DB_PATH}",
app_label="models",
)
request.addfinalizer(finalizer)

View File

@ -0,0 +1,5 @@
from mobilizon_bots import __version__
def test_version():
assert __version__ == "0.1.0"

14
tests/test_models.py Normal file
View File

@ -0,0 +1,14 @@
from tortoise.contrib import test
class TestSomething(test.TestCase):
async def test_something_async(self):
...
@test.skip("Skip this")
def test_skip(self):
...
@test.expectedFailure
def test_something(self):
...