From ab69908a666823898a4f3b9785714c5fcc2fff40 Mon Sep 17 00:00:00 2001 From: Giacomo Leidi Date: Sun, 2 May 2021 16:51:54 +0200 Subject: [PATCH] Init storage. --- mobilizon_bots/event/__init__.py | 0 mobilizon_bots/event/db.py | 24 ++++++++++++++++++++++++ mobilizon_bots/event/model.py | 29 +++++++++++++++++++++++++++++ pyproject.toml | 3 +++ tests/configtest.py | 14 ++++++++++++++ tests/test_mobilizon_bots.py | 5 +++++ tests/test_models.py | 14 ++++++++++++++ 7 files changed, 89 insertions(+) create mode 100644 mobilizon_bots/event/__init__.py create mode 100644 mobilizon_bots/event/db.py create mode 100644 mobilizon_bots/event/model.py create mode 100644 tests/configtest.py create mode 100644 tests/test_mobilizon_bots.py create mode 100644 tests/test_models.py diff --git a/mobilizon_bots/event/__init__.py b/mobilizon_bots/event/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mobilizon_bots/event/db.py b/mobilizon_bots/event/db.py new file mode 100644 index 0000000..f287209 --- /dev/null +++ b/mobilizon_bots/event/db.py @@ -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() diff --git a/mobilizon_bots/event/model.py b/mobilizon_bots/event/model.py new file mode 100644 index 0000000..d2a4553 --- /dev/null +++ b/mobilizon_bots/event/model.py @@ -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" diff --git a/pyproject.toml b/pyproject.toml index a8cfaec..ea9f83c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,6 +6,9 @@ authors = ["Simone Robutti "] [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" diff --git a/tests/configtest.py b/tests/configtest.py new file mode 100644 index 0000000..524d8ad --- /dev/null +++ b/tests/configtest.py @@ -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) diff --git a/tests/test_mobilizon_bots.py b/tests/test_mobilizon_bots.py new file mode 100644 index 0000000..aba08af --- /dev/null +++ b/tests/test_mobilizon_bots.py @@ -0,0 +1,5 @@ +from mobilizon_bots import __version__ + + +def test_version(): + assert __version__ == "0.1.0" diff --git a/tests/test_models.py b/tests/test_models.py new file mode 100644 index 0000000..c4ddafb --- /dev/null +++ b/tests/test_models.py @@ -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): + ...