Repository: halluxdev/hallux (language: python)

Issue:
Increase unit-test coverage
query_backend.py -- WIP

DiffTarget tests: - Sergey - DONE
FileDiff - Sergey - DONE



Discussion / hints:

Fix patch:
diff --git a/.vscode/launch.json b/.vscode/launch.json
index 6802a45..0840efd 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -9,10 +9,22 @@
       "program": "${workspaceFolder}/bin/hallux.py",
       "cwd": "${workspaceFolder}",
       "env": {
-        "PYTHONPATH": "${workspaceFolder}/bin",
+        "PYTHONPATH": "${workspaceFolder}/bin:$PYTHONPATH",
       },
       "args" : ["--verbose", "--rest", "./tests/test-python-project"]
     },
+    {
+      "name": "Python: Current test file",
+      "type": "python",
+      "request": "launch",
+      "program": "${file}",
+      "purpose": ["debug-test"],
+      "console": "integratedTerminal",
+      "cwd": "${workspaceFolder}",
+      "env": {
+        "PYTHONPATH": "${workspaceFolder}/src:${workspaceFolder}/tests:$PYTHONPATH",
+      }
+    },
     {
       "name": "Python: Current File",
       "type": "python",
diff --git a/README.md b/README.md
index ab71122..fbbbf0d 100644
--- a/README.md
+++ b/README.md
@@ -26,8 +26,9 @@ For more command-line commands and just type `$ hallux`
 
 ## Installation
 
-In order to setup Python virtual environment run `./scripts/setup-python.sh`
+In order to setup Python virtual environment run `./scripts/setup-venv.sh`
 
 For activating environment run `cd /path/to/hallux && source activate.sh` 
 
 You might need to add `export PATH="/Path/to/hallux/bin:$PATH"` into your `~/.bashrc` or `~/.zshrc` file.
+
diff --git a/src/backends/rest_backend.py b/src/backends/rest_backend.py
index e579332..e36021c 100644
--- a/src/backends/rest_backend.py
+++ b/src/backends/rest_backend.py
@@ -14,10 +14,12 @@
 
 
 class RestBackend(QueryBackend):
+    PROMPT_STRING = "$PROMPT"
+
     def __init__(
         self,
         url: str,
-        request_body: Any = "$PROMPT",
+        request_body: str | dict = PROMPT_STRING,
         response_body: str = "$RESPONSE",
         token: str | None = None,
         type="rest",
@@ -107,11 +109,12 @@ def _parse_response(self, response: str | None | dict) -> list[str]:
     def query(self, request: str, issue: IssueDescriptor | None = None, issue_lines: list[str] = list) -> list[str]:
         if self._is_object(self.request_body):
             json_data = json.dumps(self.request_body)
-            parsed_request = json_data.replace("$PROMPT", request)
+            parsed_request = json_data.replace(self.PROMPT_STRING, request)
             self.headers.update({"Content-Type": "application/json"})
 
         elif self._is_string(self.request_body):
-            parsed_request = self.request_body.replace("$PROMPT", request)
+            parsed_request = self.request_body.replace(
+                self.PROMPT_STRING, request)
             self.headers.update({"Content-Type": "text/plain"})
 
         else:


Test patch:
diff --git a/tests/unit/backends/rest_backend_test.py b/tests/unit/backends/rest_backend_test.py
new file mode 100644
index 0000000..8f08324
--- /dev/null
+++ b/tests/unit/backends/rest_backend_test.py
@@ -0,0 +1,52 @@
+import os
+from unittest.mock import patch, MagicMock
+
+import pytest
+from unit.common.testing_issue import TestingIssue
+
+from backends.rest_backend import RestBackend
+
+
+class TestRestBackend:
+    @pytest.fixture
+    def setup_issue(self):
+        return TestingIssue(
+            language="en", tool="tool1",
+            filename="file1",
+            description="issue1")
+
+    @pytest.fixture
+    def setup_rest_backend(self):
+        return RestBackend(url="https://example.com/api")
+
+    @pytest.fixture
+    def setup_rest_backend_json(self):
+        return RestBackend(
+            url="https://example.com/api",
+            request_body={"query": "$PROMPT"},
+            response_body="$RESPONSE.answer.values.0"
+        )
+
+    @patch("requests.post")
+    def test_text_response(self, mock_create, setup_rest_backend, setup_issue):
+        mock_create.return_value = MagicMock(
+            status_code=200,
+            text="test text answer",
+            headers={"Content-Type": "text/plain"}
+        )
+        result = setup_rest_backend.query("request", setup_issue)
+        assert result == ["test text answer"]
+
+    @patch("requests.post")
+    def test_json_response(self, mock_create, setup_rest_backend_json, setup_issue):
+        mock_create.return_value = MagicMock(
+            status_code=200,
+            json=lambda: {"answer": {"values": ["test json answer"]}},
+            headers={"Content-Type": "application/json"}
+        )
+        result = setup_rest_backend_json.query("request", setup_issue)
+        assert result == ["test json answer"]
+
+    def test_error_response(self, setup_rest_backend_json, setup_issue):
+        result = setup_rest_backend_json.query("request", setup_issue)
+        assert result == []