<s> import unittest <EOL> from rx . observable import Observable <EOL> from rx . testing import TestScheduler , ReactiveTest <EOL> from rx . disposables import Disposable , SerialDisposable <EOL> on_next = ReactiveTest . on_next <EOL> on_completed = ReactiveTest . on_completed <EOL> on_error = ReactiveTest . on_error <EOL> subscribe = ReactiveTest . subscribe <EOL> subscribed = ReactiveTest . subscribed <EOL> disposed = ReactiveTest . disposed <EOL> created = ReactiveTest . created <EOL> class RxException ( Exception ) : <EOL> pass <EOL> def _raise ( ex ) : <EOL> raise RxException ( ex ) <EOL> class TestSingleOrDefault ( unittest . TestCase ) : <EOL> def test_single_or_default_async_empty ( self ) : <EOL> scheduler = TestScheduler ( ) <EOL> xs = scheduler . create_hot_observable ( on_next ( <NUM_LIT> , <NUM_LIT:1> ) , on_completed ( <NUM_LIT> ) ) <EOL> def create ( ) : <EOL> return xs . single_or_default ( None , <NUM_LIT:0> ) <EOL> res = scheduler . start ( create = create ) <EOL> res . messages . assert_equal ( on_next ( <NUM_LIT> , <NUM_LIT:0> ) , on_completed ( <NUM_LIT> ) ) <EOL> xs . subscriptions . assert_equal ( subscribe ( <NUM_LIT:200> , <NUM_LIT> ) ) <EOL> def test_single_or_default_async_one ( self ) : <EOL> scheduler = TestScheduler ( ) <EOL> xs = scheduler . create_hot_observable ( on_next ( <NUM_LIT> , <NUM_LIT:1> ) , on_next ( <NUM_LIT> , <NUM_LIT:2> ) , on_completed ( <NUM_LIT> ) ) <EOL> def create ( ) : <EOL> return xs . single_or_default ( None , <NUM_LIT:0> ) <EOL> res = scheduler . start ( create = create ) <EOL> res . messages . assert_equal ( on_next ( <NUM_LIT> , <NUM_LIT:2> ) , on_completed ( <NUM_LIT> ) ) <EOL> xs . subscriptions . assert_equal ( subscribe ( <NUM_LIT:200> , <NUM_LIT> ) ) <EOL> def test_single_or_default_async_many ( self ) : <EOL> scheduler = TestScheduler ( ) <EOL> xs = scheduler . create_hot_observable ( on_next ( <NUM_LIT> , <NUM_LIT:1> ) , on_next ( <NUM_LIT> , <NUM_LIT:2> ) , on_next ( <NUM_LIT> , <NUM_LIT:3> ) , on_completed ( <NUM_LIT> ) ) <EOL> def create ( ) : <EOL> return xs . single_or_default ( None , <NUM_LIT:0> ) <EOL> res = scheduler . start ( create = create ) <EOL> def predicate ( e ) : <EOL> return not e is None <EOL> res . messages . assert_equal ( on_error ( <NUM_LIT> , predicate ) ) <EOL> xs . subscriptions . assert_equal ( subscribe ( <NUM_LIT:200> , <NUM_LIT> ) ) <EOL> def test_single_or_default_async_error ( self ) : <EOL> ex = '<STR_LIT>' <EOL> scheduler = TestScheduler ( ) <EOL> xs = scheduler . create_hot_observable ( on_next ( <NUM_LIT> , <NUM_LIT:1> ) , on_error ( <NUM_LIT> , ex ) ) <EOL> def create ( ) : <EOL> return xs . single_or_default ( None , <NUM_LIT:0> ) <EOL> res = scheduler . start ( create = create ) <EOL> res . messages . assert_equal ( on_error ( <NUM_LIT> , ex ) ) <EOL> xs . subscriptions . assert_equal ( subscribe ( <NUM_LIT:200> , <NUM_LIT> ) ) <EOL> def test_single_or_default_async_predicate ( self ) : <EOL> scheduler = TestScheduler ( ) <EOL> xs = scheduler . create_hot_observable ( on_next ( <NUM_LIT> , <NUM_LIT:1> ) , on_next ( <NUM_LIT> , <NUM_LIT:2> ) , on_next ( <NUM_LIT> , <NUM_LIT:3> ) , on_next ( <NUM_LIT> , <NUM_LIT:4> ) , on_next ( <NUM_LIT> , <NUM_LIT:5> ) , on_completed ( <NUM_LIT> ) ) <EOL> def create ( ) : <EOL> def predicate ( x ) : <EOL> return x % <NUM_LIT:2> == <NUM_LIT:1> <EOL> return xs . single_or_default ( predicate , <NUM_LIT:0> ) <EOL> res = scheduler . start ( create = create ) <EOL> def predicate ( e ) : <EOL> return not e is None <EOL> res . messages . assert_equal ( on_error ( <NUM_LIT> , predicate ) ) <EOL> xs . subscriptions . assert_equal ( subscribe ( <NUM_LIT:200> , <NUM_LIT> ) ) <EOL> def test_single_or_default_async_predicate_empty ( self ) : <EOL> scheduler = TestScheduler ( ) <EOL> xs = scheduler . create_hot_observable ( on_next ( <NUM_LIT> , <NUM_LIT:1> ) , on_completed ( <NUM_LIT> ) ) <EOL> def create ( ) : <EOL> def predicate ( x ) : <EOL> return x % <NUM_LIT:2> == <NUM_LIT:1> <EOL> return xs . single_or_default ( predicate , <NUM_LIT:0> ) <EOL> res = scheduler . start ( create = create ) <EOL> res . messages . assert_equal ( on_next ( <NUM_LIT> , <NUM_LIT:0> ) , on_completed ( <NUM_LIT> ) ) <EOL> xs . subscriptions . assert_equal ( subscribe ( <NUM_LIT:200> , <NUM_LIT> ) ) <EOL> def test_single_or_default_async_predicate_one ( self ) : <EOL> ex = '<STR_LIT>' <EOL> scheduler = TestScheduler ( ) <EOL> xs = scheduler . create_hot_observable ( on_next ( <NUM_LIT> , <NUM_LIT:1> ) , on_next ( <NUM_LIT> , <NUM_LIT:2> ) , on_next ( <NUM_LIT> , <NUM_LIT:3> ) , on_next ( <NUM_LIT> , <NUM_LIT:4> ) , on_next ( <NUM_LIT> , <NUM_LIT:5> ) , on_completed ( <NUM_LIT> ) ) <EOL> def create ( ) : <EOL> def predicate ( x ) : <EOL> return x == <NUM_LIT:4> <EOL> return xs . single_or_default ( predicate , <NUM_LIT:0> ) <EOL> res = scheduler . start ( create = create ) <EOL> res . messages . assert_equal ( on_next ( <NUM_LIT> , <NUM_LIT:4> ) , on_completed ( <NUM_LIT> ) ) <EOL> xs . subscriptions . assert_equal ( subscribe ( <NUM_LIT:200> , <NUM_LIT> ) ) <EOL> def test_single_or_default_async_predicate_none ( self ) : <EOL> scheduler = TestScheduler ( ) <EOL> xs = scheduler . create_hot_observable ( on_next ( <NUM_LIT> , <NUM_LIT:1> ) , on_next ( <NUM_LIT> , <NUM_LIT:2> ) , on_next ( <NUM_LIT> , <NUM_LIT:3> ) , on_next ( <NUM_LIT> , <NUM_LIT:4> ) , on_next ( <NUM_LIT> , <NUM_LIT:5> ) , on_completed ( <NUM_LIT> ) ) <EOL> def create ( ) : <EOL> def predicate ( x ) : <EOL> return x > <NUM_LIT:10> <EOL> return xs . single_or_default ( predicate , <NUM_LIT:0> ) <EOL> res = scheduler . start ( create = create ) <EOL> res . messages . assert_equal ( on_next ( <NUM_LIT> , <NUM_LIT:0> ) , on_completed ( <NUM_LIT> ) ) <EOL> xs . subscriptions . assert_equal ( subscribe ( <NUM_LIT:200> , <NUM_LIT> ) ) <EOL> def test_single_or_default_async_predicate_throw ( self ) : <EOL> ex = '<STR_LIT>' <EOL> scheduler = TestScheduler ( ) <EOL> xs = scheduler . create_hot_observable ( on_next ( <NUM_LIT> , <NUM_LIT:1> ) , on_error ( <NUM_LIT> , ex ) ) <EOL> def create ( ) : <EOL> def predicate ( x ) : <EOL> return x > <NUM_LIT:10> <EOL> return xs . single_or_default ( predicate , <NUM_LIT:0> ) <EOL> res = scheduler . start ( create = create ) <EOL> res . messages . assert_equal ( on_error ( <NUM_LIT> , ex ) ) <EOL> xs . subscriptions . assert_equal ( subscribe ( <NUM_LIT:200> , <NUM_LIT> ) ) <EOL> def test_single_or_default_async_predicate_throws ( self ) : <EOL> ex = '<STR_LIT>' <EOL> scheduler = TestScheduler ( ) <EOL> xs = scheduler . create_hot_observable ( on_next ( <NUM_LIT> , <NUM_LIT:1> ) , on_next ( <NUM_LIT> , <NUM_LIT:2> ) , on_next ( <NUM_LIT> , <NUM_LIT:3> ) , on_next ( <NUM_LIT> , <NUM_LIT:4> ) , on_next ( <NUM_LIT> , <NUM_LIT:5> ) , on_completed ( <NUM_LIT> ) ) <EOL> def create ( ) : <EOL> def predicate ( x ) : <EOL> if x < <NUM_LIT:4> : <EOL> return False <EOL> else : <EOL> raise Exception ( ex ) <EOL> return xs . single_or_default ( predicate , <NUM_LIT:0> ) <EOL> res = scheduler . start ( create = create ) <EOL> res . messages . assert_equal ( on_error ( <NUM_LIT> , ex ) ) <EOL> xs . subscriptions . assert_equal ( subscribe ( <NUM_LIT:200> , <NUM_LIT> ) ) </s>
