subeeshcbabu-zz / swagmock

Mock data generator for swagger api
MIT License
173 stars 38 forks source link

fix arrayMock exceeds max allowed #16

Open gaboesquivel opened 7 years ago

gaboesquivel commented 7 years ago

there's bug were arrayMock generates 1 item more than the specified in maxItems this solves this problem.

subeeshcbabu-zz commented 7 years ago

Thanks for the PR. good catch. Wondering if simply starting the min=1 would solve this issue or not? for (; arr.length < max; min++) { arr.push(mock(items)); } ^ here the idea of min++ has no effect on the iteration.

Starting min=1 would solve both min and max use case I believe.

gaboesquivel commented 7 years ago

It won't pass the tests if min=1 and max=1 and for(; min < max; min++) nor (; min <= max; min++) it needs tests for maxItems use case, I don't have much time tho.

gaboesquivel commented 7 years ago

this seems more accurate

function arrayMock(schema) {
    var items = schema.items;
    var arr = [];

    if (items) {
        let min = schema.minItems || 1;
        let max = schema.maxItems || 2;

        // max needs to be iqual or greater than min
        max = ( min > max)? min : max;

        while ( arr.length < max) {
            arr.push(mock(items));
        }
    }
    return arr;
}

If max default is 1 the tests will fail

  27 passing (4s)
  2 failing

  1) Parameter Mock generator should generate parameter mock for path /user/createWithArray:
     Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

  2) Request Mock generator should generate request mock for path /user/createWithArray:
     Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

it seems these tests expect 2 items in the response, but max=1 when executed.
That's why max default has to be 2

Assumptions:

subeeshcbabu-zz commented 7 years ago

I see, lemme fix the tests to look for only one item by default.

subeeshcbabu-zz commented 7 years ago

@gaboesquivel - #17 - added these additional checks and boundary conditions, If makes sense, can you help verify? Also fixed the test case and added additional test cases.