diff --git a/.flake8 b/.flake8 deleted file mode 100644 index 96c59bf..0000000 --- a/.flake8 +++ /dev/null @@ -1,3 +0,0 @@ -[flake8] -max-line-length = 88 -extend-ignore = E203, E704, Q000 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 8d593aa..0a0b60c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,177 @@ -.vscode -__pycache__ -jsons -config.py +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# UV +# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +#uv.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/latest/usage/project/#working-with-version-control +.pdm.toml +.pdm-python +.pdm-build/ + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +# Ruff stuff: +.ruff_cache/ + +# PyPI configuration file +.pypirc + +# Other _build \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9c734d2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,9 @@ +{ + "editor.formatOnSave": true, + "editor.defaultFormatter": "charliermarsh.ruff", + "[python]": { + "editor.codeActionsOnSave": { + "source.organizeImports": "explicit" + } + } +} \ No newline at end of file diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..d072e13 --- /dev/null +++ b/conftest.py @@ -0,0 +1,11 @@ +import pytest + + +@pytest.fixture +def marketplace_tf_api_key() -> str: + return "api_key" + + +@pytest.fixture +def backpack_tf_token() -> str: + return "backpack_tf_token" diff --git a/src/tf2_utils/__init__.py b/src/tf2_utils/__init__.py index 758865d..c629e00 100644 --- a/src/tf2_utils/__init__.py +++ b/src/tf2_utils/__init__.py @@ -1,7 +1,7 @@ # flake8: noqa __title__ = "tf2-utils" __author__ = "offish" -__version__ = "2.2.0" +__version__ = "2.3.0" __license__ = "MIT" from .sku import * diff --git a/src/tf2_utils/backpack_tf.py b/src/tf2_utils/backpack_tf.py index dfec4be..e1e13b8 100644 --- a/src/tf2_utils/backpack_tf.py +++ b/src/tf2_utils/backpack_tf.py @@ -1,12 +1,11 @@ -import requests - from dataclasses import dataclass, field from hashlib import md5 -from .schema import SchemaItemsUtils -from .sku import sku_to_quality, sku_is_craftable -from . import __title__ +import requests +from . import __title__ +from .schema import SchemaItemsUtils +from .sku import sku_is_craftable, sku_to_quality __all__ = [ "Currencies", diff --git a/src/tf2_utils/currency.py b/src/tf2_utils/currency.py index 9461117..e9cc392 100644 --- a/src/tf2_utils/currency.py +++ b/src/tf2_utils/currency.py @@ -24,7 +24,7 @@ def __init__( self.key_price = key_price self.item_is_not_pure = item_is_not_pure - self.__is_possible = False + self._is_possible = False self.their_scrap = 0 self.our_scrap = 0 self.their_overview = {} @@ -74,12 +74,8 @@ def get_pure_in_inventory(self, inventory: list[dict]) -> tuple[int, list[dict]] return scrap, metal - def is_possible(self) -> bool: - return self.__is_possible - - def __overview_to_items( - self, combination: list[str], inventory: list[dict] - ) -> list[dict]: + @staticmethod + def _overview_to_items(combination: list[str], inventory: list[dict]) -> list[dict]: items = [] for metal_name in combination: @@ -97,12 +93,12 @@ def __overview_to_items( return items def get_currencies(self) -> tuple[list[dict], list[dict]]: - assert self.__is_possible, "Currencies does not add up" + assert self._is_possible, "Currencies does not add up" - their_items = self.__overview_to_items( + their_items = self._overview_to_items( self.their_combination, self.their_inventory ) - our_items = self.__overview_to_items(self.our_combination, self.our_inventory) + our_items = self._overview_to_items(self.our_combination, self.our_inventory) return their_items, our_items @@ -125,7 +121,7 @@ def format_overview(pure: list[dict]) -> dict: return overview - def __pick_currencies(self, user: str) -> tuple[bool, list[str]]: + def _pick_currencies(self, user: str) -> tuple[bool, list[str]]: """ref, ref, scrap, rec""" overview = ( self.their_overview.copy() if user == "them" else self.our_overview.copy() @@ -166,7 +162,7 @@ def __pick_currencies(self, user: str) -> tuple[bool, list[str]]: return True, combination - def __does_add_up(self) -> bool: + def _adds_up(self) -> bool: their_value = 0 our_value = 0 @@ -184,9 +180,9 @@ def __does_add_up(self) -> bool: return their_value == our_value - def __set_combinations(self) -> bool: - success_our, our_combination = self.__pick_currencies("us") - success_their, their_combination = self.__pick_currencies("them") + def _set_combinations(self) -> bool: + success_our, our_combination = self._pick_currencies("us") + success_their, their_combination = self._pick_currencies("them") if not success_our or not success_their: return False @@ -196,7 +192,7 @@ def __set_combinations(self) -> bool: return True - def __has_enough(self) -> bool: + def _has_enough(self) -> bool: temp_price = self.scrap_price if self.item_is_not_pure: @@ -214,11 +210,11 @@ def calculate(self) -> None: self.their_overview = self.format_overview(their_pure) self.our_overview = self.format_overview(our_pure) - if not self.__has_enough(): + if not self._has_enough(): return - while self.__has_enough(): - success = self.__set_combinations() + while self._has_enough(): + success = self._set_combinations() if success: break @@ -227,8 +223,12 @@ def calculate(self) -> None: # try again, do this till we have either user does not have enough anymore self.scrap_price += 1 - if not self.__does_add_up(): + if not self._adds_up(): return # everything adds up and looks good - self.__is_possible = True + self._is_possible = True + + @property + def is_possible(self) -> bool: + return self._is_possible diff --git a/src/tf2_utils/inventory.py b/src/tf2_utils/inventory.py index 6e30ca0..6ed9db2 100644 --- a/src/tf2_utils/inventory.py +++ b/src/tf2_utils/inventory.py @@ -1,11 +1,11 @@ +import requests + +from .providers.custom import Custom +from .providers.steamapis import SteamApis from .providers.steamcommunity import SteamCommunity from .providers.steamsupply import SteamSupply -from .providers.steamapis import SteamApis -from .providers.custom import Custom from .sku import get_sku -import requests - def map_inventory(inventory: dict, add_skus: bool = False) -> list[dict]: """Matches classids and instanceids, merges these and diff --git a/src/tf2_utils/item.py b/src/tf2_utils/item.py index 1f871e9..a3dc246 100644 --- a/src/tf2_utils/item.py +++ b/src/tf2_utils/item.py @@ -1,4 +1,4 @@ -from tf2_data import QUALITIES, KILLSTREAKS, EXTERIORS +from tf2_data import EXTERIORS, KILLSTREAKS, QUALITIES class Item: diff --git a/src/tf2_utils/marketplace_tf.py b/src/tf2_utils/marketplace_tf.py index 6deb519..eef99b0 100644 --- a/src/tf2_utils/marketplace_tf.py +++ b/src/tf2_utils/marketplace_tf.py @@ -1,10 +1,9 @@ -from .schema import SchemaItemsUtils -from .sku import sku_to_quality_name, sku_is_craftable - import time import requests +from .schema import SchemaItemsUtils +from .sku import sku_is_craftable, sku_to_quality_name __all__ = ["MarketplaceTF", "MarketplaceTFException", "SKUDoesNotMatch", "NoAPIKey"] diff --git a/src/tf2_utils/offer.py b/src/tf2_utils/offer.py index d8e426e..8b85ad1 100644 --- a/src/tf2_utils/offer.py +++ b/src/tf2_utils/offer.py @@ -1,7 +1,7 @@ -from .utils import account_id_to_steam_id - import enum +from .utils import account_id_to_steam_id + class TradeOfferState(enum.IntEnum): Invalid = 1 diff --git a/src/tf2_utils/prices_tf.py b/src/tf2_utils/prices_tf.py index 8a8a323..882dabf 100644 --- a/src/tf2_utils/prices_tf.py +++ b/src/tf2_utils/prices_tf.py @@ -1,8 +1,8 @@ -import requests import time -from .utils import to_refined +import requests +from .utils import to_refined __all__ = [ "PricesTF", diff --git a/src/tf2_utils/schema.py b/src/tf2_utils/schema.py index 10f82a8..f2b0d88 100644 --- a/src/tf2_utils/schema.py +++ b/src/tf2_utils/schema.py @@ -1,15 +1,15 @@ +from tf2_data import EFFECTS, KILLSTREAKS, SchemaItems +from tf2_sku import to_sku + from .sku import ( - sku_to_defindex, - sku_to_quality_name, - sku_is_uncraftable, get_sku_effect, get_sku_killstreak, + sku_is_uncraftable, + sku_to_defindex, + sku_to_quality_name, strange_in_sku, ) -from tf2_data import SchemaItems, EFFECTS, KILLSTREAKS -from tf2_sku import to_sku - class SchemaItemsUtils(SchemaItems): def __init__( diff --git a/src/tf2_utils/sku.py b/src/tf2_utils/sku.py index 0d0a239..cf3b337 100644 --- a/src/tf2_utils/sku.py +++ b/src/tf2_utils/sku.py @@ -1,10 +1,9 @@ -from .item import Item +import re -from tf2_data import EFFECTS, COLORS, QUALITIES +from tf2_data import COLORS, EFFECTS, QUALITIES from tf2_sku import to_sku -import re - +from .item import Item __all__ = [ "get_sku_properties", diff --git a/src/tf2_utils/sockets.py b/src/tf2_utils/sockets.py index a655fec..ee823ac 100644 --- a/src/tf2_utils/sockets.py +++ b/src/tf2_utils/sockets.py @@ -1,10 +1,10 @@ -from .prices_tf import PricesTF - -from typing import Callable import json +from typing import Callable from websockets.sync.client import ClientConnection, connect +from .prices_tf import PricesTF + class BackpackTFSocket: URL = "wss://ws.backpack.tf/events" diff --git a/src/tf2_utils/utils.py b/src/tf2_utils/utils.py index 8de1b5b..1550d1b 100644 --- a/src/tf2_utils/utils.py +++ b/src/tf2_utils/utils.py @@ -1,6 +1,6 @@ -import struct -import math import json +import math +import struct __all__ = [ "to_scrap", diff --git a/tests/test_backpack_tf.py b/tests/test_backpack_tf.py index 9c909ce..7c41453 100644 --- a/tests/test_backpack_tf.py +++ b/tests/test_backpack_tf.py @@ -1,76 +1,71 @@ -from unittest import TestCase - -from .config import BACKPACK_TF_TOKEN from src.tf2_utils import BackpackTF, Currencies +bptf = None + + +def test_initiate_backpack_tf(backpack_tf_token: str) -> None: + global bptf + bptf = BackpackTF(backpack_tf_token, "76561198253325712") + + +def test_currencies() -> None: + assert Currencies(1, 1.5).__dict__ == {"keys": 1, "metal": 1.5} + assert Currencies().__dict__ == {"keys": 0, "metal": 0.0} + assert Currencies(**{"metal": 10.55}).__dict__ == {"keys": 0, "metal": 10.55} + + +def test_construct_listing_item() -> None: + assert bptf._construct_listing_item("263;6") == { + "baseName": "Ellis' Cap", + "craftable": True, + "quality": {"id": 6}, + "tradable": True, + } -class TestBackpackTF(TestCase): - def setUp(cls) -> None: - cls.bptf = BackpackTF(BACKPACK_TF_TOKEN, "76561198253325712") - def test_currencies(self): - self.assertEqual(Currencies(1, 1.5).__dict__, {"keys": 1, "metal": 1.5}) - self.assertEqual(Currencies().__dict__, {"keys": 0, "metal": 0.0}) - self.assertEqual( - Currencies(**{"metal": 10.55}).__dict__, {"keys": 0, "metal": 10.55} - ) +def test_construct_listing() -> None: + assert bptf._construct_listing( + "263;6", + "sell", + {"keys": 1, "metal": 1.55}, + "my description", + 13201231975, + ) == { + "buyout": True, + "offers": True, + "promoted": False, + "item": { + "baseName": "Ellis' Cap", + "craftable": True, + "quality": {"id": 6}, + "tradable": True, + }, + "currencies": {"keys": 1, "metal": 1.55}, + "details": "my description", + "id": 13201231975, + } - def test_construct_listing_item(self): - self.assertDictEqual( - self.bptf._construct_listing_item("263;6"), - { - "baseName": "Ellis' Cap", - "craftable": True, - "quality": {"id": 6}, - "tradable": True, - }, - ) + assert bptf._construct_listing( + "263;6", "buy", {"keys": 1, "metal": 1.55}, "my description" + ) == { + "buyout": True, + "offers": True, + "promoted": False, + "item": { + "baseName": "Ellis' Cap", + "craftable": True, + "quality": {"id": 6}, + "tradable": True, + }, + "currencies": {"keys": 1, "metal": 1.55}, + "details": "my description", + } - def test_construct_listing(self): - self.assertDictEqual( - self.bptf._construct_listing( - "263;6", - "sell", - {"keys": 1, "metal": 1.55}, - "my description", - 13201231975, - ), - { - "item": { - "baseName": "Ellis' Cap", - "craftable": True, - "quality": {"id": 6}, - "tradable": True, - }, - "currencies": {"keys": 1, "metal": 1.55}, - "details": "my description", - "id": 13201231975, - }, - ) - self.assertDictEqual( - self.bptf._construct_listing( - "263;6", "buy", {"keys": 1, "metal": 1.55}, "my description" - ), - { - "item": { - "baseName": "Ellis' Cap", - "craftable": True, - "quality": {"id": 6}, - "tradable": True, - }, - "currencies": {"keys": 1, "metal": 1.55}, - "details": "my description", - }, - ) - def test_hash_name(self): - self.assertEqual( - self.bptf._get_sku_item_hash("5021;6"), "d9f847ff5dfcf78576a9fca04cbf6c07" - ) - self.assertEqual( - self.bptf._get_sku_item_hash("30397;6"), "8010db121d19610e9bcbac47432bd78c" - ) - self.assertEqual( - self.bptf._get_item_hash("The Bruiser's Bandanna"), - "8010db121d19610e9bcbac47432bd78c", - ) +def test_hash_name() -> None: + assert bptf._get_sku_item_hash("5021;6") == "d9f847ff5dfcf78576a9fca04cbf6c07" + assert bptf._get_sku_item_hash("30397;6") == "8010db121d19610e9bcbac47432bd78c" + assert ( + bptf._get_item_hash("The Bruiser's Bandanna") + == "8010db121d19610e9bcbac47432bd78c" + ) diff --git a/tests/test_currency.py b/tests/test_currency.py index c0e05c8..339f96a 100644 --- a/tests/test_currency.py +++ b/tests/test_currency.py @@ -1,10 +1,7 @@ -from src.tf2_utils.inventory import map_inventory from src.tf2_utils.currency import CurrencyExchange +from src.tf2_utils.inventory import map_inventory from src.tf2_utils.utils import read_json_file -from unittest import TestCase - - INVENTORY = read_json_file("./tests/json/inventory.json") PICKED_METALS = read_json_file("./tests/json/picked_metals.json") @@ -20,320 +17,274 @@ ] -class TestCurrency(TestCase): - def test_utils(self): - c = CurrencyExchange( - [ - {"market_hash_name": "Refined Metal", "tags": TAG}, - {"market_hash_name": "Reclaimed Metal", "tags": TAG}, - {"market_hash_name": "Scrap Metal", "tags": TAG}, - {"market_hash_name": "Refined Metal", "tags": TAG}, - ], - [ - {"market_hash_name": "Refined Metal", "tags": TAG}, - {"market_hash_name": "Reclaimed Metal", "tags": TAG}, - ], - "buy", - 4, - KEY_RATE, - ) - c.calculate() - - self.assertEqual(22, c.their_scrap) - self.assertEqual(12, c.our_scrap) - - self.assertEqual( - { - "Mann Co. Supply Crate Key": 0, - "Refined Metal": 2, - "Reclaimed Metal": 1, - "Scrap Metal": 1, - }, - c.their_overview, - ) - self.assertEqual( - { - "Mann Co. Supply Crate Key": 0, - "Refined Metal": 1, - "Reclaimed Metal": 1, - "Scrap Metal": 0, - }, - c.our_overview, - ) - - self.assertFalse(c.is_possible()) - - def test_not_enough(self): - c = CurrencyExchange( - [ - {"market_hash_name": "Refined Metal", "tags": TAG}, - {"market_hash_name": "Refined Metal", "tags": TAG}, - {"market_hash_name": "Scrap Metal", "tags": TAG}, - ], - [], - "sell", - 20, - KEY_RATE, - ) - c.calculate() - - self.assertEqual(19, c.their_scrap) - self.assertFalse(c.is_possible()) - - def test_no_combination(self): - c = CurrencyExchange( - [ - {"market_hash_name": "Refined Metal", "tags": TAG}, - {"market_hash_name": "Refined Metal", "tags": TAG}, - ], - [ - {"market_hash_name": "Reclaimed Metal", "tags": TAG}, - {"market_hash_name": "Refined Metal", "tags": TAG}, - ], - "buy", - 17, # 1.88 - KEY_RATE, - ) - c.calculate() - - self.assertEqual(18, c.their_scrap) - self.assertEqual(12, c.our_scrap) - - # they have enough but there is no combo which would work - self.assertFalse(c.is_possible()) - - def test_possible(self): - c = CurrencyExchange( - [ - {"market_hash_name": "Refined Metal", "tags": TAG}, - {"market_hash_name": "Scrap Metal", "tags": TAG}, - {"market_hash_name": "Scrap Metal", "tags": TAG}, - {"market_hash_name": "Scrap Metal", "tags": TAG}, - {"market_hash_name": "Scrap Metal", "tags": TAG}, - {"market_hash_name": "Scrap Metal", "tags": TAG}, - {"market_hash_name": "Scrap Metal", "tags": TAG}, - ], - [ - {"market_hash_name": "Refined Metal", "tags": TAG}, - {"market_hash_name": "Reclaimed Metal", "tags": TAG}, - ], - "buy", - 4, - KEY_RATE, - ) - c.calculate() - - self.assertEqual(15, c.their_scrap) - self.assertEqual(12, c.our_scrap) - - self.assertEqual( - { - "Mann Co. Supply Crate Key": 0, - "Refined Metal": 1, - "Reclaimed Metal": 0, - "Scrap Metal": 6, - }, - c.their_overview, - ) - self.assertEqual( - { - "Mann Co. Supply Crate Key": 0, - "Refined Metal": 1, - "Reclaimed Metal": 1, - "Scrap Metal": 0, - }, - c.our_overview, - ) - - self.assertTrue(c.is_possible()) - - # 0.44, but we pay 1 ref - # that means they have to add 0.55 - self.assertEqual(["Refined Metal"], c.our_combination) - self.assertEqual( - [ - "Scrap Metal", - "Scrap Metal", - "Scrap Metal", - "Scrap Metal", - "Scrap Metal", - ], - c.their_combination, - ) - - def test_best_possible(self): - c = CurrencyExchange( - [ - {"market_hash_name": "Refined Metal", "tags": TAG}, - {"market_hash_name": "Scrap Metal", "tags": TAG}, - {"market_hash_name": "Mann Co. Supply Crate Key", "tags": TAG}, - {"market_hash_name": "Scrap Metal", "tags": TAG}, - {"market_hash_name": "Scrap Metal", "tags": TAG}, - {"market_hash_name": "Scrap Metal", "tags": TAG}, - {"market_hash_name": "Scrap Metal", "tags": TAG}, - {"market_hash_name": "Reclaimed Metal", "tags": TAG}, - {"market_hash_name": "Reclaimed Metal", "tags": TAG}, - ], - [ - {"market_hash_name": "Refined Metal", "tags": TAG}, - {"market_hash_name": "Refined Metal", "tags": TAG}, - ], - "sell", - 14, - KEY_RATE, - ) - c.calculate() - - self.assertEqual(KEY_RATE + 20, c.their_scrap) - self.assertEqual(18, c.our_scrap) - - self.assertEqual( - { - "Mann Co. Supply Crate Key": 1, - "Refined Metal": 1, - "Reclaimed Metal": 2, - "Scrap Metal": 5, - }, - c.their_overview, - ) - self.assertEqual( - { - "Mann Co. Supply Crate Key": 0, - "Refined Metal": 2, - "Reclaimed Metal": 0, - "Scrap Metal": 0, - }, - c.our_overview, - ) - - self.assertTrue(c.is_possible()) - - self.assertEqual( - [ - "Scrap Metal", - "Scrap Metal", - "Reclaimed Metal", - "Refined Metal", - ], - c.their_combination, - ) - self.assertEqual( - [], - c.our_combination, - ) - - def test_only_metal(self): - c = CurrencyExchange( - [ - {"market_hash_name": "Scrap Metal", "tags": TAG}, - {"market_hash_name": "Scrap Metal", "tags": TAG}, - {"market_hash_name": "Scrap Metal", "tags": TAG}, - {"market_hash_name": "Reclaimed Metal", "tags": TAG}, - {"market_hash_name": "Reclaimed Metal", "tags": TAG}, - ], - [ - {"market_hash_name": "Refined Metal", "tags": TAG}, - {"market_hash_name": "Refined Metal", "tags": TAG}, - ], - "buy", - 9, - KEY_RATE, - False, - ) - c.calculate() - - self.assertEqual(9, c.their_scrap) - self.assertEqual(18, c.our_scrap) - - self.assertEqual( - { - "Mann Co. Supply Crate Key": 0, - "Refined Metal": 0, - "Reclaimed Metal": 2, - "Scrap Metal": 3, - }, - c.their_overview, - ) - self.assertEqual( - { - "Mann Co. Supply Crate Key": 0, - "Refined Metal": 2, - "Reclaimed Metal": 0, - "Scrap Metal": 0, - }, - c.our_overview, - ) - - self.assertTrue(c.is_possible()) - - self.assertEqual( - [ - "Reclaimed Metal", - "Reclaimed Metal", - "Scrap Metal", - "Scrap Metal", - "Scrap Metal", - ], - c.their_combination, - ) - self.assertEqual( - ["Refined Metal"], - c.our_combination, - ) - - def test_real_inventory(self): - mapped_inventory = map_inventory(INVENTORY, True) - - c = CurrencyExchange( - mapped_inventory, - [], - "sell", - 15, - KEY_RATE, - ) - - c.calculate() - - self.assertEqual(591, c.their_scrap) - self.assertEqual(0, c.our_scrap) - - self.assertEqual( - { - "Mann Co. Supply Crate Key": 0, - "Refined Metal": 65, - "Reclaimed Metal": 1, - "Scrap Metal": 3, - }, - c.their_overview, - ) - self.assertEqual( - { - "Mann Co. Supply Crate Key": 0, - "Refined Metal": 0, - "Reclaimed Metal": 0, - "Scrap Metal": 0, - }, - c.our_overview, - ) - - self.assertTrue(c.is_possible()) - - self.assertEqual( - [ - "Reclaimed Metal", - "Scrap Metal", - "Scrap Metal", - "Scrap Metal", - "Refined Metal", - ], - c.their_combination, - ) - self.assertEqual( - [], - c.our_combination, - ) - - their_items, our_items = c.get_currencies() - - self.assertEqual(PICKED_METALS, their_items) - self.assertEqual([], our_items) +def test_utils() -> None: + c = CurrencyExchange( + [ + {"market_hash_name": "Refined Metal", "tags": TAG}, + {"market_hash_name": "Reclaimed Metal", "tags": TAG}, + {"market_hash_name": "Scrap Metal", "tags": TAG}, + {"market_hash_name": "Refined Metal", "tags": TAG}, + ], + [ + {"market_hash_name": "Refined Metal", "tags": TAG}, + {"market_hash_name": "Reclaimed Metal", "tags": TAG}, + ], + "buy", + 4, + KEY_RATE, + ) + c.calculate() + + assert 22 == c.their_scrap + assert 12 == c.our_scrap + + assert { + "Mann Co. Supply Crate Key": 0, + "Refined Metal": 2, + "Reclaimed Metal": 1, + "Scrap Metal": 1, + } == c.their_overview + assert { + "Mann Co. Supply Crate Key": 0, + "Refined Metal": 1, + "Reclaimed Metal": 1, + "Scrap Metal": 0, + } == c.our_overview + + assert not c.is_possible + + +def test_not_enough() -> None: + c = CurrencyExchange( + [ + {"market_hash_name": "Refined Metal", "tags": TAG}, + {"market_hash_name": "Refined Metal", "tags": TAG}, + {"market_hash_name": "Scrap Metal", "tags": TAG}, + ], + [], + "sell", + 20, + KEY_RATE, + ) + c.calculate() + + assert 19 == c.their_scrap + assert not c.is_possible + + +def test_no_combination() -> None: + c = CurrencyExchange( + [ + {"market_hash_name": "Refined Metal", "tags": TAG}, + {"market_hash_name": "Refined Metal", "tags": TAG}, + ], + [ + {"market_hash_name": "Reclaimed Metal", "tags": TAG}, + {"market_hash_name": "Refined Metal", "tags": TAG}, + ], + "buy", + 17, # 1.88 + KEY_RATE, + ) + c.calculate() + + assert 18 == c.their_scrap + assert 12 == c.our_scrap + + # they have enough but there is no combo which would work + assert not c.is_possible + + +def test_possible() -> None: + c = CurrencyExchange( + [ + {"market_hash_name": "Refined Metal", "tags": TAG}, + {"market_hash_name": "Scrap Metal", "tags": TAG}, + {"market_hash_name": "Scrap Metal", "tags": TAG}, + {"market_hash_name": "Scrap Metal", "tags": TAG}, + {"market_hash_name": "Scrap Metal", "tags": TAG}, + {"market_hash_name": "Scrap Metal", "tags": TAG}, + {"market_hash_name": "Scrap Metal", "tags": TAG}, + ], + [ + {"market_hash_name": "Refined Metal", "tags": TAG}, + {"market_hash_name": "Reclaimed Metal", "tags": TAG}, + ], + "buy", + 4, + KEY_RATE, + ) + c.calculate() + + assert 15 == c.their_scrap + assert 12 == c.our_scrap + + assert { + "Mann Co. Supply Crate Key": 0, + "Refined Metal": 1, + "Reclaimed Metal": 0, + "Scrap Metal": 6, + } == c.their_overview + assert { + "Mann Co. Supply Crate Key": 0, + "Refined Metal": 1, + "Reclaimed Metal": 1, + "Scrap Metal": 0, + } == c.our_overview + + assert c.is_possible + + # 0.44, but we pay 1 ref + # that means they have to add 0.55 + assert ["Refined Metal"] == c.our_combination + assert [ + "Scrap Metal", + "Scrap Metal", + "Scrap Metal", + "Scrap Metal", + "Scrap Metal", + ] == c.their_combination + + +def test_best_possible() -> None: + c = CurrencyExchange( + [ + {"market_hash_name": "Refined Metal", "tags": TAG}, + {"market_hash_name": "Scrap Metal", "tags": TAG}, + {"market_hash_name": "Mann Co. Supply Crate Key", "tags": TAG}, + {"market_hash_name": "Scrap Metal", "tags": TAG}, + {"market_hash_name": "Scrap Metal", "tags": TAG}, + {"market_hash_name": "Scrap Metal", "tags": TAG}, + {"market_hash_name": "Scrap Metal", "tags": TAG}, + {"market_hash_name": "Reclaimed Metal", "tags": TAG}, + {"market_hash_name": "Reclaimed Metal", "tags": TAG}, + ], + [ + {"market_hash_name": "Refined Metal", "tags": TAG}, + {"market_hash_name": "Refined Metal", "tags": TAG}, + ], + "sell", + 14, + KEY_RATE, + ) + c.calculate() + + assert KEY_RATE + 20 == c.their_scrap + assert 18 == c.our_scrap + + assert { + "Mann Co. Supply Crate Key": 1, + "Refined Metal": 1, + "Reclaimed Metal": 2, + "Scrap Metal": 5, + } == c.their_overview + assert { + "Mann Co. Supply Crate Key": 0, + "Refined Metal": 2, + "Reclaimed Metal": 0, + "Scrap Metal": 0, + } == c.our_overview + + assert c.is_possible + + assert [ + "Scrap Metal", + "Scrap Metal", + "Reclaimed Metal", + "Refined Metal", + ] == c.their_combination + assert [] == c.our_combination + + +def test_only_metal() -> None: + c = CurrencyExchange( + [ + {"market_hash_name": "Scrap Metal", "tags": TAG}, + {"market_hash_name": "Scrap Metal", "tags": TAG}, + {"market_hash_name": "Scrap Metal", "tags": TAG}, + {"market_hash_name": "Reclaimed Metal", "tags": TAG}, + {"market_hash_name": "Reclaimed Metal", "tags": TAG}, + ], + [ + {"market_hash_name": "Refined Metal", "tags": TAG}, + {"market_hash_name": "Refined Metal", "tags": TAG}, + ], + "buy", + 9, + KEY_RATE, + False, + ) + c.calculate() + + assert 9 == c.their_scrap + assert 18 == c.our_scrap + + assert { + "Mann Co. Supply Crate Key": 0, + "Refined Metal": 0, + "Reclaimed Metal": 2, + "Scrap Metal": 3, + } == c.their_overview + assert { + "Mann Co. Supply Crate Key": 0, + "Refined Metal": 2, + "Reclaimed Metal": 0, + "Scrap Metal": 0, + } == c.our_overview + + assert c.is_possible + + assert [ + "Reclaimed Metal", + "Reclaimed Metal", + "Scrap Metal", + "Scrap Metal", + "Scrap Metal", + ] == c.their_combination + assert ["Refined Metal"] == c.our_combination + + +def test_real_inventory() -> None: + mapped_inventory = map_inventory(INVENTORY, True) + + c = CurrencyExchange( + mapped_inventory, + [], + "sell", + 15, + KEY_RATE, + ) + + c.calculate() + + assert 591 == c.their_scrap + assert 0 == c.our_scrap + + assert { + "Mann Co. Supply Crate Key": 0, + "Refined Metal": 65, + "Reclaimed Metal": 1, + "Scrap Metal": 3, + } == c.their_overview + assert { + "Mann Co. Supply Crate Key": 0, + "Refined Metal": 0, + "Reclaimed Metal": 0, + "Scrap Metal": 0, + } == c.our_overview + + assert c.is_possible + + assert [ + "Reclaimed Metal", + "Scrap Metal", + "Scrap Metal", + "Scrap Metal", + "Refined Metal", + ] == c.their_combination + assert [] == c.our_combination + + their_items, our_items = c.get_currencies() + + assert PICKED_METALS == their_items + assert [] == our_items diff --git a/tests/test_inventory.py b/tests/test_inventory.py index 2735b09..8a12ca4 100644 --- a/tests/test_inventory.py +++ b/tests/test_inventory.py @@ -1,14 +1,12 @@ -from src.tf2_utils.utils import read_json_file from src.tf2_utils import map_inventory - -from unittest import TestCase +from src.tf2_utils.utils import read_json_file INVENTORY = read_json_file("./tests/json/bot_inventory.json") +inventory = map_inventory(INVENTORY, True) -class TestInventory(TestCase): - def setUp(cls) -> None: - cls.inventory = map_inventory(INVENTORY, True) +def test_inventory() -> None: + assert 500 == len(inventory) - def test_inventory(self): - self.assertEqual(500, len(self.inventory)) + for item in inventory: + assert "sku" in item diff --git a/tests/test_item.py b/tests/test_item.py index 8c110e0..03dca07 100644 --- a/tests/test_item.py +++ b/tests/test_item.py @@ -1,7 +1,5 @@ -from src.tf2_utils.utils import read_json_file from src.tf2_utils import Item, get_sku - -from unittest import TestCase +from src.tf2_utils.utils import read_json_file file_path = "./tests/json/{}.json" @@ -18,61 +16,65 @@ def get_item(file_name: str) -> Item: ELLIS_CAP = get_item("ellis_cap") -class TestUtils(TestCase): - def test_craft_hat(self): - is_craft_hat = ELLIS_CAP.is_craft_hat() - is_special = ELLIS_CAP.is_special() - is_painted = ELLIS_CAP.is_painted() - - self.assertTrue(is_craft_hat) - self.assertFalse(is_special) - self.assertFalse(is_painted) - - def test_uncraftable_hat(self): - is_craftable = UNCRAFTABLE_HAT.is_craftable() - is_craft_hat = UNCRAFTABLE_HAT.is_craft_hat() - is_uncraftable = UNCRAFTABLE_HAT.is_uncraftable() - - self.assertFalse(is_craftable) - self.assertFalse(is_craft_hat) - self.assertTrue(is_uncraftable) - - def test_painted(self): - is_special = PAINTED_HAT.is_special() - is_painted = PAINTED_HAT.is_painted() - paint = PAINTED_HAT.get_paint() - is_craft_hat = PAINTED_HAT.is_craft_hat() - - self.assertTrue(is_special) - self.assertTrue(is_painted) - self.assertEqual("Australium Gold", paint) - self.assertTrue(is_craft_hat) - - def test_spell(self): - is_special = SPELLED_ITEM.is_special() - has_spell = SPELLED_ITEM.has_spell() - is_strange = SPELLED_ITEM.is_strange() - # spell=SPELLED_ITEM.get_spell() - - self.assertTrue(is_special) - self.assertTrue(has_spell) - self.assertTrue(is_strange) - # self.assertEqual("Exorcism", spell) - - def test_unusual(self): - is_unusual = HONG_KONG_CONE.is_unusual() - effect = HONG_KONG_CONE.get_effect() - paint = HONG_KONG_CONE.get_paint() - is_craft_hat = HONG_KONG_CONE.is_craft_hat() - strange_in_name = HONG_KONG_CONE.has_strange_in_name() - - self.assertTrue(is_unusual) - self.assertEqual("Neutron Star", effect) - self.assertEqual("An Extraordinary Abundance of Tinge", paint) - self.assertFalse(is_craft_hat) - self.assertTrue(strange_in_name) - - def test_sku_from_item_class(self): - sku = get_sku(ELLIS_CAP) - - self.assertEqual("263;6", sku) +def test_craft_hat() -> None: + is_craft_hat = ELLIS_CAP.is_craft_hat() + is_special = ELLIS_CAP.is_special() + is_painted = ELLIS_CAP.is_painted() + + assert is_craft_hat + assert not is_special + assert not is_painted + + +def test_uncraftable_hat() -> None: + is_craftable = UNCRAFTABLE_HAT.is_craftable() + is_craft_hat = UNCRAFTABLE_HAT.is_craft_hat() + is_uncraftable = UNCRAFTABLE_HAT.is_uncraftable() + + assert not is_craftable + assert not is_craft_hat + assert is_uncraftable + + +def test_painted() -> None: + is_special = PAINTED_HAT.is_special() + is_painted = PAINTED_HAT.is_painted() + paint = PAINTED_HAT.get_paint() + is_craft_hat = PAINTED_HAT.is_craft_hat() + + assert is_special + assert is_painted + assert "Australium Gold" == paint + assert is_craft_hat + + +def test_spell() -> None: + is_special = SPELLED_ITEM.is_special() + has_spell = SPELLED_ITEM.has_spell() + is_strange = SPELLED_ITEM.is_strange() + # spell=SPELLED_ITEM.get_spell() + + assert is_special + assert has_spell + assert is_strange + # assert "Exorcism"==spell + + +def test_unusual() -> None: + is_unusual = HONG_KONG_CONE.is_unusual() + effect = HONG_KONG_CONE.get_effect() + paint = HONG_KONG_CONE.get_paint() + is_craft_hat = HONG_KONG_CONE.is_craft_hat() + strange_in_name = HONG_KONG_CONE.has_strange_in_name() + + assert is_unusual + assert "Neutron Star" == effect + assert "An Extraordinary Abundance of Tinge" == paint + assert not is_craft_hat + assert strange_in_name + + +def test_sku_from_item_class() -> None: + sku = get_sku(ELLIS_CAP) + + assert "263;6" == sku diff --git a/tests/test_marketplace_tf.py b/tests/test_marketplace_tf.py index bc97d51..9bf41ee 100644 --- a/tests/test_marketplace_tf.py +++ b/tests/test_marketplace_tf.py @@ -1,58 +1,59 @@ from src.tf2_utils import MarketplaceTF -from .config import MARKETPLACE_TF_API_KEY -from unittest import TestCase +mplc = None -class TestMarketplaceTF(TestCase): - def setUp(cls) -> None: - cls.mplc = MarketplaceTF(MARKETPLACE_TF_API_KEY) +def test_initiate_marketplace_tf(marketplace_tf_api_key: str) -> None: + global mplc + mplc = MarketplaceTF(marketplace_tf_api_key) - # def test_no_api_key(self): - # self.mplc.api_key = "" - # endpoints = self.mplc.get_endpoints() - # self.assertTrue("endpoints" in endpoints) - # self.assertRaises(NoAPIKey, self.mplc.get_bots) - # self.assertRaises(NoAPIKey, self.mplc.get_is_banned("76561198253325712")) - # self.mplc.api_key = MARKETPLACE_TF_API_KEY +def test_key_data() -> None: + price = mplc.fetch_item("5021;6") - # def test_get_bots(self): - # self.assertTrue(self.mplc.get_bots()["success"]) + assert mplc.get_sku() == "5021;6" + assert mplc.get_stock() > 1 + assert mplc.get_price() > 1.4 + assert mplc.get_highest_buy_order() < 2.5 + assert mplc.get_item_data() == price + assert mplc.get_lowest_price() == mplc.get_price() - # def test_get_bans(self): - # self.assertEqual("confern", self.mplc.get_name("76561198253325712")) - # self.assertTrue(self.mplc.get_is_seller("76561198253325712")) - # self.assertFalse(self.mplc.get_is_banned("76561198253325712")) - # self.assertEqual(self.mplc.get_seller_id("76561198253325712"), 195002) - # self.assertTrue(self.mplc.get_is_banned("76561198115857578")) - # self.assertFalse(self.mplc.get_is_seller("76561198115857578")) +# def test_no_api_key(self): +# self.mplc.api_key = "" +# endpoints = self.mplc.get_endpoints() - # def test_get_dashboard_items(self): - # dashboard = self.mplc.get_dashboard_items() +# self.assertTrue("endpoints" in endpoints) +# self.assertRaises(NoAPIKey, self.mplc.get_bots) +# self.assertRaises(NoAPIKey, self.mplc.get_is_banned("76561198253325712")) +# self.mplc.api_key = MARKETPLACE_TF_API_KEY - # self.assertTrue("items" in dashboard) - # self.assertTrue(dashboard["success"]) +# def test_get_bots(self): +# self.assertTrue(self.mplc.get_bots()["success"]) - # def test_get_sales(self): - # sales = self.mplc.get_sales() +# def test_get_bans(self): +# self.assertEqual("confern", self.mplc.get_name("76561198253325712")) +# self.assertTrue(self.mplc.get_is_seller("76561198253325712")) +# self.assertFalse(self.mplc.get_is_banned("76561198253325712")) +# self.assertEqual(self.mplc.get_seller_id("76561198253325712"), 195002) - # self.assertTrue("sales" in sales) - # self.assertTrue(sales["success"]) +# self.assertTrue(self.mplc.get_is_banned("76561198115857578")) +# self.assertFalse(self.mplc.get_is_seller("76561198115857578")) - # sales = self.mplc.get_sales(number=1) - # self.assertTrue(len(sales["sales"]) == 1) +# def test_get_dashboard_items(self): +# dashboard = self.mplc.get_dashboard_items() - # sales = self.mplc.get_sales(start_before=0) - # self.assertEqual(sales, {}) +# self.assertTrue("items" in dashboard) +# self.assertTrue(dashboard["success"]) - def test_key_data(self): - price = self.mplc.fetch_item("5021;6") +# def test_get_sales(self): +# sales = self.mplc.get_sales() - self.assertEqual(self.mplc.get_item_data(), price) - self.assertGreater(self.mplc.get_stock(), 1) - self.assertGreater(self.mplc.get_price(), 1.4) - self.assertLess(self.mplc.get_highest_buy_order(), 2.5) - self.assertEqual(self.mplc.get_lowest_price(), self.mplc.get_price()) - self.assertEqual(self.mplc.get_sku(), "5021;6") +# self.assertTrue("sales" in sales) +# self.assertTrue(sales["success"]) + +# sales = self.mplc.get_sales(number=1) +# self.assertTrue(len(sales["sales"]) == 1) + +# sales = self.mplc.get_sales(start_before=0) +# self.assertEqual(sales, {}) diff --git a/tests/test_offer.py b/tests/test_offer.py index ccf564c..a0479a8 100644 --- a/tests/test_offer.py +++ b/tests/test_offer.py @@ -1,25 +1,24 @@ -from src.tf2_utils.utils import read_json_file from src.tf2_utils import Offer - -from unittest import TestCase +from src.tf2_utils.utils import read_json_file OFFER = read_json_file("./tests/json/offer.json") offer = Offer(OFFER) -class TestUtils(TestCase): - def test_offer_state(self): - self.assertFalse(offer.is_active()) - self.assertFalse(offer.is_declined()) - self.assertTrue(offer.is_accepted()) +def test_offer_state() -> None: + assert not offer.is_active() + assert not offer.is_declined() + assert offer.is_accepted() + + +def test_offer_sides() -> None: + assert not offer.is_our_offer() + assert not offer.is_scam() + assert not offer.is_gift() + assert offer.is_two_sided() - def test_offer_sides(self): - self.assertFalse(offer.is_our_offer()) - self.assertFalse(offer.is_scam()) - self.assertFalse(offer.is_gift()) - self.assertTrue(offer.is_two_sided()) - def test_offer_partner(self): - self.assertFalse(offer.has_trade_hold()) - self.assertEqual("76561198253325712", offer.get_partner()) +def test_offer_partner() -> None: + assert not offer.has_trade_hold() + assert "76561198253325712" == offer.get_partner() diff --git a/tests/test_schema.py b/tests/test_schema.py index 7d14810..4e15b4b 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -1,102 +1,107 @@ from src.tf2_utils import SchemaItemsUtils -from unittest import TestCase +schema_items = SchemaItemsUtils() # uses local files by default -schema_items = SchemaItemsUtils() # uses local files by default +def test_map_defindex_names() -> None: + response = schema_items.map_defindex_name() + assert {} != response + + +def test_name_to_sku_tod() -> None: + sku = schema_items.name_to_sku("Uncraftable Tour of Duty Ticket") + assert "725;6;uncraftable" == sku + + +def test_name_to_sku_key() -> None: + sku = schema_items.name_to_sku("Mann Co. Supply Crate Key") + assert "5021;6" == sku + + +def test_name_to_sku_name_tag() -> None: + sku = schema_items.name_to_sku("Name Tag") + assert "5020;6" == sku + + +def test_name_to_sku_pure() -> None: + ref = schema_items.name_to_sku("Refined Metal") + rec = schema_items.name_to_sku("Reclaimed Metal") + scrap = schema_items.name_to_sku("Scrap Metal") + + assert "5002;6" == ref + assert "5001;6" == rec + assert "5000;6" == scrap + + +def test_name_to_sku_non_existing() -> None: + # this item does not exist + item_name = "Non-Craftable Strange Team Captain" + sku = schema_items.name_to_sku(item_name) + defindex = schema_items.name_to_defindex(item_name) + + assert "378;11;uncraftable" == sku + assert -1 == defindex + + +def test_name_to_sku_qualities() -> None: + unique = schema_items.name_to_sku("Team Captain") + genuine = schema_items.name_to_sku("Genuine Team Captain") + haunted = schema_items.name_to_sku("Haunted Team Captain") + strange = schema_items.name_to_sku("Strange Team Captain") + vintage = schema_items.name_to_sku("Vintage Team Captain") + collectors = schema_items.name_to_sku("Collector's Team Captain") + + assert "378;1" == genuine + assert "378;3" == vintage + assert "378;6" == unique + assert "378;11" == strange + assert "378;13" == haunted + assert "378;14" == collectors + + +def test_defindex_to_name() -> None: + assert "Tour of Duty Ticket" == schema_items.defindex_to_name(725) + assert "Mann Co. Supply Crate Key" == schema_items.defindex_to_name(5021) + assert "Random Craft Hat" == schema_items.defindex_to_name(-100) + assert "Random Craft Weapon" == schema_items.defindex_to_name(-50) + assert "Scrap Metal" == schema_items.defindex_to_name(5000) + assert "Reclaimed Metal" == schema_items.defindex_to_name(5001) + assert "Refined Metal" == schema_items.defindex_to_name(5002) + + +def test_sku_to_base_name_tod() -> None: + name = schema_items.sku_to_base_name("725;6;uncraftable") + assert "Tour of Duty Ticket" == name + + +def test_sku_to_name_tod() -> None: + name = schema_items.sku_to_name("725;6;uncraftable") + assert "Uncraftable Tour of Duty Ticket" == name + + +def test_sku_to_non_craftable_name() -> None: + name = schema_items.sku_to_name("725;6;uncraftable", use_uncraftable=False) + assert "Non-Craftable Tour of Duty Ticket" == name + + +def test_sku_to_name_key() -> None: + name = schema_items.sku_to_name("5021;6") + assert "Mann Co. Supply Crate Key" == name + + +def test_team_captain() -> None: + # craftable + assert schema_items.sku_to_base_name("378;6"), "Team Captain" + assert schema_items.sku_to_full_name("378;6"), "The Team Captain" + assert schema_items.sku_to_name("378;6"), "Team Captain" + # uncraftable + assert schema_items.sku_to_name("378;6;uncraftable"), "Uncraftable Team Captain" + # strange + assert schema_items.sku_to_base_name("378;11"), "Team Captain" + assert schema_items.sku_to_name("378;11"), "Strange Team Captain" -class TestUtils(TestCase): - def test_map_defindex_names(self): - response = schema_items.map_defindex_name() - self.assertNotEqual({}, response) - - def test_name_to_sku_tod(self): - sku = schema_items.name_to_sku("Uncraftable Tour of Duty Ticket") - self.assertEqual("725;6;uncraftable", sku) - - def test_name_to_sku_key(self): - sku = schema_items.name_to_sku("Mann Co. Supply Crate Key") - self.assertEqual("5021;6", sku) - - def test_name_to_sku_name_tag(self): - sku = schema_items.name_to_sku("Name Tag") - self.assertEqual("5020;6", sku) - - def test_name_to_sku_pure(self): - ref = schema_items.name_to_sku("Refined Metal") - rec = schema_items.name_to_sku("Reclaimed Metal") - scrap = schema_items.name_to_sku("Scrap Metal") - - self.assertEqual("5002;6", ref) - self.assertEqual("5001;6", rec) - self.assertEqual("5000;6", scrap) - - def test_name_to_sku_non_existing(self): - # this item does not exist - item_name = "Non-Craftable Strange Team Captain" - sku = schema_items.name_to_sku(item_name) - defindex = schema_items.name_to_defindex(item_name) - - self.assertEqual("378;11;uncraftable", sku) - self.assertEqual(-1, defindex) - - def test_name_to_sku_qualities(self): - unique = schema_items.name_to_sku("Team Captain") - genuine = schema_items.name_to_sku("Genuine Team Captain") - haunted = schema_items.name_to_sku("Haunted Team Captain") - strange = schema_items.name_to_sku("Strange Team Captain") - vintage = schema_items.name_to_sku("Vintage Team Captain") - collectors = schema_items.name_to_sku("Collector's Team Captain") - - self.assertEqual("378;1", genuine) - self.assertEqual("378;3", vintage) - self.assertEqual("378;6", unique) - self.assertEqual("378;11", strange) - self.assertEqual("378;13", haunted) - self.assertEqual("378;14", collectors) - - def test_defindex_to_name(self): - self.assertEqual("Tour of Duty Ticket", schema_items.defindex_to_name(725)) - self.assertEqual( - "Mann Co. Supply Crate Key", schema_items.defindex_to_name(5021) - ) - self.assertEqual("Random Craft Hat", schema_items.defindex_to_name(-100)) - self.assertEqual("Random Craft Weapon", schema_items.defindex_to_name(-50)) - self.assertEqual("Scrap Metal", schema_items.defindex_to_name(5000)) - self.assertEqual("Reclaimed Metal", schema_items.defindex_to_name(5001)) - self.assertEqual("Refined Metal", schema_items.defindex_to_name(5002)) - - def test_sku_to_base_name_tod(self): - name = schema_items.sku_to_base_name("725;6;uncraftable") - self.assertEqual("Tour of Duty Ticket", name) - - def test_sku_to_name_tod(self): - name = schema_items.sku_to_name("725;6;uncraftable") - self.assertEqual("Uncraftable Tour of Duty Ticket", name) - - def test_sku_to_non_craftable_name(self): - name = schema_items.sku_to_name("725;6;uncraftable", use_uncraftable=False) - self.assertEqual("Non-Craftable Tour of Duty Ticket", name) - - def test_sku_to_name_key(self): - name = schema_items.sku_to_name("5021;6") - self.assertEqual("Mann Co. Supply Crate Key", name) - - def test_team_captain(self): - # craftable - self.assertEqual(schema_items.sku_to_base_name("378;6"), "Team Captain") - self.assertEqual(schema_items.sku_to_full_name("378;6"), "The Team Captain") - self.assertEqual(schema_items.sku_to_name("378;6"), "Team Captain") - # uncraftable - self.assertEqual( - schema_items.sku_to_name("378;6;uncraftable"), "Uncraftable Team Captain" - ) - # strange - self.assertEqual(schema_items.sku_to_base_name("378;11"), "Team Captain") - self.assertEqual(schema_items.sku_to_name("378;11"), "Strange Team Captain") - - def test_image_equal(self): - ellis_cap = schema_items.defindex_to_image_url(263) - random_craft_hat = schema_items.sku_to_image_url("-100;6") - self.assertEqual(ellis_cap, random_craft_hat) +def test_image_equal() -> None: + ellis_cap = schema_items.defindex_to_image_url(263) + random_craft_hat = schema_items.sku_to_image_url("-100;6") + assert ellis_cap, random_craft_hat diff --git a/tests/test_sku.py b/tests/test_sku.py index a7f82af..7308d98 100644 --- a/tests/test_sku.py +++ b/tests/test_sku.py @@ -1,20 +1,18 @@ -from src.tf2_utils.utils import read_json_file +import pytest + from src.tf2_utils import ( + get_metal, get_sku, get_sku_properties, - sku_to_defindex, - sku_to_quality, - sku_is_uncraftable, + is_metal, + is_pure, is_sku, + sku_is_uncraftable, sku_to_color, - is_pure, - is_metal, - get_metal, + sku_to_defindex, + sku_to_quality, ) - -# TODO:add tests for the rest of the functions - -from unittest import TestCase +from src.tf2_utils.utils import read_json_file file_path = "./tests/json/{}.json" @@ -29,80 +27,90 @@ def get_item_dict(file_name: str) -> dict: ELLIS_CAP = get_item_dict("ellis_cap") -class TestUtils(TestCase): - def test_ellis_cap_sku(self): - sku = get_sku(ELLIS_CAP) - - # https://marketplace.tf/items/tf2/263;6 - self.assertEqual("263;6", sku) - - def test_ellis_cap_sku_properties(self): - sku = get_sku_properties(ELLIS_CAP) - - self.assertEqual( - { - "defindex": 263, - "quality": 6, - "australium": False, - "craftable": True, - "wear": -1, - "killstreak_tier": -1, - "festivized": False, - "strange": False, - }, - sku, - ) - - def test_crusaders_crossbow_sku(self): - sku = get_sku(CRUSADERS_CROSSBOW) - - # https://marketplace.tf/items/tf2/305;11;kt-3;festive - self.assertEqual("305;11;kt-3;festive", sku) - - def test_strange_unusual_hong_kong_cone(self): - sku = get_sku(HONG_KONG_CONE) - - # https://marketplace.tf/items/tf2/30177;5;u107;strange - self.assertEqual("30177;5;u107;strange", sku) - - def test_uncraftable_hat(self): - sku = get_sku(UNCRAFTABLE_HAT) - - # https://marketplace.tf/items/tf2/734;6;uncraftable - self.assertEqual("734;6;uncraftable", sku) - - def test_properties(self): - sku = "734;6;uncraftable" - - self.assertEqual(734, sku_to_defindex(sku)) - self.assertEqual(6, sku_to_quality(sku)) - self.assertTrue(sku_is_uncraftable(sku)) - - def test_is_sku(self): - self.assertTrue(is_sku("734;6;uncraftable")) - self.assertTrue(is_sku("something;text")) - self.assertFalse(is_sku("734")) - - def test_is_metal(self): - self.assertTrue(is_metal("5000;6")) - self.assertTrue(is_metal("5001;6")) - self.assertTrue(is_metal("5002;6")) - self.assertFalse(is_metal("5021;6")) - - def test_is_pure(self): - self.assertTrue(is_pure("5000;6")) - self.assertTrue(is_pure("5001;6")) - self.assertTrue(is_pure("5002;6")) - self.assertTrue(is_pure("5021;6")) - self.assertFalse(is_pure("5021;7")) - - def test_get_metal(self): - self.assertEqual(9, get_metal("5002;6")) - self.assertEqual(3, get_metal("5001;6")) - self.assertEqual(1, get_metal("5000;6")) - self.assertRaises(AssertionError, get_metal, "5021;6") - - def test_sku_to_color(self): - self.assertEqual("7D6D00", sku_to_color("734;6;uncraftable")) - self.assertEqual("4D7455", sku_to_color("30469;1")) - self.assertRaises(AssertionError, sku_to_color, "notsku") +def test_ellis_cap_sku() -> None: + sku = get_sku(ELLIS_CAP) + + # https://marketplace.tf/items/tf2/263;6 + assert "263;6" == sku + + +def test_ellis_cap_sku_properties() -> None: + sku = get_sku_properties(ELLIS_CAP) + + assert { + "defindex": 263, + "quality": 6, + "australium": False, + "craftable": True, + "wear": -1, + "killstreak_tier": -1, + "festivized": False, + "strange": False, + } == sku + + +def test_crusaders_crossbow_sku() -> None: + sku = get_sku(CRUSADERS_CROSSBOW) + + # https://marketplace.tf/items/tf2/305;11;kt-3;festive + assert "305;11;kt-3;festive" == sku + + +def test_strange_unusual_hong_kong_cone() -> None: + sku = get_sku(HONG_KONG_CONE) + + # https://marketplace.tf/items/tf2/30177;5;u107;strange + assert "30177;5;u107;strange" == sku + + +def test_uncraftable_hat() -> None: + sku = get_sku(UNCRAFTABLE_HAT) + + # https://marketplace.tf/items/tf2/734;6;uncraftable + assert "734;6;uncraftable" == sku + + +def test_properties() -> None: + sku = "734;6;uncraftable" + + assert 734, sku_to_defindex(sku) + assert 6, sku_to_quality(sku) + assert sku_is_uncraftable(sku) + + +def test_is_sku() -> None: + assert is_sku("734;6;uncraftable") + assert is_sku("something;text") + assert not is_sku("734") + + +def test_is_metal() -> None: + assert is_metal("5000;6") + assert is_metal("5001;6") + assert is_metal("5002;6") + assert not is_metal("5021;6") + + +def test_is_pure() -> None: + assert is_pure("5000;6") + assert is_pure("5001;6") + assert is_pure("5002;6") + assert is_pure("5021;6") + assert not is_pure("5021;7") + + +def test_get_metal() -> None: + assert 9, get_metal("5002;6") + assert 3, get_metal("5001;6") + assert 1, get_metal("5000;6") + + with pytest.raises(AssertionError): + get_metal("5021;6") + + +def test_sku_to_color() -> None: + assert "7D6D00" == sku_to_color("734;6;uncraftable") + assert "4D7455" == sku_to_color("30469;1") + + with pytest.raises(AssertionError): + sku_to_color("notsku") diff --git a/tests/test_utils.py b/tests/test_utils.py index 00726b2..e891ae9 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,54 +1,58 @@ from src.tf2_utils import ( account_id_to_steam_id, - steam_id_to_account_id, - to_scrap, - to_refined, - refinedify, get_account_id_from_trade_url, get_steam_id_from_trade_url, get_token_from_trade_url, + refinedify, + steam_id_to_account_id, + to_refined, + to_scrap, ) -from unittest import TestCase - STEAM_ID = "76561198253325712" ACCOUNT_ID = "293059984" -class TestUtils(TestCase): - def test_steam_id(self): - self.assertEqual(STEAM_ID, account_id_to_steam_id(ACCOUNT_ID)) - self.assertEqual(ACCOUNT_ID, steam_id_to_account_id(STEAM_ID)) - self.assertEqual(STEAM_ID, account_id_to_steam_id(int(ACCOUNT_ID))) - self.assertEqual(ACCOUNT_ID, steam_id_to_account_id(int(STEAM_ID))) +def test_steam_id() -> None: + assert STEAM_ID == account_id_to_steam_id(ACCOUNT_ID) + assert ACCOUNT_ID == steam_id_to_account_id(STEAM_ID) + assert STEAM_ID == account_id_to_steam_id(int(ACCOUNT_ID)) + assert ACCOUNT_ID == steam_id_to_account_id(int(STEAM_ID)) + + +def test_to_refined() -> None: + scrap = 43 + refined = to_refined(scrap) + + assert 4.77 == refined + + +def test_to_scrap() -> None: + refined = 2.44 + scrap = to_scrap(refined) - def test_to_refined(self): - scrap = 43 - refined = to_refined(scrap) + assert 22 == scrap - self.assertEqual(4.77, refined) - def test_to_scrap(self): - refined = 2.44 - scrap = to_scrap(refined) +def test_refinedify_up() -> None: + wrong_value = 32.53 + refined = refinedify(wrong_value) - self.assertEqual(22, scrap) + assert 32.55 == refined - def test_refinedify_up(self): - wrong_value = 32.53 - refined = refinedify(wrong_value) - self.assertEqual(32.55, refined) +def test_refinedify_down() -> None: + wrong_value = 12.47 + refined = refinedify(wrong_value) - def test_refinedify_down(self): - wrong_value = 12.47 - refined = refinedify(wrong_value) + assert 12.44 == refined - self.assertEqual(12.44, refined) - def test_trade_url(self): - trade_url = "https://steamcommunity.com/tradeoffer/new/?partner=293059984&token=0-l_idZR" # noqa +def test_trade_url() -> None: + trade_url = ( + "https://steamcommunity.com/tradeoffer/new/?partner=293059984&token=0-l_idZR" # noqa + ) - self.assertEqual(ACCOUNT_ID, get_account_id_from_trade_url(trade_url)) - self.assertEqual(STEAM_ID, get_steam_id_from_trade_url(trade_url)) - self.assertEqual("0-l_idZR", get_token_from_trade_url(trade_url)) + assert ACCOUNT_ID == get_account_id_from_trade_url(trade_url) + assert STEAM_ID == get_steam_id_from_trade_url(trade_url) + assert "0-l_idZR" == get_token_from_trade_url(trade_url)