Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_cost_map_is_used_to_calculate_query_cost(schema):
ast = parse("{ constant }")
rule = cost_validator(maximum_cost=1, cost_map=cost_map)
result = validate(schema, ast, [rule])
assert result == [
GraphQLError(
"The query exceeds the maximum cost of 1. Actual cost is 3",
extensions={"cost": {"requestedQueryCost": 3, "maximumAvailable": 1}},
)
def test_complex_field_cost_defined_in_map_is_multiplied_by_values_from_literal(schema):
query = "{ complex(valueA: 5, valueB: 6) }"
ast = parse(query)
rule = cost_validator(maximum_cost=3, cost_map=cost_map)
result = validate(schema, ast, [rule])
assert result == [
GraphQLError(
"The query exceeds the maximum cost of 3. Actual cost is 11",
extensions={"cost": {"requestedQueryCost": 11, "maximumAvailable": 3}},
)
def test_child_field_cost_defined_in_map_is_multiplied_by_values_from_variables(schema):
query = """
query testQuery($value: Int!) {
child(value: $value) { name online }
}
"""
ast = parse(query)
rule = cost_validator(maximum_cost=3, variables={"value": 5}, cost_map=cost_map)
result = validate(schema, ast, [rule])
assert result == [
GraphQLError(
"The query exceeds the maximum cost of 3. Actual cost is 20",
extensions={"cost": {"requestedQueryCost": 20, "maximumAvailable": 3}},
)
def test_child_field_cost_defined_in_directive_is_multiplied_by_values_from_variables(
schema_with_costs
):
query = """
query testQuery($value: Int!) {
child(value: $value) { name online }
}
"""
ast = parse(query)
rule = cost_validator(maximum_cost=3, variables={"value": 5})
result = validate(schema_with_costs, ast, [rule])
assert result == [
GraphQLError(
"The query exceeds the maximum cost of 3. Actual cost is 20",
extensions={"cost": {"requestedQueryCost": 20, "maximumAvailable": 3}},
)
def test_child_field_cost_defined_in_directive_is_multiplied_by_values_from_literal(
schema_with_costs
):
query = "{ child(value: 5) { name online } }"
ast = parse(query)
rule = cost_validator(maximum_cost=3)
result = validate(schema_with_costs, ast, [rule])
assert result == [
GraphQLError(
"The query exceeds the maximum cost of 3. Actual cost is 20",
extensions={"cost": {"requestedQueryCost": 20, "maximumAvailable": 3}},
)
def test_field_cost_defined_in_map_is_multiplied_by_value_from_literal(schema):
query = "{ simple(value: 5) }"
ast = parse(query)
rule = cost_validator(maximum_cost=3, cost_map=cost_map)
result = validate(schema, ast, [rule])
assert result == [
GraphQLError(
"The query exceeds the maximum cost of 3. Actual cost is 5",
extensions={"cost": {"requestedQueryCost": 5, "maximumAvailable": 3}},
)
def test_field_cost_defined_in_map_is_multiplied_by_value_from_variables(schema):
query = """
query testQuery($value: Int!) {
simple(value: $value)
}
"""
ast = parse(query)
rule = cost_validator(maximum_cost=3, variables={"value": 5}, cost_map=cost_map)
result = validate(schema, ast, [rule])
assert result == [
GraphQLError(
"The query exceeds the maximum cost of 3. Actual cost is 5",
extensions={"cost": {"requestedQueryCost": 5, "maximumAvailable": 3}},
)
def test_complex_field_cost_defined_in_directive_is_multiplied_by_values_from_variables(
schema_with_costs
):
query = """
query testQuery($valueA: Int!, $valueB: Int!) {
complex(valueA: $valueA, valueB: $valueB)
}
"""
ast = parse(query)
rule = cost_validator(maximum_cost=3, variables={"valueA": 5, "valueB": 6})
result = validate(schema_with_costs, ast, [rule])
assert result == [
GraphQLError(
"The query exceeds the maximum cost of 3. Actual cost is 11",
extensions={"cost": {"requestedQueryCost": 11, "maximumAvailable": 3}},
)
def test_field_cost_defined_in_directive_is_multiplied_by_value_from_variables(
schema_with_costs
):
query = """
query testQuery($value: Int!) {
simple(value: $value)
}
"""
ast = parse(query)
rule = cost_validator(maximum_cost=3, variables={"value": 5})
result = validate(schema_with_costs, ast, [rule])
assert result == [
GraphQLError(
"The query exceeds the maximum cost of 3. Actual cost is 5",
extensions={"cost": {"requestedQueryCost": 5, "maximumAvailable": 3}},
)
def test_field_cost_defined_in_directive_is_multiplied_by_value_from_literal(
schema_with_costs
):
query = "{ simple(value: 5) }"
ast = parse(query)
rule = cost_validator(maximum_cost=3)
result = validate(schema_with_costs, ast, [rule])
assert result == [
GraphQLError(
"The query exceeds the maximum cost of 3. Actual cost is 5",
extensions={"cost": {"requestedQueryCost": 5, "maximumAvailable": 3}},
)