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

[Uptime] Add unit tests for QueryContext time calculation #56671

Merged
merged 2 commits into from
Feb 4, 2020

Conversation

andrewvc
Copy link
Contributor

@andrewvc andrewvc commented Feb 3, 2020

Add Unit tests for the QueryContext class that was missing testing. This would have caught #56612

@andrewvc andrewvc added technical debt Improvement of the software architecture and operational architecture Team:Uptime - DEPRECATED Synthetics & RUM sub-team of Application Observability v7.7.0 backport:skip This commit does not require backporting labels Feb 3, 2020
@andrewvc andrewvc self-assigned this Feb 3, 2020
@elasticmachine
Copy link
Contributor

Pinging @elastic/uptime (Team:uptime)

@andrewvc andrewvc added release_note:skip Skip the PR/issue when compiling release notes and removed backport:skip This commit does not require backporting labels Feb 3, 2020
Copy link
Contributor

@justinkambic justinkambic left a comment

Choose a reason for hiding this comment

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

Had a few recommendations and shared my opinion about the test paradigm outlined here.

qc.hasTimespan = mockHasTimespan;

expect(await qc.dateRangeFilter()).toEqual({
bool: {
Copy link
Contributor

Choose a reason for hiding this comment

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

This type of assertion is fine with me if we all agree to use/follow it. I personally would use a snapshot for this type of response. The spirit of the snapshot is that, I have evaluated its contents and I know it's what I want in the initial write. After that it should never change unless the module's expected output changes. It results in cleaner-looking test files IMO. But this assertion is effectively the same thing as what a snapshot does, so really it's a semantic preference more than anything else. I am fine either way; if this is more helpful for evaluating what's going on in the test we should do it.

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 think my preference goes the other way. If the output of timespanClause() changes, this test will still work and no snapshot will need to be updated. This test isn't testing timespanClause, there's a dedicated test for that, so changes to the logic there shouldn't affect the pass/fail nature of the test here.

Would you disagree that this property is a benefit?

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a pretty good point, I do agree that in this case we don't necessarily want to test the output of functionality that's tested elsewhere.

const makeQueryContext = () => {
return new QueryContext({}, rangeStart, rangeEnd, pagination, null, 10);
};
beforeEach(() => (qc = makeQueryContext()));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
beforeEach(() => (qc = makeQueryContext()));
beforeEach(() => (qc = new QueryContext({}, rangeStart, rangeEnd, pagination, null, 10)));

Could we just instantiate a new context directly here, since makeQueryContext isn't referenced anywhere else in the file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I left it there as a style preference, since I thought we might use it in the future, but I think you're right and YAGNI applies here.

range: {
'monitor.timespan': {
// end date minus 5m
gte: new Date(Date.parse(rangeEnd) - 5 * 60 * 1000).toISOString(),
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to me like it will be ok, we're simply modifying a constant value, not computing any relative time.

I view these types of assertions with healthy skepticism always. 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, the idea with using math here instead of a writing it out was to communicate the semantic intent and also to allow the value at the top of the file to change without breaking the test if that's useful for a future test.

Copy link
Contributor

Choose a reason for hiding this comment

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

++ I agree with the implementation. I just look at anything with date manipulation 3x more than the rest of the code in a given diff 😆

describe('when hasTimespan() is true', () => {
it('should create a date range filter including the timespan', async () => {
const mockHasTimespan = jest.fn();
mockHasTimespan.mockReturnValue(Promise.resolve(true));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
mockHasTimespan.mockReturnValue(Promise.resolve(true));
mockHasTimespan.mockReturnValue(true);

I don't think we need the Promise.resolve.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you sure? That function is async, so should always return a promise.

describe('when hasTimespan() is false', () => {
it('should only use the timestamp fields in the returned filter', async () => {
const mockHasTimespan = jest.fn();
mockHasTimespan.mockReturnValue(Promise.resolve(false));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
mockHasTimespan.mockReturnValue(Promise.resolve(false));
mockHasTimespan.mockReturnValue(false);

I think it's ok to just return the boolean here.

Copy link
Contributor

@justinkambic justinkambic left a comment

Choose a reason for hiding this comment

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

LGTM

range: {
'monitor.timespan': {
// end date minus 5m
gte: new Date(Date.parse(rangeEnd) - 5 * 60 * 1000).toISOString(),
Copy link
Contributor

Choose a reason for hiding this comment

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

++ I agree with the implementation. I just look at anything with date manipulation 3x more than the rest of the code in a given diff 😆

qc.hasTimespan = mockHasTimespan;

expect(await qc.dateRangeFilter()).toEqual({
bool: {
Copy link
Contributor

Choose a reason for hiding this comment

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

That's a pretty good point, I do agree that in this case we don't necessarily want to test the output of functionality that's tested elsewhere.

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@andrewvc andrewvc merged commit 653c28a into elastic:master Feb 4, 2020
@andrewvc andrewvc deleted the add-qc-unit-tests branch February 4, 2020 02:10
andrewvc added a commit to andrewvc/kibana that referenced this pull request Feb 4, 2020
)

Add Unit tests for the QueryContext class that was missing testing. This would have caught elastic#56612
gmmorris added a commit to gmmorris/kibana that referenced this pull request Feb 4, 2020
* master: (42 commits)
  Move kuery_autocomplete ⇒ NP (elastic#56607)
  [ML] Functional tests - stabilize job row and analytics result view assertions (elastic#56595)
  [Discover] Inline angular directives only used in this plugin (elastic#56119)
  [Discover] Migrate get_sort.js test from mocha to TypeScript (elastic#56011)
  [SIEM] Enable flow_target_select_connected unit tests (elastic#55618)
  Start consuming np logging config (elastic#56480)
  [SIEM] Add eslint-plugin-react-perf (elastic#55960)
  Mention changed SAML ACS endpoint URL in breaking changes doc. (elastic#56613)
  Add `getServerInfo` API to http setup contract (elastic#56636)
  Updates Monitoring alert Jest snapshots
  Kibana property config migrations (elastic#55937)
  Vislib replacement toggle (elastic#56439)
  [Uptime] Add unit tests for QueryContext time calculation (elastic#56671)
  [SIEM][Detection Engine] Critical blocker, fixes pre-packaged rule miscounts
  Upgrade EUI to v18.3.0 (elastic#56228)
  [Maps] Fix server log (elastic#56679)
  [SIEM] Fixes FTUE when APM node is present (elastic#56574)
  [Reporting/FieldFormats] expose `setFieldFormats` and call from ReportingPlugin.start (elastic#56563)
  Update EMS API urls for production (elastic#56657)
  Ability to delete alerts even when AAD is out of sync (elastic#56543)
  ...
andrewvc added a commit that referenced this pull request Feb 5, 2020
…56718)

Add Unit tests for the QueryContext class that was missing testing. This would have caught #56612

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
cauemarcondes pushed a commit to cauemarcondes/kibana that referenced this pull request Feb 5, 2020
)

Add Unit tests for the QueryContext class that was missing testing. This would have caught elastic#56612
majagrubic pushed a commit to majagrubic/kibana that referenced this pull request Feb 10, 2020
)

Add Unit tests for the QueryContext class that was missing testing. This would have caught elastic#56612
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release_note:skip Skip the PR/issue when compiling release notes Team:Uptime - DEPRECATED Synthetics & RUM sub-team of Application Observability technical debt Improvement of the software architecture and operational architecture v7.7.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants