Strategy for dictionary with optional keys

Milo

Currently I am using hypothesis fixed_dictionaries strategy to generate a dictionary with specific keys and data types that are considered valid for my application. I need a strategy which produces this fixed dictionary as well as others with specific keys removed. Or a dictionary with a certain minimal set of keys with optional additional ones, preferably in a way that produces the various combinations of these optional keys.

This is an example of the json schema that needs to be validated, with the 2 optional fields. I'd like to generate all possible valid data for this schema.

'user_stub': {
    '_id':        {'type': 'string'},
    'username':   {'type': 'string'},
    'social':     {'type': 'string'},
    'api_name':   {'type':     'string',
                   'required': False},
    'profile_id': {'type':     'integer',
                   'required': False},
}

This is what I came up with but it is incorrect because it retains the keys but uses None as the value, and I want instead that the keys are removed.

return st.fixed_dictionaries({
    '_id':        st.text(),
    'username':   st.text(),
    'social':     st.text(),
    'api_name':   st.one_of(st.none(),
                            st.text()),
    'profile_id': st.one_of(st.none(),
                            st.integers()),
})

EDIT: updated composite strategy ->

Seems like it would be best to separate the additional optional dictionaries based on the type of data being returned, otherwise might get keys with mismatched values.

@st.composite
def generate_data(draw):
    base_data = st.fixed_dictionaries({
        '_id':      st.text(),
        'username': st.text(),
        'social':   st.text(),
    })
    optional_strs = st.dictionaries(
        keys=st.just('api_name'),
        values=st.text()
    )
    optional_ints = st.dictionaries(
        keys=st.just('profile_id'),
        values=st.integers()
    )

    b = draw(base_data)
    s = draw(optional_strs)
    i = draw(optional_ints)
    return {**b, **s, **i}  # noice
das-g

Draw from a fixed_dictionaries strategy for the required entries and from a dictionaries strategy for the optional ones. Then combine them into one dictionary by merging them.

You can either do that in your test, or create a composite strategy that does that for you.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Swap keys for values in dictionary?

分類Dev

Moving Keys in dictionary

分類Dev

How to update keys in dictionary?

分類Dev

Sort dictionary keys where the key is also a dictionary

分類Dev

Compare keys of dictionary with values of another dictionary

分類Dev

Construct a dictionary from a list of dictionary keys, sub-keys and values

分類Dev

Assign list values to dictionary keys

分類Dev

Assigning values to keys in a mutable dictionary

分類Dev

Match keys to values in dictionary in python

分類Dev

How to get dictionary keys and values if both keys are in two separated dictionaries?

分類Dev

Creating a dictionary with values identical to keys from another dictionary

分類Dev

Merge two keys of a single dictionary in python

分類Dev

Python dictionary.keys()エラー

分類Dev

Python dictionary.keys()エラー

分類Dev

efficiency of long (str) keys in python dictionary

分類Dev

How to scrub a dynamic python dictionary of specific keys?

分類Dev

How are the keys selected in a dictionary while in iteration in Python

分類Dev

Access keys inside nested dictionary python

分類Dev

Print only odd dictionary keys and their items

分類Dev

Filling a dataframe from a dictionary keys and values

分類Dev

Zip two lists of strings to use as keys in dictionary

分類Dev

lookup/map multiple keys in a dictionary and add to a list

分類Dev

How to make Json Serialize ignore dictionary keys

分類Dev

Write a csv file from a dictionary with compound keys

分類Dev

retrieve values from dataframe using keys in dictionary

分類Dev

How to add multiple values to dictionary keys in Python

分類Dev

Python pandas: convert dictionary to DataFrame with keys as row

分類Dev

Edit nested dictionary duplicate values in same keys

分類Dev

How to make a dictionary in Python with files as keys?

Related 関連記事

  1. 1

    Swap keys for values in dictionary?

  2. 2

    Moving Keys in dictionary

  3. 3

    How to update keys in dictionary?

  4. 4

    Sort dictionary keys where the key is also a dictionary

  5. 5

    Compare keys of dictionary with values of another dictionary

  6. 6

    Construct a dictionary from a list of dictionary keys, sub-keys and values

  7. 7

    Assign list values to dictionary keys

  8. 8

    Assigning values to keys in a mutable dictionary

  9. 9

    Match keys to values in dictionary in python

  10. 10

    How to get dictionary keys and values if both keys are in two separated dictionaries?

  11. 11

    Creating a dictionary with values identical to keys from another dictionary

  12. 12

    Merge two keys of a single dictionary in python

  13. 13

    Python dictionary.keys()エラー

  14. 14

    Python dictionary.keys()エラー

  15. 15

    efficiency of long (str) keys in python dictionary

  16. 16

    How to scrub a dynamic python dictionary of specific keys?

  17. 17

    How are the keys selected in a dictionary while in iteration in Python

  18. 18

    Access keys inside nested dictionary python

  19. 19

    Print only odd dictionary keys and their items

  20. 20

    Filling a dataframe from a dictionary keys and values

  21. 21

    Zip two lists of strings to use as keys in dictionary

  22. 22

    lookup/map multiple keys in a dictionary and add to a list

  23. 23

    How to make Json Serialize ignore dictionary keys

  24. 24

    Write a csv file from a dictionary with compound keys

  25. 25

    retrieve values from dataframe using keys in dictionary

  26. 26

    How to add multiple values to dictionary keys in Python

  27. 27

    Python pandas: convert dictionary to DataFrame with keys as row

  28. 28

    Edit nested dictionary duplicate values in same keys

  29. 29

    How to make a dictionary in Python with files as keys?

ホットタグ

アーカイブ