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

Adding unit tests to statistics #43

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jamescrowley
Copy link

@jamescrowley jamescrowley commented Sep 20, 2024

Replaces #35 to solve half of #32

I made some changes from @Phuc-Hong-Tran's approach, hope this is ok @chienleng

  1. I added tests to try and capture intended behaviour more explicitly, rather than relying on large JSON files and a large expected output where (at least as someone new to the project), it wasn't clear.
  2. Added tests for Statistic class itself, total minus loads.
  3. Tidied up some of the type definitions to remove errors
  4. Fixed a condition that allowed for an infinite loop for interpolate (last commit)
  5. Added a test coverage npm command
  6. I placed the tests alongside their corresponding file (personal preference as for me makes it much clearer which files have corresponding tests, and they are right there when editing) - happy to switch if you disagree.

I should also say

  • I'm not entirely clear on the intended behaviour for the interpolate-data steps (and correspondingly in the Statistic class). I've added tests but it would be good to review that the inputs/outputs actually make sense, or if there can be some more meaningful examples.
  • I have not yet added any tests for timeseries
  • If you have any preference re unit tests on the individual functions, vs the 'public' interface boundary of Statistics let me know - I ended up doing both but personally would prioritise the latter if doing time series, in hindsight.

@jamescrowley jamescrowley force-pushed the 32-Adding-unit-tests-to-Statistics branch from e3f8d11 to 3b83bdb Compare September 20, 2024 06:33
expect(statistic.data[0].history.last).toEqual(data[0].forecast.last);
expect(statistic.data[1].forecast).toEqual(data[1].forecast);
expect(statistic.data[1].history).not.toEqual(data[1].history);
// TODO: don't fully understand the intended behaviour here
Copy link
Author

Choose a reason for hiding this comment

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

See here re intended behaviour. Why 4 values? Why does last get set to 09:38?

Copy link
Member

Choose a reason for hiding this comment

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

not entirely sure, but I am guessing this is trying to test a 1min interval interpolation.

Copy link
Author

Choose a reason for hiding this comment

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

@chienleng yes - I wrote the test, in case that wasn't clear :D This was just related to not fully understanding the intent behind the behaviour around start/last and the number of interpolated values - and therefore harder to write a meaningful test for it. So long as you think the test actually makes sense, then 👍

Copy link
Member

Choose a reason for hiding this comment

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

ah, got it. :) Understood about the confusion of those values. The Stats class will need a refactor at some point, I am mostly porting this from a previous code base from OpenNEM.

I will take a closer look at reviewing this some time this week.

thanks for your help though.

@jamescrowley jamescrowley force-pushed the 32-Adding-unit-tests-to-Statistics branch from 3b83bdb to bc0fbba Compare September 20, 2024 06:35
it('returns interpolated data from 30 minutes interval to 5 minutes interval', () => {
const minInterval = parseInterval('5m');
const data = [
// TODO: not sure why we require this to be here for interpolation to occur?
Copy link
Author

Choose a reason for hiding this comment

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

And here - I wasn't clear why there needed to be another entry in the data array. Would be great to make the test explicit to explain further.

Copy link
Member

Choose a reason for hiding this comment

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

it's because the function only performs the interpolation if it detects there is a difference in the intervals returned. Will add that comment to the interpolate-data function.

Copy link
Author

@jamescrowley jamescrowley Sep 23, 2024

Choose a reason for hiding this comment

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

@chienleng thanks :) Given the desired minInterval is passed in, the context I think I'm missing was around the relationship of the start/end date of those StatsData objects that match the desired minInterval and the corresponding filtering - ie around https://github.com/opennem/openelectricity/pull/43/files/bc0fbba86875f99da82bcbcf2881b4f07c6e1946#diff-fc1bb786b10f9d5da7716d320830a20042c0a7e5b60670ff93f3f9798ad74beeR56-R66 - as that results in an empty data field being returned otherwise. This only stood out while trying to build test cases - I haven't yet dug into the broader usage when I'm sure this would be clear, so no need to address as such :)

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