Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python test fail while function works in isolation

This is the function

def update_content_if_reusable(content_data):
    """ Add reusable counter """
    content = json.loads(content_data)
    if 'layouts' in content:
        for layout in content['layouts'].values():
            if 'isReusable' in layout:
                content['reusableBlocks'] += 1
    return json.dumps(content, indent=4)

The error in test

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

content_data = '"{\\n    \\"page\\": {\\n        \\"layouts\\": []\\n    },\\n    \\"reusableBlocks\\": 0,\\n    \\"layouts\\": {},\\n    \\"columns\\": {},\\n    \\"components\\": {}\\n}"'

    def update_content_if_reusable(content_data):
        """ Add reusable counter """
        content = json.loads(content_data)
        if 'layouts' in content:
>           for layout in content['layouts'].values():
E           TypeError: string indices must be integers

models.py:82: TypeError

The python version is 3.6

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

https://www.online-python.com/5VH28jsWYQ

>Solution :

The issue is that you are passing in a JSON string that represents an actual string that contains the data you want. You would need to call json.loads twice to get the dictionary out of that.

update_content_if_reusable(json.loads(content_data))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading