Ejemplo n.º 1
0
def test_timer():
    from ldap2pg.utils import Timer

    my = Timer()
    assert repr(my)

    # Init checks
    assert 0 == my.delta.seconds
    assert 0 == my.delta.microseconds

    # Just do nothing.
    with my:
        pass
    assert my.delta.microseconds

    # Ensure delta is increased.
    first = my.delta.microseconds
    with my:
        # For the test, we only need to waste 1ms. Actually the syscall is
        # enough.
        sleep(0.00001)
    assert my.delta.microseconds > first

    # Time iteration
    for _ in my.time_iter(iter([0, 1])):
        pass
Ejemplo n.º 2
0
def test_synchronize(mocker):
    from ldap2pg.utils import Timer
    PSQL = mocker.patch('ldap2pg.script.PSQL', autospec=True)
    PSQL.return_value.timer = Timer()
    clc = mocker.patch('ldap2pg.script.ldap.connect')
    SM = mocker.patch('ldap2pg.script.SyncManager', autospec=True)
    manager = SM.return_value
    manager.sync.return_value = 0

    from ldap2pg.script import synchronize, Configuration

    config = mocker.MagicMock(name='config', spec=Configuration)
    # Dry run
    config.get.return_value = True
    synchronize(config=config)

    # Real mode
    config.get.return_value = False
    PSQL.return_value.return_value = mocker.MagicMock(name='psql')
    synchronize(config=config)

    assert clc.called is True
    assert manager.sync.called is True

    # No LDAP
    clc.reset_mock()
    config.has_ldapsearch.return_value = []
    synchronize(config=config)

    assert clc.called is False
Ejemplo n.º 3
0
def test_wrapped_main(mocker):
    from ldap2pg.utils import Timer
    mocker.patch('ldap2pg.script.dictConfig', autospec=True)
    PSQL = mocker.patch('ldap2pg.script.PSQL', autospec=True)
    PSQL.return_value.timer = Timer()
    clc = mocker.patch('ldap2pg.script.ldap.connect')
    SM = mocker.patch('ldap2pg.script.SyncManager', autospec=True)
    manager = SM.return_value
    manager.sync.return_value = 0

    from ldap2pg.script import wrapped_main

    config = mocker.MagicMock(name='config')
    # Dry run
    config.get.return_value = True
    wrapped_main(config=config)

    # Real mode
    config.get.return_value = False
    PSQL.return_value.return_value = mocker.MagicMock(name='psql')
    wrapped_main(config=config)

    assert clc.called is True
    assert manager.sync.called is True

    # No LDAP
    clc.reset_mock()
    config.has_ldap_query.return_value = []
    wrapped_main(config=config)

    assert clc.called is False
Ejemplo n.º 4
0
def test_timer():
    from ldap2pg.utils import Timer

    my = Timer()
    assert repr(my)

    # Init checks
    assert 0 == my.delta.seconds
    assert 0 == my.delta.microseconds

    # Just do nothing.
    with my:
        pass
    assert my.delta.microseconds

    # Ensure delta is increased.
    first = my.delta.microseconds
    with my:
        pass
    assert my.delta.microseconds > first

    # Time iteration
    for _ in my.time_iter(iter([0, 1])):
        pass