def test_toggle_closing_mapper_on_error(self): class TestException(Exception): pass ex = TestException('test exception') with marbles_testing(timespan=1.0) as (start, cold, hot, exp): lookup = {'a': [2, 3], 'b': [5], 'c': [7], 'd': [8, 9]} openings = hot('---a-------b------c---d-------|') a = cold(' ------1|') b = cold(' ---#', error=ex) c = cold(' ---1|') d = cold(' -----1|') source = hot(' -1--2--3--4--5--6---7--8--9---|') expected = exp('---------a----#', lookup=lookup, error=ex) # 012345678901234567890123456789 # 0 1 2 closings = {'a': a, 'b': b, 'c': c, 'd': d} def closing_mapper(key): return closings[key] obs = source.pipe(ops.buffer_toggle(openings, closing_mapper)) results = start(obs) assert results == expected
def test_when_source_on_error(self): class TestException(Exception): pass ex = TestException('test exception') with marbles_testing(timespan=1.0) as (start, cold, hot, exp): def closing_mapper(): return cold('-----1|') lookup = {'a': [1, 2], 'b': [3], 'c': [4, 5], 'd': [6], 'e': [7]} # -----1| # -----1| # -----1| # -----1| # -----1| source = hot(' -1--2---3--4--5--#', error=ex) expected = exp('-----a----b----c-#', lookup=lookup, error=ex) # 012345678901234567890123456789 # 0 1 2 obs = source.pipe(ops.buffer_when(closing_mapper)) results = start(obs) assert results == expected
def test_start_with_cold_empty(self): with marbles_testing() as (start, cold, hot, exp): obs = cold("------|") " 012345678901234567890" def create(): return obs results = start(create) expected = [ReactiveTest.on_completed(206)] assert results == expected
def test_start_with_cold_never(self): with marbles_testing() as (start, cold, hot, exp): obs = cold("----") " 012345678901234567890" def create(): return obs results = start(create) expected = [] assert results == expected
def test_start_with_cold_and_exp_group(self): with marbles_testing() as (start, cold, hot, exp): obs = cold(" 12--(3,6.5)----(5,#)") expected = exp(" 12--(3,6.5)----(5,#)") " 012345678901234567890" def create(): return obs results = start(create) assert results == expected
def test_start_with_hot_and_exp(self): with marbles_testing() as (start, cold, hot, exp): obs = hot(" --3--4--5-|") expected = exp("--3--4--5-|") " 012345678901234567890" def create(): return obs results = start(create) assert results == expected
def test_start_with_cold_no_create_function(self): with marbles_testing() as (start, cold, hot, exp): obs = cold("12--3-|") " 012345678901234567890" results = start(obs) expected = [ ReactiveTest.on_next(200.0, 12), ReactiveTest.on_next(204.0, 3), ReactiveTest.on_completed(206.0), ] assert results == expected
def test_exp(self): with marbles_testing() as (start, cold, hot, exp): results = exp("12--3--4--5-|") " 012345678901234567890" expected = [ ReactiveTest.on_next(200.0, 12), ReactiveTest.on_next(204.0, 3), ReactiveTest.on_next(207.0, 4), ReactiveTest.on_next(210.0, 5), ReactiveTest.on_completed(212.0), ] assert results == expected
def test_start_with_cold_normal(self): with marbles_testing() as (start, cold, hot, exp): obs = cold("12--3-|") " 012345678901234567890" def create(): return obs results = start(create) expected = [ ReactiveTest.on_next(200.0, 12), ReactiveTest.on_next(204.0, 3), ReactiveTest.on_completed(206.0), ] assert results == expected
def test_when_closing_only_on_first_item(self): with marbles_testing(timespan=1.0) as (start, cold, hot, exp): def closing_mapper(): return cold('-----1--2--3--4--|') lookup = {'a': [1, 2], 'b': [3], 'c': [4, 5], 'd': [6], 'e': [7]} # -----1--2--3--4--| # -----1--2--3--4--| # -----1--2--3--4--| # -----1--2--3--4--| source = hot(' -1--2---3--4--5--6---7--|') expected = exp('-----a----b----c----d---(e,|)', lookup=lookup) # 012345678901234567890123456789 # 0 1 2 obs = source.pipe(ops.buffer_when(closing_mapper)) results = start(obs) assert results == expected
def test_when_closing_on_error(self): class TestException(Exception): pass ex = TestException('test exception') with marbles_testing(timespan=1.0) as (start, cold, hot, exp): def closing_mapper(): return cold('-----#', error=ex) # -----# source = hot(' -1--2---3--4--5--6---7--|') expected = exp('-----#', error=ex) # 012345678901234567890123456789 # 0 1 2 obs = source.pipe(ops.buffer_when(closing_mapper)) results = start(obs) assert results == expected
def test_toggle_closing_with_only_first_item(self): with marbles_testing(timespan=1.0) as (start, cold, hot, exp): lookup = {'a': [2, 3], 'b': [5], 'c': [7], 'd': [8, 9]} openings = hot('---a-------b------c---d-------|') a = cold(' ------1-2-|') b = cold(' ----1-2-|') c = cold(' ---1-2-|') d = cold(' -----1-2|') source = hot(' -1--2--3--4--5--6---7--8--9---|') expected = exp('---------a-----b-----c-----d--|', lookup=lookup) # 012345678901234567890123456789 # 0 1 2 closings = {'a': a, 'b': b, 'c': c, 'd': d} def closing_mapper(key): return closings[key] obs = source.pipe(ops.buffer_toggle(openings, closing_mapper)) results = start(obs) assert results == expected
from rx import operators as ops from rx.testing.marbles import marbles_testing """ Tests MergeMap from rxjs https://github.com/ReactiveX/rxjs/blob/master/spec/operators/mergeMap-spec.ts it should flat_map many regular interval inners """ with marbles_testing(timespan=1.0) as context: start, cold, hot, exp = context a = cold(' ----a---a----a----(a,|) ') b = cold(' ----1----b----(b,|) ') c = cold(' -------c---c---c----c---(c,|)') d = cold(' -------(d,|) ') e1 = hot('-a---b-----------c-------d------------| ') ex = exp('-----a---(a,1)(a,b)(a,b)c---c---(c,d)c---(c,|)') expected = ex observableLookup = {"a": a, "b": b, "c": c, "d": d} obs = e1.pipe( ops.flat_map(lambda value: observableLookup[value]) ) results = start(obs) assert results == expected print('flat_map: results vs expected') for r, e in zip(results, expected):
from rx import operators as ops from rx.testing.marbles import marbles_testing """ Tests debounceTime from rxjs https://github.com/ReactiveX/rxjs/blob/master/spec/operators/debounceTime-spec.ts it should delay all element by the specified time """ with marbles_testing(timespan=1.0) as (start, cold, hot, exp): e1 = cold('-a--------b------c----|') ex = exp('------a--------b------(c,|)') expected = ex def create(): return e1.pipe(ops.debounce(5), ) results = start(create) assert results == expected print('debounce: results vs expected') for r, e in zip(results, expected): print(r, e)
from rx import operators as ops from rx.testing.marbles import marbles_testing """ Tests debounceTime from rxjs https://github.com/ReactiveX/rxjs/blob/master/spec/operators/debounceTime-spec.ts it should delay all element by the specified time """ with marbles_testing(timespan=1.0) as (start, cold, hot, exp): e1 = cold('-a--------b------c----|') ex = exp( '------a--------b------(c,|)') expected = ex def create(): return e1.pipe( ops.debounce(5), ) results = start(create) assert results == expected print('debounce: results vs expected') for r, e in zip(results, expected): print(r, e)