Coverage for skema/program_analysis/comment_extractor/tests/test_comment_server.py: 100%
43 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-30 17:15 +0000
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-30 17:15 +0000
1import shutil
2from tempfile import TemporaryDirectory, TemporaryFile
3from pathlib import Path
4from typing import List
5from fastapi.testclient import TestClient
7import skema.program_analysis.comment_extractor.comment_extractor as comment_service
8from skema.program_analysis.comment_extractor.server import app
10client = TestClient(app)
12def test_comments_get_supported_languages():
13 '''Test case for /comments-get-supported-languages endpoint.'''
14 response = client.get("/comment_service/comments-get-supported-languages")
15 assert response.status_code == 200
17 languages = comment_service.SupportedLanguageResponse(**response.json())
18 assert isinstance(languages, comment_service.SupportedLanguageResponse)
19 assert len(languages.languages) > 0
22def test_comments_get_supprted_file_extensions():
23 '''Test cast for /comments-get-supported-file-extensions'''
24 response = client.get("/comment_service/comments-get-supported-file-extensions")
25 assert response.status_code == 200
27 extensions = response.json()
28 assert isinstance(extensions, List)
29 assert len(extensions) > 0
31def test_comments_extract():
32 '''Test cast for /comments-extract endpoint'''
33 request = {
34 "source": "# Simple comment extraction example",
35 "language": "python"
36 }
37 response = client.post("/comment_service/comments-extract", json=request)
38 assert response.status_code == 200
40 comments = comment_service.SingleFileCommentResponse(**response.json())
41 assert isinstance(comments, comment_service.SingleFileCommentResponse)
44def test_comments_extract_zip():
45 """Test case for /comments-extract-zip endpoint"""
46 system = {
47 "files": ["example1.py", "dir/example2.py"],
48 "blobs": [
49 "greet = lambda: print('howdy!')\ngreet()",
50 "#Variable declaration\nx=2\n#Function definition\ndef foo(x):\n '''Increment the input variable'''\n return x+1", # Content of dir/example2.py
51 ],
52 "root_name": "example",
53 "system_name": "example"
54 }
55 with TemporaryDirectory() as tmp:
56 system_path = Path(tmp) / system["root_name"]
57 system_path.mkdir()
58 system_zip_path = Path(tmp) / f"{system['root_name']}.zip"
60 for index, file in enumerate(system["files"]):
61 file_path = Path(tmp, system["root_name"], file)
62 file_path.parent.mkdir(parents=True, exist_ok=True)
63 file_path.write_text(system["blobs"][index])
65 input_path = Path(tmp, system["root_name"])
66 output_path = Path(tmp, f"{system['root_name']}.zip")
67 shutil.make_archive(input_path, "zip", input_path)
69 response = client.post(
70 "/comment_service/comments-extract-zip",
71 files={"zip_file": open(output_path, "rb")},
72 )
73 assert response.status_code == 200
75 comments = comment_service.MultiFileCommentResponse(**response.json())
76 assert isinstance(comments, comment_service.MultiFileCommentResponse)