Invalid default values
One resource runs into an issue that I haven't had before and cannot solve. I've already updated all dependencies and removed the _build directory without success.
The resource has some attributes
which are accepted by the create action. I use a simple generator to create records for testing:
When generating a record (
generate(scheme(type: :invoice, tenant: organization))
), my test sometimes succeed but mostly fail, because I get weird values for the default values:
This happens for all three fields and is not predictable even when using mix test --seed 0
.
11 Replies
It generates effectively random values for fields
What makes that a problem? Do you have validation somewhere checking the values?
Regardless, you can provide the
defaults
option and customize the default valuesIt's only an issue for the
pattern
attribute. We checked agains nil
first to use a default value first and moved it into the attribute's default
value now but that doesn't seem to get used.
Shouldn't the default values get used?Oh, hm...yes you're right
What version of Ash are you on?
Latest release (3.5.15)
I've thought about it and default values are quite tricky, I think. I can see how it's very powerful to test random values and for fields without a default value just setting a
nil
default seems to be a good compromise, even though it's a little bit counter-intuitive as not passing a value to a regular changeset sets it to nil
or the default.
When an attribute does have a default value though, it feels more difficult. Currently one would have to set the default value manually, but how could I test the default value then? If this changes and the default value would be used, how could I switch back to random values?
One solution I see would be to require an explicit nil
default in the generator, which would then use the actual default value to generate the record
Maybe there's a more explicit solution like setting :_default
but it's quite ugly.
With a more explicit solution, maybe another "unset" value could be added as well, so a generator default would be removed? (:_unset
or :_random
)
So
would create
I think the bug here is simpler
We do test defaults IIRC
By sometimes including the input and sometimes not
So the question is why is it a problem to sometimes not use the default value?
Wouldn't this mean that other values never get generated? This seems to be agains the idea of StreamData
No I mean we sometimes generate the map with no key for that value
And sometimes with a random value
So this means there's no bug and I have to explicitly set the default value in tests when I expect it?
Yeah, I think so?
Makes sense. Still, what about a way to "unset" defaults? This would allow to use a generator with a default value and still let it generate any value
Yeah, I think we can reuse the pattern from
Ash.Seed