From 32c570eede7beecd151ebc60e3dc4b7d213bb779 Mon Sep 17 00:00:00 2001 From: Shane Jaroch Date: Fri, 12 Apr 2024 16:06:58 -0400 Subject: [PATCH] bugs: mock API call, still need to mock DB call --- Makefile | 9 ++++----- ntclient/persistence/sql/__init__.py | 16 +++++++++------- ntclient/services/api/__init__.py | 2 +- ntclient/services/bugs.py | 6 +++--- tests/services/test_bug.py | 11 ++++++++--- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 89aae81..860aaf4 100644 --- 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),) diff --git a/ntclient/persistence/sql/__init__.py b/ntclient/persistence/sql/__init__.py index 18d28da..4fb11ce 100644 --- a/ntclient/persistence/sql/__init__.py +++ b/ntclient/persistence/sql/__init__.py @@ -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) diff --git a/ntclient/services/api/__init__.py b/ntclient/services/api/__init__.py index 60dd084..6a67d99 100644 --- a/ntclient/services/api/__init__.py +++ b/ntclient/services/api/__init__.py @@ -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 diff --git a/ntclient/services/bugs.py b/ntclient/services/bugs.py index b408bb7..68e3708 100644 --- a/ntclient/services/bugs.py +++ b/ntclient/services/bugs.py @@ -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 diff --git a/tests/services/test_bug.py b/tests/services/test_bug.py index 6f258c1..93b5a69 100644 --- a/tests/services/test_bug.py +++ b/tests/services/test_bug.py @@ -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() -- 2.52.0