Skip to content

Commit

Permalink
Merge pull request #28 from samudradev/on_start
Browse files Browse the repository at this point in the history
⚙ FIX: where clauses and error handling
  • Loading branch information
Thaza-Kun authored Apr 5, 2023
2 parents d8d8281 + 433d69e commit 25b5681
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 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.2"
version = "0.9.3"
description = ""
authors = ["Thaza_Kun <[email protected]>"]

Expand Down
29 changes: 22 additions & 7 deletions samudra/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import List, Optional
from peewee import JOIN

import peewee as pw
Expand All @@ -8,13 +8,25 @@
class LemmaQueryBuilder:
_query_stmt = models.Konsep.select()

def __init__(self, konsep: str, lemma: str = None) -> None:
def __init__(
self, *, lemma: Optional[str] = None, konsep: Optional[str] = None
) -> None:
if (lemma == None) and (konsep == None):
raise ValueError(
f"Please specify query. `lemma` dan `konsep` cannot both be None."
)
elif lemma and konsep:
whereclause = models.Lemma.nama.contains(
lemma
) or models.Konsep.keterangan.contains(konsep)
elif lemma:
whereclause = models.Lemma.nama.contains(lemma)
elif konsep:
whereclause = models.Konsep.keterangan.contains(konsep)

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))
)
.where(whereclause)
.join_from(models.Konsep, models.GolonganKata, JOIN.LEFT_OUTER)
)

Expand All @@ -31,7 +43,10 @@ def get_kata_asing(self) -> "LemmaQueryBuilder":
return self

def collect(self) -> List[models.Konsep]:
return pw.prefetch(self._query_stmt.get())
try:
return pw.prefetch(self._query_stmt.get())
except pw.DoesNotExist:
return None


class NewLemmaBuilder:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,33 @@ def wrapper(*args, **kwargs):
@connect_test_database
def test_konsep_builder(create_data):
"""Testing query for konsep"""
# Test query of both konsep and lemma
query = LemmaQueryBuilder(konsep="keterangan", lemma="nama").get_cakupan().collect()
assert query.keterangan == "keterangan"
assert query.lemma.nama == "nama"
assert query.golongan.nama == "nama"

# Test query of konsep
query = LemmaQueryBuilder(konsep="keterangan").get_cakupan().collect()
assert query.keterangan == "keterangan"
assert query.lemma.nama == "nama"
assert query.golongan.nama == "nama"

# Test query of lemma
query = LemmaQueryBuilder(lemma="nama").get_cakupan().collect()
assert query.keterangan == "keterangan"
assert query.lemma.nama == "nama"
assert query.golongan.nama == "nama"

# Test query with no matching instance
query = LemmaQueryBuilder(lemma="nm").get_cakupan().collect()
assert query == None

# Test assert kwargs only
with pytest.raises(TypeError):
# Ambiguity: Are you querying for lemma or konsep?
LemmaQueryBuilder("nm")

# Test None query raise ValueError
with pytest.raises(ValueError):
LemmaQueryBuilder(konsep=None, lemma=None)

0 comments on commit 25b5681

Please sign in to comment.