From 0ab33a58118208a7cb7ab6161680281ad6d1b9b4 Mon Sep 17 00:00:00 2001 From: Thaza_Kun <61819672+Thaza-Kun@users.noreply.github.com> Date: Tue, 30 May 2023 15:05:01 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20NEW:=20Lemma=20Editor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 2 +- samudra/interfaces.py | 34 ++++++++++++++++++------------ samudra/models/base.py | 2 +- tests/test_main.py | 48 +++++++++++++++++++++++++++++++----------- 4 files changed, 58 insertions(+), 28 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index e5a4303..837cddf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "samudra" -version = "0.9.7" +version = "0.9.8" description = "" authors = ["Thaza_Kun <61819672+Thaza-Kun@users.noreply.github.com>"] diff --git a/samudra/interfaces.py b/samudra/interfaces.py index 70bd4c1..faba341 100644 --- a/samudra/interfaces.py +++ b/samudra/interfaces.py @@ -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: @@ -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( @@ -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() diff --git a/samudra/models/base.py b/samudra/models/base.py index 5f2e533..259b97b 100644 --- a/samudra/models/base.py +++ b/samudra/models/base.py @@ -12,7 +12,7 @@ import peewee as pw -database_proxy = pw.DatabaseProxy() +database_proxy: pw.Database = pw.DatabaseProxy() class BaseDataTable(pw.Model): diff --git a/tests/test_main.py b/tests/test_main.py index 74762ec..c1681f8 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -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 ( @@ -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, @@ -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): @@ -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() @@ -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