def test_redis_data_incr_without_arguments(capfd):
    set_module_args({})
    with pytest.raises(SystemExit) as results:
        redis_data_incr.main()
    out, err = capfd.readouterr()
    assert not err
    assert json.loads(out)['failed']
def test_redis_data_incr_float_wrong_value(capfd):
    set_module_args({
        'login_host': 'localhost',
        'login_user': '******',
        'login_password': '******',
        'key': 'foo',
        'increment_float': 'not_a_number'
    })
    with pytest.raises(SystemExit):
        redis_data_incr.main()
    out, err = capfd.readouterr()
    print(out)
    assert not err
    assert json.loads(out)['failed']
def test_redis_data_incr_no_username(capfd, mocker):
    set_module_args({
        'login_host': 'localhost',
        'login_password': '******',
        'key': 'foo',
    })
    mocker.patch('redis.Redis.incr', return_value=57)
    with pytest.raises(SystemExit):
        redis_data_incr.main()
    out, err = capfd.readouterr()
    print(out)
    assert not err
    assert json.loads(out)['value'] == 57.0
    assert json.loads(out)['msg'] == 'Incremented key: foo to 57'
    assert json.loads(out)['changed']
def test_redis_data_incr_check_mode(capfd, mocker):
    set_module_args({
        'login_host': 'localhost',
        'login_password': '******',
        'key': 'foo',
        '_ansible_check_mode': True
    })
    mocker.patch('redis.Redis.get', return_value=10)
    with pytest.raises(SystemExit):
        redis_data_incr.main()
    out, err = capfd.readouterr()
    print(out)
    assert not err
    assert json.loads(out)['value'] == 11.0
    assert json.loads(out)['msg'] == 'Incremented key: foo by 1 to 11.0'
    assert not json.loads(out)['changed']
def test_redis_data_incr_fail_username(capfd, mocker):
    set_module_args({
        'login_host': 'localhost',
        'login_user': '******',
        'login_password': '******',
        'key': 'foo',
        '_ansible_check_mode': False
    })
    with pytest.raises(SystemExit):
        redis_data_incr.main()
    out, err = capfd.readouterr()
    print(out)
    assert not err
    assert json.loads(out)['failed']
    assert json.loads(
        out
    )['msg'] == 'The option `username` in only supported with redis >= 3.4.0.'
def test_redis_data_incr_check_mode_not_incrementable(capfd, mocker):
    set_module_args({
        'login_host': 'localhost',
        'login_password': '******',
        'key': 'foo',
        '_ansible_check_mode': True
    })
    mocker.patch('redis.Redis.get', return_value='bar')
    with pytest.raises(SystemExit):
        redis_data_incr.main()
    out, err = capfd.readouterr()
    print(out)
    assert not err
    assert json.loads(out)['failed']
    assert json.loads(out)[
        'msg'] == "Value: bar of key: foo is not incrementable(int or float)"
    assert 'value' not in json.loads(out)
    assert not json.loads(out)['changed']
def test_redis_data_incr_check_mode_permissions(capfd, mocker):
    set_module_args({
        'login_host': 'localhost',
        'login_password': '******',
        'key': 'foo',
        '_ansible_check_mode': True
    })
    redis.Redis.get = mocker.Mock(side_effect=NoPermissionError(
        "this user has no permissions to run the 'get' command or its subcommand"
    ))
    with pytest.raises(SystemExit):
        redis_data_incr.main()
    out, err = capfd.readouterr()
    print(out)
    assert not err
    assert json.loads(out)['failed']
    assert json.loads(out)['msg'].startswith(
        'Failed to get value of key: foo with exception:')
    assert 'value' not in json.loads(out)
    assert not json.loads(out)['changed']