mercredi 3 octobre 2018

Hypothesis strategy: for each "bucket", draw one value from the bucket

I have the following method for generating random data in one of my tests:

import random

data_categories = {
    'a': [1, 2, 3],
    'b': [4, 5],
    'c': [6, 7, 8]
}

def make_record():
    return [random.choice(vals) for vals in data_categories.values()]

How can I convert this to a Hypothesis strategy? I'm aware of hypothesis.strategies.composite but I'm not really sure I'm using it right.

This is my attempt, but it's hard to know if I'm using it correctly:

import hypothesis.strategies as hs

@hs.composite
def make_record(draw):
    return [draw(hs.sampled_from(kcs)) for kcs in column_kc_mapping.values()]

Aucun commentaire:

Enregistrer un commentaire