]> Nutra Git (v2) - nutratech/cli.git/commitdiff
bugs: mock API call, still need to mock DB call
authorShane Jaroch <chown_tee@proton.me>
Fri, 12 Apr 2024 20:06:58 +0000 (16:06 -0400)
committerShane Jaroch <chown_tee@proton.me>
Fri, 12 Apr 2024 20:06:58 +0000 (16:06 -0400)
Makefile
ntclient/persistence/sql/__init__.py
ntclient/services/api/__init__.py
ntclient/services/bugs.py
tests/services/test_bug.py

index 89aae81d8e92705eeb6cd35c5d536def1d2d58ad..860aaf418364f18326571f4d5fcec6175eece247 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -76,6 +76,10 @@ deps: _venv  ## Install requirements
 # Format, lint, test
 # ---------------------------------------
 
+# LINT_LOCS := ntclient/ tests/ setup.py
+CHANGED_FILES_RST ?= $(shell git diff origin/master --name-only --diff-filter=MACRU \*.rst)
+CHANGED_FILES_PY ?= $(shell git diff origin/master --name-only --diff-filter=MACRU \*.py)
+
 .PHONY: format
 format: _venv  ## Format with isort & black
 ifneq ($(CHANGED_FILES_PY),)
@@ -85,11 +89,6 @@ else
        $(info No changed Python files, skipping.)
 endif
 
-
-# LINT_LOCS := ntclient/ tests/ setup.py
-CHANGED_FILES_RST ?= $(shell git diff origin/master --name-only --diff-filter=MACRU \*.rst)
-CHANGED_FILES_PY ?= $(shell git diff origin/master --name-only --diff-filter=MACRU \*.py)
-
 .PHONY: lint
 lint: _venv    ## Lint code and documentation
 ifneq ($(CHANGED_FILES_RST),)
index 18d28da01e4bc3c8a4fb6bcca006d6015e106b0c..4fb11ce58c1bf3055f56b586d74c587e7bf9e4eb 100644 (file)
@@ -19,14 +19,13 @@ def sql_entries(sql_result: sqlite3.Cursor) -> tuple[list, list, int, Optional[i
     TODO: return object: metadata, command, status, errors, etc?
     """
 
+    rows = sql_result.fetchall()
+    headers = [x[0] for x in (sql_result.description if sql_result.description else [])]
+
     return (
-        # rows
-        sql_result.fetchall(),
-        # headers
-        [x[0] for x in sql_result.description],
-        # row_count
+        rows,
+        headers,
         sql_result.rowcount,
-        # last_row_id
         sql_result.lastrowid,
     )
 
@@ -92,8 +91,11 @@ def _prep_query(
     if values:
         if isinstance(values, list):
             cur.executemany(query, values)
-        else:  # tuple
+        elif isinstance(values, tuple):
             cur.execute(query, values)
+        else:
+            raise TypeError("'values' must be a list or tuple!")
+
     else:
         cur.execute(query)
 
index 60dd084610cbf019188d893685069bbf3a414461..6a67d99c599287d678fc2c6e41709b329c27e46d 100644 (file)
@@ -14,7 +14,7 @@ REQUEST_CONNECT_TIMEOUT = 5
 
 # TODO: try all of these; cache (save in prefs.json) the one which works first
 URLS_API = (
-    "https://api.nutra.tk",
+    "https://api.nutra.tk",
     "https://api.dev.nutra.tk",
     "http://216.218.228.93",  # dev
     "http://216.218.216.163",  # prod
index b408bb74e814a5770b26d78c75b6be7b36e8eeb9..68e370854154100b06feea1afd5bdf26275dcd93 100644 (file)
@@ -71,7 +71,7 @@ def list_bugs() -> tuple[list, list]:
 
 def submit_bugs() -> int:
     """Submit bug reports to developer, return n_submitted."""
-
+    # TODO: mock sql_nt() for testing
     # Gather bugs for submission
     rows, _, _, _ = sql_nt("SELECT * FROM bug WHERE submitted = 0")
     api_client = ntclient.services.api.ApiClient()
@@ -87,9 +87,9 @@ def submit_bugs() -> int:
 
         # Distinguish bug which are unique vs. duplicates (someone else submitted)
         if _res.status_code == 201:
-            sql_nt("UPDATE bug SET submitted = 1 WHERE id = %s", bug.id)
+            sql_nt("UPDATE bug SET submitted = 1 WHERE id = ?", (bug["id"],))
         elif _res.status_code == 204:
-            sql_nt("UPDATE bug SET submitted = 2 WHERE id = %s", bug.id)
+            sql_nt("UPDATE bug SET submitted = 2 WHERE id = ?", (bug["id"],))
         else:
             print("WARN: unknown status [{0}]".format(_res.status_code))
             continue
index 6f258c13b63840f6bb96c0964c564422315776f9..93b5a693b7ece808160d56794409225a220a7c36 100644 (file)
@@ -5,6 +5,7 @@ Created on Sun Feb 25 16:18:08 2024
 @author: shane
 """
 import unittest
+from unittest.mock import MagicMock, patch
 
 import pytest
 
@@ -24,8 +25,12 @@ class TestBug(unittest.TestCase):
         """Tests the functions for listing bugs"""
         bugs.list_bugs()
 
-    @unittest.expectedFailure
-    @pytest.mark.xfail(reason="Work in progress, need to get mocks working")
-    def test_bug_report(self) -> None:
+    @patch("ntclient.services.api.cache_mirrors", return_value="https://someurl.com")
+    @patch(
+        "ntclient.services.api.ApiClient.post",
+        return_value=MagicMock(status_code=201),
+    )
+    # pylint: disable=unused-argument
+    def test_bug_report(self, *args: MagicMock) -> None:
         """Tests the functions for submitting bugs"""
         bugs.submit_bugs()