Skip to content

Commit b572f20

Browse files
committed
chore: Use Hypothesis' InvalidArgument exception when an invalid input is passed to the generator functions
1 parent a0d8e26 commit b572f20

File tree

5 files changed

+13
-6
lines changed

5 files changed

+13
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- The `from_schema` function which takes a GraphQL schema and returns a Hypothesis strategy for defined queries and mutations.
88

9+
### Changed
10+
11+
- Use Hypothesis' `InvalidArgument` exception when an invalid input is passed to the generator functions.
12+
913
### Removed
1014

1115
- `hypothesis_graphql.schemas` as it is not complete and not tested well.

src/hypothesis_graphql/_strategies/strategy.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def queries(
374374
"""
375375
parsed_schema = validation.maybe_parse_schema(schema)
376376
if parsed_schema.query_type is None:
377-
raise ValueError("Query type is not defined in the schema")
377+
raise InvalidArgument("Query type is not defined in the schema")
378378
return (
379379
_make_strategy(parsed_schema, type_=parsed_schema.query_type, fields=fields, custom_scalars=custom_scalars)
380380
.map(make_query)
@@ -401,7 +401,7 @@ def mutations(
401401
"""
402402
parsed_schema = validation.maybe_parse_schema(schema)
403403
if parsed_schema.mutation_type is None:
404-
raise ValueError("Mutation type is not defined in the schema")
404+
raise InvalidArgument("Mutation type is not defined in the schema")
405405
return (
406406
_make_strategy(parsed_schema, type_=parsed_schema.mutation_type, fields=fields, custom_scalars=custom_scalars)
407407
.map(make_mutation)
@@ -454,5 +454,5 @@ def from_schema(
454454
if type_ is not None and (type_fields is None or len(type_fields) > 0)
455455
]
456456
if not strategies:
457-
raise ValueError("Query or Mutation type must be provided")
457+
raise InvalidArgument("Query or Mutation type must be provided")
458458
return reduce(or_, strategies)

test/test_from_schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from hypothesis import given
33
from hypothesis import strategies as st
4+
from hypothesis.errors import InvalidArgument
45

56
from hypothesis_graphql import from_schema
67

@@ -41,5 +42,5 @@ def test_from_schema(data, schema, validate_operation, types, available_fields):
4142

4243

4344
def test_no_query_no_mutation(schema, validate_operation):
44-
with pytest.raises(ValueError, match="Query or Mutation type must be provided"):
45+
with pytest.raises(InvalidArgument, match="Query or Mutation type must be provided"):
4546
from_schema(schema)

test/test_mutations.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from hypothesis import given
33
from hypothesis import strategies as st
4+
from hypothesis.errors import InvalidArgument
45

56
from hypothesis_graphql import mutations
67

@@ -33,5 +34,5 @@ def test_mutation(data, mutation, validate_operation, fields):
3334

3435

3536
def test_no_mutation(schema):
36-
with pytest.raises(ValueError, match="Mutation type is not defined in the schema"):
37+
with pytest.raises(InvalidArgument, match="Mutation type is not defined in the schema"):
3738
mutations(schema)

test/test_queries.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from graphql import GraphQLNamedType
44
from hypothesis import assume, find, given, settings
55
from hypothesis import strategies as st
6+
from hypothesis.errors import InvalidArgument
67

78
from hypothesis_graphql import nodes, queries
89
from hypothesis_graphql._strategies.strategy import GraphQLStrategy
@@ -172,7 +173,7 @@ def test_missing_query():
172173
schema = """type Author {
173174
name: String
174175
}"""
175-
with pytest.raises(ValueError, match="Query type is not defined in the schema"):
176+
with pytest.raises(InvalidArgument, match="Query type is not defined in the schema"):
176177
queries(schema)
177178

178179

0 commit comments

Comments
 (0)