Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix arrayMock exceeds max allowed #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gaboesquivel
Copy link

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

@subeeshcbabu-zz
Copy link
Owner

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
Copy link
Author

gaboesquivel commented Aug 31, 2016

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
Copy link
Author

gaboesquivel commented Aug 31, 2016

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:

  • maxItems and minItems will never be 0
  • you want at least 2 items in your mocked array

@subeeshcbabu-zz
Copy link
Owner

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

subeeshcbabu-zz pushed a commit to subii/swagmock that referenced this pull request Sep 2, 2016
@subeeshcbabu-zz
Copy link
Owner

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants