CoolFace
Apppublic

coding-alt/AutoGPT

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
test_json_parser.py112 linesDownload Raw Back to tests
1import unittest2 3import tests.context4from autogpt.json_utils.json_fix_llm import fix_and_parse_json5 6 7class TestParseJson(unittest.TestCase):8    def test_valid_json(self):9        # Test that a valid JSON string is parsed correctly10        json_str = '{"name": "John", "age": 30, "city": "New York"}'11        obj = fix_and_parse_json(json_str)12        self.assertEqual(obj, {"name": "John", "age": 30, "city": "New York"})13 14    def test_invalid_json_minor(self):15        # Test that an invalid JSON string can be fixed with gpt16        json_str = '{"name": "John", "age": 30, "city": "New York",}'17        with self.assertRaises(Exception):18            fix_and_parse_json(json_str, try_to_fix_with_gpt=False)19 20    def test_invalid_json_major_with_gpt(self):21        # Test that an invalid JSON string raises an error when try_to_fix_with_gpt is False22        json_str = 'BEGIN: "name": "John" - "age": 30 - "city": "New York" :END'23        with self.assertRaises(Exception):24            fix_and_parse_json(json_str, try_to_fix_with_gpt=False)25 26    def test_invalid_json_major_without_gpt(self):27        # Test that a REALLY invalid JSON string raises an error when try_to_fix_with_gpt is False28        json_str = 'BEGIN: "name": "John" - "age": 30 - "city": "New York" :END'29        # Assert that this raises an exception:30        with self.assertRaises(Exception):31            fix_and_parse_json(json_str, try_to_fix_with_gpt=False)32 33    def test_invalid_json_leading_sentence_with_gpt(self):34        # Test that a REALLY invalid JSON string raises an error when try_to_fix_with_gpt is False35        json_str = """I suggest we start by browsing the repository to find any issues that we can fix.36 37{38    "command": {39        "name": "browse_website",40        "args":{41            "url": "https://github.com/Torantulino/Auto-GPT"42        }43    },44    "thoughts":45    {46        "text": "I suggest we start browsing the repository to find any issues that we can fix.",47        "reasoning": "Browsing the repository will give us an idea of the current state of the codebase and identify any issues that we can address to improve the repo.",48        "plan": "- Look through the repository to find any issues.\n- Investigate any issues to determine what needs to be fixed\n- Identify possible solutions to fix the issues\n- Open Pull Requests with fixes",49        "criticism": "I should be careful while browsing so as not to accidentally introduce any new bugs or issues.",50        "speak": "I will start browsing the repository to find any issues we can fix."51    }52}"""53        good_obj = {54            "command": {55                "name": "browse_website",56                "args": {"url": "https://github.com/Torantulino/Auto-GPT"},57            },58            "thoughts": {59                "text": "I suggest we start browsing the repository to find any issues that we can fix.",60                "reasoning": "Browsing the repository will give us an idea of the current state of the codebase and identify any issues that we can address to improve the repo.",61                "plan": "- Look through the repository to find any issues.\n- Investigate any issues to determine what needs to be fixed\n- Identify possible solutions to fix the issues\n- Open Pull Requests with fixes",62                "criticism": "I should be careful while browsing so as not to accidentally introduce any new bugs or issues.",63                "speak": "I will start browsing the repository to find any issues we can fix.",64            },65        }66        # Assert that this raises an exception:67        self.assertEqual(68            fix_and_parse_json(json_str, try_to_fix_with_gpt=False), good_obj69        )70 71    def test_invalid_json_leading_sentence_with_gpt(self):72        # Test that a REALLY invalid JSON string raises an error when try_to_fix_with_gpt is False73        json_str = """I will first need to browse the repository (https://github.com/Torantulino/Auto-GPT) and identify any potential bugs that need fixing. I will use the "browse_website" command for this.74 75{76    "command": {77        "name": "browse_website",78        "args":{79            "url": "https://github.com/Torantulino/Auto-GPT"80        }81    },82    "thoughts":83    {84        "text": "Browsing the repository to identify potential bugs",85        "reasoning": "Before fixing bugs, I need to identify what needs fixing. I will use the 'browse_website' command to analyze the repository.",86        "plan": "- Analyze the repository for potential bugs and areas of improvement",87        "criticism": "I need to ensure I am thorough and pay attention to detail while browsing the repository.",88        "speak": "I am browsing the repository to identify potential bugs."89    }90}"""91        good_obj = {92            "command": {93                "name": "browse_website",94                "args": {"url": "https://github.com/Torantulino/Auto-GPT"},95            },96            "thoughts": {97                "text": "Browsing the repository to identify potential bugs",98                "reasoning": "Before fixing bugs, I need to identify what needs fixing. I will use the 'browse_website' command to analyze the repository.",99                "plan": "- Analyze the repository for potential bugs and areas of improvement",100                "criticism": "I need to ensure I am thorough and pay attention to detail while browsing the repository.",101                "speak": "I am browsing the repository to identify potential bugs.",102            },103        }104        # Assert that this raises an exception:105        self.assertEqual(106            fix_and_parse_json(json_str, try_to_fix_with_gpt=False), good_obj107        )108 109 110if __name__ == "__main__":111    unittest.main()112