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 counter vizualization #4385

Merged
merged 5 commits into from
Dec 10, 2019
Merged

Conversation

dmudro
Copy link
Contributor

@dmudro dmudro commented Nov 22, 2019

What type of PR is this? (check all applicable)

  • Add Unit Tests
  • Bug Fix

Description

Small change in behaviour of the counter visualisation as we want to always display a value when count rows option is enabled; even when the value is 0 (i. e. 0 rows in the underlying data set). Introduces tests for logic contained in utils, though there is more to be done in this area (another PR coming as it touches a more general point).

Related Tickets & Documents

Closes #4265.

@@ -0,0 +1,168 @@
import { getCounterData } from './utils';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not able to stub getCounterData and other private functions in utils at the moment. Will be addressed in another PR.

Copy link
Member

Choose a reason for hiding this comment

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

Why would you want to stub it?

Copy link
Contributor Author

@dmudro dmudro Nov 24, 2019

Choose a reason for hiding this comment

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

I meant to say 'getRowNumber and other private functions'. Stubbing (mocking) would depend on the testing strategy though.

Atm all these 'private' functions including getRowNumber, numberFormat, formatValue etc:

  1. Contain logic and are not covered. This is fixable by simply exporting and running cases on them.
  2. From a puristic perspective, when executing tests on getCounterData one would want to mock the calls to these private functions to isolate the test subject. Otherwise it's more of an integration testing (which is not too bad anyway). This type of mocking is not an easy one in React as it does not have dependency injection and available solutions are cumbersome or would require changing coding style.

I assume it's ok to test on integration level and gradually add tests for other functions.

Copy link
Member

Choose a reason for hiding this comment

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

I meant to say 'getRowNumber and other private functions'.

Oh, ok. This makes sense now :-)

I assume it's ok to test on integration level and gradually add tests for other functions.

It's indeed ok. We're not purists here but rather pragmatists :-) So even having just a test for the getCounterData function is enough considering its scope/size.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're not purists here but rather pragmatists :-)

Nice one. Aligned.

@dmudro dmudro marked this pull request as ready for review November 22, 2019 10:32
Copy link
Member

@arikfr arikfr left a comment

Choose a reason for hiding this comment

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

Thank you for fixing this! Taking the extra effort of adding the tests is appreciated 🙇‍♂️

@@ -70,31 +70,27 @@ function formatTooltip(value, formatString) {
return toString(value);
}

export function getCounterData(rows, options, visualizationName) {
export function getCounterData(rows = [], options = {}, visualizationName = '') {
Copy link
Member

Choose a reason for hiding this comment

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

Why add the default values here? This function has a very specific usage and will always get these values.

If it's for the tests, then it's better to add the defaults in the tests... Will also make sure we're actually testing real use cases.

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 agree refactoring code base for the sole sake of testing is generally not ideal.

rows = [] was meant to protect against calling getCounterData() at runtime which would trow TypeError on rows.length (and since there is no type checking at build time). Fair enough such case is not realistic and rows should be a mandatory param.

Same for options = {} although the nature of options may suggest they should be optional. In that sense keeping the default value is not just for tests and the same is true for visualizationName = '' which one may want to fallback to an empty string.

Would following amended function signature do the job here?:
function getCounterData(rows, options = {}, visualizationName = '')

Copy link
Member

Choose a reason for hiding this comment

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

There will always be options and a visualization name... I'm actually not sure about rows as I don't know what happens when we're still waiting for the results to load. But having a default value won't help in such case anyway, because we definitely pass some value, it just might be empty.

My suggestion is to revert the function definition to be as it was to make it clear that it needs these values, but for convenience in the test you can wrap it with a function that has default values.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok cool, default param values removed

@@ -0,0 +1,168 @@
import { getCounterData } from './utils';
Copy link
Member

Choose a reason for hiding this comment

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

Why would you want to stub it?

@dmudro
Copy link
Contributor Author

dmudro commented Nov 24, 2019

Thank you for fixing this! Taking the extra effort of adding the tests is appreciated 🙇

Am not a huge fan of putting an extreme emphasis on tests but here the code with logic has been begging for decent coverage 🤓.

On related note getCounterData() seems to be slightly bloated and may benefit from splitting in more atomic parts, perhaps while addressing #4366. Wondering if values formatting could be done outside, i. e. in the React component.

Copy link
Member

@gabrieldutra gabrieldutra left a comment

Choose a reason for hiding this comment

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

Thanks @dmudro, I'll check quick wins for getCounterData later on #4392, so far I removed the getRowNumber in favor of lodash functions, thus it's one less function to worry about.

{ city: 'Tokyo', population: 38140000 },
],
options: {},
visualisationName: 'Visualisation Name',
Copy link
Member

@gabrieldutra gabrieldutra Nov 29, 2019

Choose a reason for hiding this comment

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

visualisation -> visualization 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch and both forms are correct in terms of grammar if it's what you meant. These are just test dummies; might as well be foo name. Please feel free to rename to whatever you prefer.

Copy link
Member

Choose a reason for hiding this comment

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

It was actually to keep it as is in the rest of the codebase, but NP, I'll revisit those tests in #4392.

Copy link
Member

@gabrieldutra gabrieldutra left a comment

Choose a reason for hiding this comment

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

@arikfr got any more comments? A note is that I plan to grow those tests in #4392 with the different aggregation functions :)

{ city: 'Tokyo', population: 38140000 },
],
options: {},
visualisationName: 'Visualisation Name',
Copy link
Member

Choose a reason for hiding this comment

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

It was actually to keep it as is in the rest of the codebase, but NP, I'll revisit those tests in #4392.

@arikfr arikfr merged commit 0385b6f into getredash:master Dec 10, 2019
@arikfr
Copy link
Member

arikfr commented Dec 10, 2019

Thank you @dmudro for the contribution and patience during the review :-)

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

Successfully merging this pull request may close these issues.

Counter Visualization: count rows never shows zero
4 participants