diff --git a/conftest.py b/conftest.py index 39c9385..459e417 100644 --- a/conftest.py +++ b/conftest.py @@ -1,8 +1,11 @@ from os import getenv +from pathlib import Path import pytest from dotenv import load_dotenv +from src.tf2_utils.utils import read_json_file + assert load_dotenv() MARKETPLACE_TF_API_KEY = getenv("MARKETPLACE_TF_API_KEY") @@ -10,11 +13,30 @@ EXPRESS_LOAD_API_KEY = getenv("EXPRESS_LOAD_API_KEY") +def get_item_data(file_name: str) -> dict: + path = Path(__file__).parent / f"tests/json/{file_name}.json" + return read_json_file(path) + + +# items +CRUSADERS_CROSSBOW = get_item_data("crusaders_crossbow") +UNCRAFTABLE_HAT = get_item_data("uncraftable_hat") +HONG_KONG_CONE = get_item_data("hong_kong_cone") +SPELLED_ITEM = get_item_data("spelled_item") +PAINTED_HAT = get_item_data("painted_hat") +ELLIS_CAP = get_item_data("ellis_cap") + + @pytest.fixture def steam_id() -> str: return "76561198253325712" +@pytest.fixture +def account_id() -> str: + return "293059984" + + @pytest.fixture def marketplace_tf_api_key() -> str: return MARKETPLACE_TF_API_KEY @@ -28,3 +50,34 @@ def backpack_tf_token() -> str: @pytest.fixture def express_load_api_key() -> str: return EXPRESS_LOAD_API_KEY + + +# items +@pytest.fixture +def crusaders_crossbow() -> dict: + return CRUSADERS_CROSSBOW + + +@pytest.fixture +def uncraftable_hat() -> dict: + return UNCRAFTABLE_HAT + + +@pytest.fixture +def hong_kong_cone() -> dict: + return HONG_KONG_CONE + + +@pytest.fixture +def spelled_item() -> dict: + return SPELLED_ITEM + + +@pytest.fixture +def painted_hat() -> dict: + return PAINTED_HAT + + +@pytest.fixture +def ellis_cap() -> dict: + return ELLIS_CAP diff --git a/src/tf2_utils/__init__.py b/src/tf2_utils/__init__.py index c250f35..ffde49a 100644 --- a/src/tf2_utils/__init__.py +++ b/src/tf2_utils/__init__.py @@ -1,12 +1,13 @@ __title__ = "tf2-utils" __author__ = "offish" -__version__ = "2.3.2" +__version__ = "2.3.3" __license__ = "MIT" from .currency import CurrencyExchange from .exceptions import InvalidInventory, TF2UtilsError from .inventory import Inventory, map_inventory from .item import Item +from .item_name import * from .marketplace_tf import ( MarketplaceTF, MarketplaceTFException, @@ -27,4 +28,4 @@ from .sku import * from .utils import * -# flake8: noqa +# flake8: noqa: F401, F403 diff --git a/src/tf2_utils/item_name.py b/src/tf2_utils/item_name.py new file mode 100644 index 0000000..d844048 --- /dev/null +++ b/src/tf2_utils/item_name.py @@ -0,0 +1,94 @@ +from tf2_data import EFFECTS, QUALITIES + +__all__ = [ + "has_festivized_in_name", + "has_uncraftable_in_name", + "has_non_craftable_in_name", + "is_craftable", + "has_australium_in_name", + "has_strange_in_name", + "has_basic_killstreak_in_name", + "has_specialized_killstreak_in_name", + "has_professional_killstreak_in_name", + "has_killstreak_in_name", + "get_effect_in_name", + "get_quality_from_name", +] + +QUALITY_NAMES = [ + "Genuine", + "Vintage", + "Unusual", + "Unique", + "Strange", + "Haunted", + "Collector's", + "Decorated Weapon", +] + +EFFECT_NAMES = [i for i in EFFECTS.keys() if not i.isnumeric()] + + +def has_festivized_in_name(name: str) -> bool: + return "Festivized " in name + + +def has_uncraftable_in_name(name: str) -> bool: + return "Uncraftable " in name + + +def has_non_craftable_in_name(name: str) -> bool: + return "Non-Craftable " in name + + +def is_craftable(name: str) -> bool: + return not (has_uncraftable_in_name(name) or has_non_craftable_in_name(name)) + + +def has_australium_in_name(name: str) -> bool: + return "Australium " in name + + +def has_strange_in_name(name: str) -> bool: + return "Strange " in name + + +def has_basic_killstreak_in_name(name: str) -> bool: + return name.startswith("Basic Killstreak ") + + +def has_specialized_killstreak_in_name(name: str) -> bool: + return name.startswith("Specialized ") + + +def has_professional_killstreak_in_name(name: str) -> bool: + return name.startswith("Professional ") + + +def has_killstreak_in_name(name: str) -> bool: + return ( + has_basic_killstreak_in_name(name) + or has_specialized_killstreak_in_name(name) + or has_professional_killstreak_in_name(name) + ) + + +def get_effect_in_name(name: str) -> int: + for effect in EFFECT_NAMES: + if effect in name: + return EFFECTS[effect] + + return -1 + + +def get_quality_from_name(name: str) -> int: + quality = 6 + + for part in name.split(" "): + if part not in QUALITY_NAMES: + continue + + quality = QUALITIES[part] + break + + return quality diff --git a/src/tf2_utils/schema.py b/src/tf2_utils/schema.py index 6011b3f..a9618d1 100644 --- a/src/tf2_utils/schema.py +++ b/src/tf2_utils/schema.py @@ -1,9 +1,22 @@ -from tf2_data import EFFECTS, KILLSTREAKS, QUALITIES, SchemaItems +from tf2_data import SchemaItems from tf2_sku import to_sku +from .item_name import ( + get_effect_in_name, + get_quality_from_name, + has_australium_in_name, + has_basic_killstreak_in_name, + has_festivized_in_name, + has_professional_killstreak_in_name, + has_specialized_killstreak_in_name, + has_strange_in_name, + is_craftable, +) from .sku import ( - get_sku_effect, - get_sku_killstreak, + australium_in_sku, + festive_in_sku, + get_effect_name_from_sku, + get_killstreak_name_from_sku, sku_is_uncraftable, sku_to_defindex, sku_to_quality_name, @@ -32,7 +45,7 @@ def defindex_to_full_name(self, defindex: int) -> str: return self.defindex_full_names.get(str(defindex), "") - def name_to_defindex(self, name: str) -> int: + def name_to_defindex(self, name: str, index: int = 0) -> int: if name == "Random Craft Weapon": return -50 @@ -57,9 +70,11 @@ def name_to_defindex(self, name: str) -> int: if name == "Name Tag": return defindexes[last_index] - return defindexes[0] + print(index) + print(defindexes) + return defindexes[index] - def defindex_to_image_url(self, defindex: int, large: bool = False) -> str: + def defindex_to_image_url(self, defindex: int, large_image: bool = False) -> str: # random craft weapon => shotgun if defindex == -50: defindex = 9 @@ -72,42 +87,28 @@ def defindex_to_image_url(self, defindex: int, large: bool = False) -> str: if item["defindex"] != defindex: continue - return item["image_url"] if not large else item["image_url_large"] + return item["image_url_large"] if large_image else item["image_url"] return "" - def sku_to_image_url(self, sku: str, large: bool = False) -> str: + def sku_to_image_url(self, sku: str, large_image: bool = False) -> str: defindex = sku_to_defindex(sku) - return self.defindex_to_image_url(defindex, large) - - def name_to_sku(self, name: str) -> str: - """This is not accurate, be careful when using this.""" - parts = name.split(" ") - - defindex = -1 - craftable = True - quality = 6 + return self.defindex_to_image_url(defindex, large_image) - for part in parts: - if part in ["Uncraftable", "Non-Craftable"]: - craftable = False - - if part in QUALITIES: - quality = QUALITIES[part] + def get_defindex_from_name(self, name: str, entry_index: int = 0) -> int: + # try whole name, then remove everything till the + # first space for each iteration if defindex + # for that name doesnt exist + # example: + # Uncraftable Strange Team Captain => -1 + # Strange Team Captain => -1 + # Team Captain => 378 != -1, so break defindex_name = name + defindex = -1 while True: - # try whole name, then remove everything till the - # first space for each iteration if defindex - # for that name doesnt exist - - # example: - # Uncraftable Strange Team Captain => -1 - # Strange Team Captain => -1 - # Team Captain => 378 != -1, so break - - defindex = self.name_to_defindex(defindex_name) + defindex = self.name_to_defindex(defindex_name, entry_index) if defindex != -1: break @@ -119,10 +120,46 @@ def name_to_sku(self, name: str) -> str: defindex_name = defindex_name[index + 1 :] + return defindex + + def name_to_sku(self, name: str) -> str: + quality = get_quality_from_name(name) + defindex = self.get_defindex_from_name(name) + effect = get_effect_in_name(name) + is_australium = False + killstreak_tier = -1 + + if effect != -1: + quality = 5 + + if has_professional_killstreak_in_name(name): + killstreak_tier = 3 + + if has_specialized_killstreak_in_name(name): + killstreak_tier = 2 + + if has_basic_killstreak_in_name(name): + killstreak_tier = 1 + + # must be strange to be australium + if "Australium Gold" not in name and has_australium_in_name(name): + quality = 11 + is_australium = True + + # australium weapons are the second entry in the defindex list, need to + # get the defindex again + if is_australium: + defindex = self.get_defindex_from_name(name, 1) + sku_properties = { "defindex": defindex, "quality": quality, - "craftable": craftable, + "effect": effect, + "strange": has_strange_in_name(name) and quality != 11, + "festivized": has_festivized_in_name(name), + "craftable": is_craftable(name), + "killstreak_tier": killstreak_tier, + "australium": is_australium, } return to_sku(sku_properties) @@ -135,49 +172,54 @@ def sku_to_full_name(self, sku: str) -> str: defindex = sku_to_defindex(sku) return self.defindex_to_full_name(defindex) - def sku_to_name(self, sku: str, use_uncraftable: bool = True) -> str: + def format_name( + self, + item_name: str, + quality: str = "", + craftable: str = "", + effect: str = "", + killstreak: str = "", + strange: str = "", + festivized: str = "", + ) -> str: + return "".join( + [killstreak, strange, effect, craftable, quality, festivized, item_name] + ) + + def sku_to_name(self, sku: str, as_uncraftable: bool = True) -> str: name = self.sku_to_base_name(sku) - craftable = "" quality = sku_to_quality_name(sku) - effect = get_sku_effect(sku) - killstreak = get_sku_killstreak(sku) - strange = strange_in_sku(sku) + craftable = "" + festivized = "Festivized " if festive_in_sku(sku) else "" + effect = get_effect_name_from_sku(sku) + killstreak = get_killstreak_name_from_sku(sku) + is_australium = australium_in_sku(sku) + strange = "" + + if strange_in_sku(sku): + strange = "Strange " if quality not in ["Unusual", "Unique"]: quality += " " else: quality = "" - if effect != -1: - effect = EFFECTS[str(effect)] + " " - else: - effect = "" - - if killstreak != -1: - killstreak = KILLSTREAKS[str(killstreak)] + " " - else: - killstreak = "" - - if strange: - strange = "Strange " - else: - strange = "" + if is_australium: + quality = "" + name = "Australium " + name if sku_is_uncraftable(sku): - if use_uncraftable: + if as_uncraftable: craftable = "Uncraftable " else: craftable = "Non-Craftable " - festive = "" - festivized = "" - wear = "" - - # TODO: add killstreaks and other properties (strange unusual etc.) - # festive - # festivzed - # wear - - return "".join( - [killstreak, strange, festive, festivized, craftable, quality, name, wear] + return self.format_name( + name, + quality=quality, + craftable=craftable, + effect=effect, + killstreak=killstreak, + strange=strange, + festivized=festivized, ) diff --git a/src/tf2_utils/sku.py b/src/tf2_utils/sku.py index 319a5eb..e81f28f 100644 --- a/src/tf2_utils/sku.py +++ b/src/tf2_utils/sku.py @@ -6,6 +6,7 @@ from .item import Item __all__ = [ + "get_sku", "get_sku_properties", "is_sku", "is_key", @@ -22,9 +23,12 @@ "sku_is_uncraftable", "sku_is_craftable", "strange_in_sku", + "australium_in_sku", + "festive_in_sku", "get_sku_killstreak", + "get_killstreak_name_from_sku", "get_sku_effect", - "get_sku", + "get_effect_name_from_sku", ] @@ -136,6 +140,14 @@ def strange_in_sku(sku: str) -> bool: return ";strange" in sku +def australium_in_sku(sku: str) -> bool: + return ";australium" in sku + + +def festive_in_sku(sku: str) -> bool: + return ";festive" in sku + + def get_sku_killstreak(sku: str) -> int: value = get_property_by_key(sku, "kt-") @@ -145,6 +157,22 @@ def get_sku_killstreak(sku: str) -> int: return int(value.replace("kt-", "")) +def get_killstreak_name_from_sku(sku: str) -> str: + tier = get_sku_killstreak(sku) + name = "" + + if tier == 1: + name = "Basic Killstreak " + + if tier == 2: + name = "Specialized " + + if tier == 3: + name = "Professional " + + return name + + def get_sku_effect(sku: str) -> int: value = get_property_by_key(sku, "u") @@ -154,6 +182,16 @@ def get_sku_effect(sku: str) -> int: return int(value.replace("u", "")) +def get_effect_name_from_sku(sku: str) -> str: + effect = get_sku_effect(sku) + name = "" + + if effect != -1: + name = EFFECTS[str(effect)] + " " + + return name + + def get_sku(item: Item | dict) -> str: if isinstance(item, dict): item = Item(item) diff --git a/src/tf2_utils/utils.py b/src/tf2_utils/utils.py index 1550d1b..2540e8e 100644 --- a/src/tf2_utils/utils.py +++ b/src/tf2_utils/utils.py @@ -1,6 +1,7 @@ import json import math import struct +from pathlib import Path __all__ = [ "to_scrap", @@ -14,7 +15,7 @@ ] -def read_json_file(path: str) -> dict | list: +def read_json_file(path: Path | str) -> dict | list: data = {} with open(path, "r") as f: @@ -23,7 +24,7 @@ def read_json_file(path: str) -> dict | list: return data -def write_json_file(path: str, data: dict | list) -> None: +def write_json_file(path: Path | str, data: dict | list) -> None: with open(path, "w") as f: json.dump(data, f, indent=4) diff --git a/tests/test_currency.py b/tests/test_currency.py index 339f96a..7efe122 100644 --- a/tests/test_currency.py +++ b/tests/test_currency.py @@ -35,22 +35,20 @@ def test_utils() -> None: ) c.calculate() - assert 22 == c.their_scrap - assert 12 == c.our_scrap - - assert { + assert c.their_scrap == 22 + assert c.our_scrap == 12 + assert c.their_overview == { "Mann Co. Supply Crate Key": 0, "Refined Metal": 2, "Reclaimed Metal": 1, "Scrap Metal": 1, - } == c.their_overview - assert { + } + assert c.our_overview == { "Mann Co. Supply Crate Key": 0, "Refined Metal": 1, "Reclaimed Metal": 1, "Scrap Metal": 0, - } == c.our_overview - + } assert not c.is_possible @@ -68,7 +66,7 @@ def test_not_enough() -> None: ) c.calculate() - assert 19 == c.their_scrap + assert c.their_scrap == 19 assert not c.is_possible @@ -88,10 +86,9 @@ def test_no_combination() -> None: ) c.calculate() - assert 18 == c.their_scrap - assert 12 == c.our_scrap - - # they have enough but there is no combo which would work + assert c.their_scrap == 18 + assert c.our_scrap == 12 + # they have enough but there is no combination which would work assert not c.is_possible @@ -116,34 +113,31 @@ def test_possible() -> None: ) c.calculate() - assert 15 == c.their_scrap - assert 12 == c.our_scrap - - assert { + assert c.their_scrap == 15 + assert c.our_scrap == 12 + assert c.their_overview == { "Mann Co. Supply Crate Key": 0, "Refined Metal": 1, "Reclaimed Metal": 0, "Scrap Metal": 6, - } == c.their_overview - assert { + } + assert c.our_overview == { "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 [ + assert c.our_combination == ["Refined Metal"] + assert c.their_combination == [ "Scrap Metal", "Scrap Metal", "Scrap Metal", "Scrap Metal", "Scrap Metal", - ] == c.their_combination + ] def test_best_possible() -> None: @@ -169,31 +163,28 @@ def test_best_possible() -> None: ) c.calculate() - assert KEY_RATE + 20 == c.their_scrap - assert 18 == c.our_scrap - - assert { + assert c.their_scrap == KEY_RATE + 20 + assert c.our_scrap == 18 + assert c.their_overview == { "Mann Co. Supply Crate Key": 1, "Refined Metal": 1, "Reclaimed Metal": 2, "Scrap Metal": 5, - } == c.their_overview - assert { + } + assert c.our_overview == { "Mann Co. Supply Crate Key": 0, "Refined Metal": 2, "Reclaimed Metal": 0, "Scrap Metal": 0, - } == c.our_overview - + } assert c.is_possible - - assert [ + assert c.their_combination == [ "Scrap Metal", "Scrap Metal", "Reclaimed Metal", "Refined Metal", - ] == c.their_combination - assert [] == c.our_combination + ] + assert c.our_combination == [] def test_only_metal() -> None: @@ -216,32 +207,29 @@ def test_only_metal() -> None: ) c.calculate() - assert 9 == c.their_scrap - assert 18 == c.our_scrap - - assert { + assert c.their_scrap == 9 + assert c.our_scrap == 18 + assert c.their_overview == { "Mann Co. Supply Crate Key": 0, "Refined Metal": 0, "Reclaimed Metal": 2, "Scrap Metal": 3, - } == c.their_overview - assert { + } + assert c.our_overview == { "Mann Co. Supply Crate Key": 0, "Refined Metal": 2, "Reclaimed Metal": 0, "Scrap Metal": 0, - } == c.our_overview - + } assert c.is_possible - - assert [ + assert c.their_combination == [ "Reclaimed Metal", "Reclaimed Metal", "Scrap Metal", "Scrap Metal", "Scrap Metal", - ] == c.their_combination - assert ["Refined Metal"] == c.our_combination + ] + assert c.our_combination == ["Refined Metal"] def test_real_inventory() -> None: @@ -257,34 +245,31 @@ def test_real_inventory() -> None: c.calculate() - assert 591 == c.their_scrap - assert 0 == c.our_scrap - - assert { + assert c.their_scrap == 591 + assert c.our_scrap == 0 + assert c.their_overview == { "Mann Co. Supply Crate Key": 0, "Refined Metal": 65, "Reclaimed Metal": 1, "Scrap Metal": 3, - } == c.their_overview - assert { + } + assert c.our_overview == { "Mann Co. Supply Crate Key": 0, "Refined Metal": 0, "Reclaimed Metal": 0, "Scrap Metal": 0, - } == c.our_overview - + } assert c.is_possible - - assert [ + assert c.their_combination == [ "Reclaimed Metal", "Scrap Metal", "Scrap Metal", "Scrap Metal", "Refined Metal", - ] == c.their_combination - assert [] == c.our_combination + ] + assert c.our_combination == [] their_items, our_items = c.get_currencies() - assert PICKED_METALS == their_items - assert [] == our_items + assert their_items == PICKED_METALS + assert our_items == [] diff --git a/tests/test_item.py b/tests/test_item.py index 03dca07..766e69d 100644 --- a/tests/test_item.py +++ b/tests/test_item.py @@ -1,80 +1,47 @@ from src.tf2_utils import Item, get_sku -from src.tf2_utils.utils import read_json_file -file_path = "./tests/json/{}.json" +def test_craft_hat(ellis_cap: dict) -> None: + item = Item(ellis_cap) -def get_item(file_name: str) -> Item: - return Item(read_json_file(file_path.format(file_name))) + assert item.is_craft_hat() + assert not item.is_special() + assert not item.is_painted() + assert get_sku(item) == "263;6" -CRUSADERS_CROSSBOW = get_item("crusaders_crossbow") -UNCRAFTABLE_HAT = get_item("uncraftable_hat") -HONG_KONG_CONE = get_item("hong_kong_cone") -SPELLED_ITEM = get_item("spelled_item") -PAINTED_HAT = get_item("painted_hat") -ELLIS_CAP = get_item("ellis_cap") +def test_uncraftable_hat(uncraftable_hat: dict) -> None: + item = Item(uncraftable_hat) + assert item.is_uncraftable() + assert not item.is_craftable() + assert not item.is_craft_hat() -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_painted(painted_hat: dict) -> None: + item = Item(painted_hat) + assert item.is_painted() + assert item.is_special() + assert item.get_paint() == "Australium Gold" + assert item.is_craft_hat() -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_spell(spelled_item: dict) -> None: + item = Item(spelled_item) + assert item.is_special() + assert item.has_spell() + assert item.is_strange() + # assert item.get_spell() == "Exorcism" -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_unusual(hong_kong_cone: dict) -> None: + item = Item(hong_kong_cone) - -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 + assert item.is_unusual() + assert not item.is_strange() + assert item.get_effect() == "Neutron Star" + assert item.get_paint() == "An Extraordinary Abundance of Tinge" + assert not item.is_craft_hat() + assert item.has_strange_in_name() diff --git a/tests/test_offer.py b/tests/test_offer.py index a0479a8..88fcefd 100644 --- a/tests/test_offer.py +++ b/tests/test_offer.py @@ -21,4 +21,4 @@ def test_offer_sides() -> None: def test_offer_partner() -> None: assert not offer.has_trade_hold() - assert "76561198253325712" == offer.get_partner() + assert offer.get_partner() == "76561198253325712" diff --git a/tests/test_schema.py b/tests/test_schema.py index 4e15b4b..5aab6b4 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -1,107 +1,141 @@ from src.tf2_utils import SchemaItemsUtils -schema_items = SchemaItemsUtils() # uses local files by default +schema = SchemaItemsUtils() # uses local files by default def test_map_defindex_names() -> None: - response = schema_items.map_defindex_name() + response = schema.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 + assert schema.name_to_sku("Refined Metal") == "5002;6" + assert schema.name_to_sku("Reclaimed Metal") == "5001;6" + assert schema.name_to_sku("Scrap Metal") == "5000;6" -def test_name_to_sku_non_existing() -> None: - # this item does not exist +def test_name_non_existing_item() -> None: 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 + assert schema.name_to_sku(item_name) == "378;11;uncraftable" + assert schema.name_to_defindex(item_name) == -1 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 + assert schema.name_to_sku("Genuine Team Captain") == "378;1" + assert schema.name_to_sku("Vintage Team Captain") == "378;3" + assert schema.name_to_sku("Team Captain") == "378;6" + assert schema.name_to_sku("Strange Team Captain") == "378;11" + assert schema.name_to_sku("Haunted Team Captain") == "378;13" + assert schema.name_to_sku("Collector's Team Captain") == "378;14" 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 + assert schema.defindex_to_name(378) == "Team Captain" + assert schema.defindex_to_name(725) == "Tour of Duty Ticket" + assert schema.defindex_to_name(5021) == "Mann Co. Supply Crate Key" + assert schema.defindex_to_name(-100) == "Random Craft Hat" + assert schema.defindex_to_name(-50) == "Random Craft Weapon" + assert schema.defindex_to_name(5000) == "Scrap Metal" + assert schema.defindex_to_name(5001) == "Reclaimed Metal" + assert schema.defindex_to_name(5002) == "Refined Metal" -def test_sku_to_name_key() -> None: - name = schema_items.sku_to_name("5021;6") - assert "Mann Co. Supply Crate Key" == name +def test_tod_sku() -> None: + assert schema.sku_to_base_name("725;6;uncraftable") == "Tour of Duty Ticket" + assert schema.sku_to_name("725;6;uncraftable") == "Uncraftable Tour of Duty Ticket" + assert ( + schema.sku_to_name("725;6;uncraftable", as_uncraftable=False) + == "Non-Craftable Tour of Duty Ticket" + ) 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" - - -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 + assert schema.sku_to_base_name("378;6"), "Team Captain" + assert schema.sku_to_full_name("378;6"), "The Team Captain" + assert schema.sku_to_name("378;6"), "Team Captain" + assert schema.sku_to_name("378;6;uncraftable"), "Uncraftable Team Captain" + + +def test_image_url() -> None: + assert schema.defindex_to_image_url(263) + assert schema.defindex_to_image_url(263, True) != schema.defindex_to_image_url(263) + assert schema.sku_to_image_url("-100;6") + assert schema.defindex_to_image_url(263) == schema.sku_to_image_url("-100;6") + assert schema.sku_to_image_url("5021;6") + assert not schema.defindex_to_image_url(-1) + assert not schema.sku_to_image_url("-1;6") + + +def test_name_to_sku() -> None: + assert schema.name_to_sku("Mann Co. Supply Crate Key") == "5021;6" + assert schema.name_to_sku("Uncraftable Tour of Duty Ticket") == "725;6;uncraftable" + assert schema.name_to_sku("Tour of Duty Ticket") == "725;6" + assert schema.name_to_sku("Max's Severed Head") == "162;6" + assert schema.name_to_sku("Name Tag") == "5020;6" + assert schema.name_to_sku("Taunt: The Schadenfreude") == "463;6" + assert schema.name_to_sku("Paint: Australium Gold") == "5037;6" + assert ( + schema.name_to_sku("Uncraftable Paint: An Extraordinary Abundance of Tinge") + == "5039;6;uncraftable" + ) + assert schema.name_to_sku("Earbuds") == "143;6" + assert schema.name_to_sku("Uncraftable Ap-Sap") == "933;6;uncraftable" + assert schema.name_to_sku("Professional Black Rose") == "727;6;kt-3" + assert schema.name_to_sku("Uncraftable Festivizer") == "5839;6;uncraftable" + assert schema.name_to_sku("Scorching Flames Killer Exclusive") == "538;5;u14" + assert schema.name_to_sku("Holy Grail Taunt: The Victory Lap") == "1172;5;u3003" + assert schema.name_to_sku("Strange Stormy Storm Bonk Boy") == "451;5;u29;strange" + assert schema.name_to_sku("Strange Festive Sandvich") == "1002;11" + assert ( + schema.name_to_sku("Professional Collector's Festivized Tomislav") + == "424;14;kt-3;festive" + ) + assert ( + schema.name_to_sku("Professional Strange Festive Rocket Launcher") + == "658;11;kt-3" + ) + assert ( + schema.name_to_sku("Professional Festivized Australium Sniper Rifle") + == "201;11;australium;kt-3;festive" + ) + assert schema.name_to_sku("Professional Genuine Original") == "513;1;kt-3" + assert schema.name_to_sku("Genuine Texas Ten Gallon") == "94;1" + assert schema.name_to_sku("Professional Vintage Lugermorph") == "160;3;kt-3" + + +def test_sku_to_name() -> None: + assert schema.sku_to_name("5021;6") == "Mann Co. Supply Crate Key" + assert schema.sku_to_name("725;6;uncraftable") == "Uncraftable Tour of Duty Ticket" + assert schema.sku_to_name("725;6") == "Tour of Duty Ticket" + assert schema.sku_to_name("162;6") == "Max's Severed Head" + assert schema.sku_to_name("5020;6") == "Name Tag" + assert schema.sku_to_name("463;6") == "Taunt: The Schadenfreude" + assert schema.sku_to_name("5037;6") == "Australium Gold" + assert ( + schema.sku_to_name("5039;6;uncraftable") + == "Uncraftable An Extraordinary Abundance of Tinge" + ) + assert schema.sku_to_name("143;6") == "Earbuds" + assert schema.sku_to_name("933;6;uncraftable") == "Uncraftable Ap-Sap" + assert schema.sku_to_name("727;6;kt-3") == "Professional Black Rose" + assert schema.sku_to_name("5839;6;uncraftable") == "Uncraftable Festivizer" + assert schema.sku_to_name("538;5;u14") == "Scorching Flames Killer Exclusive" + assert schema.sku_to_name("1172;5;u3003") == "Holy Grail Taunt: The Victory Lap" + assert schema.sku_to_name("451;5;u29;strange") == "Strange Stormy Storm Bonk Boy" + assert schema.sku_to_name("1002;11") == "Strange Festive Sandvich" + assert ( + schema.sku_to_name("424;14;kt-3;festive") + == "Professional Collector's Festivized Tomislav" + ) + assert ( + schema.sku_to_name("658;11;kt-3") + == "Professional Strange Festive Rocket Launcher" + ) + assert ( + schema.sku_to_name("201;11;australium;kt-3;festive") + == "Professional Festivized Australium Sniper Rifle" + ) + assert schema.sku_to_name("513;1;kt-3") == "Professional Genuine Original" + assert schema.sku_to_name("94;1") == "Genuine Texas Ten Gallon" + assert schema.sku_to_name("160;3;kt-3") == "Professional Vintage Lugermorph" diff --git a/tests/test_sku.py b/tests/test_sku.py index 7308d98..b238ff2 100644 --- a/tests/test_sku.py +++ b/tests/test_sku.py @@ -12,32 +12,10 @@ sku_to_defindex, sku_to_quality, ) -from src.tf2_utils.utils import read_json_file -file_path = "./tests/json/{}.json" - -def get_item_dict(file_name: str) -> dict: - return read_json_file(file_path.format(file_name)) - - -CRUSADERS_CROSSBOW = get_item_dict("crusaders_crossbow") -UNCRAFTABLE_HAT = get_item_dict("uncraftable_hat") -HONG_KONG_CONE = get_item_dict("hong_kong_cone") -ELLIS_CAP = get_item_dict("ellis_cap") - - -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 { +def test_ellis_cap_sku_properties(ellis_cap: dict) -> None: + assert get_sku_properties(ellis_cap) == { "defindex": 263, "quality": 6, "australium": False, @@ -46,35 +24,30 @@ def test_ellis_cap_sku_properties() -> None: "killstreak_tier": -1, "festivized": False, "strange": False, - } == sku - + } -def test_crusaders_crossbow_sku() -> None: - sku = get_sku(CRUSADERS_CROSSBOW) +def test_get_sku_items( + ellis_cap: dict, + crusaders_crossbow: dict, + hong_kong_cone: dict, + uncraftable_hat: dict, +) -> None: + # https://marketplace.tf/items/tf2/263;6 + assert get_sku(ellis_cap) == "263;6" # 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) - + assert get_sku(crusaders_crossbow) == "305;11;kt-3;festive" # 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) - + assert get_sku(hong_kong_cone) == "30177;5;u107;strange" # https://marketplace.tf/items/tf2/734;6;uncraftable - assert "734;6;uncraftable" == sku + assert get_sku(uncraftable_hat) == "734;6;uncraftable" def test_properties() -> None: sku = "734;6;uncraftable" - assert 734, sku_to_defindex(sku) - assert 6, sku_to_quality(sku) + assert sku_to_defindex(sku) == 734 + assert sku_to_quality(sku) == 6 assert sku_is_uncraftable(sku) @@ -100,17 +73,17 @@ def test_is_pure() -> None: def test_get_metal() -> None: - assert 9, get_metal("5002;6") - assert 3, get_metal("5001;6") - assert 1, get_metal("5000;6") + assert get_metal("5002;6") == 9 + assert get_metal("5001;6") == 3 + assert get_metal("5000;6") == 1 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") + assert sku_to_color("734;6;uncraftable") == "7D6D00" + assert sku_to_color("30469;1") == "4D7455" with pytest.raises(AssertionError): sku_to_color("notsku") diff --git a/tests/test_utils.py b/tests/test_utils.py index e891ae9..6fd19d7 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -9,50 +9,32 @@ to_scrap, ) -STEAM_ID = "76561198253325712" -ACCOUNT_ID = "293059984" - -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_steam_id(steam_id: str, account_id: str) -> None: + assert account_id_to_steam_id(account_id) == steam_id + assert steam_id_to_account_id(steam_id) == account_id + assert account_id_to_steam_id(int(account_id)) == steam_id + assert steam_id_to_account_id(int(steam_id)) == account_id def test_to_refined() -> None: - scrap = 43 - refined = to_refined(scrap) - - assert 4.77 == refined + assert to_refined(43) == 4.77 def test_to_scrap() -> None: - refined = 2.44 - scrap = to_scrap(refined) - - assert 22 == scrap - - -def test_refinedify_up() -> None: - wrong_value = 32.53 - refined = refinedify(wrong_value) - - assert 32.55 == refined - + assert to_scrap(2.44) == 22 -def test_refinedify_down() -> None: - wrong_value = 12.47 - refined = refinedify(wrong_value) - assert 12.44 == refined +def test_refinedify() -> None: + assert refinedify(32.53) == 32.55 + assert refinedify(12.47) == 12.44 -def test_trade_url() -> None: +def test_trade_url(steam_id: str, account_id: str) -> None: trade_url = ( - "https://steamcommunity.com/tradeoffer/new/?partner=293059984&token=0-l_idZR" # noqa + "https://steamcommunity.com/tradeoffer/new/?partner=293059984&token=0-l_idZR" ) - 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) + assert get_account_id_from_trade_url(trade_url) == account_id + assert get_steam_id_from_trade_url(trade_url) == steam_id + assert get_token_from_trade_url(trade_url) == "0-l_idZR"