Skip to content

Commit

Permalink
Merge pull request #27 from samudradev/on_start
Browse files Browse the repository at this point in the history
⚙ FIX: Imports
  • Loading branch information
Thaza-Kun authored Apr 3, 2023
2 parents 60ea916 + a2ef7d3 commit d8d8281
Show file tree
Hide file tree
Showing 10 changed files with 555 additions and 451 deletions.
15 changes: 0 additions & 15 deletions Dockerfile

This file was deleted.

819 changes: 441 additions & 378 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "samudra"
version = "0.9.1"
version = "0.9.2"
description = ""
authors = ["Thaza_Kun <[email protected]>"]

Expand Down
6 changes: 0 additions & 6 deletions samudra/conf/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import pytomlpp as toml

from samudra.conf import get_database
from samudra.conf.database.core import get_active_database
from samudra.conf.database.options import DatabaseEngine

try:
Expand All @@ -16,8 +15,3 @@ def access_database(local: bool = True, name: str = None) -> peewee.Database:
return get_database(name=name, engine=DatabaseEngine.SQLite, new=False)
else:
raise NotImplementedError("Only local database is implemented")


def bind_proxy_with_active_database(proxy: peewee.Database) -> peewee.Database:
proxy.initialize(get_active_database())
return proxy
4 changes: 2 additions & 2 deletions samudra/core/crud/lemma.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


from samudra import models
from samudra.interfaces import LemmaBuilder
from samudra.interfaces import LemmaQueryBuilder


def create_lemma(lemma: str, force: bool = False) -> models.Lemma:
Expand All @@ -13,7 +13,7 @@ def create_lemma(lemma: str, force: bool = False) -> models.Lemma:


def get_lemma(nama: str) -> List[models.Lemma]:
return LemmaBuilder(nama=nama).get_cakupan().get_kata_asing().query()
return LemmaQueryBuilder(nama=nama).get_cakupan().get_kata_asing().query()


def delete_lemma(lemma: models.Lemma) -> int:
Expand Down
49 changes: 34 additions & 15 deletions samudra/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,49 @@
from samudra import models


class LemmaBuilder:
_query_stmt = models.Lemma.select()

def __init__(self, nama: str) -> None:
self._query_stmt = self._query_stmt.where(models.Lemma.nama == nama)
self.get_konsep()

