How to generate potentially malformed uuids? #6319
-
|
I have a function that filters an array of strings to remove invalid uuids. To test this, I'm looking for a way to generate uuids that occasionally generates an invalid uuid. The |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
What would you consider as an ok generator? Actually not being a valid uuid is wide, a random string is not an uuid, an uuid-looking string with some "g"... In fast-check for now, one way to generate a non-uuid is to do something like that: fc.string().filter(v => isNotUuid(v))But you might be too far from an uuid. Otherwise you can play with things such as: fc.tuple(fc.string(), fc.uuid(), fc.string()).map(([s1, u, s2]) => `${s1}${u}${s2}`).filter(v => isNotUuid(v))Or even more advanced things where you take only a subpart of the u. You could also play with |
Beta Was this translation helpful? Give feedback.
-
|
How about randomly choosing between const sometimesUuid = fc.oneof(fc.string(), fc.uuid())With that you get a valid UUID ~50% of the time. If you want a different ratio, you can tweak the weight parameter. for (const item of fc.sample(sometimesUuid, 20)) {
console.debug(item)
}If you want the invalid UUIDs to only be broken in certain ways, you can additionally constrain // e.g. length should match valid UUID but we allow arbitrary characters:
const invalidUuid = fc.string({ minLength: 36, maxLength: 36 })
const sometimesUuid = fc.oneof(invalidUuid, fc.uuid())
for (const item of fc.sample(sometimesUuid, 20)) {
console.debug(item)
} |
Beta Was this translation helpful? Give feedback.
How about randomly choosing between
fc.stringandfc.uuid?With that you get a valid UUID ~50% of the time. If you want a different ratio, you can tweak the weight parameter.