Skip to content

Commit

Permalink
✨ NEW: Lemma Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaza-Kun committed May 30, 2023
1 parent 1845d89 commit 0ab33a5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 28 deletions.
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.7"
version = "0.9.8"
description = ""
authors = ["Thaza_Kun <[email protected]>"]

Expand Down
34 changes: 20 additions & 14 deletions samudra/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,27 @@ def collect(self) -> Optional[LemmaData]:


class LemmaEditor:
def __init__(self, original: LemmaData) -> None:
self.old = original
self.new = deepcopy(original)
def __init__(self, data: LemmaData) -> None:
self.data = data
self.to_save: List[pw.Model] = []

def rename(self, new: str) -> "LemmaEditor":
self.new.nama = new

def rewrite_konsep(self, index: int, new: str) -> "LemmaEditor":
self.new.konsep[index].keterangan = new
_lemma: models.Lemma = models.Lemma.get_by_id(self.data.id)
_lemma.nama = new
self.to_save.append(_lemma)
return self

def new_cakupan(self, index: int, cakupan: str) -> "LemmaEditor":
cakupan_data = models.Cakupan.get_or_create(nama=cakupan)[0]
def rewrite_konsep(self, index: int, keterangan: str) -> "LemmaEditor":
_konsep: models.Konsep = models.Konsep.get_by_id(self.data.konsep[index].id)
_konsep.keterangan = keterangan
self.to_save.append(_konsep)
return self

# def add_konsep(self, konsep: KonsepData) -> "LemmaEditor":
# ...
def save(self) -> None:
while len(self.to_save) != 0:
record = self.to_save.pop(0)
record.update()
record.save()


class NewLemmaBuilder:
Expand All @@ -133,7 +139,6 @@ class NewLemmaBuilder:
"""

def __init__(self, konsep: str, lemma: str, golongan: str) -> None:
# TODO Fix dependence on first item tuple
self.lemma = self.get_or_new(models.Lemma, nama=lemma)
self.golongan = models.GolonganKata.get(id=golongan)
self.konsep = self.get_or_new(
Expand All @@ -156,8 +161,9 @@ def get_or_new(model: pw.Model, *args, **kwargs) -> pw.Model:
return data

def save(self) -> None:
"""Saves the data."""
for record in self.to_save:
"""Saves the built record."""
while len(self.to_save) != 0:
record = self.to_save.pop(0)
record.update() # Fill previously NULL values
record.save()

Expand Down
2 changes: 1 addition & 1 deletion samudra/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import peewee as pw

database_proxy = pw.DatabaseProxy()
database_proxy: pw.Database = pw.DatabaseProxy()


class BaseDataTable(pw.Model):
Expand Down
48 changes: 36 additions & 12 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import functools
import peewee as pw
import pytest
from samudra.interfaces import LemmaQueryBuilder, NewLemmaBuilder, new_golongan_kata
from samudra.interfaces import (
LemmaEditor,
LemmaQueryBuilder,
NewLemmaBuilder,
new_golongan_kata,
)

from samudra.models.base import database_proxy as proxy
from samudra.models import (
Expand All @@ -14,19 +19,9 @@
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).set_cakupan(
nama="cakupan"
).save()


def connect_test_database(function: callable, *args, **kwargs) -> callable:
"""Decorator to open and close a test database connection"""
proxy.create_tables(
[
Lemma,
Expand All @@ -38,6 +33,17 @@ def connect_test_database(function: callable, *args, **kwargs) -> callable:
KataAsingXKonsep,
]
)
gol = new_golongan_kata(id="NAMA", nama="nama", keterangan="...")
NewLemmaBuilder(konsep="keterangan", lemma="nama", golongan=gol.id).set_cakupan(
nama="cakupan"
).save()


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


def connect_test_database(function: callable, *args, **kwargs) -> callable:
"""Decorator to open and close a test database connection"""

@functools.wraps(function)
def wrapper(*args, **kwargs):
Expand All @@ -50,7 +56,7 @@ def wrapper(*args, **kwargs):


@connect_test_database
def test_konsep_builder(create_data):
def test_lemma_query(create_data):
"""Testing query for konsep"""
# Test query of both konsep and lemma
query = LemmaQueryBuilder(konsep="keterangan", lemma="nama").get_cakupan().collect()
Expand Down Expand Up @@ -83,3 +89,21 @@ def test_konsep_builder(create_data):
# Test None query raise ValueError
with pytest.raises(ValueError):
LemmaQueryBuilder(konsep=None, lemma=None)


@connect_test_database
def test_lemma_editor(create_data):
query = LemmaQueryBuilder(konsep="keterangan", lemma="nama").collect()
assert query.nama == "nama"

# Test edit lemma
newlemma = LemmaEditor(query)
newlemma.rename("baharu").save()
assert LemmaQueryBuilder(lemma="nama").collect() is None

# Test edit konsep
query = LemmaQueryBuilder(konsep="keterangan", lemma="baharu").collect()
assert query.nama == "baharu"
edit = LemmaEditor(query)
edit.rewrite_konsep(0, "ujian suntingan").save()
assert LemmaQueryBuilder(konsep="keterangan").collect() is None

0 comments on commit 0ab33a5

Please sign in to comment.