146 lines
3.7 KiB
Python
146 lines
3.7 KiB
Python
import shutil
|
|
from os import path
|
|
import os
|
|
|
|
import pytest
|
|
|
|
from core.builtin_concepts import ReturnValueConcept, BuiltinConcepts
|
|
from core.sheerka import Sheerka, ExecutionContext
|
|
import core.builtin_helpers
|
|
|
|
tests_root = path.abspath("../build/tests")
|
|
root_folder = "init_folder"
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def init_test():
|
|
if path.exists(tests_root):
|
|
shutil.rmtree(tests_root)
|
|
|
|
if not path.exists(tests_root):
|
|
os.makedirs(tests_root)
|
|
current_pwd = os.getcwd()
|
|
os.chdir(tests_root)
|
|
|
|
yield None
|
|
|
|
os.chdir(current_pwd)
|
|
|
|
|
|
def test_i_can_use_expect_one_when_empty():
|
|
sheerka = get_sheerka()
|
|
|
|
res = core.builtin_helpers.expect_one(get_context(sheerka), [])
|
|
assert not res.status
|
|
assert sheerka.isinstance(res.value, BuiltinConcepts.IS_EMPTY)
|
|
|
|
|
|
def test_i_can_use_expect_one_when_too_many_success():
|
|
sheerka = get_sheerka()
|
|
|
|
items = [
|
|
ReturnValueConcept("who", True, "value1"),
|
|
ReturnValueConcept("who", True, "value2"),
|
|
]
|
|
res = core.builtin_helpers.expect_one(get_context(sheerka), items)
|
|
assert not res.status
|
|
assert sheerka.isinstance(res.value, BuiltinConcepts.TOO_MANY_SUCCESS)
|
|
assert res.value.obj == items
|
|
assert res.parents == items
|
|
|
|
|
|
def test_i_can_use_expect_one_when_same_success():
|
|
sheerka = get_sheerka()
|
|
|
|
items = [
|
|
ReturnValueConcept("who", True, "value"),
|
|
ReturnValueConcept("who", True, "value"),
|
|
]
|
|
res = core.builtin_helpers.expect_one(get_context(sheerka), items)
|
|
assert res.status
|
|
assert res.value == items[0].value
|
|
assert res.parents == items
|
|
|
|
|
|
def test_i_can_use_expect_when_only_errors_1():
|
|
sheerka = get_sheerka()
|
|
|
|
items = [
|
|
ReturnValueConcept("who", False, None),
|
|
]
|
|
res = core.builtin_helpers.expect_one(get_context(sheerka), items)
|
|
assert not res.status
|
|
assert sheerka.isinstance(res.value, BuiltinConcepts.TOO_MANY_ERRORS)
|
|
assert res.value.obj == items
|
|
assert res.parents == items
|
|
|
|
|
|
def test_i_can_use_expect_when_only_errors_2():
|
|
sheerka = get_sheerka()
|
|
|
|
items = [
|
|
ReturnValueConcept("who", False, None),
|
|
ReturnValueConcept("who", False, None),
|
|
]
|
|
res = core.builtin_helpers.expect_one(get_context(sheerka), items)
|
|
assert not res.status
|
|
assert sheerka.isinstance(res.value, BuiltinConcepts.TOO_MANY_ERRORS)
|
|
assert res.value.obj == items
|
|
assert res.parents == items
|
|
|
|
|
|
def test_i_can_use_expect_one_when_one_success_1():
|
|
sheerka = get_sheerka()
|
|
|
|
items = [
|
|
ReturnValueConcept("who", True, None),
|
|
]
|
|
res = core.builtin_helpers.expect_one(get_context(sheerka), items)
|
|
assert res.status
|
|
assert res.body == items[0].body
|
|
assert res.parents == items
|
|
|
|
|
|
def test_i_can_use_expect_one_when_one_success_2():
|
|
sheerka = get_sheerka()
|
|
|
|
items = [
|
|
ReturnValueConcept("who", False, None),
|
|
ReturnValueConcept("who", True, None),
|
|
ReturnValueConcept("who", False, None),
|
|
]
|
|
res = core.builtin_helpers.expect_one(get_context(sheerka), items)
|
|
assert res.status
|
|
assert res.body == items[1].body
|
|
assert res.parents == items
|
|
|
|
|
|
def test_i_can_use_expect_one_when_not_a_list_true():
|
|
sheerka = get_sheerka()
|
|
|
|
item = ReturnValueConcept("who", True, None)
|
|
res = core.builtin_helpers.expect_one(get_context(sheerka), item)
|
|
assert res.status
|
|
assert res == item
|
|
|
|
|
|
def test_i_can_use_expect_one_when_not_a_list_false():
|
|
sheerka = get_sheerka()
|
|
|
|
item = ReturnValueConcept("who", False, None)
|
|
res = core.builtin_helpers.expect_one(get_context(sheerka), item)
|
|
|
|
assert not res.status
|
|
assert res == item
|
|
|
|
|
|
def get_sheerka():
|
|
sheerka = Sheerka()
|
|
sheerka.initialize(root_folder)
|
|
|
|
return sheerka
|
|
|
|
|
|
def get_context(sheerka):
|
|
return ExecutionContext("test", "xxx", sheerka)
|