Skip to content

Commit fb86776

Browse files
authored
feat: Adds max_string_length and max_array_items options (#54)
1 parent 965e02e commit fb86776

9 files changed

Lines changed: 257 additions & 117 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
```erl
1919
{restcheck, [
2020
{spec_path, string()},
21-
{spec_parser, module()}, % defaults to erf_oas_3_0
21+
{spec_parser, module()}, % defaults to erf_parser_oas_3_0
2222
{pbt_backend, module()}, % defaults to restcheck_triq
2323
{host, string()}, % defaults to "localhost"
2424
{base_path, string()}, % empty by default
@@ -30,6 +30,8 @@
3030
{num_requests, pos_integer()}, % defaults to 5000
3131
{retries, non_neg_integer()}, % defaults to 0
3232
{retry_interval, non_neg_integer()}, % defaults to 100
33+
{max_string_length, pos_integer()}, % defaults to 255
34+
{max_array_items, non_neg_integer()}, % defaults to 3
3335
{log_file, string()} % disabled by default
3436
]}.
3537
```

‎src/restcheck.erl‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
num_requests => pos_integer(),
4343
retries => non_neg_integer(),
4444
retry_interval => non_neg_integer(),
45+
max_string_length => pos_integer(),
46+
max_array_items => non_neg_integer(),
4547
output_fun => restcheck_pbt:output_fun()
4648
}.
4749
-type test_result() :: {OperationId :: binary(), Result :: ok | {error, Reason :: term()}}.
@@ -99,6 +101,8 @@ init(State) ->
99101
" {num_requests, pos_integer()} % number of requests per operation, defaults to 5000\n"
100102
" {retries, non_neg_integer()} % retries when the server is unreachable, defaults to 0\n"
101103
" {retry_interval, non_neg_integer()} % wait between retries in ms, defaults to 100\n"
104+
" {max_string_length, pos_integer()} % default max length for strings without maxLength, defaults to 255\n"
105+
" {max_array_items, non_neg_integer()} % default max items for arrays without maxItems, defaults to 3\n"
102106
" {log_file, string()} % path to file where timestamped events are logged, disabled if no path is provided\n"
103107
"]}."}
104108
]),
@@ -125,6 +129,8 @@ do(State) ->
125129
num_requests => proplists:get_value(num_requests, RawConf, 100),
126130
retries => proplists:get_value(retries, RawConf, 0),
127131
retry_interval => proplists:get_value(retry_interval, RawConf, 100),
132+
max_string_length => proplists:get_value(max_string_length, RawConf, 255),
133+
max_array_items => proplists:get_value(max_array_items, RawConf, 3),
128134
auth => proplists:get_value(auth, RawConf, undefined)
129135
},
130136
LogFile = proplists:get_value(log_file, RawConf, undefined),
@@ -356,7 +362,7 @@ file_log(LogFile, Format, Args) ->
356362
Reason :: term().
357363
generate_and_load_suite(Conf) ->
358364
SpecPath = maps:get(spec_path, Conf),
359-
SpecParser = maps:get(spec_format, Conf, erf_parser_oas_3_0),
365+
SpecParser = maps:get(spec_parser, Conf, erf_parser_oas_3_0),
360366
case erf_parser:parse(SpecPath, SpecParser) of
361367
{ok, API} ->
362368
APIName = maps:get(name, API),
@@ -367,9 +373,14 @@ generate_and_load_suite(Conf) ->
367373
auth => maps:get(auth, Conf, undefined),
368374
timeout => maps:get(timeout, Conf, 5000)
369375
},
376+
GenerationOpts = #{
377+
recursion_max_depth => 5,
378+
max_string_length => maps:get(max_string_length, Conf, 255),
379+
max_array_items => maps:get(max_array_items, Conf, 3)
380+
},
370381
DTOs = maps:get(schemas, API, []),
371382
load_dtos(maps:to_list(DTOs)),
372-
{SuiteName, Suite} = restcheck_suite:generate(API),
383+
{SuiteName, Suite} = restcheck_suite:generate(API, GenerationOpts),
373384
ok = restcheck_suite:load(Suite),
374385
Tests = lists:flatmap(
375386
fun(Endpoint) ->

‎src/restcheck_backend.erl‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
%%%-----------------------------------------------------------------------------
2424
%%% BEHAVIOUR CALLBACKS
2525
%%%-----------------------------------------------------------------------------
26-
-callback dto(Schema) -> Generator when
26+
-callback dto(Schema, Opts) -> Generator when
2727
Schema :: restcheck_pbt:schema(),
28+
Opts :: restcheck_pbt:opts(),
2829
Generator :: restcheck_pbt:generator().
29-
%% Returns a DTO generator from a given schema.
30+
%% Returns a DTO generator from a given schema and generation options.
3031

3132
-callback forall(Generators, Prop) -> ForAll when
3233
Generators :: [restcheck_pbt:generator()],

‎src/restcheck_pbt.erl‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
%%% EXTERNAL EXPORTS
1919
-export([
20-
dto/2,
20+
dto/3,
2121
forall/3,
2222
noshrink/2,
2323
quickcheck/3,
@@ -28,6 +28,7 @@
2828
-type backend() :: restcheck_backend:t().
2929
-type generator() :: term().
3030
-type num_tests() :: pos_integer().
31+
-type opts() :: restcheck_triq:opts().
3132
-type output_fun() :: fun((string(), [term()]) -> ok).
3233
-type prop() :: fun((term()) -> true | {false, Reason :: term()}).
3334
-type property() :: term().
@@ -38,6 +39,7 @@
3839
backend/0,
3940
generator/0,
4041
num_tests/0,
42+
opts/0,
4143
output_fun/0,
4244
property/0,
4345
prop/0,
@@ -47,13 +49,15 @@
4749
%%%-----------------------------------------------------------------------------
4850
%%% EXTERNAL EXPORTS
4951
%%%-----------------------------------------------------------------------------
50-
-spec dto(Backend, Schema) -> Generator when
52+
-spec dto(Backend, Schema, Opts) -> Generator when
5153
Backend :: backend(),
5254
Schema :: schema(),
55+
Opts :: opts(),
5356
Generator :: generator().
54-
%% @doc Asks the given backend for a DTO generator from a given schema.
55-
dto(Backend, Schema) ->
56-
Backend:dto(Schema).
57+
%% @doc Asks the given backend for a DTO generator from a given schema and
58+
%% generation options.
59+
dto(Backend, Schema, Opts) ->
60+
Backend:dto(Schema, Opts).
5761

5862
-spec forall(Backend, Generators, Prop) -> ForAll when
5963
Backend :: backend(),

‎src/restcheck_suite.erl‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
%%% EXTERNAL EXPORTS
2222
-export([
23-
generate/1,
23+
generate/2,
2424
load/1,
2525
load/2
2626
]).
@@ -31,13 +31,14 @@
3131
%%%-----------------------------------------------------------------------------
3232
%%% EXTERNAL EXPORTS
3333
%%%-----------------------------------------------------------------------------
34-
-spec generate(API) -> Result when
34+
-spec generate(API, Opts) -> Result when
3535
API :: erf_parser:api(),
36+
Opts :: restcheck_pbt:opts(),
3637
Result :: {ModuleName, Suite},
3738
ModuleName :: module(),
3839
Suite :: t().
3940
%% @doc Generates a <code>restcheck</code> test suite from an API AST.
40-
generate(API) ->
41+
generate(API, Opts) ->
4142
ModuleHeader = erl_syntax:comment(?COPYRIGHT ++ [?NOTE]),
4243
ModuleName =
4344
erlang:binary_to_atom(
@@ -86,7 +87,8 @@ generate(API) ->
8687
erl_syntax:atom(dto),
8788
[
8889
erl_syntax:variable('Backend'),
89-
Schema
90+
Schema,
91+
erl_syntax:abstract(Opts)
9092
]
9193
)
9294
]
@@ -112,7 +114,8 @@ generate(API) ->
112114
erl_syntax:atom(dto),
113115
[
114116
erl_syntax:variable('Backend'),
115-
RequestBodySchema
117+
RequestBodySchema,
118+
erl_syntax:abstract(Opts)
116119
]
117120
)
118121
]

0 commit comments

Comments
 (0)