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

Add unit tests to increase code coverage for query generation #72

Merged

Conversation

antekresic
Copy link
Contributor

Making a small PR for unit tests to give people a chance to review and comment on testing style.

Once this one gets approved and merged, expect a bigger PR covering more code using the same style.

@codecov-io
Copy link

Codecov Report

Merging #72 into master will increase coverage by 3.19%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #72      +/-   ##
==========================================
+ Coverage   53.93%   57.13%   +3.19%     
==========================================
  Files          76       76              
  Lines        3719     3719              
==========================================
+ Hits         2006     2125     +119     
+ Misses       1690     1570     -120     
- Partials       23       24       +1
Impacted Files Coverage Δ
cmd/tsbs_generate_queries/uses/devops/common.go 96.82% <0%> (+9.52%) ⬆️
...s_generate_queries/databases/timescaledb/devops.go 98.61% <0%> (+78.47%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 19932e7...c8d8859. Read the comment docs.

@codecov-io
Copy link

codecov-io commented May 16, 2019

Codecov Report

Merging #72 into master will increase coverage by 3.19%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #72      +/-   ##
==========================================
+ Coverage   53.93%   57.13%   +3.19%     
==========================================
  Files          76       76              
  Lines        3719     3719              
==========================================
+ Hits         2006     2125     +119     
+ Misses       1690     1570     -120     
- Partials       23       24       +1
Impacted Files Coverage Δ
cmd/tsbs_generate_queries/uses/devops/common.go 96.82% <0%> (+9.52%) ⬆️
...s_generate_queries/databases/timescaledb/devops.go 98.61% <0%> (+78.47%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 19932e7...fc4ba15. Read the comment docs.

Copy link
Contributor

@atanasovskib atanasovskib left a comment

Choose a reason for hiding this comment

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

The code seems good. I have a few questions not related to the changes, but more general

expectedHumanLabel: "TimescaleDB last row per host",
expectedHumanDesc: "TimescaleDB last row per host",
expectedHypertable: "cpu",
expectedSQLQuery: "SELECT DISTINCT ON (hostname) * FROM cpu ORDER BY hostname, time DESC",
Copy link
Contributor

Choose a reason for hiding this comment

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

I noticed that the LastPointPerHost queries do not depend on the qi arugment of the function, solely on the state of the Devops struct. They use Sprintf with no args, which can be removed.

Also, I'm not sure but since the query is a complete string, can we keep it as a constant in devops.go and reference the same constant here in the tests?

Copy link
Contributor

Choose a reason for hiding this comment

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

And with queries that are not constant but have specific parts that are filled in, I suggest that we keep the fixed parts as templates in constant, and then format them with the generated values.

This way the tests will be a lot shorter and all the queries will be in one place in one file for a quick review

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My reasoning was keeping the same style of test for all the query generation functions. Thats why I'm testing a fixed string (which could change to a dynamic one in the future).

As for making the tests shorter by reusing the templates, I guess you would have to basically rewrite all the logic in the function by reusing the same functions used to fill out the template and the test would be mirroring the function in test. I'm not fond of that approach since you are just writing the same logic twice and checking that it generates the same output. I decided to go this way so we can at least see the whole query visually in the test to check for sanity.

I, also, wanted to make a point of not changing the current code until we have some test coverage. After that we can introduce some refactoring at which point we can refactor the tests if there is a need for it.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I do like the approach for now of having the whole query, even if it is a bit cluttered. I think @blagojts 's suggestions for that function are good ones for a follow up PR (since as @antekresic mentioned, we will then have tests to cover it in case the refactor goes wrong). Those changes being making the strings consts and not use Sprintf for no reason.

Copy link
Member

@RobAtticus RobAtticus left a comment

Choose a reason for hiding this comment

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

Couple of small changes, but overall I like the approach.

Additionally consider adding an extended description to the commit with something like

For now the tests are mainly matching the output against pre-generated/known
outputs to ensure we have some coverage. A more robust checking, e.g., making
sure the semantics of the query are actually correct, is a future task.

expectedHumanLabel: "TimescaleDB last row per host",
expectedHumanDesc: "TimescaleDB last row per host",
expectedHypertable: "cpu",
expectedSQLQuery: "SELECT DISTINCT ON (hostname) * FROM cpu ORDER BY hostname, time DESC",
Copy link
Member

Choose a reason for hiding this comment

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

Yeah I do like the approach for now of having the whole query, even if it is a bit cluttered. I think @blagojts 's suggestions for that function are good ones for a follow up PR (since as @antekresic mentioned, we will then have tests to cover it in case the refactor goes wrong). Those changes being making the strings consts and not use Sprintf for no reason.

@antekresic antekresic force-pushed the maint/generate-query-unit-tests branch from c8d8859 to bd7112f Compare May 17, 2019 12:53
@antekresic
Copy link
Contributor Author

Thanks for the feedback @blagojts and @RobAtticus .

I incorporated your suggestions and force pushed the commit with matching extended description.

Let me know if there is anything else blocking us from merging this.

@antekresic antekresic force-pushed the maint/generate-query-unit-tests branch from bd7112f to be70914 Compare May 17, 2019 13:15
Copy link
Member

@RobAtticus RobAtticus left a comment

Choose a reason for hiding this comment

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

Few changes

For now the tests are mainly matching the output against pre-generated/known
outputs to ensure we have some coverage. A more robust checking, e.g., making
sure the semantics of the query are actually correct, is a future task.
@antekresic antekresic force-pushed the maint/generate-query-unit-tests branch from be70914 to fc4ba15 Compare May 18, 2019 05:28
@antekresic
Copy link
Contributor Author

@RobAtticus fixed all the durations. Should be good to go now.

@antekresic antekresic merged commit 3cfaae0 into timescale:master May 20, 2019
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.

4 participants