Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
[CLEANUP]: Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
amadolid committed Jun 26, 2024
1 parent 7b9d302 commit 715c00a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 95 deletions.
89 changes: 2 additions & 87 deletions jaclang/core/architype.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

import unittest
from dataclasses import dataclass, field
from enum import Enum
from os import getenv
Expand Down Expand Up @@ -121,11 +120,11 @@ def ref(cls, ref_id: str) -> Optional[ObjectAnchor]:

def save(self) -> None:
"""Save Anchor."""
raise Exception(f"Invalid Reference {self.ref_id}")
raise NotImplementedError("save must be implemented in subclasses")

def destroy(self) -> None:
"""Save Anchor."""
raise Exception(f"Invalid Reference {self.ref_id}")
raise NotImplementedError("destroy must be implemented in subclasses")

def sync(self, node: Optional["NodeAnchor"] = None) -> Optional[Architype]:
"""Retrieve the Architype from db and return."""
Expand Down Expand Up @@ -796,87 +795,3 @@ class DSFunc:
def resolve(self, cls: type) -> None:
"""Resolve the function."""
self.func = getattr(cls, self.name)


class JacTestResult(unittest.TextTestResult):
"""Jac test result class."""

def __init__(
self,
stream, # noqa
descriptions, # noqa
verbosity: int,
max_failures: Optional[int] = None,
) -> None:
"""Initialize FailFastTestResult object."""
super().__init__(stream, descriptions, verbosity) # noqa
self.failures_count = JacTestCheck.failcount
self.max_failures = max_failures

def addFailure(self, test, err) -> None: # noqa
"""Count failures and stop."""
super().addFailure(test, err)
self.failures_count += 1
if self.max_failures is not None and self.failures_count >= self.max_failures:
self.stop()

def stop(self) -> None:
"""Stop the test execution."""
self.shouldStop = True


class JacTextTestRunner(unittest.TextTestRunner):
"""Jac test runner class."""

def __init__(self, max_failures: Optional[int] = None, **kwargs) -> None: # noqa
"""Initialize JacTextTestRunner object."""
self.max_failures = max_failures
super().__init__(**kwargs)

def _makeResult(self) -> JacTestResult: # noqa
"""Override the method to return an instance of JacTestResult."""
return JacTestResult(
self.stream,
self.descriptions,
self.verbosity,
max_failures=self.max_failures,
)


class JacTestCheck:
"""Jac Testing and Checking."""

test_case = unittest.TestCase()
test_suite = unittest.TestSuite()
breaker = False
failcount = 0

@staticmethod
def reset() -> None:
"""Clear the test suite."""
JacTestCheck.test_case = unittest.TestCase()
JacTestCheck.test_suite = unittest.TestSuite()

@staticmethod
def run_test(xit: bool, maxfail: int | None, verbose: bool) -> None:
"""Run the test suite."""
verb = 2 if verbose else 1
runner = JacTextTestRunner(max_failures=maxfail, failfast=xit, verbosity=verb)
result = runner.run(JacTestCheck.test_suite)
if result.wasSuccessful():
print("Passed successfully.")
else:
fails = len(result.failures)
JacTestCheck.failcount += fails
JacTestCheck.breaker = (
(JacTestCheck.failcount >= maxfail) if maxfail else True
)

@staticmethod
def add_test(test_fun: Callable) -> None:
"""Create a new test."""
JacTestCheck.test_suite.addTest(unittest.FunctionTestCase(test_fun))

def __getattr__(self, name: str) -> object:
"""Make convenient check.Equal(...) etc."""
return getattr(JacTestCheck.test_case, name)
16 changes: 8 additions & 8 deletions jaclang/core/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def find(
filter: Optional[Callable[[ObjectAnchor], ObjectAnchor]] = None,
extended: Optional[bool] = False,
) -> Generator[Union[UUID, ObjectAnchor], None, None]:
"""Temporary."""
"""Find anchors from memory by ids with filter."""
if not isinstance(ids, list):
ids = [ids]

Expand All @@ -73,11 +73,11 @@ def find_one(
ids: IDS,
filter: Optional[Callable[[ObjectAnchor], ObjectAnchor]] = None,
) -> Optional[ObjectAnchor]:
"""Temporary."""
"""Find one anchor from memory by ids with filter."""
return next(self.find(ids, filter), None)

def set(self, data: Union[ObjectAnchor, list[ObjectAnchor]]) -> None:
"""Temporary."""
"""Save anchor/s to memory."""
if isinstance(data, list):
for d in data:
if str(d.id) not in self.__trash__:
Expand All @@ -86,7 +86,7 @@ def set(self, data: Union[ObjectAnchor, list[ObjectAnchor]]) -> None:
self.__mem__[str(data.id)] = data

def remove(self, data: Union[ObjectAnchor, list[ObjectAnchor]]) -> None:
"""Temporary."""
"""Remove anchor/s from memory."""
if isinstance(data, list):
for d in data:
self.__mem__.pop(str(d.id), None)
Expand All @@ -98,7 +98,7 @@ def remove(self, data: Union[ObjectAnchor, list[ObjectAnchor]]) -> None:

@dataclass
class ShelfMemory(Memory):
"""Generic Memory Handler."""
"""Shelf Handler."""

__shelf__: Optional[Shelf[ObjectAnchor]] = None

Expand Down Expand Up @@ -130,7 +130,7 @@ def find(
filter: Optional[Callable[[ObjectAnchor], ObjectAnchor]] = None,
extended: Optional[bool] = None,
) -> Generator[ObjectAnchor, None, None]:
"""Temporary."""
"""Find anchors from datasource by ids with filter."""
objs = super().find(ids, filter, True)

if isinstance(self.__shelf__, Shelf):
Expand All @@ -154,7 +154,7 @@ def find(
def set(
self, data: Union[ObjectAnchor, list[ObjectAnchor]], mem_only: bool = False
) -> None:
"""Temporary."""
"""Save anchor/s to datasource."""
super().set(data)

if not mem_only and isinstance(self.__shelf__, Shelf):
Expand All @@ -165,7 +165,7 @@ def set(
self.__shelf__[str(data.id)] = data

def remove(self, data: Union[ObjectAnchor, list[ObjectAnchor]]) -> None:
"""Temporary."""
"""Remove anchor/s from datasource."""
super().remove(data)
if isinstance(self.__shelf__, Shelf) and ENABLE_MANUAL_SAVE:
if isinstance(data, list):
Expand Down

0 comments on commit 715c00a

Please sign in to comment.