|
| 1 | +import datetime |
| 2 | +import sqlite3 |
| 3 | +import unittest |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +from tests import load_resource |
| 7 | +from tibiawikisql.api import Article |
| 8 | +from tibiawikisql.generation import ( |
| 9 | + WEAPON_PROFICIENCY_NAME_ARTICLE, |
| 10 | + WEAPON_PROFICIENCY_TABLES_ARTICLE, |
| 11 | + generate_item_proficiency_perks, |
| 12 | + wiki_client, |
| 13 | +) |
| 14 | +from tibiawikisql.schema import ItemProficiencyPerkTable, ItemTable |
| 15 | + |
| 16 | + |
| 17 | +class TestGeneration(unittest.TestCase): |
| 18 | + def setUp(self): |
| 19 | + self.conn = sqlite3.connect(":memory:") |
| 20 | + self.conn.executescript(ItemTable.get_create_table_statement()) |
| 21 | + self.conn.executescript(ItemProficiencyPerkTable.get_create_table_statement()) |
| 22 | + timestamp = datetime.datetime.fromisoformat("2024-01-01T00:00:00+00:00") |
| 23 | + ItemTable.insert(self.conn, article_id=1, title="Amber Axe", timestamp=timestamp) |
| 24 | + ItemTable.insert(self.conn, article_id=2, title="Amber Cudgel", timestamp=timestamp) |
| 25 | + |
| 26 | + def tearDown(self): |
| 27 | + self.conn.close() |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + def build_article(title: str, content: str) -> Article: |
| 31 | + return Article( |
| 32 | + article_id=9999, |
| 33 | + title=title, |
| 34 | + timestamp=datetime.datetime.fromisoformat("2024-01-01T00:00:00+00:00"), |
| 35 | + content=content, |
| 36 | + ) |
| 37 | + |
| 38 | + def test_generate_item_proficiency_perks(self): |
| 39 | + mapping_article = self.build_article( |
| 40 | + WEAPON_PROFICIENCY_NAME_ARTICLE, |
| 41 | + load_resource("content_weapon_proficiency_name.txt"), |
| 42 | + ) |
| 43 | + tables_article = self.build_article( |
| 44 | + WEAPON_PROFICIENCY_TABLES_ARTICLE, |
| 45 | + load_resource("content_weapon_proficiency_tables.txt"), |
| 46 | + ) |
| 47 | + data_store = {"items_map": {"amber axe": 1, "amber cudgel": 2}} |
| 48 | + |
| 49 | + with ( |
| 50 | + patch.object(wiki_client, "get_article", side_effect=[mapping_article, tables_article]), |
| 51 | + patch("tibiawikisql.generation.click.echo") as mock_echo, |
| 52 | + ): |
| 53 | + generate_item_proficiency_perks(self.conn, data_store) |
| 54 | + |
| 55 | + rows = self.conn.execute( |
| 56 | + "SELECT item_id, proficiency_level, skill_image, icon, effect " |
| 57 | + "FROM item_proficiency_perk ORDER BY rowid", |
| 58 | + ).fetchall() |
| 59 | + self.assertEqual(4, len(rows)) |
| 60 | + self.assertEqual( |
| 61 | + [ |
| 62 | + (1, 1, "Axe Skill Bonus", None, "+1 Axe Fighting"), |
| 63 | + (1, 2, "Auto-Attack Critical Extra Damage", None, "+10% critical extra damage for auto-attacks"), |
| 64 | + ( |
| 65 | + 1, |
| 66 | + 2, |
| 67 | + "Axe Extra Damage Auto-Attack", |
| 68 | + "Special Icon", |
| 69 | + "+5% of your Axe Fighting as extra damage for auto-attacks", |
| 70 | + ), |
| 71 | + (2, 1, "Club Skill Bonus", None, "+1 Club Fighting"), |
| 72 | + ], |
| 73 | + [tuple(row) for row in rows], |
| 74 | + ) |
| 75 | + messages = " ".join(call.args[0] for call in mock_echo.call_args_list if call.args) |
| 76 | + self.assertIn("Parsed weapon proficiency perks", messages) |
| 77 | + |
| 78 | + def test_generate_item_proficiency_perks_warn_and_continue(self): |
| 79 | + mapping_article = self.build_article( |
| 80 | + WEAPON_PROFICIENCY_NAME_ARTICLE, |
| 81 | + """{{#switch:{{{1|}}} |
| 82 | +| Amber Axe = Sanguine 1H Axe |
| 83 | +| Amber Cudgel = Missing Section |
| 84 | +| Unknown Item = Sanguine 1H Club |
| 85 | +|#default = |
| 86 | +}}""", |
| 87 | + ) |
| 88 | + tables_article = self.build_article( |
| 89 | + WEAPON_PROFICIENCY_TABLES_ARTICLE, |
| 90 | + load_resource("content_weapon_proficiency_tables.txt"), |
| 91 | + ) |
| 92 | + data_store = {"items_map": {"amber axe": 1, "amber cudgel": 2}} |
| 93 | + |
| 94 | + with ( |
| 95 | + patch.object(wiki_client, "get_article", side_effect=[mapping_article, tables_article]), |
| 96 | + patch("tibiawikisql.generation.click.echo") as mock_echo, |
| 97 | + ): |
| 98 | + generate_item_proficiency_perks(self.conn, data_store) |
| 99 | + |
| 100 | + rows = self.conn.execute( |
| 101 | + "SELECT item_id, proficiency_level, skill_image, icon, effect FROM item_proficiency_perk", |
| 102 | + ).fetchall() |
| 103 | + self.assertEqual(3, len(rows)) |
| 104 | + messages = " ".join(call.args[0] for call in mock_echo.call_args_list if call.args) |
| 105 | + self.assertIn("Could not map 1 item names to IDs", messages) |
| 106 | + self.assertIn("Could not find 1 proficiency sections", messages) |
| 107 | + self.assertIn("Skipped 1 malformed weapon proficiency perks", messages) |
| 108 | + |
| 109 | + def test_generate_item_proficiency_perks_no_title_fallback(self): |
| 110 | + mapping_article = self.build_article( |
| 111 | + WEAPON_PROFICIENCY_NAME_ARTICLE, |
| 112 | + load_resource("content_weapon_proficiency_name.txt"), |
| 113 | + ) |
| 114 | + data_store = {"items_map": {"amber axe": 1, "amber cudgel": 2}} |
| 115 | + |
| 116 | + def get_article_side_effect(title: str) -> Article | None: |
| 117 | + if title == WEAPON_PROFICIENCY_NAME_ARTICLE: |
| 118 | + return mapping_article |
| 119 | + return None |
| 120 | + |
| 121 | + with ( |
| 122 | + patch.object(wiki_client, "get_article", side_effect=get_article_side_effect) as mock_get_article, |
| 123 | + patch("tibiawikisql.generation.click.echo") as mock_echo, |
| 124 | + ): |
| 125 | + generate_item_proficiency_perks(self.conn, data_store) |
| 126 | + |
| 127 | + rows = self.conn.execute("SELECT COUNT(*) FROM item_proficiency_perk").fetchone() |
| 128 | + self.assertEqual(0, rows[0]) |
| 129 | + self.assertEqual( |
| 130 | + [WEAPON_PROFICIENCY_NAME_ARTICLE, WEAPON_PROFICIENCY_TABLES_ARTICLE], |
| 131 | + [call.args[0] for call in mock_get_article.call_args_list], |
| 132 | + ) |
| 133 | + messages = " ".join(call.args[0] for call in mock_echo.call_args_list if call.args) |
| 134 | + self.assertIn("Could not fetch weapon proficiency pages", messages) |
| 135 | + |
| 136 | + def test_generate_item_proficiency_perks_case_insensitive_section_match(self): |
| 137 | + timestamp = datetime.datetime.fromisoformat("2024-01-01T00:00:00+00:00") |
| 138 | + ItemTable.insert(self.conn, article_id=3, title="Stale Bread of Ancientness", timestamp=timestamp) |
| 139 | + |
| 140 | + mapping_article = self.build_article( |
| 141 | + WEAPON_PROFICIENCY_NAME_ARTICLE, |
| 142 | + """{{#switch:{{{1|}}} |
| 143 | +| Stale Bread of Ancientness = Club 1H Stale Bread of Ancientness |
| 144 | +|#default = |
| 145 | +}}""", |
| 146 | + ) |
| 147 | + tables_article = self.build_article( |
| 148 | + WEAPON_PROFICIENCY_TABLES_ARTICLE, |
| 149 | + """===Club 1H Stale Bread Of Ancientness=== |
| 150 | +{{Weapon Proficiency Table |
| 151 | +|perk_1 = |
| 152 | +{{Weapon Proficiency Button |skill_image=Club Skill Bonus|icon=|text=+1 Club Fighting}} |
| 153 | +}}""", |
| 154 | + ) |
| 155 | + data_store = {"items_map": {"stale bread of ancientness": 3}} |
| 156 | + |
| 157 | + with ( |
| 158 | + patch.object(wiki_client, "get_article", side_effect=[mapping_article, tables_article]), |
| 159 | + patch("tibiawikisql.generation.click.echo"), |
| 160 | + ): |
| 161 | + generate_item_proficiency_perks(self.conn, data_store) |
| 162 | + |
| 163 | + rows = self.conn.execute( |
| 164 | + "SELECT item_id, proficiency_level, skill_image, icon, effect FROM item_proficiency_perk", |
| 165 | + ).fetchall() |
| 166 | + self.assertEqual([(3, 1, "Club Skill Bonus", None, "+1 Club Fighting")], [tuple(row) for row in rows]) |
0 commit comments