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/.github/workflows/hallux.yml b/.github/workflows/hallux.yml
index 7f8b104..25ac1b8 100644
--- a/.github/workflows/hallux.yml
+++ b/.github/workflows/hallux.yml
@@ -24,11 +24,12 @@ jobs:
       run: |
         export PATH=$(pwd)/bin:$PATH
         export PYTHONPATH=$(pwd)/bin:${PYTHONPATH}
-        python -m coverage run -m pytest tests/unit
-
-    - name: Fail if coverage is less than 80%
-      run: |
-        coverage report --fail-under=80
+        python -m pytest \
+          --cov=bin \
+          --cov-branch \
+          --cov-report=term \
+          --cov-fail-under=75 \
+          tests/unit
 
     - name: Integration Tests
       run: |
diff --git a/requirements.txt b/requirements.txt
index 5400c9d..eb9f6eb 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -5,4 +5,3 @@ black==23.3.0
 openai==0.27.8
 pyyaml==6.0
 PyGithub==1.59
-coverage==7.2.7


Test patch:
diff --git a/tests/unit/query_backend/dummy_backend_test.py b/tests/unit/query_backend/dummy_backend_test.py
new file mode 100644
index 0000000..beda894
--- /dev/null
+++ b/tests/unit/query_backend/dummy_backend_test.py
@@ -0,0 +1,62 @@
+#!/bin/env python
+# Copyright: Hallux team, 2023
+
+import pytest
+import json
+from pathlib import Path
+from query_backend import DummyBackend
+from issue import IssueDescriptor
+from unittest.mock import Mock, patch, mock_open
+
+
+class MyIssueDescriptor(IssueDescriptor):
+    def try_fixing(self):
+        # implement the try_fixing method here
+        pass
+
+
+class TestDummyBackend:
+    @pytest.fixture
+    def setup_dummy_backend(self):
+        filename = "/tmp/test.json"
+        base_path = Path("./tests/unit/query_backend")
+        self.base_path = base_path
+        with open(filename, "wt") as file:
+            json.dump({"python": {"tool1": {"file1.py": {"issue1": [
+                      "issue1 fixed"], "issue2": ["issue2 fixed"]}}}}, file)
+        return DummyBackend(filename, base_path)
+
+    def test_init(self, setup_dummy_backend):
+        assert setup_dummy_backend.filename == "/tmp/test.json"
+        assert setup_dummy_backend.base_path == Path(self.base_path)
+        assert setup_dummy_backend.json == {"python": {"tool1": {"file1.py": {"issue1": [
+            "issue1 fixed"], "issue2": ["issue2 fixed"]}}}}
+
+    def test_query(self, setup_dummy_backend):
+        filename = self.base_path.joinpath("file1.py")
+        issue = MyIssueDescriptor(
+            language="python", tool="tool1", filename=filename, description="issue1")
+        result = setup_dummy_backend.query("request", issue)
+        assert result == ["issue1 fixed"]
+
+    def test_query_no_issue(self, setup_dummy_backend):
+        result = setup_dummy_backend.query("request")
+        assert result == []
+
+    def test_query_no_match(self, setup_dummy_backend):
+        issue = MyIssueDescriptor(
+            language="python", tool="tool1", filename="file1.py", description="issue3")
+        result = setup_dummy_backend.query("request", issue)
+        assert result == []
+
+    def test_query_no_language(self, setup_dummy_backend):
+        issue = MyIssueDescriptor(
+            language="", tool="tool1", filename="file1.py", description="issue1")
+        result = setup_dummy_backend.query("request", issue)
+        assert result == []
+
+    def test_query_no_tool(self, setup_dummy_backend):
+        issue = MyIssueDescriptor(
+            language="cpp", tool="newTool", filename="file1.py", description="issue1")
+        result = setup_dummy_backend.query("request", issue)
+        assert result == []
diff --git a/tests/unit/query_backend/file1.py b/tests/unit/query_backend/file1.py
new file mode 100644
index 0000000..4794253
--- /dev/null
+++ b/tests/unit/query_backend/file1.py
@@ -0,0 +1,3 @@
+hello
+issue1
+issue2
diff --git a/tests/unit/query_backend/open_ai_test.py b/tests/unit/query_backend/open_ai_test.py
new file mode 100644
index 0000000..06e32c6
--- /dev/null
+++ b/tests/unit/query_backend/open_ai_test.py
@@ -0,0 +1,31 @@
+import os
+import pytest
+from unittest.mock import patch, Mock
+from query_backend import OpenAiChatGPT
+from issue import IssueDescriptor
+
+class MyIssueDescriptor(IssueDescriptor):
+    def try_fixing(self):
+        # implement the try_fixing method here
+        pass
+
+class TestOpenAiChatGPT:
+    @pytest.fixture
+    def setup_openai_chat_gpt(self):
+        config = {"model": "gpt-3.5-turbo", "max_tokens": 60}
+        with patch.dict(os.environ, {"OPENAI_API_KEY": "test_key"}):
+            return OpenAiChatGPT(config)
+
+    @patch('openai.ChatCompletion.create')
+    def test_query(self, mock_create, setup_openai_chat_gpt):
+        # Mock the response from ChatCompletion.create
+        mock_create.return_value = {
+            "choices": [
+                {"message": {"content": "answer1"}},
+                {"message": {"content": "answer2"}},
+            ]
+        }
+
+        issue = MyIssueDescriptor(language="en", tool="tool1", filename="file1", description="issue1")
+        result = setup_openai_chat_gpt.query("request", issue)
+        assert result == ["answer1", "answer2"]