Files
Sheerka/tests/conftest.py
T

115 lines
2.9 KiB
Python

import inspect
from contextlib import contextmanager
import pytest
from helpers import GetNextId
from parsers.tokenizer import Token
from server.authentication import User
DEFAULT_ONTOLOGY_NAME = "current_test_"
@pytest.fixture(scope="session")
def sheerka():
from core.Sheerka import Sheerka
sheerka = Sheerka()
sheerka.initialize("mem://")
return sheerka
@pytest.fixture(scope="module", autouse=True)
def on_new_module(sheerka, request):
"""
For each new module, make sure to create a new ontology
Remove it at the end of the module
:param sheerka:
:type sheerka:
:param request:
:type request:
:return:
:rtype:
"""
from core.Event import Event
from core.ExecutionContext import ExecutionContext, ContextActions
module_name = request.module.__name__.split(".")[-1]
context = ExecutionContext("test",
Event(message=f"Executing module {module_name}"),
sheerka,
ContextActions.TESTING,
None)
ontology = sheerka.om.push_ontology(module_name)
yield
sheerka.om.revert_ontology(context, ontology)
@pytest.fixture(scope="function")
def context(sheerka):
from core.Event import Event
from core.ExecutionContext import ExecutionContext, ContextActions
return ExecutionContext("test",
Event(message=""),
sheerka,
ContextActions.TESTING,
None)
@pytest.fixture()
def next_id():
return GetNextId()
@pytest.fixture()
def user():
return User(username="johan doe", email="johan.doe@sheerka.com", firstname="johan", lastname="doe")
class TestUsingFileBasedSheerka:
@pytest.fixture(scope="class")
def sheerka(self):
sheerka = Sheerka()
sheerka.initialize()
return sheerka
class NewOntology:
"""
For some test who may need to declare the same concepts across the tests
"""
from core.ExecutionContext import ExecutionContext
def __init__(self, context: ExecutionContext, name=None):
self.sheerka = context.sheerka
self.context = context
self.name = name
self.ontology = None
if self.name is None:
self.name = inspect.stack()[1][3]
def __enter__(self):
self.ontology = self.sheerka.om.push_ontology(self.name)
return self.ontology
def __exit__(self, exc_type, exc_val, exc_tb):
self.sheerka.om.revert_ontology(self.context, self.ontology)
return False
def simple_token_compare(a, b):
return a.type == b.type and a.value == b.value
@contextmanager
def comparable_tokens():
eq = Token.__eq__
ne = Token.__ne__
setattr(Token, "__eq__", simple_token_compare)
setattr(Token, "__ne__", lambda a, b: not simple_token_compare(a, b))
yield
setattr(Token, "__eq__", eq)
setattr(Token, "__ne__", ne)