Finishing testing promises.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
ef465b34e7
commit
a182c703f4
8 changed files with 114 additions and 36 deletions
|
@ -21,7 +21,7 @@ from typing import Callable, Self
|
|||
from .base import Assertion, repr_, AssertionInterface
|
||||
from .int import NumberComparisonAssertionInterface
|
||||
|
||||
PRINT_PREFIX = (" " * 24)
|
||||
PRINT_PREFIX = (" " * 3)
|
||||
|
||||
|
||||
class SpyAssertion(Assertion):
|
||||
|
|
|
@ -30,4 +30,4 @@ class Spy:
|
|||
def __call__(self, *args, **kwargs):
|
||||
self.calls.append((args, kwargs))
|
||||
if self.function is not None:
|
||||
self.function(*args, **kwargs)
|
||||
return self.function(*args, **kwargs)
|
||||
|
|
|
@ -134,13 +134,13 @@ def test_add_natural_complex():
|
|||
assert that(2).equals.one.minus.two
|
||||
|
||||
def test_spy():
|
||||
spy = Spy()
|
||||
spy = Spy(lambda *args, **kw: 10)
|
||||
assert that(spy).is_.an.instance_of(Spy)
|
||||
assert that(spy).is_(callable)
|
||||
# Check calls
|
||||
assert that(spy).was.never.called
|
||||
assert that(spy).was.called.zero.times
|
||||
spy(30, arg="string")
|
||||
assert spy(30, arg="string") == 10
|
||||
assert that(spy).was.called
|
||||
assert that(spy).was.called.once
|
||||
assert that(spy).was.called.one.time
|
||||
|
@ -159,7 +159,7 @@ def test_spy():
|
|||
assert that(spy).was.called.with_no_argument()
|
||||
|
||||
def test_spy_seral_calls():
|
||||
spy = Spy(lambda *args, **kw: None)
|
||||
spy = Spy()
|
||||
obj = object()
|
||||
spy()
|
||||
spy(30, arg="string")
|
||||
|
|
|
@ -22,12 +22,13 @@ from os.path import exists, join
|
|||
from PySide6.QtGui import QIcon
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
from .globals import app
|
||||
|
||||
from LogarithmPlotter.logarithmplotter import get_linux_theme, LINUX_THEMES, get_platform_qt_style, \
|
||||
register_icon_directories, install_translation, create_engine
|
||||
from LogarithmPlotter.util import config
|
||||
from LogarithmPlotter.util.helper import Helper
|
||||
from LogarithmPlotter.util.latex import Latex
|
||||
from globals import app
|
||||
|
||||
THEMES = [
|
||||
"Basic",
|
||||
|
|
|
@ -17,9 +17,10 @@
|
|||
"""
|
||||
from time import sleep
|
||||
|
||||
import pytest
|
||||
from PySide6.QtQml import QJSValue
|
||||
|
||||
from tests.plugins.natural import that, Spy
|
||||
from .plugins.natural import that, Spy
|
||||
from LogarithmPlotter.util.js import PyJSValue
|
||||
from LogarithmPlotter.util.promise import PyPromise
|
||||
|
||||
|
@ -56,6 +57,10 @@ def async_throw():
|
|||
|
||||
class TestPyPromise:
|
||||
|
||||
def test_invalid_function(self):
|
||||
with pytest.raises(ValueError):
|
||||
promise = PyPromise("not a function")
|
||||
|
||||
def test_fulfill_values(self, qtbot):
|
||||
qjsv = QJSValue(3)
|
||||
values = [
|
||||
|
@ -70,7 +75,6 @@ class TestPyPromise:
|
|||
for [value, test] in values:
|
||||
promise = PyPromise(create_async_func(value))
|
||||
with qtbot.assertNotEmitted(promise.rejected, wait=1000):
|
||||
print("Testing", value)
|
||||
with qtbot.waitSignal(promise.fulfilled, check_params_cb=test, timeout=2000):
|
||||
assert promise.state == "pending"
|
||||
assert promise.state == "fulfilled"
|
||||
|
@ -84,30 +88,90 @@ class TestPyPromise:
|
|||
assert promise.state == "rejected"
|
||||
|
||||
def test_fulfill(self, qtbot):
|
||||
spy_fulfilled = Spy()
|
||||
spy_rejected = Spy()
|
||||
fulfilled = Spy()
|
||||
rejected = Spy()
|
||||
promise = PyPromise(create_async_func(3))
|
||||
then_res = promise.then(spy_fulfilled, spy_rejected)
|
||||
then_res = promise.then(fulfilled, rejected)
|
||||
# Check if the return value is the same promise (so we can chain then)
|
||||
assert then_res == promise
|
||||
assert that(then_res).does.equal(promise)
|
||||
# Check on our spy.
|
||||
with qtbot.waitSignal(promise.fulfilled, timeout=10000):
|
||||
pass
|
||||
assert that(spy_fulfilled).was.called.once
|
||||
assert that(spy_fulfilled).was.not_called.with_arguments(3)
|
||||
assert that(spy_fulfilled).was.called.with_arguments_matching(check_promise_result(3))
|
||||
assert spy_rejected.was.not_called
|
||||
assert that(fulfilled).was.called.once
|
||||
assert that(fulfilled).was.NOT.called.with_arguments(3)
|
||||
assert that(fulfilled).was.called.with_arguments_matching(check_promise_result(3))
|
||||
assert that(rejected).was.never.called
|
||||
|
||||
def test_rejected(self, qtbot):
|
||||
spy_fulfilled = Spy()
|
||||
spy_rejected = Spy()
|
||||
fulfilled = Spy()
|
||||
rejected = Spy()
|
||||
promise = PyPromise(async_throw)
|
||||
then_res = promise.then(spy_fulfilled, spy_rejected)
|
||||
then_res = promise.then(fulfilled, rejected)
|
||||
# Check if the return value is the same promise (so we can chain then)
|
||||
assert that(then_res).is_equal.to(promise)
|
||||
assert that(then_res).does.equal(promise)
|
||||
# Check on our spies.
|
||||
with qtbot.waitSignal(promise.rejected, timeout=10000):
|
||||
pass
|
||||
assert that(spy_rejected).was.called.once
|
||||
assert that(spy_rejected).was.called.with_arguments("Exception('aaaa')")
|
||||
assert that(spy_fulfilled).was.not_called
|
||||
assert that(rejected).was.called.once
|
||||
assert that(rejected).was.called.with_arguments("Exception('aaaa')")
|
||||
assert that(fulfilled).has.never.been.called
|
||||
|
||||
def test_chain_fulfill(self, qtbot):
|
||||
convert = Spy(lambda v: v.toVariant())
|
||||
plus = Spy(lambda v: v + 1)
|
||||
rejected = Spy()
|
||||
promise = PyPromise(create_async_func(5))
|
||||
then_res = promise.then(convert, rejected).then(plus, rejected).then(plus, rejected).then(plus, rejected)
|
||||
# Check if the return value is the same promise (so we can chain then)
|
||||
assert that(then_res).does.equal(promise)
|
||||
with qtbot.waitSignal(promise.fulfilled, timeout=10000):
|
||||
pass
|
||||
assert that(convert).was.called.once.with_arguments_matching(check_promise_result(5))
|
||||
assert that(rejected).was.never.called
|
||||
assert that(plus).was.called.three.times
|
||||
assert that(plus).was.called.once.with_exact_arguments(5)
|
||||
assert that(plus).was.called.once.with_exact_arguments(6)
|
||||
assert that(plus).was.called.once.with_exact_arguments(7)
|
||||
|
||||
def test_chain_reject(self, qtbot):
|
||||
fulfilled = Spy()
|
||||
convert = Spy(lambda v: len(v))
|
||||
minus = Spy(lambda v: v - 1)
|
||||
promise = PyPromise(async_throw)
|
||||
then_res = promise.then(fulfilled, convert).then(fulfilled, minus).then(fulfilled, minus).then(fulfilled, minus)
|
||||
# Check if the return value is the same promise (so we can chain then)
|
||||
assert that(then_res).does.equal(promise)
|
||||
with qtbot.waitSignal(promise.rejected, timeout=10000):
|
||||
pass
|
||||
assert that(fulfilled).was.never.called
|
||||
assert that(convert).was.called.once.with_arguments_matching(check_promise_result("Exception('aaaa')"))
|
||||
assert that(minus).was.called.three.times
|
||||
assert that(minus).was.called.once.with_exact_arguments(17)
|
||||
assert that(minus).was.called.once.with_exact_arguments(16)
|
||||
assert that(minus).was.called.once.with_exact_arguments(15)
|
||||
|
||||
def test_check_calls_upon(self):
|
||||
promise = PyPromise(async_throw)
|
||||
fulfilled = Spy()
|
||||
rejected = Spy()
|
||||
promise.then(fulfilled, rejected)
|
||||
assert promise.calls_upon_fulfillment(fulfilled)
|
||||
assert promise.calls_upon_rejection(rejected)
|
||||
assert not promise.calls_upon_fulfillment(rejected)
|
||||
assert not promise.calls_upon_rejection(fulfilled)
|
||||
|
||||
def test_reject_in_fulfill(self, qtbot):
|
||||
def fulfilled_throw(x):
|
||||
raise Exception('noooo')
|
||||
promise = PyPromise(create_async_func("3"))
|
||||
fulfilled_throw = Spy(fulfilled_throw)
|
||||
fulfilled = Spy()
|
||||
rejected = Spy()
|
||||
then_res = promise.then(fulfilled, rejected).then(fulfilled_throw, rejected).then(fulfilled, rejected).then(fulfilled, rejected)
|
||||
# Check if the return value is the same promise (so we can chain then)
|
||||
assert that(then_res).does.equal(promise)
|
||||
with qtbot.waitSignal(promise.fulfilled, timeout=10000):
|
||||
pass
|
||||
assert that(fulfilled_throw).has.been.called.once
|
||||
assert that(rejected).has.been.called.three.times
|
||||
assert that(rejected).has.been.called.three.times.with_arguments("Exception('noooo')")
|
|
@ -21,7 +21,6 @@ from re import Pattern
|
|||
from PySide6.QtQml import QJSEngine, QJSValue
|
||||
|
||||
from LogarithmPlotter.util.js import PyJSValue, InvalidAttributeValueException, NotAPrimitiveException
|
||||
from globals import app
|
||||
|
||||
@pytest.fixture()
|
||||
def data():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue