def test_3_2_6_3_if_rejected(): """ Testing return of pending promises to make sure they are properly chained. This covers the case where the root promise is rejected before the chaining is defined. """ p1 = Promise() p1.reject("Error") pending = Promise() pending.fulfill(10) pr = p1.then(None, lambda r: pending) assert pending.isFulfilled() assert_equals(10, pending.value) assert pr.isFulfilled() assert_equals(10, pr.value) p2 = Promise() p2.reject("Error") bad = Promise() bad.reject("Assertion") pr = p2.then(None, lambda r: bad) assert bad.isRejected() assert_equals("Assertion", bad.reason) assert pr.isRejected() assert_equals("Assertion", pr.reason)
def test_3_2_6_3_when_rejected(): """ Testing return of pending promises to make sure they are properly chained. This covers the case where the root promise is rejected after the chaining is defined. """ p1 = Promise() pending = Promise() pr = p1.then(None, lambda r: pending) assert pending.isPending assert pr.isPending p1.reject(Exception("Error")) pending.fulfill(10) assert pending.isFulfilled assert_equals(10, pending.value) assert pr.isFulfilled assert_equals(10, pr.value) p2 = Promise() bad = Promise() pr = p2.then(None, lambda r: bad) assert bad.isPending assert pr.isPending p2.reject(Exception("Error")) bad.reject(Exception("Assertion")) assert bad.isRejected assert_exception(bad.reason, Exception, "Assertion") assert pr.isRejected assert_exception(pr.reason, Exception, "Assertion")
def test_3_2_6_3_if_rejected(): """ Testing return of pending promises to make sure they are properly chained. This covers the case where the root promise is rejected before the chaining is defined. """ p1 = Promise() p1.reject(Exception("Error")) pending = Promise() pending.fulfill(10) pr = p1.then(None, lambda r: pending) assert pending.isFulfilled assert_equals(10, pending.value) assert pr.isFulfilled assert_equals(10, pr.value) p2 = Promise() p2.reject(Exception("Error")) bad = Promise() bad.reject(Exception("Assertion")) pr = p2.then(None, lambda r: bad) assert bad.isRejected assert_exception(bad.reason, Exception, "Assertion") assert pr.isRejected assert_exception(pr.reason, Exception, "Assertion")
def test_3_2_6_3_when_fulfilled(): """ Testing return of pending promises to make sure they are properly chained. This covers the case where the root promise is fulfilled after the chaining is defined. """ p1 = Promise() pending = Promise() pf = p1.then(lambda r: pending) assert pending.isPending() assert pf.isPending() p1.fulfill(10) pending.fulfill(5) assert pending.isFulfilled() assert_equals(5, pending.value) assert pf.isFulfilled() assert_equals(5, pf.value) p2 = Promise() bad = Promise() pr = p2.then(lambda r: bad) assert bad.isPending() assert pr.isPending() p2.fulfill(10) bad.reject("Error") assert bad.isRejected() assert_equals("Error", bad.reason) assert pr.isRejected() assert_equals("Error", pr.reason)
def test_3_2_6_3_when_fulfilled(): """ Testing return of pending promises to make sure they are properly chained. This covers the case where the root promise is fulfilled after the chaining is defined. """ p1 = Promise() pending = Promise() pf = p1.then(lambda r: pending) assert pending.isPending assert pf.isPending p1.fulfill(10) pending.fulfill(5) assert pending.isFulfilled assert_equals(5, pending.value) assert pf.isFulfilled assert_equals(5, pf.value) p2 = Promise() bad = Promise() pr = p2.then(lambda r: bad) assert bad.isPending assert pr.isPending p2.fulfill(10) bad.reject(Exception("Error")) assert bad.isRejected assert_exception(bad.reason, Exception, "Error") assert pr.isRejected assert_exception(pr.reason, Exception, "Error")
def test_3_2_1(): """ Test that the arguments to 'then' are optional. """ p1 = Promise() p2 = p1.then() p3 = Promise() p4 = p3.then() p1.fulfill(5) p3.reject(Exception("How dare you!"))
def test_3_2_1(): """ Test that the arguments to 'then' are optional. """ p1 = Promise() p2 = p1.then() p3 = Promise() p4 = p3.then() p1.fulfill(5) p3.reject("How dare you!")
def test_3_2_6_1(): """ Promises returned by then must be fulfilled when the promise they are chained from is fulfilled IF the fulfillment value is not a promise. """ p1 = Promise() pf = p1.then(lambda v: v * v) p1.fulfill(5) assert_equals(pf.value, 25) p2 = Promise() pr = p2.then(None, lambda r: 5) p2.reject(Exception("Error")) assert_equals(5, pr.value)
def testNonFunction(nonFunction): def foo(k, r): results[k] = r p1 = Promise() p2 = p1.then(lambda r: foo(str(nonFunction), r), nonFunction) p1.fulfill("Error: " + str(nonFunction))
def testNonFunction(nonFunction): def foo(k, r): results[k] = r p1 = Promise() p2 = p1.then(nonFunction, lambda r: foo(str(nonFunction), r)) p1.reject(Exception("Error: " + str(nonFunction)))
def test_3_2_6_1(): """ Promises returned by then must be fulfilled when the promise they are chained from is fulfilled IF the fulfillment value is not a promise. """ p1 = Promise() pf = p1.then(lambda v: v*v) p1.fulfill(5) assert_equals(pf.value, 25) p2 = Promise() pr = p2.then(None, lambda r: 5) p2.reject("Error") assert_equals(5, pr.value)
def test_3_2_6_2_if(): """ Promises returned by then must be rejected when any of their callbacks throw an exception. """ def fail(v): raise AssertionError("Exception Message") p1 = Promise() p1.fulfill(5) pf = p1.then(fail) assert pf.isRejected() assert_equals("Exception Message", pf.reason) p2 = Promise() p2.reject("Error") pr = p2.then(None, fail) assert pr.isRejected() assert_equals("Exception Message", pr.reason)
def test_3_2_6_5_rejected(): """ Handles the case where the arguments to then are values, not functions or promises. """ p1 = Promise() p1.reject("Error") p2 = p1.then(None, 5) assert_equals("Error", p1.reason) assert p2.isRejected() assert_equals("Error", p2.reason)
def test_3_2_6_5_rejected(): """ Handles the case where the arguments to then are values, not functions or promises. """ p1 = Promise() p1.reject(Exception("Error")) p2 = p1.then(None, 5) assert_exception(p1.reason, Exception, "Error") assert p2.isRejected assert_exception(p2.reason, Exception, "Error")
def test_3_2_6_4_fulfilled(): """ Handles the case where the arguments to then are values, not functions or promises. """ p1 = Promise() p1.fulfill(10) p2 = p1.then(5) assert_equals(10, p1.value) assert p2.isFulfilled assert_equals(10, p2.value)
def test_3_2_6_4_fulfilled(): """ Handles the case where the arguments to then are values, not functions or promises. """ p1 = Promise() p1.fulfill(10) p2 = p1.then(5) assert_equals(10, p1.value) assert p2.isFulfilled() assert_equals(10, p2.value)
def test_3_2_6_2_if(): """ Promises returned by then must be rejected when any of their callbacks throw an exception. """ def fail(v): raise AssertionError("Exception Message") p1 = Promise() p1.fulfill(5) pf = p1.then(fail) assert pf.isRejected assert_exception(pf.reason, AssertionError, "Exception Message") p2 = Promise() p2.reject(Exception("Error")) pr = p2.then(None, fail) assert pr.isRejected assert_exception(pr.reason, AssertionError, "Exception Message")
def test_3_2_3_3(): """ Make sure rejected callback never called if promise is fulfilled """ cf = Counter() cr = Counter() p1 = Promise() p2 = p1.then(lambda v: cf.tick(), lambda r: cr.tick()) p1.fulfill(5) assert_equals(0, cr.value()) assert_equals(1, cf.value())
def test_3_2_6_1_literally(): """ If _either_ onFulfilled or onRejected returns a value that is not a promise, promise2 must be fulfilled with that value """ def _raise(e): raise e p1 = Promise() pf = p1.then(lambda v : _raise(Exception("Error")), lambda x : "catched") p1.fulfill(5) assert_equals(pf.value, "catched")
def transaction_fields_completed_promise(tx_json): p = Promise() if 'Flags' in tx_json: tx_json['Flags'] |= tfFullyCanonicalSig else: tx_json['Flags'] = 0 tx_json['Fee'] = stellar.get_fee() if 'Sequence' in tx_json: p.fulfill(tx_json) else: def inner(res): tx_json['Sequence'] = res['Sequence'] p.fulfill(tx_json) p = stellar.get_account_info_promise(tx_json['Account']) p.then(inner) return p
def test_3_2_3_1(): """ The second argument to 'then' must be called when a promise is rejected. """ c = Counter() def check(r, c): assert_equals(r, "Error") c.tick() p1 = Promise() p2 = p1.then(None, lambda r: check(r, c)) p1.reject("Error") assert_equals(1, c.value())
def test_3_2_2_1(): """ The first argument to 'then' must be called when a promise is fulfilled. """ c = Counter() def check(v, c): assert_equals(v, 5) c.tick() p1 = Promise() p2 = p1.then(lambda v: check(v, c)) p1.fulfill(5) assert_equals(1, c.value())
def test_3_2_3_1(): """ The second argument to 'then' must be called when a promise is rejected. """ c = Counter() def check(r, c): assert_exception(r, Exception, "Error") c.tick() p1 = Promise() p2 = p1.then(None, lambda r: check(r, c)) p1.reject(Exception("Error")) assert_equals(1, c.value())
def test_3_2_2_2(): """ Make sure callbacks are never called more than once. """ c = Counter() p1 = Promise() p2 = p1.then(lambda v: c.tick()) p1.fulfill(5) try: # I throw an exception p1.fulfill(5) assert False # Should not get here! except AssertionError: # This is expected pass assert_equals(1, c.value())
def test_3_2_3_2(): """ Make sure callbacks are never called more than once. """ c = Counter() p1 = Promise() p2 = p1.then(None, lambda v: c.tick()) p1.reject(Exception("Error")) try: # I throw an exception p1.reject(Exception("Error")) assert False # Should not get here! except AssertionError: # This is expected pass assert_equals(1, c.value())
def test_3_2_5_2_if(): """ Then can be called multiple times on the same promise and callbacks must be called in the order of the then calls. """ def add(l, v): l.append(v) p1 = Promise() p1.reject("Error") order = [] p2 = p1.then(None, lambda v: add(order, "p2")) p3 = p1.then(None, lambda v: add(order, "p3")) assert_equals(2, len(order)) assert_equals("p2", order[0]) assert_equals("p3", order[1])
def test_3_2_5_1_if(): """ Then can be called multiple times on the same promise and callbacks must be called in the order of the then calls. """ def add(l, v): l.append(v) p1 = Promise() p1.fulfill(2) order = [] p2 = p1.then(lambda v: add(order, "p2")) p3 = p1.then(lambda v: add(order, "p3")) assert_equals(2, len(order)) assert_equals("p2", order[0]) assert_equals("p3", order[1])
def test_3_2_5_2_if(): """ Then can be called multiple times on the same promise and callbacks must be called in the order of the then calls. """ def add(l, v): l.append(v) p1 = Promise() p1.reject(Exception("Error")) order = [] p2 = p1.then(None, lambda v: add(order, "p2")) p3 = p1.then(None, lambda v: add(order, "p3")) assert_equals(2, len(order)) assert_equals("p2", order[0]) assert_equals("p3", order[1])
def testNonFunction(nonFunction, results): def foo(results, k, r): results[k] = r p1 = Promise() p2 = p1.then(nonFunction, lambda r: foo(results, str(nonFunction), r)) p1.reject("Error: "+str(nonFunction))