diff --git a/pyproject.toml b/pyproject.toml index babaab9..f00626f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,7 @@ asyncio_default_fixture_loop_scope = "function" [tool.ruff] line-length = 100 -target-version = "py312" +target-version = "py315" [tool.ruff.lint] select = ["B", "E", "F", "W", "I", "N", "PT", "D", "UP", "RUF", "SIM"] # Choose your rule categories diff --git a/src/pypricecharting/__init__.py b/src/pypricecharting/__init__.py index e69de29..4631696 100644 --- a/src/pypricecharting/__init__.py +++ b/src/pypricecharting/__init__.py @@ -0,0 +1,4 @@ +from pypricecharting.asyncpricecharting import AsyncPriceCharting +from pypricecharting.pricecharting import PriceCharting + +__all__ = ["AsyncPriceCharting", "PriceCharting"] diff --git a/src/pypricecharting/asyncpricecharting.py b/src/pypricecharting/asyncpricecharting.py index 41d70a2..c5dfa56 100644 --- a/src/pypricecharting/asyncpricecharting.py +++ b/src/pypricecharting/asyncpricecharting.py @@ -4,7 +4,7 @@ from httpx2 import AsyncClient -class AsyncPyPriceCharting: +class AsyncPriceCharting: """Asynchronous PriceCharting API client.""" def __init__(self, credential: str): diff --git a/src/pypricecharting/models.py b/src/pypricecharting/models.py new file mode 100644 index 0000000..673f4e7 --- /dev/null +++ b/src/pypricecharting/models.py @@ -0,0 +1,69 @@ +from datetime import date + +from pydantic import BaseModel, ConfigDict + +from pypricecharting.types import OfferStatus + + +def to_kebab(name: str) -> str: + """Convert a snake_case field name to the API's kebab-case key.""" + return name.replace("_", "-") + + +class PriceChartingModel(BaseModel): + """Base model that maps snake_case fields to the API's kebab-case keys.""" + + model_config = ConfigDict( + alias_generator=to_kebab, + populate_by_name=True, + ) + + +class ProductSummary(PriceChartingModel): + """A product as listed in /api/products search results. + + https://www.pricecharting.com/api-documentation#api-products + """ + + id: str | None = None + product_name: str | None = None + console_name: str | None = None + + +class Offer(PriceChartingModel): + """An offer as listed in /api/offers results. + + https://www.pricecharting.com/api-documentation#offers + """ + + offer_id: str | None = None + offer_status: OfferStatus | None = None + offer_url: str | None = None # relative URL, prepend https://www.pricecharting.com + id: int | None = None # PriceCharting product ID + product_name: str | None = None + console_name: str | None = None + condition_string: str | None = None + include_string: str | None = None + price: int | None = None # current price, or sale price with shipping premium if sold + is_available: bool | None = None + is_sold: bool | None = None + is_ended: bool | None = None + is_shipped: bool | None = None + sale_time: date | None = None + shipped_time: date | None = None + ended_time: date | None = None + cert_id: str | None = None # grading company certificate number, graded offers only + grade_co_id: int | None = None # grading company ID, graded offers only + + +class Buyer(PriceChartingModel): + """The buyer of a sold offer, as returned by /api/offer-details.""" + + buyer_email: str | None = None + shipping_name: str | None = None + shipping_line1: str | None = None + shipping_line2: str | None = None + shipping_city: str | None = None + shipping_state: str | None = None + shipping_zip: str | None = None + shipping_country: str | None = None diff --git a/src/pypricecharting/pypricecharting.py b/src/pypricecharting/pricecharting.py similarity index 98% rename from src/pypricecharting/pypricecharting.py rename to src/pypricecharting/pricecharting.py index 6bf374b..5c8e9ab 100644 --- a/src/pypricecharting/pypricecharting.py +++ b/src/pypricecharting/pricecharting.py @@ -4,7 +4,7 @@ from httpx2 import Client -class PyPriceCharting: +class PriceCharting: """Synchronous PriceCharting API client.""" def __init__(self, credential: str): diff --git a/src/pypricecharting/requests.py b/src/pypricecharting/requests.py new file mode 100644 index 0000000..0f4dc2a --- /dev/null +++ b/src/pypricecharting/requests.py @@ -0,0 +1,42 @@ +from typing import Self + +from pydantic import PositiveInt, model_validator + +from pypricecharting.models import PriceChartingModel +from pypricecharting.types import State + + +class OfferPublishRequest(PriceChartingModel): + """api/offer-publish request + + https://www.pricecharting.com/api-documentation#offer-publish + """ + + product: str | None = None + upc: str | None = None + asin: str | None = None + epid: str | None = None + offer_id: str | None = None + price_max: int + condition_id: str + add_to_collection: str | None = None + cost_basis: int | None = None + description: str | None = None + broken: State | None = None + pristine: State | None = None + scratch: State | None = None + stickers: State | None = None + tear: State | None = None + writing: State | None = None + price_min: PositiveInt | None = None + sku: str | None = None + quantity: PositiveInt | None = None + + @model_validator(mode="after") + def check_identifier(self) -> Self: + """Validate at least one identifier provided""" + ids = [self.product, self.upc, self.asin, self.epid, self.offer_id] + for id in ids: + if id is not None: + return self + raise ValueError("One of product, upc, asin, epid, or offer_id not provided") diff --git a/src/pypricecharting/responses.py b/src/pypricecharting/responses.py new file mode 100644 index 0000000..0d3e5fa --- /dev/null +++ b/src/pypricecharting/responses.py @@ -0,0 +1,184 @@ +from datetime import date + +from pydantic import Field + +from pypricecharting.models import Buyer, Offer, PriceChartingModel, ProductSummary +from pypricecharting.types import OfferStatus, Rating, Status + + +class ProductResponse(ProductSummary): + """api/product response + + Prices are in pennies. Price keys mean different conditions for video games, cards and + comics, noted below as video games / cards / comics. + + https://www.pricecharting.com/api-documentation#api-product + """ + + status: Status + genre: str | None = None + release_date: date | None = None + upc: str | None = None + asin: str | None = None # Amazon ASIN + epid: str | None = None # eBay ePID + tcg_id: str | None = None # TCGplayer ID, trading cards only + sales_volume: int | None = None # yearly units sold + + loose_price: int | None = None # item only / ungraded / ungraded + cib_price: int | None = None # box and manual / grade 7-7.5 / 4.0-4.5 + new_price: int | None = None # new and sealed / grade 8-8.5 / 6.0-6.5 + graded_price: int | None = None # graded by WATA or VGA / grade 9 / 8.0-8.5 + box_only_price: int | None = None # box only / grade 9.5 / 9.2 + manual_only_price: int | None = None # manual only / PSA 10 / 9.8 + bgs_10_price: int | None = None # cards BGS 10 / comics 10.0 + condition_9_price: int | None = None # cards grade 1 / comics 2.0 + condition_10_price: int | None = None # cards grade 2 / comics 9.6 + condition_13_price: int | None = None # cards grade 3 / comics 3.0 + condition_14_price: int | None = None # cards grade 4 / comics 5.0 + condition_15_price: int | None = None # cards grade 5 / comics 7.0 + condition_16_price: int | None = None # cards grade 6 / comics 9.0 + condition_17_price: int | None = None # cards CGC 10 / comics 9.4 + condition_18_price: int | None = None # cards SGC 10 + condition_19_price: int | None = None # cards CGC 10 Pristine + condition_20_price: int | None = None # cards BGS 10 Black + condition_21_price: int | None = None # cards TAG 10 + condition_22_price: int | None = None # cards ACE 10 + + gamestop_price: int | None = None # GameStop pre-owned price + retail_loose_buy: int | None = None + retail_loose_sell: int | None = None + retail_cib_buy: int | None = None + retail_cib_sell: int | None = None + retail_new_buy: int | None = None + retail_new_sell: int | None = None + + +class ProductsResponse(PriceChartingModel): + """api/products response + + https://www.pricecharting.com/api-documentation#api-products + """ + + status: Status + products: list[ProductSummary] = Field(default_factory=list) + + +class OffersResponse(PriceChartingModel): + """api/offers response + + https://www.pricecharting.com/api-documentation#offers + """ + + status: Status + offers: list[Offer] = Field(default_factory=list) + + +class OfferDetailsResponse(PriceChartingModel): + """api/offer-details response + + https://www.pricecharting.com/api-documentation#offer-details + """ + + status: Status + buyer: Buyer | None = None + buyer_left_feedback: bool | None = None + seller_left_feedback: bool | None = None + id: int | None = None # PriceCharting product ID + product_name: str | None = None + console_name: str | None = None + condition: str | None = None + includes: str | None = None + cost_basis: int | None = None + price: int | None = None + max_price: int | None = None + min_price: int | None = None + shipping_premium: int | None = None # extra paid by the buyer for international shipping + has_pictures: bool | None = None + is_collection: bool | None = None + is_ended: bool | None = None + is_price_descending: bool | None = None # reverse auction + is_refunded: bool | None = None + is_shipped: bool | None = None + is_sold: bool | None = None + offer_id: str | None = None + offer_status: OfferStatus | None = None + offer_url: str | None = None + sku: str | None = None # only returned to the seller + tracking_number: str | None = None + start_time: date | None = None + sale_time: date | None = None + shipped_time: date | None = None + ended_time: date | None = None + refunded_time: date | None = None + + +class OfferPublishResponse(PriceChartingModel): + """api/offer-publish response + + https://www.pricecharting.com/api-documentation#offer-publish + """ + + status: Status + offer_id: str | None = None + offer_url: str | None = None + offer_status: OfferStatus | None = None + product_name: str | None = None + console_name: str | None = None + condition_string: str | None = None + include_string: str | None = None + has_pictures: bool | None = None + price: int | None = None + cost_basis: int | None = None + sku: str | None = None + is_collection: bool | None = None + quantity: int | None = None + start_time: date | None = None + + +class OfferFeedbackResponse(PriceChartingModel): + """api/offer-feedback response + + https://www.pricecharting.com/api-documentation#offer-feedback + """ + + status: Status + offer_id: str | None = None + rating: Rating | None = None + comment: str | None = None + + +class OfferShipResponse(PriceChartingModel): + """api/offer-ship response + + https://www.pricecharting.com/api-documentation#offer-ship + """ + + status: Status + offer_id: str | None = None + is_shipped: bool | None = None + shipped_time: date | None = None + tracking_number: str | None = None + + +class OfferEndResponse(PriceChartingModel): + """api/offer-end response + + https://www.pricecharting.com/api-documentation#offer-end + """ + + status: Status + offer_id: str | None = None + is_ended: bool | None = None + ended_time: date | None = None + + +class OfferRefundResponse(PriceChartingModel): + """api/offer-refund response + + https://www.pricecharting.com/api-documentation#offer-refund + """ + + status: Status + offer_id: str | None = None + is_refunded: bool | None = None + refunded_time: date | None = None diff --git a/src/pypricecharting/types/console.py b/src/pypricecharting/types.py similarity index 63% rename from src/pypricecharting/types/console.py rename to src/pypricecharting/types.py index d4c4cbe..3109c10 100644 --- a/src/pypricecharting/types/console.py +++ b/src/pypricecharting/types.py @@ -1,11 +1,204 @@ +import datetime from enum import Enum, unique -# https://www.pricecharting.com/api-documentation#console-ids + +@unique +class Status(Enum): + """PriceCharting API response status""" + + SUCCESS = "success" + ERROR = "error" + + +@unique +class OfferStatus(Enum): + """PriceCharting Marketplace offer status + + PENDING means a vacation hold or payment issue. + """ + + AVAILABLE = "available" + SOLD = "sold" + ENDED = "ended" + PENDING = "pending" + COLLECTION = "collection" + + +@unique +class Rating(Enum): + """PriceCharting Marketplace feedback rating""" + + GREAT = 2 + GOOD = 1 + OK = 0 + BAD = -1 + AWFUL = -2 + + +@unique +class State(Enum): + """State flag""" + + ON = "on" + + +@unique +class RateLimit(Enum): + """Minimum wait between PriceCharting API calls.""" + + API = datetime.timedelta(seconds=1) + CSV = datetime.timedelta(minutes=10) + + # Offers requests rate limited to 5 minutes for the SAME query + OFFERS_POLLING = datetime.timedelta(minutes=5) + + +@unique +class VideoGames(Enum): + """Condition IDs for video game products. + + https://www.pricecharting.com/api-documentation#includes + """ + + LOOSE = 1 + NEW_AND_SEALED = 2 + CIB = 3 + GRADED = 5 + BOX_ONLY = 6 + MANUAL_ONLY = 7 + ITEM_AND_BOX = 8 + ITEM_AND_MANUAL = 9 + BOX_AND_MANUAL = 10 + GRADED_CIB = 13 + + +@unique +class Cards(Enum): + """Condition IDs for trading card products. + + https://www.pricecharting.com/api-documentation#includes + """ + + UNGRADED = 1 + GRADE_8 = 2 + GRADE_7 = 3 + GRADE_9 = 5 + GRADE_9_5 = 6 + GRADE_10 = 7 + BGS_10 = 8 + GRADE_1 = 9 + GRADE_2 = 10 + GRADE_3 = 13 + + +@unique +class Comics(Enum): + """Condition IDs for comic book products. + + https://www.pricecharting.com/api-documentation#includes + """ + + UNGRADED = 1 + GRADE_6_0 = 2 + GRADE_4_0 = 3 + GRADE_8_0 = 5 + GRADE_9_2 = 6 + GRADE_9_8 = 7 + GRADE_10_0 = 8 + GRADE_2_0 = 9 + GRADE_9_6 = 10 + GRADE_3_0 = 13 + + +@unique +class Coins(Enum): + """Condition IDs for coin products. + + https://www.pricecharting.com/api-documentation#includes + """ + + UNGRADED = 1 + AU_50_58 = 2 + VF_20_35 = 3 + MS_62 = 5 + MS_64 = 6 + MS_66 = 7 + F_12_15 = 8 + XF_40_45 = 9 + MS_60 = 10 + MS_61 = 13 + + +@unique +class Condition(Enum): + """Condition ID enums grouped by product category. + + https://www.pricecharting.com/api-documentation#includes + """ + + VIDEOGAMES = VideoGames + CARDS = Cards + COMICS = Comics + COINS = Coins + + +@unique +class Genre(Enum): + """Product genres as labeled by PriceCharting. + + https://www.pricecharting.com/api-documentation#genres + """ + + ACCESSORIES = "Accessories" + ACTION_AND_ADVENTURE = "Action & Adventure" + ARCADE = "Arcade" + BASEBALL = "Baseball" + BEAT_EM_UP = "Beat'em Up" + BASKETBALL = "Basketball" + BOARD_AND_CARD = "Board & Card" + CASINO = "Casino" + COMPILATION = "Compilation" + CONTROLLERS = "Controllers" + DANCE = "Dance" + DEMO_AND_NFR = "Demo & NFR" + EXTREME_SPORTS = "Extreme Sports" + EDUCATIONAL = "Educational" + FIGHTING = "Fighting" + FOOTBALL = "Football" + FPS = "FPS" + GOLF = "Golf" + HACK_AND_SLASH = "Hack and Slash" + HORROR = "Horror" + LIGHT_GUN = "Light Gun" + MAGAZINE = "Magazine" + MINI_GAMES = "Mini Games" + MOVIE = "Movie" + MUSIC = "Music" + OTHER = "Other" + PARTY = "Party" + PINBALL = "Pinball" + PLATFORMER = "Platformer" + PUZZLE = "Puzzle" + RACING = "Racing" + RPG = "RPG" + SIMULATION = "Simulation" + SHOOT_EM_UP = "Shoot'em Up" + SOCCER = "Soccer" + SPORTS = "Sports" + STRATEGY = "Strategy" + SYSTEMS = "Systems" + THIRD_PERSON_SHOOTER = "Third Person Shooter" + TRIVIA = "Trivia" + VISUAL_NOVEL = "Visual Novel" + WRESTLING = "Wrestling" @unique class Console(Enum): - """PriceCharting Console IDs""" + """PriceCharting Console IDs + + https://www.pricecharting.com/api-documentation#console-ids + """ PANASONIC_3DO = "G25" ACTION_MAX = "G36545" diff --git a/src/pypricecharting/types/condition.py b/src/pypricecharting/types/condition.py deleted file mode 100644 index 8c8132d..0000000 --- a/src/pypricecharting/types/condition.py +++ /dev/null @@ -1,77 +0,0 @@ -from enum import Enum, unique - -# https://www.pricecharting.com/api-documentation#includes - - -@unique -class VideoGames(Enum): - """Condition IDs for video game products.""" - - LOOSE = 1 - NEW_AND_SEALED = 2 - CIB = 3 - GRADED = 5 - BOX_ONLY = 6 - MANUAL_ONLY = 7 - ITEM_AND_BOX = 8 - ITEM_AND_MANUAL = 9 - BOX_AND_MANUAL = 10 - GRADED_CIB = 13 - - -@unique -class Cards(Enum): - """Condition IDs for trading card products.""" - - UNGRADED = 1 - GRADE_8 = 2 - GRADE_7 = 3 - GRADE_9 = 5 - GRADE_9_5 = 6 - GRADE_10 = 7 - BGS_10 = 8 - GRADE_1 = 9 - GRADE_2 = 10 - GRADE_3 = 13 - - -@unique -class Comics(Enum): - """Condition IDs for comic book products.""" - - UNGRADED = 1 - GRADE_6_0 = 2 - GRADE_4_0 = 3 - GRADE_8_0 = 5 - GRADE_9_2 = 6 - GRADE_9_8 = 7 - GRADE_10_0 = 8 - GRADE_2_0 = 9 - GRADE_9_6 = 10 - GRADE_3_0 = 13 - - -@unique -class Coins(Enum): - """Condition IDs for coin products.""" - - UNGRADED = 1 - AU_50_58 = 2 - VF_20_35 = 3 - MS_62 = 5 - MS_64 = 6 - MS_66 = 7 - F_12_15 = 8 - XF_40_45 = 9 - MS_60 = 10 - MS_61 = 13 - - -@unique -class Condition(Enum): - """Condition ID enums grouped by product category.""" - - VIDEOGAMES = VideoGames - CARDS = Cards - COMICS = Comics - COINS = Coins diff --git a/src/pypricecharting/types/genre.py b/src/pypricecharting/types/genre.py deleted file mode 100644 index 9421e43..0000000 --- a/src/pypricecharting/types/genre.py +++ /dev/null @@ -1,51 +0,0 @@ -from enum import Enum, unique - -# https://www.pricecharting.com/api-documentation#genres - - -@unique -class Genre(Enum): - """Product genres as labeled by PriceCharting.""" - - ACCESSORIES = "Accessories" - ACTION_AND_ADVENTURE = "Action & Adventure" - ARCADE = "Arcade" - BASEBALL = "Baseball" - BEAT_EM_UP = "Beat'em Up" - BASKETBALL = "Basketball" - BOARD_AND_CARD = "Board & Card" - CASINO = "Casino" - COMPILATION = "Compilation" - CONTROLLERS = "Controllers" - DANCE = "Dance" - DEMO_AND_NFR = "Demo & NFR" - EXTREME_SPORTS = "Extreme Sports" - EDUCATIONAL = "Educational" - FIGHTING = "Fighting" - FOOTBALL = "Football" - FPS = "FPS" - GOLF = "Golf" - HACK_AND_SLASH = "Hack and Slash" - HORROR = "Horror" - LIGHT_GUN = "Light Gun" - MAGAZINE = "Magazine" - MINI_GAMES = "Mini Games" - MOVIE = "Movie" - MUSIC = "Music" - OTHER = "Other" - PARTY = "Party" - PINBALL = "Pinball" - PLATFORMER = "Platformer" - PUZZLE = "Puzzle" - RACING = "Racing" - RPG = "RPG" - SIMULATION = "Simulation" - SHOOT_EM_UP = "Shoot'em Up" - SOCCER = "Soccer" - SPORTS = "Sports" - STRATEGY = "Strategy" - SYSTEMS = "Systems" - THIRD_PERSON_SHOOTER = "Third Person Shooter" - TRIVIA = "Trivia" - VISUAL_NOVEL = "Visual Novel" - WRESTLING = "Wrestling" diff --git a/src/pypricecharting/types/rate_limit.py b/src/pypricecharting/types/rate_limit.py deleted file mode 100644 index a1c4224..0000000 --- a/src/pypricecharting/types/rate_limit.py +++ /dev/null @@ -1,13 +0,0 @@ -import datetime -from enum import Enum, unique - - -@unique -class RateLimit(Enum): - """Minimum wait between PriceCharting API calls.""" - - API = datetime.timedelta(seconds=1) - CSV = datetime.timedelta(minutes=10) - - # Offers requests rate limited to 5 minutes for the SAME query - OFFERS_POLLING = datetime.timedelta(minutes=5) diff --git a/src/pypricecharting/types/status.py b/src/pypricecharting/types/status.py deleted file mode 100644 index 0404a6f..0000000 --- a/src/pypricecharting/types/status.py +++ /dev/null @@ -1,9 +0,0 @@ -from enum import Enum, unique - - -@unique -class Status(Enum): - """PriceCharting API response status""" - - SUCCESS = "success" - ERROR = "error" diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/models/__init__.py b/tests/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/models/test_buyer.py b/tests/models/test_buyer.py new file mode 100644 index 0000000..4642fd0 --- /dev/null +++ b/tests/models/test_buyer.py @@ -0,0 +1,44 @@ +import json + +from pypricecharting.models import Buyer + +BUYER_JSON = { + "buyer-email": "buyer@example.com", + "shipping-name": "Jane Doe", + "shipping-line1": "123 Main St", + "shipping-line2": "Apt 4", + "shipping-city": "Springfield", + "shipping-state": "IL", + "shipping-zip": "62701", + "shipping-country": "US", +} + + +def test_buyer_json_to_model(): + buyer = Buyer.model_validate_json(json.dumps(BUYER_JSON)) + + assert buyer.buyer_email == "buyer@example.com" + assert buyer.shipping_name == "Jane Doe" + assert buyer.shipping_line1 == "123 Main St" + assert buyer.shipping_line2 == "Apt 4" + assert buyer.shipping_city == "Springfield" + assert buyer.shipping_state == "IL" + assert buyer.shipping_zip == "62701" + assert buyer.shipping_country == "US" + + +def test_buyer_model_to_json(): + buyer = Buyer( + buyer_email="buyer@example.com", + shipping_name="Jane Doe", + shipping_line1="123 Main St", + shipping_line2="Apt 4", + shipping_city="Springfield", + shipping_state="IL", + shipping_zip="62701", + shipping_country="US", + ) + + dumped = buyer.model_dump_json(by_alias=True, exclude_none=True) + + assert json.loads(dumped) == BUYER_JSON diff --git a/tests/models/test_offer.py b/tests/models/test_offer.py new file mode 100644 index 0000000..fcac5bb --- /dev/null +++ b/tests/models/test_offer.py @@ -0,0 +1,84 @@ +import json +from datetime import date + +import pytest +from pydantic import ValidationError + +from pypricecharting.models import Offer +from pypricecharting.types import OfferStatus + +OFFER_JSON = { + "offer-id": "smzksacsy5i2dsrddxsrlzsp6i", + "offer-status": "sold", + "offer-url": "/offer/smzksacsy5i2dsrddxsrlzsp6i", + "id": 9307, + "product-name": "M.C. Kids", + "console-name": "NES", + "condition-string": "Normal wear", + "include-string": "Game only", + "price": 1473, + "is-available": False, + "is-sold": True, + "is-ended": True, + "is-shipped": False, + "sale-time": "2017-10-31", + "shipped-time": "0001-01-01", + "ended-time": "2017-10-31", + "cert-id": "5672930123", + "grade-co-id": 1, +} + + +def test_offer_json_to_model(): + offer = Offer.model_validate_json(json.dumps(OFFER_JSON)) + + assert offer.offer_id == "smzksacsy5i2dsrddxsrlzsp6i" + assert offer.offer_status is OfferStatus.SOLD + assert offer.offer_url == "/offer/smzksacsy5i2dsrddxsrlzsp6i" + assert offer.id == 9307 + assert offer.product_name == "M.C. Kids" + assert offer.console_name == "NES" + assert offer.condition_string == "Normal wear" + assert offer.include_string == "Game only" + assert offer.price == 1473 + assert offer.is_available is False + assert offer.is_sold is True + assert offer.is_ended is True + assert offer.is_shipped is False + assert offer.sale_time == date(2017, 10, 31) + assert offer.shipped_time == date(1, 1, 1) + assert offer.ended_time == date(2017, 10, 31) + assert offer.cert_id == "5672930123" + assert offer.grade_co_id == 1 + + +def test_offer_model_to_json(): + offer = Offer( + offer_id="smzksacsy5i2dsrddxsrlzsp6i", + offer_status=OfferStatus.SOLD, + offer_url="/offer/smzksacsy5i2dsrddxsrlzsp6i", + id=9307, + product_name="M.C. Kids", + console_name="NES", + condition_string="Normal wear", + include_string="Game only", + price=1473, + is_available=False, + is_sold=True, + is_ended=True, + is_shipped=False, + sale_time=date(2017, 10, 31), + shipped_time=date(1, 1, 1), + ended_time=date(2017, 10, 31), + cert_id="5672930123", + grade_co_id=1, + ) + + dumped = offer.model_dump_json(by_alias=True, exclude_none=True) + + assert json.loads(dumped) == OFFER_JSON + + +def test_offer_unknown_status_raises(): + with pytest.raises(ValidationError, match="offer-status"): + Offer.model_validate({"offer-status": "invalid"}) diff --git a/tests/models/test_price_charting_model.py b/tests/models/test_price_charting_model.py new file mode 100644 index 0000000..9e23cb7 --- /dev/null +++ b/tests/models/test_price_charting_model.py @@ -0,0 +1,41 @@ +import pytest + +from pypricecharting.models import PriceChartingModel, to_kebab + + +class Example(PriceChartingModel): + console_name: str + bgs_10_price: int + + +@pytest.mark.parametrize( + ("name", "expected"), + [ + ("id", "id"), + ("console_name", "console-name"), + ("bgs_10_price", "bgs-10-price"), + ("shipping_line1", "shipping-line1"), + ], +) +def test_to_kebab(name, expected): + assert to_kebab(name) == expected + + +def test_model_validates_by_alias(): + model = Example.model_validate({"console-name": "NES", "bgs-10-price": 100}) + + assert model.console_name == "NES" + assert model.bgs_10_price == 100 + + +def test_model_validates_by_name(): + model = Example(console_name="NES", bgs_10_price=100) + + assert model.console_name == "NES" + assert model.bgs_10_price == 100 + + +def test_model_dumps_by_alias(): + model = Example(console_name="NES", bgs_10_price=100) + + assert model.model_dump(by_alias=True) == {"console-name": "NES", "bgs-10-price": 100} diff --git a/tests/models/test_product_summary.py b/tests/models/test_product_summary.py new file mode 100644 index 0000000..c801bd0 --- /dev/null +++ b/tests/models/test_product_summary.py @@ -0,0 +1,33 @@ +import json + +from pypricecharting.models import ProductSummary + +PRODUCT_SUMMARY_JSON = { + "id": "4801", + "product-name": "Tactics Ogre", + "console-name": "Playstation", +} + + +def test_product_summary_json_to_model(): + summary = ProductSummary.model_validate_json(json.dumps(PRODUCT_SUMMARY_JSON)) + + assert summary.id == "4801" + assert summary.product_name == "Tactics Ogre" + assert summary.console_name == "Playstation" + + +def test_product_summary_model_to_json(): + summary = ProductSummary(id="4801", product_name="Tactics Ogre", console_name="Playstation") + + dumped = summary.model_dump_json(by_alias=True, exclude_none=True) + + assert json.loads(dumped) == PRODUCT_SUMMARY_JSON + + +def test_product_summary_defaults_to_none(): + summary = ProductSummary() + + assert summary.id is None + assert summary.product_name is None + assert summary.console_name is None diff --git a/tests/requests/__init__.py b/tests/requests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/requests/test_request.py b/tests/requests/test_request.py new file mode 100644 index 0000000..1853d74 --- /dev/null +++ b/tests/requests/test_request.py @@ -0,0 +1,54 @@ +import json + +import pytest +from pydantic import ValidationError + +from pypricecharting.requests import OfferPublishRequest +from pypricecharting.types import State + +OFFER_PUBLISH_JSON = { + "product": "6910", + "price-max": 2500, + "price-min": 1500, + "condition-id": "1", + "cost-basis": 1000, + "broken": "on", + "sku": "ABC-123", + "quantity": 2, +} + + +def test_offer_publish_json_to_model(): + request = OfferPublishRequest.model_validate_json(json.dumps(OFFER_PUBLISH_JSON)) + + assert request.product == "6910" + assert request.price_max == 2500 + assert request.price_min == 1500 + assert request.condition_id == "1" + assert request.cost_basis == 1000 + assert request.broken is State.ON + assert request.sku == "ABC-123" + assert request.quantity == 2 + assert request.upc is None + + +def test_offer_publish_model_to_json(): + request = OfferPublishRequest( + product="6910", + price_max=2500, + price_min=1500, + condition_id="1", + cost_basis=1000, + broken=State.ON, + sku="ABC-123", + quantity=2, + ) + + dumped = request.model_dump_json(by_alias=True, exclude_none=True) + + assert json.loads(dumped) == OFFER_PUBLISH_JSON + + +def test_offer_publish_model_raises(): + with pytest.raises(ValidationError, match="One of product, upc, asin, epid, or offer_id"): + OfferPublishRequest(price_max=2500, condition_id="1") diff --git a/tests/responses/__init__.py b/tests/responses/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/responses/test_offer_details.py b/tests/responses/test_offer_details.py new file mode 100644 index 0000000..ffd7fc6 --- /dev/null +++ b/tests/responses/test_offer_details.py @@ -0,0 +1,106 @@ +import json +from datetime import date + +from pypricecharting.models import Buyer +from pypricecharting.responses import OfferDetailsResponse +from pypricecharting.types import OfferStatus, Status + +OFFER_DETAILS_JSON = { + "status": "success", + "buyer": { + "buyer-email": "buyer@example.com", + "shipping-name": "Jane Doe", + "shipping-line1": "123 Main St", + "shipping-city": "Springfield", + "shipping-state": "IL", + "shipping-zip": "62701", + "shipping-country": "US", + }, + "buyer-left-feedback": False, + "seller-left-feedback": True, + "id": 9307, + "product-name": "M.C. Kids", + "console-name": "NES", + "condition": "Normal wear", + "includes": "Game only", + "cost-basis": 500, + "price": 1473, + "max-price": 2000, + "min-price": 1000, + "shipping-premium": 0, + "has-pictures": True, + "is-collection": False, + "is-ended": True, + "is-price-descending": True, + "is-refunded": False, + "is-shipped": True, + "is-sold": True, + "offer-id": "smzksacsy5i2dsrddxsrlzsp6i", + "offer-status": "sold", + "offer-url": "/offer/smzksacsy5i2dsrddxsrlzsp6i", + "sku": "ABC-123", + "tracking-number": "901234567890000", + "start-time": "2017-10-01", + "sale-time": "2017-10-31", + "shipped-time": "2017-11-01", + "ended-time": "2017-10-31", + "refunded-time": "0001-01-01", +} + + +def test_offer_details_json_to_model(): + details = OfferDetailsResponse.model_validate_json(json.dumps(OFFER_DETAILS_JSON)) + + assert details.status is Status.SUCCESS + assert details.buyer == Buyer( + buyer_email="buyer@example.com", + shipping_name="Jane Doe", + shipping_line1="123 Main St", + shipping_city="Springfield", + shipping_state="IL", + shipping_zip="62701", + shipping_country="US", + ) + assert details.buyer_left_feedback is False + assert details.seller_left_feedback is True + assert details.id == 9307 + assert details.product_name == "M.C. Kids" + assert details.console_name == "NES" + assert details.condition == "Normal wear" + assert details.includes == "Game only" + assert details.cost_basis == 500 + assert details.price == 1473 + assert details.max_price == 2000 + assert details.min_price == 1000 + assert details.shipping_premium == 0 + assert details.has_pictures is True + assert details.is_collection is False + assert details.is_ended is True + assert details.is_price_descending is True + assert details.is_refunded is False + assert details.is_shipped is True + assert details.is_sold is True + assert details.offer_id == "smzksacsy5i2dsrddxsrlzsp6i" + assert details.offer_status is OfferStatus.SOLD + assert details.offer_url == "/offer/smzksacsy5i2dsrddxsrlzsp6i" + assert details.sku == "ABC-123" + assert details.tracking_number == "901234567890000" + assert details.start_time == date(2017, 10, 1) + assert details.sale_time == date(2017, 10, 31) + assert details.shipped_time == date(2017, 11, 1) + assert details.ended_time == date(2017, 10, 31) + assert details.refunded_time == date(1, 1, 1) + + +def test_offer_details_model_to_json(): + details = OfferDetailsResponse.model_validate_json(json.dumps(OFFER_DETAILS_JSON)) + + dumped = details.model_dump_json(by_alias=True, exclude_none=True) + + assert json.loads(dumped) == OFFER_DETAILS_JSON + + +def test_offer_details_without_buyer(): + details = OfferDetailsResponse.model_validate_json('{"status": "success"}') + + assert details.buyer is None diff --git a/tests/responses/test_offer_end.py b/tests/responses/test_offer_end.py new file mode 100644 index 0000000..8f8d402 --- /dev/null +++ b/tests/responses/test_offer_end.py @@ -0,0 +1,34 @@ +import json +from datetime import date + +from pypricecharting.responses import OfferEndResponse +from pypricecharting.types import Status + +OFFER_END_JSON = { + "status": "success", + "offer-id": "vtdsww72uyslgooawzyyijsllm", + "is-ended": True, + "ended-time": "2017-10-31", +} + + +def test_offer_end_json_to_model(): + response = OfferEndResponse.model_validate_json(json.dumps(OFFER_END_JSON)) + + assert response.status is Status.SUCCESS + assert response.offer_id == "vtdsww72uyslgooawzyyijsllm" + assert response.is_ended is True + assert response.ended_time == date(2017, 10, 31) + + +def test_offer_end_model_to_json(): + response = OfferEndResponse( + status=Status.SUCCESS, + offer_id="vtdsww72uyslgooawzyyijsllm", + is_ended=True, + ended_time=date(2017, 10, 31), + ) + + dumped = response.model_dump_json(by_alias=True, exclude_none=True) + + assert json.loads(dumped) == OFFER_END_JSON diff --git a/tests/responses/test_offer_feedback.py b/tests/responses/test_offer_feedback.py new file mode 100644 index 0000000..ba45a70 --- /dev/null +++ b/tests/responses/test_offer_feedback.py @@ -0,0 +1,41 @@ +import json + +import pytest +from pydantic import ValidationError + +from pypricecharting.responses import OfferFeedbackResponse +from pypricecharting.types import Rating, Status + +OFFER_FEEDBACK_JSON = { + "status": "success", + "offer-id": "vtdsww72uyslgooawzyyijsllm", + "rating": 2, + "comment": "Great buyer", +} + + +def test_offer_feedback_json_to_model(): + response = OfferFeedbackResponse.model_validate_json(json.dumps(OFFER_FEEDBACK_JSON)) + + assert response.status is Status.SUCCESS + assert response.offer_id == "vtdsww72uyslgooawzyyijsllm" + assert response.rating is Rating.GREAT + assert response.comment == "Great buyer" + + +def test_offer_feedback_model_to_json(): + response = OfferFeedbackResponse( + status=Status.SUCCESS, + offer_id="vtdsww72uyslgooawzyyijsllm", + rating=Rating.GREAT, + comment="Great buyer", + ) + + dumped = response.model_dump_json(by_alias=True, exclude_none=True) + + assert json.loads(dumped) == OFFER_FEEDBACK_JSON + + +def test_offer_feedback_unknown_rating_raises(): + with pytest.raises(ValidationError, match="rating"): + OfferFeedbackResponse.model_validate({"status": "success", "rating": 3}) diff --git a/tests/responses/test_offer_publish.py b/tests/responses/test_offer_publish.py new file mode 100644 index 0000000..58f1eda --- /dev/null +++ b/tests/responses/test_offer_publish.py @@ -0,0 +1,67 @@ +import json +from datetime import date + +from pypricecharting.responses import OfferPublishResponse +from pypricecharting.types import OfferStatus, Status + +OFFER_PUBLISH_JSON = { + "status": "success", + "offer-id": "vtdsww72uyslgooawzyyijsllm", + "offer-url": "/offer/vtdsww72uyslgooawzyyijsllm", + "offer-status": "available", + "product-name": "EarthBound", + "console-name": "Super Nintendo", + "condition-string": "Normal wear", + "include-string": "Game only", + "has-pictures": False, + "price": 2500, + "cost-basis": 1000, + "sku": "ABC-123", + "is-collection": False, + "quantity": 2, + "start-time": "2017-10-31", +} + + +def test_offer_publish_json_to_model(): + response = OfferPublishResponse.model_validate_json(json.dumps(OFFER_PUBLISH_JSON)) + + assert response.status is Status.SUCCESS + assert response.offer_id == "vtdsww72uyslgooawzyyijsllm" + assert response.offer_url == "/offer/vtdsww72uyslgooawzyyijsllm" + assert response.offer_status is OfferStatus.AVAILABLE + assert response.product_name == "EarthBound" + assert response.console_name == "Super Nintendo" + assert response.condition_string == "Normal wear" + assert response.include_string == "Game only" + assert response.has_pictures is False + assert response.price == 2500 + assert response.cost_basis == 1000 + assert response.sku == "ABC-123" + assert response.is_collection is False + assert response.quantity == 2 + assert response.start_time == date(2017, 10, 31) + + +def test_offer_publish_model_to_json(): + response = OfferPublishResponse( + status=Status.SUCCESS, + offer_id="vtdsww72uyslgooawzyyijsllm", + offer_url="/offer/vtdsww72uyslgooawzyyijsllm", + offer_status=OfferStatus.AVAILABLE, + product_name="EarthBound", + console_name="Super Nintendo", + condition_string="Normal wear", + include_string="Game only", + has_pictures=False, + price=2500, + cost_basis=1000, + sku="ABC-123", + is_collection=False, + quantity=2, + start_time=date(2017, 10, 31), + ) + + dumped = response.model_dump_json(by_alias=True, exclude_none=True) + + assert json.loads(dumped) == OFFER_PUBLISH_JSON diff --git a/tests/responses/test_offer_refund.py b/tests/responses/test_offer_refund.py new file mode 100644 index 0000000..ec48080 --- /dev/null +++ b/tests/responses/test_offer_refund.py @@ -0,0 +1,34 @@ +import json +from datetime import date + +from pypricecharting.responses import OfferRefundResponse +from pypricecharting.types import Status + +OFFER_REFUND_JSON = { + "status": "success", + "offer-id": "vtdsww72uyslgooawzyyijsllm", + "is-refunded": True, + "refunded-time": "2017-10-31", +} + + +def test_offer_refund_json_to_model(): + response = OfferRefundResponse.model_validate_json(json.dumps(OFFER_REFUND_JSON)) + + assert response.status is Status.SUCCESS + assert response.offer_id == "vtdsww72uyslgooawzyyijsllm" + assert response.is_refunded is True + assert response.refunded_time == date(2017, 10, 31) + + +def test_offer_refund_model_to_json(): + response = OfferRefundResponse( + status=Status.SUCCESS, + offer_id="vtdsww72uyslgooawzyyijsllm", + is_refunded=True, + refunded_time=date(2017, 10, 31), + ) + + dumped = response.model_dump_json(by_alias=True, exclude_none=True) + + assert json.loads(dumped) == OFFER_REFUND_JSON diff --git a/tests/responses/test_offer_ship.py b/tests/responses/test_offer_ship.py new file mode 100644 index 0000000..7ead4c6 --- /dev/null +++ b/tests/responses/test_offer_ship.py @@ -0,0 +1,37 @@ +import json +from datetime import date + +from pypricecharting.responses import OfferShipResponse +from pypricecharting.types import Status + +OFFER_SHIP_JSON = { + "status": "success", + "offer-id": "vtdsww72uyslgooawzyyijsllm", + "is-shipped": True, + "shipped-time": "2017-10-31", + "tracking-number": "901234567890000", +} + + +def test_offer_ship_json_to_model(): + response = OfferShipResponse.model_validate_json(json.dumps(OFFER_SHIP_JSON)) + + assert response.status is Status.SUCCESS + assert response.offer_id == "vtdsww72uyslgooawzyyijsllm" + assert response.is_shipped is True + assert response.shipped_time == date(2017, 10, 31) + assert response.tracking_number == "901234567890000" + + +def test_offer_ship_model_to_json(): + response = OfferShipResponse( + status=Status.SUCCESS, + offer_id="vtdsww72uyslgooawzyyijsllm", + is_shipped=True, + shipped_time=date(2017, 10, 31), + tracking_number="901234567890000", + ) + + dumped = response.model_dump_json(by_alias=True, exclude_none=True) + + assert json.loads(dumped) == OFFER_SHIP_JSON diff --git a/tests/responses/test_offers.py b/tests/responses/test_offers.py new file mode 100644 index 0000000..e53112b --- /dev/null +++ b/tests/responses/test_offers.py @@ -0,0 +1,49 @@ +import json + +from pypricecharting.models import Offer +from pypricecharting.responses import OffersResponse +from pypricecharting.types import OfferStatus, Status + +OFFERS_JSON = { + "status": "success", + "offers": [ + {"offer-id": "smzksacsy5i2dsrddxsrlzsp6i", "offer-status": "sold", "price": 1473}, + {"offer-id": "vtdsww72uyslgooawzyyijsllm", "offer-status": "available", "price": 2500}, + ], +} + + +def test_offers_json_to_model(): + response = OffersResponse.model_validate_json(json.dumps(OFFERS_JSON)) + + assert response.status is Status.SUCCESS + assert response.offers == [ + Offer(offer_id="smzksacsy5i2dsrddxsrlzsp6i", offer_status=OfferStatus.SOLD, price=1473), + Offer( + offer_id="vtdsww72uyslgooawzyyijsllm", offer_status=OfferStatus.AVAILABLE, price=2500 + ), + ] + + +def test_offers_model_to_json(): + response = OffersResponse( + status=Status.SUCCESS, + offers=[ + Offer(offer_id="smzksacsy5i2dsrddxsrlzsp6i", offer_status=OfferStatus.SOLD, price=1473), + Offer( + offer_id="vtdsww72uyslgooawzyyijsllm", + offer_status=OfferStatus.AVAILABLE, + price=2500, + ), + ], + ) + + dumped = response.model_dump_json(by_alias=True, exclude_none=True) + + assert json.loads(dumped) == OFFERS_JSON + + +def test_offers_defaults_to_empty_list(): + response = OffersResponse.model_validate_json('{"status": "success"}') + + assert response.offers == [] diff --git a/tests/responses/test_products.py b/tests/responses/test_products.py new file mode 100644 index 0000000..7baea4b --- /dev/null +++ b/tests/responses/test_products.py @@ -0,0 +1,98 @@ +import json +from datetime import date + +from pypricecharting.models import ProductSummary +from pypricecharting.responses import ProductResponse, ProductsResponse +from pypricecharting.types import Status + +PRODUCT_JSON = { + "status": "success", + "id": "6910", + "product-name": "EarthBound", + "console-name": "Super Nintendo", + "release-date": "1995-06-05", + "sales-volume": 120, + "loose-price": 17244, + "cib-price": 42995, + "new-price": 53000, + "bgs-10-price": 60000, + "condition-9-price": 9000, + "retail-loose-buy": 12000, +} + +PRODUCTS_JSON = { + "status": "success", + "products": [ + {"console-name": "Playstation", "id": "4801", "product-name": "Tactics Ogre"}, + {"console-name": "GameBoy Advance", "id": "2611", "product-name": "Tactics Ogre"}, + ], +} + + +def test_product_json_to_model(): + product = ProductResponse.model_validate_json(json.dumps(PRODUCT_JSON)) + + assert product.status is Status.SUCCESS + assert product.id == "6910" + assert product.product_name == "EarthBound" + assert product.console_name == "Super Nintendo" + assert product.release_date == date(1995, 6, 5) + assert product.sales_volume == 120 + assert product.loose_price == 17244 + assert product.cib_price == 42995 + assert product.new_price == 53000 + assert product.bgs_10_price == 60000 + assert product.condition_9_price == 9000 + assert product.retail_loose_buy == 12000 + assert product.upc is None + + +def test_product_model_to_json(): + product = ProductResponse( + status=Status.SUCCESS, + id="6910", + product_name="EarthBound", + console_name="Super Nintendo", + release_date=date(1995, 6, 5), + sales_volume=120, + loose_price=17244, + cib_price=42995, + new_price=53000, + bgs_10_price=60000, + condition_9_price=9000, + retail_loose_buy=12000, + ) + + dumped = product.model_dump_json(by_alias=True, exclude_none=True) + + assert json.loads(dumped) == PRODUCT_JSON + + +def test_products_json_to_model(): + response = ProductsResponse.model_validate_json(json.dumps(PRODUCTS_JSON)) + + assert response.status is Status.SUCCESS + assert response.products == [ + ProductSummary(id="4801", product_name="Tactics Ogre", console_name="Playstation"), + ProductSummary(id="2611", product_name="Tactics Ogre", console_name="GameBoy Advance"), + ] + + +def test_products_model_to_json(): + response = ProductsResponse( + status=Status.SUCCESS, + products=[ + ProductSummary(id="4801", product_name="Tactics Ogre", console_name="Playstation"), + ProductSummary(id="2611", product_name="Tactics Ogre", console_name="GameBoy Advance"), + ], + ) + + dumped = response.model_dump_json(by_alias=True, exclude_none=True) + + assert json.loads(dumped) == PRODUCTS_JSON + + +def test_products_defaults_to_empty_list(): + response = ProductsResponse.model_validate_json('{"status": "success"}') + + assert response.products == [] diff --git a/tests/test_asyncpricecharting.py b/tests/test_asyncpricecharting.py index df47431..51f5e25 100644 --- a/tests/test_asyncpricecharting.py +++ b/tests/test_asyncpricecharting.py @@ -1,7 +1,7 @@ import pytest from httpx2 import AsyncClient -from pypricecharting.asyncpricecharting import AsyncPyPriceCharting +from pypricecharting.asyncpricecharting import AsyncPriceCharting # Template tests: the API methods are placeholders, so these only check they can be called. # Replace the `is None` assertions once each method is implemented. @@ -10,39 +10,39 @@ async def test_init_stores_credential(): - client = AsyncPyPriceCharting(TOKEN) + client = AsyncPriceCharting(TOKEN) assert client.credential == TOKEN await client.aclose() async def test_init_creates_http_client(): - client = AsyncPyPriceCharting(TOKEN) + client = AsyncPriceCharting(TOKEN) assert isinstance(client.client, AsyncClient) assert not client.client.is_closed await client.aclose() async def test_aclose_closes_http_client(): - client = AsyncPyPriceCharting(TOKEN) + client = AsyncPriceCharting(TOKEN) await client.aclose() assert client.client.is_closed async def test_async_with_returns_client(): - client = AsyncPyPriceCharting(TOKEN) + client = AsyncPriceCharting(TOKEN) async with client as entered: assert entered is client assert not client.client.is_closed async def test_async_with_closes_http_client(): - async with AsyncPyPriceCharting(TOKEN) as client: + async with AsyncPriceCharting(TOKEN) as client: pass assert client.client.is_closed async def test_async_with_closes_http_client_on_error(): - client = AsyncPyPriceCharting(TOKEN) + client = AsyncPriceCharting(TOKEN) with pytest.raises(ValueError, match="boom"): async with client: raise ValueError("boom") @@ -50,54 +50,54 @@ async def test_async_with_closes_http_client_on_error(): async def test_product(): - client = AsyncPyPriceCharting(TOKEN) + client = AsyncPriceCharting(TOKEN) assert await client.product() is None await client.aclose() async def test_products(): - client = AsyncPyPriceCharting(TOKEN) + client = AsyncPriceCharting(TOKEN) assert await client.products() is None await client.aclose() async def test_offers(): - client = AsyncPyPriceCharting(TOKEN) + client = AsyncPriceCharting(TOKEN) assert await client.offers() is None await client.aclose() async def test_offer_details(): - client = AsyncPyPriceCharting(TOKEN) + client = AsyncPriceCharting(TOKEN) assert await client.offer_details() is None await client.aclose() async def test_offer_publish(): - client = AsyncPyPriceCharting(TOKEN) + client = AsyncPriceCharting(TOKEN) assert await client.offer_publish() is None await client.aclose() async def test_offer_feedback(): - client = AsyncPyPriceCharting(TOKEN) + client = AsyncPriceCharting(TOKEN) assert await client.offer_feedback() is None await client.aclose() async def test_offer_ship(): - client = AsyncPyPriceCharting(TOKEN) + client = AsyncPriceCharting(TOKEN) assert await client.offer_ship() is None await client.aclose() async def test_offer_end(): - client = AsyncPyPriceCharting(TOKEN) + client = AsyncPriceCharting(TOKEN) assert await client.offer_end() is None await client.aclose() async def test_offer_refund(): - client = AsyncPyPriceCharting(TOKEN) + client = AsyncPriceCharting(TOKEN) assert await client.offer_refund() is None await client.aclose() diff --git a/tests/test_pricecharting.py b/tests/test_pricecharting.py index f0bd18a..3d806bc 100644 --- a/tests/test_pricecharting.py +++ b/tests/test_pricecharting.py @@ -1,7 +1,7 @@ import pytest from httpx2 import Client -from pypricecharting.pypricecharting import PyPriceCharting +from pypricecharting.pricecharting import PriceCharting # Template tests: the API methods are placeholders, so these only check they can be called. # Replace the `is None` assertions once each method is implemented. @@ -10,93 +10,93 @@ def test_init_stores_credential(): - client = PyPriceCharting(TOKEN) + client = PriceCharting(TOKEN) assert client.credential == TOKEN client.close() def test_init_creates_http_client(): - client = PyPriceCharting(TOKEN) + client = PriceCharting(TOKEN) assert isinstance(client.client, Client) assert not client.client.is_closed client.close() def test_close_closes_http_client(): - client = PyPriceCharting(TOKEN) + client = PriceCharting(TOKEN) client.close() assert client.client.is_closed def test_with_returns_client(): - client = PyPriceCharting(TOKEN) + client = PriceCharting(TOKEN) with client as entered: assert entered is client assert not client.client.is_closed def test_with_closes_http_client(): - with PyPriceCharting(TOKEN) as client: + with PriceCharting(TOKEN) as client: pass assert client.client.is_closed def test_with_closes_http_client_on_error(): - client = PyPriceCharting(TOKEN) + client = PriceCharting(TOKEN) with pytest.raises(ValueError, match="boom"), client: raise ValueError("boom") assert client.client.is_closed def test_product(): - client = PyPriceCharting(TOKEN) + client = PriceCharting(TOKEN) assert client.product() is None client.close() def test_products(): - client = PyPriceCharting(TOKEN) + client = PriceCharting(TOKEN) assert client.products() is None client.close() def test_offers(): - client = PyPriceCharting(TOKEN) + client = PriceCharting(TOKEN) assert client.offers() is None client.close() def test_offer_details(): - client = PyPriceCharting(TOKEN) + client = PriceCharting(TOKEN) assert client.offer_details() is None client.close() def test_offer_publish(): - client = PyPriceCharting(TOKEN) + client = PriceCharting(TOKEN) assert client.offer_publish() is None client.close() def test_offer_feedback(): - client = PyPriceCharting(TOKEN) + client = PriceCharting(TOKEN) assert client.offer_feedback() is None client.close() def test_offer_ship(): - client = PyPriceCharting(TOKEN) + client = PriceCharting(TOKEN) assert client.offer_ship() is None client.close() def test_offer_end(): - client = PyPriceCharting(TOKEN) + client = PriceCharting(TOKEN) assert client.offer_end() is None client.close() def test_offer_refund(): - client = PyPriceCharting(TOKEN) + client = PriceCharting(TOKEN) assert client.offer_refund() is None client.close() diff --git a/tests/test_route.py b/tests/test_route.py index 916b017..b188e43 100644 --- a/tests/test_route.py +++ b/tests/test_route.py @@ -28,6 +28,7 @@ def test_marketplace_route(): assert MarketPlace.OFFER_DETAILS.value == "/api/offer-details" assert MarketPlace.OFFER_FEEDBACK.value == "/api/offer-feedback" assert MarketPlace.OFFER_SHIP.value == "/api/offer-ship" + assert MarketPlace.OFFER_REFUND.value == "/api/offer-refund" assert MarketPlace.OFFER_END.value == "/api/offer-end" @@ -47,7 +48,7 @@ def test_route_routes(): assert Route.PRICES.value is Prices -def test_genre_lookup_by_value(): +def test_route_lookup_by_value(): for member in Route: assert Route(member.value) is member diff --git a/tests/types/__init__.py b/tests/types/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/types_tests/test_condition.py b/tests/types/test_condition.py similarity index 97% rename from tests/types_tests/test_condition.py rename to tests/types/test_condition.py index 5e22452..ba52754 100644 --- a/tests/types_tests/test_condition.py +++ b/tests/types/test_condition.py @@ -1,6 +1,6 @@ import pytest -from pypricecharting.types.condition import Cards, Coins, Comics, Condition, VideoGames +from pypricecharting.types import Cards, Coins, Comics, Condition, VideoGames # Source: https://www.pricecharting.com/api-documentation#includes diff --git a/tests/types_tests/test_console.py b/tests/types/test_console.py similarity index 99% rename from tests/types_tests/test_console.py rename to tests/types/test_console.py index 7b2a414..50da2b7 100644 --- a/tests/types_tests/test_console.py +++ b/tests/types/test_console.py @@ -1,6 +1,6 @@ import pytest -from pypricecharting.types.console import Console +from pypricecharting.types import Console # Source: https://www.pricecharting.com/api-documentation#console-ids diff --git a/tests/types_tests/test_genre.py b/tests/types/test_genre.py similarity index 98% rename from tests/types_tests/test_genre.py rename to tests/types/test_genre.py index c9c00f1..942c56a 100644 --- a/tests/types_tests/test_genre.py +++ b/tests/types/test_genre.py @@ -1,6 +1,6 @@ import pytest -from pypricecharting.types.genre import Genre +from pypricecharting.types import Genre # Source: https://www.pricecharting.com/api-documentation#genres diff --git a/tests/types/test_offer_status.py b/tests/types/test_offer_status.py new file mode 100644 index 0000000..846bbc9 --- /dev/null +++ b/tests/types/test_offer_status.py @@ -0,0 +1,22 @@ +import pytest + +from pypricecharting.types import OfferStatus + + +def test_offer_status_members(): + assert len(OfferStatus) == 5 + assert OfferStatus.AVAILABLE.value == "available" + assert OfferStatus.SOLD.value == "sold" + assert OfferStatus.ENDED.value == "ended" + assert OfferStatus.PENDING.value == "pending" + assert OfferStatus.COLLECTION.value == "collection" + + +def test_offer_status_lookup_by_value(): + for member in OfferStatus: + assert OfferStatus(member.value) is member + + +def test_offer_status_unknown_value_raises(): + with pytest.raises(ValueError, match="is not a valid"): + OfferStatus("invalid") diff --git a/tests/types_tests/test_ratelimit.py b/tests/types/test_ratelimit.py similarity index 89% rename from tests/types_tests/test_ratelimit.py rename to tests/types/test_ratelimit.py index 4ea0222..2bf59eb 100644 --- a/tests/types_tests/test_ratelimit.py +++ b/tests/types/test_ratelimit.py @@ -2,7 +2,7 @@ import pytest -from pypricecharting.types.rate_limit import RateLimit +from pypricecharting.types import RateLimit def test_ratelimit_members(): diff --git a/tests/types/test_rating.py b/tests/types/test_rating.py new file mode 100644 index 0000000..1054702 --- /dev/null +++ b/tests/types/test_rating.py @@ -0,0 +1,22 @@ +import pytest + +from pypricecharting.types import Rating + + +def test_rating_members(): + assert len(Rating) == 5 + assert Rating.GREAT.value == 2 + assert Rating.GOOD.value == 1 + assert Rating.OK.value == 0 + assert Rating.BAD.value == -1 + assert Rating.AWFUL.value == -2 + + +def test_rating_lookup_by_value(): + for member in Rating: + assert Rating(member.value) is member + + +def test_rating_unknown_value_raises(): + with pytest.raises(ValueError, match="is not a valid"): + Rating(3) diff --git a/tests/types/test_state.py b/tests/types/test_state.py new file mode 100644 index 0000000..24479d5 --- /dev/null +++ b/tests/types/test_state.py @@ -0,0 +1,17 @@ +import pytest + +from pypricecharting.types import State + + +def test_state_members(): + assert len(State) == 1 + assert State.ON.value == "on" + + +def test_state_lookup_by_value(): + assert State("on") is State.ON + + +def test_state_unknown_value_raises(): + with pytest.raises(ValueError, match="is not a valid"): + State("off") diff --git a/tests/types_tests/test_status.py b/tests/types/test_status.py similarity index 88% rename from tests/types_tests/test_status.py rename to tests/types/test_status.py index ffc7412..e50818f 100644 --- a/tests/types_tests/test_status.py +++ b/tests/types/test_status.py @@ -1,6 +1,6 @@ import pytest -from pypricecharting.types.status import Status +from pypricecharting.types import Status def test_status():