97 lines
5.1 KiB
Python
97 lines
5.1 KiB
Python
import pytest
|
|
from django.core.exceptions import ValidationError
|
|
from django.test import Client
|
|
from wagtail.models import Page, Site
|
|
|
|
from apps.forms.models import FormDefinition
|
|
from .industry_templates import INDUSTRIES, demo_root, seed_industry_templates
|
|
from .models import NavigationMenu
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_industry_templates_are_independent_and_non_overwriting():
|
|
site = Site.objects.get(is_default_site=True)
|
|
original_root = site.root_page_id
|
|
original_pages = list(Page.objects.values_list("pk", "title", "latest_revision_id"))
|
|
original_menus = list(NavigationMenu.objects.values_list("pk", "name"))
|
|
created = seed_industry_templates()
|
|
assert len(created) == 13 * len(INDUSTRIES)
|
|
assert len(set(demo_root(slug).pk for slug in INDUSTRIES)) == len(INDUSTRIES)
|
|
for slug in INDUSTRIES:
|
|
assert demo_root(slug).get_descendants().count() == 12
|
|
home = demo_root("manufacturing")
|
|
home.title = "人工编辑制造演示"
|
|
home.save()
|
|
revision = home.save_revision()
|
|
assert seed_industry_templates() == []
|
|
home.refresh_from_db()
|
|
assert home.title == "人工编辑制造演示"
|
|
assert home.latest_revision_id == revision.pk
|
|
site.refresh_from_db()
|
|
assert site.root_page_id == original_root
|
|
assert list(Page.objects.filter(pk__in=[p[0] for p in original_pages]).values_list("pk", "title", "latest_revision_id")) == original_pages
|
|
assert list(NavigationMenu.objects.values_list("pk", "name")) == original_menus
|
|
assert FormDefinition.objects.filter(name__contains="演示咨询").count() == len(INDUSTRIES)
|
|
assert not FormDefinition.objects.filter(name__contains="演示咨询").exclude(notification_email="").exists()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_industry_api_is_scoped_and_serializes_blocks():
|
|
seed_industry_templates()
|
|
client = Client()
|
|
listing = client.get("/api/v1/custom/core/demos/").json()
|
|
assert len(listing["items"]) == len(INDUSTRIES)
|
|
for slug, spec in INDUSTRIES.items():
|
|
result = client.get(f"/api/v1/custom/core/demos/{slug}/").json()
|
|
assert result["company"] == spec["company"]
|
|
assert result["blocks"][0]["type"] == "hero"
|
|
assert all(link["url"].startswith(f"/demo/{slug}/") for link in result["navigation"])
|
|
product_slug = spec["products"][0][0]
|
|
detail = client.get(f"/api/v1/custom/core/demos/{slug}/", {"path": f"products/{product_slug}"})
|
|
assert detail.status_code == 200
|
|
assert detail.json()["title"] == spec["products"][0][1]
|
|
contact = client.get(f"/api/v1/custom/core/demos/{slug}/", {"path": "contact"}).json()
|
|
form = next(block for block in contact["blocks"] if block["type"] == "form")["value"]["form"]
|
|
assert len(form["fields"]) == 5
|
|
assert client.get("/api/v1/custom/core/demos/unknown/").status_code == 404
|
|
for path in ["../demo-software", "products/content-platform", "products//missing", "missing"]:
|
|
assert client.get("/api/v1/custom/core/demos/manufacturing/", {"path": path}).status_code == 404
|
|
product = demo_root("software").get_children().get(slug="products").get_children().first().specific
|
|
product.unpublish()
|
|
assert client.get("/api/v1/custom/core/demos/software/", {"path": f"products/{product.slug}"}).status_code == 404
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_industry_initialization_rolls_back_on_conflict():
|
|
root = Page.get_first_root_node()
|
|
root.add_child(instance=Page(title="冲突页面", slug="demo-manufacturing"))
|
|
before = Page.objects.count()
|
|
with pytest.raises(ValidationError):
|
|
seed_industry_templates()
|
|
assert Page.objects.count() == before
|
|
assert demo_root("software") is None
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@pytest.mark.parametrize("existing_slugs", [
|
|
("software", "manufacturing", "consulting"),
|
|
("software", "manufacturing", "consulting", "education", "healthcare", "dining"),
|
|
("software", "manufacturing", "consulting", "education", "healthcare", "dining",
|
|
"construction", "real-estate", "home-renovation"),
|
|
("software", "manufacturing", "consulting", "education", "healthcare", "dining",
|
|
"construction", "real-estate", "home-renovation", "agri-food", "energy-environment", "auto-services"),
|
|
("software", "manufacturing", "consulting", "education", "healthcare", "dining",
|
|
"construction", "real-estate", "home-renovation", "agri-food", "energy-environment", "auto-services",
|
|
"culture-media", "sports-fitness", "pet-services"),
|
|
])
|
|
def test_adding_new_industries_preserves_existing_demos(monkeypatch, existing_slugs):
|
|
existing = {slug: spec for slug, spec in INDUSTRIES.items() if slug in existing_slugs}
|
|
with monkeypatch.context() as patch:
|
|
patch.setattr("apps.core.industry_templates.INDUSTRIES", existing)
|
|
seed_industry_templates()
|
|
before = list(Page.objects.order_by("pk").values_list("pk", "title", "latest_revision_id"))
|
|
assert len(seed_industry_templates()) == 13 * (len(INDUSTRIES) - len(existing))
|
|
assert list(Page.objects.filter(pk__in=[row[0] for row in before]).order_by("pk").values_list("pk", "title", "latest_revision_id")) == before
|
|
for slug in INDUSTRIES.keys() - existing.keys():
|
|
assert demo_root(slug).get_descendants().count() == 12
|