You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_db_manager.py 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import os
  2. import pytest
  3. from sqlmodel import Session, text, select
  4. from typing import Generator
  5. from datetime import datetime
  6. from autogenstudio.database import DatabaseManager
  7. from autogenstudio.datamodel import (
  8. Model, ModelConfig, Agent, AgentConfig, Tool, ToolConfig,
  9. Team, TeamConfig, ModelTypes, AgentTypes, TeamTypes, ComponentType,
  10. TerminationConfig, TerminationTypes, LinkTypes, ToolTypes
  11. )
  12. @pytest.fixture
  13. def test_db() -> Generator[DatabaseManager, None, None]:
  14. """Fixture for test database"""
  15. db_path = "test.db"
  16. db = DatabaseManager(f"sqlite:///{db_path}")
  17. db.reset_db()
  18. db.create_db_and_tables()
  19. yield db
  20. db.reset_db()
  21. try:
  22. # Close database connections before removing file
  23. db.engine.dispose()
  24. # Remove the database file
  25. if os.path.exists(db_path):
  26. os.remove(db_path)
  27. except Exception as e:
  28. print(f"Warning: Failed to remove test database file: {e}")
  29. @pytest.fixture
  30. def test_user() -> str:
  31. return "test_user@example.com"
  32. @pytest.fixture
  33. def sample_model(test_user: str) -> Model:
  34. """Create a sample model with proper config"""
  35. return Model(
  36. user_id=test_user,
  37. config=ModelConfig(
  38. model="gpt-4",
  39. model_type=ModelTypes.OPENAI,
  40. component_type=ComponentType.MODEL,
  41. version="1.0.0"
  42. ).model_dump()
  43. )
  44. @pytest.fixture
  45. def sample_tool(test_user: str) -> Tool:
  46. """Create a sample tool with proper config"""
  47. return Tool(
  48. user_id=test_user,
  49. config=ToolConfig(
  50. name="test_tool",
  51. description="A test tool",
  52. content="async def test_func(x: str) -> str:\n return f'Test {x}'",
  53. tool_type=ToolTypes.PYTHON_FUNCTION,
  54. component_type=ComponentType.TOOL,
  55. version="1.0.0"
  56. ).model_dump()
  57. )
  58. @pytest.fixture
  59. def sample_agent(test_user: str, sample_model: Model, sample_tool: Tool) -> Agent:
  60. """Create a sample agent with proper config and relationships"""
  61. return Agent(
  62. user_id=test_user,
  63. config=AgentConfig(
  64. name="test_agent",
  65. agent_type=AgentTypes.ASSISTANT,
  66. model_client=ModelConfig.model_validate(sample_model.config),
  67. tools=[ToolConfig.model_validate(sample_tool.config)],
  68. component_type=ComponentType.AGENT,
  69. version="1.0.0"
  70. ).model_dump()
  71. )
  72. @pytest.fixture
  73. def sample_team(test_user: str, sample_agent: Agent) -> Team:
  74. """Create a sample team with proper config"""
  75. return Team(
  76. user_id=test_user,
  77. config=TeamConfig(
  78. name="test_team",
  79. participants=[AgentConfig.model_validate(sample_agent.config)],
  80. termination_condition=TerminationConfig(
  81. termination_type=TerminationTypes.STOP_MESSAGE,
  82. component_type=ComponentType.TERMINATION,
  83. version="1.0.0"
  84. ).model_dump(),
  85. team_type=TeamTypes.ROUND_ROBIN,
  86. component_type=ComponentType.TEAM,
  87. version="1.0.0"
  88. ).model_dump()
  89. )
  90. class TestDatabaseOperations:
  91. def test_basic_setup(self, test_db: DatabaseManager):
  92. """Test basic database setup and connection"""
  93. with Session(test_db.engine) as session:
  94. result = session.exec(text("SELECT 1")).first()
  95. assert result[0] == 1
  96. result = session.exec(select(1)).first()
  97. assert result == 1
  98. def test_basic_entity_creation(self, test_db: DatabaseManager, sample_model: Model,
  99. sample_tool: Tool, sample_agent: Agent, sample_team: Team):
  100. """Test creating all entity types with proper configs"""
  101. with Session(test_db.engine) as session:
  102. # Add all entities
  103. session.add(sample_model)
  104. session.add(sample_tool)
  105. session.add(sample_agent)
  106. session.add(sample_team)
  107. session.commit()
  108. # Store IDs
  109. model_id = sample_model.id
  110. tool_id = sample_tool.id
  111. agent_id = sample_agent.id
  112. team_id = sample_team.id
  113. # Verify all entities were created with new session
  114. with Session(test_db.engine) as session:
  115. assert session.get(Model, model_id) is not None
  116. assert session.get(Tool, tool_id) is not None
  117. assert session.get(Agent, agent_id) is not None
  118. assert session.get(Team, team_id) is not None
  119. def test_multiple_links(self, test_db: DatabaseManager, sample_agent: Agent):
  120. """Test linking multiple models to an agent"""
  121. with Session(test_db.engine) as session:
  122. # Create two models with updated configs
  123. model1 = Model(
  124. user_id="test_user",
  125. config=ModelConfig(
  126. model="gpt-4",
  127. model_type=ModelTypes.OPENAI,
  128. component_type=ComponentType.MODEL,
  129. version="1.0.0"
  130. ).model_dump()
  131. )
  132. model2 = Model(
  133. user_id="test_user",
  134. config=ModelConfig(
  135. model="gpt-3.5",
  136. model_type=ModelTypes.OPENAI,
  137. component_type=ComponentType.MODEL,
  138. version="1.0.0"
  139. ).model_dump()
  140. )
  141. # Add and commit all entities
  142. session.add(model1)
  143. session.add(model2)
  144. session.add(sample_agent)
  145. session.commit()
  146. model1_id = model1.id
  147. model2_id = model2.id
  148. agent_id = sample_agent.id
  149. # Create links using IDs
  150. test_db.link(LinkTypes.AGENT_MODEL, agent_id, model1_id)
  151. test_db.link(LinkTypes.AGENT_MODEL, agent_id, model2_id)
  152. # Verify links
  153. linked_models = test_db.get_linked_entities(
  154. LinkTypes.AGENT_MODEL, agent_id)
  155. assert len(linked_models.data) == 2
  156. # Verify model names
  157. model_names = [model.config["model"] for model in linked_models.data]
  158. assert "gpt-4" in model_names
  159. assert "gpt-3.5" in model_names