aboutsummaryrefslogtreecommitdiff
path: root/tests/managers/thl/test_category.py
diff options
context:
space:
mode:
authorstuppie2026-09-07 11:47:43 -0600
committerstuppie2026-09-07 11:47:43 -0600
commit092960233652cce1f4dc7841856034a6635e9cd9 (patch)
tree46e5fcd4d1e1b7ed0b987980c6c67ffa6e6b45c7 /tests/managers/thl/test_category.py
parent80fd8aab4c7271ddb619b0de18741d7ac77b490b (diff)
parent242579a44855873d5e054e375440e9d3492cd682 (diff)
downloadgeneralresearch-092960233652cce1f4dc7841856034a6635e9cd9.tar.gz
generalresearch-092960233652cce1f4dc7841856034a6635e9cd9.zip
Merge branch 'master' into dev-greg
Diffstat (limited to 'tests/managers/thl/test_category.py')
-rw-r--r--tests/managers/thl/test_category.py112
1 files changed, 66 insertions, 46 deletions
diff --git a/tests/managers/thl/test_category.py b/tests/managers/thl/test_category.py
index ad0f07b..a2805bc 100644
--- a/tests/managers/thl/test_category.py
+++ b/tests/managers/thl/test_category.py
@@ -1,12 +1,21 @@
+from __future__ import annotations
+
+from collections.abc import Callable
+from typing import TYPE_CHECKING
+
import pytest
from generalresearch.models.thl.category import Category
+if TYPE_CHECKING:
+ from generalresearch.managers.thl.category import CategoryManager
+ from generalresearch.pg_helper import PostgresConfig
+
class TestCategory:
@pytest.fixture
- def beauty_fitness(self, thl_web_rw):
+ def beauty_fitness(self) -> Category:
return Category(
uuid="12c1e96be82c4642a07a12a90ce6f59e",
@@ -16,72 +25,83 @@ class TestCategory:
)
@pytest.fixture
- def hair_care(self, beauty_fitness):
+ def hair_care(self, beauty_fitness: Category) -> Category:
return Category(
uuid="dd76c4b565d34f198dad3687326503d6",
adwords_vertical_id="146",
label="Hair Care",
- path="/Beauty & Fitness/Hair Care",
+ path=f"{beauty_fitness.path}/Hair Care",
)
@pytest.fixture
- def hair_loss(self, hair_care):
+ def hair_loss(self, hair_care: Category) -> Category:
return Category(
uuid="aacff523c8e246888215611ec3b823c0",
adwords_vertical_id="235",
label="Hair Loss",
- path="/Beauty & Fitness/Hair Care/Hair Loss",
+ path=f"{hair_care.path}/Hair Loss",
)
@pytest.fixture
def category_data(
- self, category_manager, thl_web_rw, beauty_fitness, hair_care, hair_loss
- ):
- cats = [beauty_fitness, hair_care, hair_loss]
- data = [x.model_dump(mode="json") for x in cats]
- # We need the parent pk's to set the parent_id. So insert all without a parent,
- # then pull back all pks and map to the parents as parsed by the parent_path
- query = """
- INSERT INTO marketplace_category
- (uuid, adwords_vertical_id, label, path)
- VALUES
- (%(uuid)s, %(adwords_vertical_id)s, %(label)s, %(path)s)
- ON CONFLICT (uuid) DO NOTHING;
- """
- with thl_web_rw.make_connection() as conn:
- with conn.cursor() as c:
- c.executemany(query=query, params_seq=data)
- conn.commit()
-
- res = thl_web_rw.execute_sql_query("SELECT id, path FROM marketplace_category")
- path_id = {x["path"]: x["id"] for x in res}
- data = [
- {"id": path_id[c.path], "parent_id": path_id[c.parent_path]}
- for c in cats
- if c.parent_path
- ]
- query = """
- UPDATE marketplace_category
- SET parent_id = %(parent_id)s
- WHERE id = %(id)s;
- """
- with thl_web_rw.make_connection() as conn:
- with conn.cursor() as c:
- c.executemany(query=query, params_seq=data)
- conn.commit()
-
- category_manager.populate_caches()
+ self,
+ category_manager: CategoryManager,
+ thl_web_rw: PostgresConfig,
+ beauty_fitness: Category,
+ hair_care: Category,
+ hair_loss: Category,
+ ) -> Callable[..., None]:
+
+ def _inner():
+ cats = [beauty_fitness, hair_care, hair_loss]
+ data = [x.model_dump(mode="json") for x in cats]
+ # We need the parent pk's to set the parent_id. So insert all without a parent,
+ # then pull back all pks and map to the parents as parsed by the parent_path
+ query = """
+ INSERT INTO marketplace_category
+ (uuid, adwords_vertical_id, label, path)
+ VALUES
+ (%(uuid)s, %(adwords_vertical_id)s, %(label)s, %(path)s)
+ ON CONFLICT (uuid) DO NOTHING;
+ """
+ with thl_web_rw.make_connection() as conn:
+ with conn.cursor() as c:
+ c.executemany(query=query, params_seq=data)
+ conn.commit()
+
+ res = thl_web_rw.execute_sql_query(
+ "SELECT id, path FROM marketplace_category"
+ )
+ path_id = {x["path"]: x["id"] for x in res}
+ data = [
+ {"id": path_id[c.path], "parent_id": path_id[c.parent_path]}
+ for c in cats
+ if c.parent_path
+ ]
+ query = """
+ UPDATE marketplace_category
+ SET parent_id = %(parent_id)s
+ WHERE id = %(id)s;
+ """
+ with thl_web_rw.make_connection() as conn:
+ with conn.cursor() as c:
+ c.executemany(query=query, params_seq=data)
+ conn.commit()
+
+ category_manager.populate_caches()
+
+ return _inner
def test(
self,
- category_data,
- category_manager,
- beauty_fitness,
- hair_care,
- hair_loss,
+ category_data: Callable[..., None],
+ category_manager: CategoryManager,
+ beauty_fitness: Category,
):
+ category_data()
+
# category_manager on init caches the category info. This rarely/never changes so this is fine,
# but now that tests get run on a new db each time, the category_manager is inited before
# the fixtures run. so category_manager's cache needs to be rerun