def test_wrap_spec_if_needed_S():
    dehydrator = Dehydrator()

    spec = S('login')
    wrapped = dehydrator.wrap_spec_if_needed(spec=spec)

    assert wrapped is spec
def test_wrap_spec_if_needed_str():
    dehydrator = Dehydrator()

    wrapped = dehydrator.wrap_spec_if_needed(spec='login')

    assert isinstance(wrapped, S)
    assert wrapped.target == 'login'
def test_wrap_spec_if_needed_str():
    dehydrator = Dehydrator()

    wrapped = dehydrator.wrap_spec_if_needed(spec='login')

    assert isinstance(wrapped, S)
    assert wrapped.target == 'login'
def test_dehydrate_none_with_fallback_factory():
    fallback = lambda obj: {'id': '', 'name': ''}
    dehydrator = Dehydrator(specs=DUMMY_SPECS, empty=fallback)
    result1 = dehydrator.dehydrate(None)
    result2 = dehydrator.dehydrate(None)
    assert result1 == result2 == {'id': '', 'name': ''}
    assert result1 is not result2
def test_wrap_spec_if_needed_S():
    dehydrator = Dehydrator()

    spec = S('login')
    wrapped = dehydrator.wrap_spec_if_needed(spec=spec)

    assert wrapped is spec
def test_select_handler_not_found_in_registry(handlers_module):
    dehydrator = Dehydrator()
    spec_handler_cls = Mock()
    handlers_module.registry = {'simple': spec_handler_cls}
    spec = Mock(type='unknown')

    with raises(DehydrationException):
        dehydrator.select_handler(spec=spec)
def test_wrap_spec_if_needed_two_str_tuple():
    dehydrator = Dehydrator()

    wrapped = dehydrator.wrap_spec_if_needed(spec=('login', 'username'))

    assert isinstance(wrapped, S)
    assert wrapped.target == 'login'
    assert wrapped.substitution == 'username'
def test_select_handler_not_found_in_registry(handlers_module):
    dehydrator = Dehydrator()
    spec_handler_cls = Mock()
    handlers_module.registry = {'simple': spec_handler_cls}
    spec = Mock(type='unknown')

    with raises(DehydrationException):
        dehydrator.select_handler(spec=spec)
def test_wrap_spec_if_needed_two_str_tuple():
    dehydrator = Dehydrator()

    wrapped = dehydrator.wrap_spec_if_needed(spec=('login', 'username'))

    assert isinstance(wrapped, S)
    assert wrapped.target == 'login'
    assert wrapped.substitution == 'username'
def test_dehydrate_spec(select_handler):
    select_handler.return_value = handler = Mock()
    dehydrator = Dehydrator()

    dehydrator.dehydrate_spec(obj=DUMMY_OBJECT, spec=DUMMY_SINGLE_SPEC)

    select_handler.assert_called_once_with(DUMMY_SINGLE_SPEC)
    handler.build_key.assert_called_once_with()
    handler.build_value.assert_called_once_with(DUMMY_OBJECT)
def test_select_handler_found_in_registry(handlers_module):
    dehydrator = Dehydrator()
    spec_handler_cls = Mock()
    handlers_module.registry = {'simple': spec_handler_cls}
    spec = Mock(type='simple')

    dehydrator.select_handler(spec=spec)

    spec_handler_cls.assert_called_once_with(spec=spec, dehydrator=dehydrator)
def test_dehydrate_spec(select_handler):
    select_handler.return_value = handler = Mock()
    dehydrator = Dehydrator()

    dehydrator.dehydrate_spec(obj=DUMMY_OBJECT, spec=DUMMY_SINGLE_SPEC)

    select_handler.assert_called_once_with(DUMMY_SINGLE_SPEC)
    handler.build_key.assert_called_once_with()
    handler.build_value.assert_called_once_with(DUMMY_OBJECT)
def test_select_handler_found_in_registry(handlers_module):
    dehydrator = Dehydrator()
    spec_handler_cls = Mock()
    handlers_module.registry = {'simple': spec_handler_cls}
    spec = Mock(type='simple')

    dehydrator.select_handler(spec=spec)

    spec_handler_cls.assert_called_once_with(spec=spec, dehydrator=dehydrator)
def test_dehydrate(dehydrate_spec_mock):
    # TODO: find out how to undecorate function with mock
    dehydrate_spec_mock.return_value = list(zip(DUMMY_SPECS, ('Tummy', 10)))

    dehydrator = Dehydrator()
    dehydrator.specs = DUMMY_SPECS

    dehydrator.dehydrate(obj=DUMMY_OBJECT)

    dehydrate_spec_mock.has_calls(
        call(DUMMY_OBJECT, DUMMY_SPECS[0]),
        call(DUMMY_OBJECT, DUMMY_SPECS[1]),
    )
def test_dehydrate(dehydrate_spec_mock):
    # TODO: find out how to undecorate function with mock
    dehydrate_spec_mock.return_value = list(zip(DUMMY_SPECS, ('Tummy', 10)))

    dehydrator = Dehydrator()
    dehydrator.specs = DUMMY_SPECS

    dehydrator.dehydrate(obj=DUMMY_OBJECT)

    dehydrate_spec_mock.has_calls(
        call(DUMMY_OBJECT, DUMMY_SPECS[0]),
        call(DUMMY_OBJECT, DUMMY_SPECS[1]),
    )
def test_wrap_spec_if_needed_wrong_type():
    dehydrator = Dehydrator()

    with raises(DehydrationException):
        dehydrator.wrap_spec_if_needed(spec={'wrong': 'type'})
def test_wrap_spec_if_needed_wrong_type():
    dehydrator = Dehydrator()

    with raises(DehydrationException):
        dehydrator.wrap_spec_if_needed(spec={'wrong': 'type'})
def test_dehydrate_none():
    dehydrator = Dehydrator(specs=DUMMY_SPECS)
    result = dehydrator.dehydrate(None)
    assert result is None
def test_dehydrate_none_with_fallback():
    fallback_object = object()
    dehydrator = Dehydrator(specs=DUMMY_SPECS, empty=fallback_object)
    result = dehydrator.dehydrate(None)
    assert result is fallback_object
def test_base_dehydrator_init():
    assert Dehydrator().specs == ()
    assert Dehydrator(specs=DUMMY_SPECS).specs == DUMMY_SPECS