def test_validate_runnable_query_with_threshold_operator(threshold_operator):
    # given
    step = Step(uuid='uuid',
                order=1,
                kind='ASSET',
                threshold=Threshold(operator=threshold_operator, value="10"))
    query = Query(uuid='uuid', steps=[step])
    # when / then
    validate_runnable_query(query)
def test_validate_runnable_query_duration_valid(duration):
    # given
    step = Step(uuid='uuid',
                order=1,
                kind='ASSET',
                compare_duration=duration,
                threshold=Threshold(operator="le", value="10"))
    query = Query(uuid='uuid', steps=[step])
    # when / then
    validate_runnable_query(query)
def test_validate_runnable_query_ref_time_timestamp_valid(timestamp):
    # given
    ref_time = ReadTime(_type='TIMESTAMP', value=timestamp, zone=None)
    step = Step(uuid='uuid',
                order=1,
                kind='ASSET',
                read_time=ref_time,
                threshold=Threshold(operator="ge", value="10"))
    query = Query(uuid='uuid', steps=[step])
    # when / then
    validate_runnable_query(query)
def test_validate_runnable_query_ref_time_from_now_valid(duration):
    # given
    ref_time = ReadTime(_type='FROM_NOW', value=duration, zone=None)
    step = Step(uuid='uuid',
                order=1,
                kind='ASSET',
                read_time=ref_time,
                threshold=Threshold(operator="le", value="10"))
    query = Query(uuid='uuid', steps=[step])
    # when / then
    validate_runnable_query(query)
def test_validate_runnable_query_no_steps():
    # given
    uuid = '57e43773-7890-4530-a667-443089a90adc'
    query = Query(uuid=uuid)
    expected_error_key = 'steps'
    expected_error_value = {'message': 'Required at least one step'}
    with pytest.raises(QBValidationError) as ex:
        # when
        validate_runnable_query(query)
    # then
    assert len(ex.value.errors) == 1, 'Should return exactly 1 error'
    assert ex.value.errors[expected_error_key] == expected_error_value
def test_validate_runnable_query_no_threshold():
    # given
    uuid = '57e43773-7890-4530-a667-443089a90adc'
    step = Step(uuid=uuid, order=1, kind='ASSET')
    query = Query(uuid=uuid, steps=[step])
    expected_error_key = 'threshold1'
    expected_error_value = {'message': 'Threshold is empty on step #1'}
    with pytest.raises(QBValidationError) as ex:
        # when
        validate_runnable_query(query)
    # then
    assert len(ex.value.errors) == 1, 'Should return exactly 1 error'
    assert ex.value.errors[expected_error_key] == expected_error_value
def test_validate_runnable_query_ref_time_from_now_invalid(duration):
    # given
    ref_time = ReadTime(_type='FROM_NOW', value=duration, zone=None)
    step = Step(uuid='uuid',
                order=1,
                kind='ASSET',
                read_time=ref_time,
                threshold=Threshold(operator="le", value="10"))
    query = Query(uuid='uuid', steps=[step])
    expected_error_key = 'readTimeValue1'
    expected_error_value = {'message': 'Read time field invalid on step #1'}
    with pytest.raises(QBValidationError) as ex:
        # when
        validate_runnable_query(query)
    # then
    assert len(ex.value.errors) == 1, 'Should return exactly 1 error'
    assert ex.value.errors[expected_error_key] == expected_error_value
def test_validate_runnable_query_duration_invalid(duration):
    # given
    step = Step(uuid='uuid',
                order=1,
                kind='ASSET',
                compare_duration=duration,
                threshold=Threshold(operator="le", value="5"))
    query = Query(uuid='uuid', steps=[step])
    expected_error_key = 'compareDuration1'
    expected_error_value = {
        'message': 'Compare duration field invalid on step #1'
    }
    with pytest.raises(QBValidationError) as ex:
        # when
        validate_runnable_query(query)
    # then
    assert len(ex.value.errors) == 1, 'Should return exactly 1 error'
    assert ex.value.errors[expected_error_key] == expected_error_value
def test_validate_runnable_query_invalid_threshold_operator(operator):
    # given
    uuid = '57e43773-7890-4530-a667-443089a90adc'
    step = Step(uuid=uuid,
                order=1,
                kind='ASSET',
                threshold=Threshold(operator=operator, value='1'))
    query = Query(uuid=uuid, steps=[step])
    expected_error_key = 'thresholdOperator1'
    expected_error_value = {
        'message':
        "Threshold operator field invalid on step #1. Valid ['lt', 'le', 'eq', 'ne', 'ge', 'gt']"
    }
    with pytest.raises(QBValidationError) as ex:
        # when
        validate_runnable_query(query)
    # then
    assert len(ex.value.errors) == 1, 'Should return exactly 1 error'
    assert ex.value.errors[expected_error_key] == expected_error_value