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

#2 push method #8

Merged
merged 36 commits into from
Jun 18, 2019
Merged

#2 push method #8

merged 36 commits into from
Jun 18, 2019

Conversation

josh-griffin
Copy link
Contributor

Fixes #2

Main addition:

  • Adds the surveyResponse method which will push data to the Tupaia server
  • Adds generic tests for all requests, based on status codes
  • Adds generic error lookup function for requests to use

Minor additions:

  • Adds a couple more error objects
  • Slightly altered the api config

Copy link
Contributor

@Chris-Petty Chris-Petty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there is a such thing as too many comments... naaaahhh

I didn't review the jest tests, is that bad?

src/errors/errorLookup.js Outdated Show resolved Hide resolved
src/errors/errorLookup.js Outdated Show resolved Hide resolved
src/errors/errorLookup.js Show resolved Hide resolved
src/errors/errorLookup.js Show resolved Hide resolved
src/utilities/requestUtilities.js Outdated Show resolved Hide resolved
src/utilities/requestUtilities.js Outdated Show resolved Hide resolved
src/utilities/requestUtilities.js Show resolved Hide resolved
src/utilities/requestUtilities.js Outdated Show resolved Hide resolved
@josh-griffin
Copy link
Contributor Author

I wouldn't mind more thoughts on the comments if you had time - did they help you, with little understanding of much else, understand the code easily? Or were they just annoying? (I like comments for functions which are obscure/dependent on strange inputs), still trying to find a balance!

Also it's fine with the tests - I was going to add more but didn't want to make the PR too big, prob make an issue for more - i.e. with malformed input etc, rather than just status codes

@josh-griffin
Copy link
Contributor Author

Just a note in case I forget. The API has changed and now returns 400 status codes when an object is invalid. Requires changes to this method that should be done in this PR. I think setting valid status codes (responses which will not throw an error) as 200 and 400 should be fine, but it has me a little worried

@Chris-Petty
Copy link
Contributor

Chris-Petty commented Jun 14, 2019

Just a note in case I forget. The API has changed and now returns 400 status codes when an object is invalid. Requires changes to this method that should be done in this PR. I think setting valid status codes (responses which will not throw an error) as 200 and 400 should be fine, but it has me a little worried

I'm not 100% up to speed, but maybe 422 would be accurate? This is largely internal right? So long as it's well documented it's probably not a big bother if it's not the absolute best option (if figuring that out takes excessive amounts of time)

https://www.bennadel.com/blog/2434-http-status-codes-for-invalid-data-400-vs-422.htm

src/utilities/requestUtilities.js Outdated Show resolved Hide resolved
src/utilities/requestUtilities.js Outdated Show resolved Hide resolved
@josh-griffin
Copy link
Contributor Author

I didn't actually know about 422, that would def be ideal. The 400 response is what is sent from the API, which I don't have any control over (they're probably sick of our suggestions already), so it will just send a 400.

@josh-griffin
Copy link
Contributor Author

Added some tests for the utlity method and request method.. sorry :(

invalidObjects.push(invalidDatum);
});
return { data: originalDataClone, invalidData: invalidObjects };
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please provide possible erroneous response in a comment below

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess i can look it up in testData/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% what you want here, you mean responses from the API that are errors? Maybe best for the wiki? I haven't added any to the test cases as that case essentially is testing the helper method (Also hard to mock default import of axios twice in one test). Hard to find a balance for number of tests written - I think trying to go for 100% coverage is a bit of a trap

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just needed an example of erroneous response, but found/figured it out

// is an error message.
const { row } = errorObject;
// If the row value is valid, remove the index from the original data
if (!Number.isInteger(Number(row)) || row >= invalidData.length) return;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

originalData.length ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if so, maybe should add index out of bound to removeInvalidObjectsTestData test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

49509f4 have added a check here

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i mean instead of row >= invalidData.length, should be row >= originalData.length ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh .. well the other one was needed also! heh.

Added test cases: bdab5fe 7532704

and solution 7d6e3d4

const { row } = errorObject;
// If the row value is valid, remove the index from the original data
if (!Number.isInteger(Number(row)) || row >= invalidData.length) return;
const [invalidDatum] = originalDataClone.splice(row, 1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try this method with removeInvalidObjectsTestData like this:

 TEST_FIVE: {
    INPUT: { originalData: [1, 2, 3], invalidData: { errors: [{ row: 0 }, { row: 2 }] } },
    OUTPUT: { data: [2], invalidData: [1,3] },
  },

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0ba216b I added a couple more test cases - one for the above comment and also this case (Although I'm not 100% sure what it was going for?)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does that test run fine ? i think it would fail, once you splice a row from originalDataClone, it becomes length of 2 ? then try to splice an index that doesn't exist (2)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yeah it does run fine - it first sorts the invalid data by row in descending order. So we would splice 2 first, then 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

// which lists the indicies which are invalid with a status code of
// 200.
const { data: response } = await Axios(apiConfig);
const { errors } = response;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we check for presence of "error" (just in case API response has unknown error with still 200 reponse)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8a4efa9 added this here

Copy link

@andreievg andreievg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joshxg, looks good to me, merge at will

@josh-griffin josh-griffin merged commit e68f1b3 into development Jun 18, 2019
@josh-griffin josh-griffin deleted the #2-push-method branch June 18, 2019 02:23
@Chris-Petty
Copy link
Contributor

Lol this PR doubled since my last review. Most tested utility method eva

* 7 : calling with an empty, but existing, errors array
* 8 : calling with an error index being out of range
*/
test('Should pass all tests', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of Jest output, isn't this just going to show:
❎ Should pass all tests
i.e. no context

Ages ago I went through the Jasmine docs as I think it has a nice way of presenting making a test suite (JEST basically took the same API): https://jasmine.github.io/tutorials/your_first_suite

Or more specifically just the docs for describe in Jest here: https://jestjs.io/docs/en/api#describename-fn

In that case it'd be

describe('removeInvalidObjects', () => {
  test('removes all data, as all have errors', () => {
    const result = removeInvalidObjects([1, 2, 3], { errors: [{ row: 0 }, { row: 1 }, { row: 2 }] })
    expect(result).toEqual([3, 2, 1])
  }
  test('removing one object, as only one has an error', () => {
  ...
  }
  ...
}

That'd make the test output a bit more useful/typical I'd presume. Also not sure what affect your forEach might have on Jest's multithread support. Ew I just went in a not so relevant rabbit hole jestjs/jest#6694

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting rabbit hole! (I hope this doesn't get to 7,000 tests :( )

Regarding the tests though: You're right on the description, it will just show no real context. I have been wanting to look more into testing and making suites, I've just been doing it very un-researched currently.

As for the forEach, I didn't look into any implications, it just seemed like a nice easy way to run, add, modify tests. Definitely given me lots to look into though!

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.

3 participants