add context tests

This commit is contained in:
geekan 2024-01-09 14:16:32 +08:00
parent b6508d1054
commit b4f049294b
3 changed files with 105 additions and 22 deletions

View file

@ -0,0 +1,63 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2024/1/9 13:52
@Author : alexanderwu
@File : test_context.py
"""
from metagpt.configs.llm_config import LLMType
from metagpt.context import AttrDict, Context, context
def test_attr_dict_1():
ad = AttrDict(name="John", age=30)
assert ad.name == "John"
assert ad.age == 30
assert ad.height is None
def test_attr_dict_2():
ad = AttrDict(name="John", age=30)
ad.height = 180
assert ad.height == 180
def test_attr_dict_3():
ad = AttrDict(name="John", age=30)
del ad.age
assert ad.age is None
def test_attr_dict_4():
ad = AttrDict(name="John", age=30)
try:
del ad.weight
except AttributeError as e:
assert str(e) == "No such attribute: weight"
def test_attr_dict_5():
ad = AttrDict.model_validate({"name": "John", "age": 30})
assert ad.name == "John"
assert ad.age == 30
def test_context_1():
ctx = Context()
assert ctx.config is not None
assert ctx.git_repo is None
assert ctx.src_workspace is None
assert ctx.cost_manager is not None
assert ctx.options is not None
def test_context_2():
llm = context.config.get_openai_llm()
assert llm is not None
assert llm.api_type == LLMType.OPENAI
kwargs = context.kwargs
assert kwargs is not None
kwargs.test_key = "test_value"
assert kwargs.test_key == "test_value"