From 1834d47655baf7ea29c621cbadb9a3456cb83936 Mon Sep 17 00:00:00 2001 From: antazoey Date: Thu, 19 Sep 2024 12:07:30 -0500 Subject: [PATCH] test: fix the tests (#48) --- main.py | 8 ++++---- test_app.py | 45 +++++++++++++++++++++++++++++---------------- vvm_versions.py | 4 +--- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/main.py b/main.py index 4da8aae..d368981 100644 --- a/main.py +++ b/main.py @@ -3,9 +3,9 @@ from enum import Enum from pathlib import Path from typing import Annotated -from ape.managers.compilers import CompilerError from ape import Project +from ape.managers.compilers import CompilerError from ethpm_types import PackageManifest from fastapi import BackgroundTasks, Body, FastAPI, HTTPException, Request from fastapi.middleware.cors import CORSMiddleware @@ -113,7 +113,7 @@ async def updated_compilation_task( background_tasks: BackgroundTasks, task_id: str, project: Annotated[PackageManifest, Body()], -)-> str: +) -> str: """ Re-triggers a compilation task using the updated project encoded as an EthPM v3 manifest. """ @@ -178,7 +178,7 @@ async def compile_project(project_root: Path, manifest: PackageManifest): # Create a contracts directory contracts_dir = project_root / "contracts" contracts_dir.mkdir() - + # add request contracts in temp directory if manifest.sources: for filename, source in manifest.sources.items(): @@ -198,5 +198,5 @@ async def compile_project(project_root: Path, manifest: PackageManifest): ] tasks[project_root.name] = TaskStatus.FAILED except Exception as e: - results[project_root.name] = {e.__class__.__name__:str(e)} + results[project_root.name] = {e.__class__.__name__: str(e)} tasks[project_root.name] = TaskStatus.FAILED diff --git a/test_app.py b/test_app.py index 8ddef97..806393d 100644 --- a/test_app.py +++ b/test_app.py @@ -1,5 +1,7 @@ -from fastapi.testclient import TestClient +import re +from pathlib import Path +from fastapi.testclient import TestClient from main import app client = TestClient(app) @@ -15,43 +17,54 @@ def test_create_compilation_task(): # empty sources should work response = client.post("/compile", json={"manifest": "ethpm/3"}) assert response.status_code == 200 - data = response.json() - assert "task_id" in data + task_id = response.json() + assert isinstance(task_id, str) def test_get_task_status(): # Test get task status response = client.get("/status/some_invalid_task_id") - assert response.status_code == 400 # Invalid task_id should return 400 Bad Request + assert response.status_code == 404 # Not found - response = client.get("/status/some_valid_task_id") + task_id = client.post("/compile", json={"manifest": "ethpm/3"}).json() + response = client.get(f"/status/{task_id}") assert response.status_code == 200 data = response.json() - assert data in ["In Progress", "Success", "Error"] + assert data.lower() in ["in progress", "success", "error"] def test_get_task_exceptions(): # Test get task exceptions response = client.get("/exceptions/some_invalid_task_id") - assert response.status_code == 400 # Invalid task_id should return 400 Bad Request + assert response.status_code == 404 # Not found # Assuming the task_id has an Error status - response = client.get("/exceptions/some_valid_task_id") - assert response.status_code == 200 + task_id = client.post("/compile", json={"manifest": "ethpm/3"}).json() + response = client.get(f"/exceptions/{task_id}") + assert response.status_code == 400 data = response.json() - assert "task_id" in data - assert "compilation_errors" in data + actual = data["detail"] + assert re.match(r"Task '\w*' is not completed with Error status", actual) def test_get_compiled_artifact(): # Test get compiled artifact response = client.get("/artifacts/some_invalid_task_id") - assert response.status_code == 400 # Invalid task_id should return 400 Bad Request + assert response.status_code == 404 # Not found # Assuming the task_id has a Success status - response = client.get("/artifacts/some_valid_task_id") + source_id = "contracts/ERC20.vy" + source_text = (Path(__file__).parent / source_id).read_text() + manifest = {"manifest": "ethpm/3", "sources": {source_id: source_text}} + task_id = client.post("/compile", json=manifest).json() + response = client.get(f"/artifacts/{task_id}") assert response.status_code == 200 data = response.json() - assert "contract_name" in data - assert "abi" in data - assert "compiler" in data + assert "name" in data + assert "contractTypes" in data + assert "compilers" in data + + # Show we get the ERC20 contract-type. + assert "ERC20" in data["contractTypes"] + assert "abi" in data["contractTypes"]["ERC20"] + assert len(data["contractTypes"]["ERC20"]["abi"]) > 1 diff --git a/vvm_versions.py b/vvm_versions.py index 1a6c20f..30cbf2c 100644 --- a/vvm_versions.py +++ b/vvm_versions.py @@ -4,9 +4,7 @@ installable_versions = vvm.get_installable_vyper_versions() for version in installable_versions: - version_str = ( - str(version.major) + "." + str(version.minor) + "." + str(version.micro) - ) + version_str = str(version.major) + "." + str(version.minor) + "." + str(version.micro) if version not in installed_versions: if version_str == "0.3.8": print("Skipping Broken version: " + version_str)