From a8559737ebc7c08709bc5a94dd9af110e884d9a7 Mon Sep 17 00:00:00 2001 From: "Emmanuel I. Obi" Date: Sat, 30 Dec 2023 14:45:14 -0600 Subject: [PATCH] linting --- pokedex/api/__main__.py | 1 - pokedex/api/client.py | 6 ++---- pokedex/constants.py | 5 ++--- pokedex/db/actions.py | 21 ++++----------------- pokedex/db/client.py | 8 +++----- pokedex/db/models.py | 4 +++- tests/fixtures/__init__.py | 2 +- tests/pokedex/db/test_client.py | 22 +++++++++------------- 8 files changed, 24 insertions(+), 45 deletions(-) diff --git a/pokedex/api/__main__.py b/pokedex/api/__main__.py index eee622a..5673005 100644 --- a/pokedex/api/__main__.py +++ b/pokedex/api/__main__.py @@ -1,4 +1,3 @@ -import json import sys from argparse import ArgumentParser diff --git a/pokedex/api/client.py b/pokedex/api/client.py index 5d60b01..f7b1b87 100644 --- a/pokedex/api/client.py +++ b/pokedex/api/client.py @@ -1,12 +1,11 @@ from typing import Generator, Iterable, List, Optional -from actionpack import Procedure import requests - +from actionpack import Procedure from actionpack.actions import Call from actionpack.utils import Closure -from pokedex.api import Pokemon, PokeApiEndpoints +from pokedex.api import PokeApiEndpoints, Pokemon from pokedex.api.constants import BASE_URL from pokedex.api.models import PokeApiRequest, PokeApiResource, PokeApiResourceRef, PokemonRef @@ -16,7 +15,6 @@ def get_endpoints(endpoints_request: PokeApiRequest) -> PokeApiEndpoints: response.raise_for_status() endpoints: PokeApiEndpoints = response.json() if isinstance(endpoints, str): - print(type(endpoints), endpoints) raise TypeError() return endpoints diff --git a/pokedex/constants.py b/pokedex/constants.py index 0015726..d83635a 100644 --- a/pokedex/constants.py +++ b/pokedex/constants.py @@ -1,6 +1,5 @@ from pathlib import Path - PROJECTROOT = Path(__file__).parent.parent.absolute() -DBROOT = PROJECTROOT / 'pokedex' / 'db' -CACHEPATH = DBROOT / 'cache' +DBROOT = PROJECTROOT / "pokedex" / "db" +CACHEPATH = DBROOT / "cache" diff --git a/pokedex/db/actions.py b/pokedex/db/actions.py index 806b263..42e8fe3 100644 --- a/pokedex/db/actions.py +++ b/pokedex/db/actions.py @@ -5,23 +5,10 @@ from actionpack import Action -from pokedex.api import Pokemon from pokedex.constants import CACHEPATH from pokedex.db.models import DeferredRequest -@dataclass -class DbInsertPokemon(Action): - pokemon: Pokemon - db: Optional[str] = None - - def instruction(self) -> bool: - db = self.db or str(CACHEPATH) - with dbm.open(db, 'c') as cache: # create db if not exists - cache[self.name] = json.dumps(self.pokemon, indent=4) # consider alternative serialization - return self.pokemon['name'] - - @dataclass class DbInsertRequestResult(Action): key: AnyStr @@ -33,8 +20,8 @@ def __post_init__(self): def instruction(self) -> bool: db = self.db or str(CACHEPATH) - response = self.value() - with dbm.open(db, 'c') as cache: + response = self.value() # external call + with dbm.open(db, "c") as cache: cache[self.key] = json.dumps(response.json()) return True @@ -47,7 +34,7 @@ class DbInsert(Action): def instruction(self) -> str: db = self.db or str(CACHEPATH) - with dbm.open(db, 'c') as cache: + with dbm.open(db, "c") as cache: cache[self.key] = self.value return self.key @@ -59,5 +46,5 @@ class DbRead(Action[str, bytes]): def instruction(self) -> bytes: db = self.db or str(CACHEPATH) - with dbm.open(db, 'c') as cache: # create db if not exists + with dbm.open(db, "c") as cache: # create db if not exists return cache[self.key] diff --git a/pokedex/db/client.py b/pokedex/db/client.py index 4c40dbe..923be5e 100644 --- a/pokedex/db/client.py +++ b/pokedex/db/client.py @@ -1,12 +1,11 @@ import json -import requests from typing import Iterable +import requests from actionpack import KeyedProcedure -from pokedex.api import Pokemon +from pokedex.db.actions import DbInsert, DbInsertRequestResult, DbRead from pokedex.db.models import DeferredRequest -from pokedex.db.actions import DbRead, DbInsert, DbInsertRequestResult, DbInsertPokemon def persist_requests(requests: Iterable[DeferredRequest]): @@ -25,6 +24,5 @@ def cached_get(url: str) -> requests.Response: else: response = requests.get(url) response.raise_for_status() # TODO: handle error states - DbInsert(key=url, value=json.dumps(response.json()), - ).perform(should_raise=True) + DbInsert(key=url, value=json.dumps(response.json())).perform(should_raise=True) return response diff --git a/pokedex/db/models.py b/pokedex/db/models.py index ff982c1..2150cbe 100644 --- a/pokedex/db/models.py +++ b/pokedex/db/models.py @@ -1,7 +1,8 @@ import json from dataclasses import asdict, dataclass -from requests import Response from typing import Any, Dict, List, Type, Union + +from requests import Response from typing_extensions import Protocol @@ -14,6 +15,7 @@ def __call__(self) -> Response: JSON = Union[Dict[str, Any], List[Any], int, str, float, bool, Type[None]] + @dataclass class Report: persisted: dict diff --git a/tests/fixtures/__init__.py b/tests/fixtures/__init__.py index 4a5d802..0a01243 100644 --- a/tests/fixtures/__init__.py +++ b/tests/fixtures/__init__.py @@ -1,6 +1,6 @@ from pathlib import Path from typing import Dict, TypeVar -from unittest.mock import MagicMock, create_autospec +from unittest.mock import MagicMock from actionpack.action import Result from requests.models import Response diff --git a/tests/pokedex/db/test_client.py b/tests/pokedex/db/test_client.py index 054e93b..fcf9eb9 100644 --- a/tests/pokedex/db/test_client.py +++ b/tests/pokedex/db/test_client.py @@ -7,11 +7,11 @@ from pokedex.api.models import PokeApiRequest from pokedex.db.actions import DbRead from pokedex.db.client import cached_get, persist_requests -from tests.fixtures import craft_response, resource, craft_result +from tests.fixtures import craft_response, craft_result, resource class TestClientCanCache(TestCase): - @patch('pokedex.db.actions.DbInsertRequestResult') + @patch("pokedex.db.actions.DbInsertRequestResult") @patch.object( target=PokeApiRequest, attribute="__call__", @@ -20,7 +20,7 @@ class TestClientCanCache(TestCase): ], ) def test_can_persist_request_(self, mock_request, mock_db_insert): - request_url = 'https://pokeapi.co/api/v2/pokemon/39/' + request_url = "https://pokeapi.co/api/v2/pokemon/39/" mock_request.url = request_url key, response_data = next(persist_requests((mock_request,))) assert key is request_url @@ -36,33 +36,29 @@ class TestClientCanRetrieveFromCache(TestCase): ], ) def test_cache_hit(self, mock_db_reads): - pokemon_response = cached_get('https://pokeapi.co/api/v2/pokemon/39/') + pokemon_response = cached_get("https://pokeapi.co/api/v2/pokemon/39/") assert pokemon_response.json()["name"] == "jigglypuff" @patch.object( target=DbRead, attribute="perform", - side_effect=[ - craft_result(value=Exception('missing key.'), successful=False) - ], + side_effect=[craft_result(value=Exception("missing key."), successful=False)], ) @patch.object( target=requests, attribute="get", side_effect=[ - craft_response('something went wrong :/', status_code=500), + craft_response("something went wrong :/", status_code=500), ], ) def test_cache_miss_request_fail(self, mock_requests, mock_db_reads): with self.assertRaises(HTTPError): - cached_get('https://pokeapi.co/api/v2/pokemon/39/') + cached_get("https://pokeapi.co/api/v2/pokemon/39/") @patch.object( target=DbRead, attribute="perform", - side_effect=[ - craft_result(value=Exception('missing key.'), successful=False) - ], + side_effect=[craft_result(value=Exception("missing key."), successful=False)], ) @patch.object( target=PokeApiRequest, @@ -72,5 +68,5 @@ def test_cache_miss_request_fail(self, mock_requests, mock_db_reads): ], ) def test_cache_miss_request_success(self, mock_requests, mock_db_reads): - pokemon_response = cached_get('https://pokeapi.co/api/v2/pokemon/39/') + pokemon_response = cached_get("https://pokeapi.co/api/v2/pokemon/39/") assert pokemon_response.json()["name"] == "jigglypuff"