xarray icon indicating copy to clipboard operation
xarray copied to clipboard

Stateful tests with Dataset

Open dcherian opened this issue 1 year ago • 5 comments

I was curious to see if the hypothesis stateful testing would catch an inconsistent sequence of index manipulation operations like #8646. Turns out rename_vars is basically broken? (filed #8659) :P

PS: this blog post is amazing.

E           state = DatasetStateMachine()
E           state.assert_invariants()
E           > ===
E
E            <xarray.Dataset>
E           Dimensions:  ()
E           Data variables:
E               *empty*
E           ===
E
E
E           > vars: ('1', '1_')
E           state.add_dim_coord(var=<xarray.Variable (1: 1)>
E           array([0], dtype=uint32))
E           state.assert_invariants()
E           > ===
E
E            <xarray.Dataset>
E           Dimensions:  (1: 1)
E           Coordinates:
E             * 1        (1) uint32 0
E           Data variables:
E               1_       (1) uint32 0
E           ===
E
E
E           > renaming 1 to 0
E           state.rename_vars(newname='0')
E           state.assert_invariants()
E           > ===
E
E            <xarray.Dataset>
E           Dimensions:  (1: 1)
E           Coordinates:
E             * 0        (1) uint32 0
E           Dimensions without coordinates: 1
E           Data variables:
E               1_       (1) uint32 0
E           ===
E
E
E           state.teardown()

dcherian avatar Jan 24 '24 16:01 dcherian

Oh this is a really cool idea!

Also yeah I've read that blog post before and my mind was also blown haha

TomNicholas avatar Jan 24 '24 16:01 TomNicholas

Great idea! Really cool.

I even wonder to what extent this could replace vast swathes of our tests — anything where the output is calculated from the input.

We'll always want some unit tests to code to when developing, but plausibly the unit tests are either a) examples or b) some saved cases that have failed these model-based tests before.

max-sixty avatar Jan 24 '24 19:01 max-sixty

That looks very cool indeed! (although I don't think that rename_vars is broken :) but instead some valid cases require skipping the default indexes invariant check).

benbovy avatar Jan 25 '24 14:01 benbovy

instead some valid cases require skipping the default indexes invariant check.

Can we write down rules for when these checks are needed?

dcherian avatar Jan 25 '24 16:01 dcherian

Yes although I think it is easier to write rules for when these checks aren't needed.

One example: skip the default indexes invariant test when the name of an existing dimension coordinate is passed as input kwarg or dict key to .rename_vars().

benbovy avatar Jan 26 '24 08:01 benbovy

This seems to have found at least 4 sort-of-edge-case failures for swap_dims:

  1. swapping a dimension to itself
  2. swapping from Index to a MultiIndex level
  3. swapping from MultiIndex to a level of the same MultiIndex
  4. swapping from Index to MultiIndex variable.

dcherian avatar Mar 15 '24 23:03 dcherian

Hah now our strategy tests are failing

dcherian avatar Apr 02 '24 01:04 dcherian

Thanks for the review @Zac-HD

dcherian avatar Apr 03 '24 21:04 dcherian