1. Refactor code to centralize unittest directory pattern using a fixture in conftest.py

2. Setup incremental workdir for testing in test_write_code.py
This commit is contained in:
mannaandpoem 2024-03-02 21:10:45 +08:00
parent eb3c6d14f9
commit 1c7f63857f
6 changed files with 40 additions and 51 deletions

View file

@ -6,8 +6,6 @@
@File : test_summarize_code.py
@Modifiled By: mashenquan, 2023-12-6. Unit test for summarize_code.py
"""
import uuid
from pathlib import Path
import pytest
@ -178,10 +176,7 @@ class Snake:
@pytest.mark.skip
@pytest.mark.asyncio
async def test_summarize_code(context):
git_dir = Path(__file__).parent / f"unittest/{uuid.uuid4().hex}"
git_dir.mkdir(parents=True, exist_ok=True)
async def test_summarize_code(context, git_dir):
context.src_workspace = context.git_repo.workdir / "src"
await context.repo.docs.system_design.save(filename="1.json", content=DESIGN_CONTENT)
await context.repo.docs.task.save(filename="1.json", content=TASK_CONTENT)

View file

@ -7,7 +7,6 @@
@Modifiled By: mashenquan, 2023-12-6. According to RFC 135
"""
import json
import uuid
from pathlib import Path
import pytest
@ -25,6 +24,17 @@ from tests.data.incremental_dev_project.mock import (
from tests.metagpt.actions.mock_markdown import TASKS_2, WRITE_CODE_PROMPT_SAMPLE
def setup_inc_workdir(context, inc: bool = False):
"""setup incremental workdir for testing"""
context.src_workspace = context.git_repo.workdir / "src"
if inc:
context.config.inc = inc
context.repo.old_workspace = context.repo.git_repo.workdir / "old"
context.config.project_path = "old"
return context
@pytest.mark.asyncio
async def test_write_code(context):
# Prerequisites
@ -89,13 +99,9 @@ async def test_write_code_deps(context):
@pytest.mark.asyncio
async def test_write_refined_code(context):
async def test_write_refined_code(context, git_dir):
# Prerequisites
git_dir = Path(__file__).parent / f"unittest/{uuid.uuid4().hex}"
git_dir.mkdir(parents=True, exist_ok=True)
context.config.inc = True
context.src_workspace = context.git_repo.workdir / "src"
context = setup_inc_workdir(context, inc=True)
await context.repo.docs.system_design.save(filename="1.json", content=json.dumps(REFINED_DESIGN_JSON))
await context.repo.docs.task.save(filename="1.json", content=json.dumps(REFINED_TASK_JSON))
await context.repo.docs.code_plan_and_change.save(
@ -103,7 +109,6 @@ async def test_write_refined_code(context):
)
# old_workspace contains the legacy code
context.repo.old_workspace = context.repo.git_repo.workdir / "old"
await context.repo.with_src_path(context.repo.old_workspace).srcs.save(
filename="game.py", content=CodeParser.parse_code(block="", text=REFINED_CODE_INPUT_SAMPLE)
)
@ -126,8 +131,7 @@ async def test_write_refined_code(context):
@pytest.mark.asyncio
async def test_get_codes(context):
# Prerequisites
context.src_workspace = context.git_repo.workdir / "src"
context.repo.old_workspace = context.repo.git_repo.workdir / "old"
context = setup_inc_workdir(context, inc=True)
for filename in ["game.py", "ui.py"]:
await context.repo.with_src_path(context.src_workspace).srcs.save(
filename=filename, content=f"# {filename}\nnew code ..."

View file

@ -6,8 +6,6 @@
@File : test_write_code_plan_and_change_an.py
"""
import json
import uuid
from pathlib import Path
import pytest
from openai._models import BaseModel
@ -32,6 +30,7 @@ from tests.data.incremental_dev_project.mock import (
REFINED_TASK_JSON,
TASK_SAMPLE,
)
from tests.metagpt.actions.test_write_code import setup_inc_workdir
def mock_code_plan_and_change():
@ -39,19 +38,12 @@ def mock_code_plan_and_change():
@pytest.mark.asyncio
async def test_write_code_plan_and_change_an(mocker, context):
# Prerequisites
git_dir = Path(__file__).parent / f"unittest/{uuid.uuid4().hex}"
git_dir.mkdir(parents=True, exist_ok=True)
context.config.inc = True
async def test_write_code_plan_and_change_an(mocker, context, git_dir):
context = setup_inc_workdir(context, inc=True)
await context.repo.docs.prd.save(filename="2.json", content=json.dumps(REFINED_PRD_JSON))
await context.repo.docs.system_design.save(filename="2.json", content=json.dumps(REFINED_DESIGN_JSON))
await context.repo.docs.task.save(filename="2.json", content=json.dumps(REFINED_TASK_JSON))
context.src_workspace = context.git_repo.workdir / "src"
await context.repo.docs.prd.save(filename="1.json", content=json.dumps(REFINED_PRD_JSON))
await context.repo.docs.system_design.save(filename="1.json", content=json.dumps(REFINED_DESIGN_JSON))
await context.repo.docs.task.save(filename="1.json", content=json.dumps(REFINED_TASK_JSON))
context.config.project_path = "old"
context.repo.old_workspace = context.repo.git_repo.workdir / "old"
await context.repo.with_src_path(context.repo.old_workspace).srcs.save(
filename="game.py", content=CodeParser.parse_code(block="", text=REFINED_CODE_INPUT_SAMPLE)
)
@ -67,9 +59,9 @@ async def test_write_code_plan_and_change_an(mocker, context):
code_plan_and_change_context = CodePlanAndChangeContext(
requirement="New requirement",
prd_filename="1.json",
design_filename="1.json",
task_filename="1.json",
prd_filename="2.json",
design_filename="2.json",
task_filename="2.json",
)
node = await WriteCodePlanAndChange(i_context=code_plan_and_change_context, context=context).run()
@ -96,12 +88,8 @@ async def test_refine_code(mocker):
@pytest.mark.asyncio
async def test_get_old_code(context):
git_dir = Path(__file__).parent / f"unittest/{uuid.uuid4().hex}"
git_dir.mkdir(parents=True, exist_ok=True)
context.config.project_path = "old"
context.repo.old_workspace = context.repo.git_repo.workdir / "old"
async def test_get_old_code(context, git_dir):
context = setup_inc_workdir(context, inc=True)
await context.repo.with_src_path(context.repo.old_workspace).srcs.save(
filename="game.py", content=REFINED_CODE_INPUT_SAMPLE
)

View file

@ -6,8 +6,6 @@
@File : test_write_prd.py
@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, replace `handle` with `run`.
"""
import uuid
from pathlib import Path
import pytest
@ -19,6 +17,7 @@ from metagpt.roles.role import RoleReactMode
from metagpt.schema import Message
from metagpt.utils.common import any_to_str
from tests.data.incremental_dev_project.mock import NEW_REQUIREMENT_SAMPLE, PRD_SAMPLE
from tests.metagpt.actions.test_write_code import setup_inc_workdir
@pytest.mark.asyncio
@ -39,11 +38,8 @@ async def test_write_prd(new_filename, context):
@pytest.mark.asyncio
async def test_write_prd_inc(new_filename, context):
git_dir = Path(__file__).parent / f"unittest/{uuid.uuid4().hex}"
git_dir.mkdir(parents=True, exist_ok=True)
context.src_workspace = context.git_repo.workdir / "src"
async def test_write_prd_inc(new_filename, context, git_dir):
context = setup_inc_workdir(context, inc=True)
await context.repo.docs.prd.save("1.txt", PRD_SAMPLE)
await context.repo.docs.save(filename=REQUIREMENT_FILENAME, content=NEW_REQUIREMENT_SAMPLE)
@ -59,10 +55,7 @@ async def test_write_prd_inc(new_filename, context):
@pytest.mark.asyncio
async def test_fix_debug(new_filename, context):
git_dir = Path(__file__).parent / f"unittest/{uuid.uuid4().hex}"
git_dir.mkdir(parents=True, exist_ok=True)
async def test_fix_debug(new_filename, context, git_dir):
context.src_workspace = context.git_repo.workdir / context.git_repo.workdir.name
await context.repo.with_src_path(context.src_workspace).srcs.save(