def get_konsep(self) -> "LemmaBuilder":
self._query_stmt = self._query_stmt.join_from(
models.Lemma, models.Konsep, JOIN.LEFT_OUTER
class LemmaQueryBuilder:
_query_stmt = models.Konsep.select()

def __init__(self, konsep: str, lemma: str = None) -> None:
self._query_stmt = (
self._query_stmt.join_from(models.Konsep, models.Lemma, JOIN.LEFT_OUTER)
.where(
(models.Konsep.keterangan.contains(konsep))
or (models.Lemma.nama.contains(lemma))
)
.join_from(models.Konsep, models.GolonganKata, JOIN.LEFT_OUTER)
)
return self

def get_cakupan(self) -> "LemmaBuilder":
def get_cakupan(self) -> "LemmaQueryBuilder":
self._query_stmt = self._query_stmt.join_from(
models.Konsep, models.CakupanXKonsep, JOIN.LEFT_OUTER
).join(models.Cakupan, JOIN.LEFT_OUTER)
return self

def get_kata_asing(self) -> "LemmaBuilder":
def get_kata_asing(self) -> "LemmaQueryBuilder":
self._query_stmt = self._query_stmt.join_from(
models.Konsep, models.KataAsingXKonsep, JOIN.LEFT_OUTER
).join(models.KataAsing, JOIN.LEFT_OUTER)
return self

def query(self) -> List[models.Lemma]:
return pw.prefetch(self._query_stmt)
def collect(self) -> List[models.Konsep]:
return pw.prefetch(self._query_stmt.get())


class NewLemmaBuilder:
def __init__(self, konsep: str, lemma: str, golongan: str) -> None:
# TODO Fix dependence on first item tuple
self.lemma = models.Lemma.get_or_create(nama=lemma)[0]
self.golongan = models.GolonganKata.get(id=golongan)
self.konsep = models.Konsep.get_or_create(
lemma=self.lemma, golongan=self.golongan, keterangan=konsep
)[0]

def save(self) -> None:
self.lemma.save()
self.konsep.save()


def new_golongan_kata(id: str, nama: str, keterangan: str) -> models.GolonganKata:
models.GolonganKata.create(id=id, nama=nama, keterangan=keterangan)
return models.GolonganKata.get(id=id)
11 changes: 0 additions & 11 deletions samudra/main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
from samudra.conf.setup import bind_proxy_to_database

from samudra.models.base import database_proxy
from samudra.models import create_tables


def on_start() -> None:
database = bind_proxy_to_database(proxy=database_proxy)
create_tables(database=database)


def on_shutdown() -> None:
...

Expand Down
27 changes: 4 additions & 23 deletions samudra/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,8 @@
# One <== Many
# Many <==> Many
# ```
from typing import List

import peewee


def create_tables(
database: peewee.Database,
auth: bool = True,
experimental: bool = False,
) -> List[str]:
"""Create tables based on selected criteria
Args:
database (peewee.Database): The database engine to bind the models
auth (bool, optional): Whether to include auth tables or not. Defaults to True
experimental (bool, optional): Whether to include experimental tables or not. Defaults to False
Returns:
List of tables created
"""
tables = database.get_tables()
database.create_tables(tables)
# TODO logging
return database.get_tables()
from samudra.models.core.lemma import Lemma
from samudra.models.core.konsep import Konsep, GolonganKata
from samudra.models.core.cakupan import Cakupan, CakupanXKonsep
from samudra.models.core.kata_asing import KataAsing, KataAsingXKonsep
17 changes: 17 additions & 0 deletions samudra/startup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import List

import peewee

from samudra.models.base import database_proxy
from samudra.models import Lemma, Konsep, Cakupan, KataAsing
from samudra.conf.database.core import get_active_database


def bind_proxy_with_active_database(proxy: peewee.Database) -> peewee.Database:
proxy.initialize(get_active_database())
return proxy


def on_start() -> None:
database = bind_proxy_with_active_database(proxy=database_proxy)
database.create_tables([Lemma, Konsep, Cakupan, KataAsing], bind_refs=True)
56 changes: 56 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import functools
import peewee as pw
import pytest
from samudra.interfaces import LemmaQueryBuilder, NewLemmaBuilder, new_golongan_kata

from samudra.models.base import database_proxy as proxy
from samudra.models import (
Lemma,
Konsep,
Cakupan,
KataAsing,
GolonganKata,
CakupanXKonsep,
KataAsingXKonsep,
)

proxy.initialize(pw.SqliteDatabase(":memory:"))


@pytest.fixture
def create_data():
gol = new_golongan_kata(id="NAMA", nama="nama", keterangan="...")
NewLemmaBuilder(konsep="keterangan", lemma="nama", golongan=gol.id).save()


def connect_test_database(function: callable, *args, **kwargs) -> callable:
"""Decorator to open and close a test database connection"""
proxy.create_tables(
[
Lemma,
Konsep,
Cakupan,
KataAsing,
GolonganKata,
CakupanXKonsep,
KataAsingXKonsep,
]
)

@functools.wraps(function)
def wrapper(*args, **kwargs):
try:
function(*args, **kwargs)
finally:
proxy.close() # All data in `:memory:` database are deleted automatically once closed

return wrapper


@connect_test_database
def test_konsep_builder(create_data):
"""Testing query for konsep"""
query = LemmaQueryBuilder(konsep="keterangan", lemma="nama").get_cakupan().collect()
assert query.keterangan == "keterangan"
assert query.lemma.nama == "nama"
assert query.golongan.nama == "nama"

0 comments on commit d8d8281

Please sign in to comment.