Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from plone/issue-2-pytest-jsonschema
Browse files Browse the repository at this point in the history
Use pytest-jsonschema for tests
  • Loading branch information
ericof authored Mar 28, 2024
2 parents e1e5571 + 0320dd7 commit 8af7b0b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 61 deletions.
4 changes: 1 addition & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ cookiecutter
isort
pytest
pytest-cookies
pytest-jsonschema >= 1.0.0a2
wheel
ruamel.yaml
jsonschema
requests
53 changes: 0 additions & 53 deletions volto_addon/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
"""Pytest configuration."""

import json
import logging
import re
from copy import deepcopy
from pathlib import Path
from typing import List

import jsonschema
import pytest
import requests
from ruamel.yaml import YAML

logger = logging.getLogger("cookiecuter-volto-volto_addon")
yaml = YAML()


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -70,47 +61,3 @@ def func(root_dir: Path) -> List[Path]:
def cutter_result(cookies_session, context):
"""Cookiecutter result."""
return cookies_session.bake(extra_context=context)


@pytest.fixture(scope="session")
def json_schemas() -> dict:
"""Validate files."""
remote_schemas = [
["package.json", "https://json.schemastore.org/package.json"],
["tsconfig.json", "https://json.schemastore.org/tsconfig.json"],
["github-workflow", "https://json.schemastore.org/github-workflow.json"],
]
schemas = {}
for name, url in remote_schemas:
response = requests.get(url)
if response.status_code == 200:
schemas[name] = response.json()
return schemas


@pytest.fixture
def validate_schema(json_schemas):
"""Validate a file against a known JSON Schema."""

def func(file_path: Path, schema_name):
validation = False
# Remove . from file extension
extension = file_path.suffix[1:]
raw = file_path.read_text()
if extension in ("yaml", "yml"):
data = yaml.load(raw)
elif extension in ("json"):
data = json.loads(raw)
else:
data = {}
schema = json_schemas[schema_name]
try:
jsonschema.validate(data, schema)
except jsonschema.ValidationError as exc:
logger.error(f"Validation of {file_path} failed {exc}")
validation = False
else:
validation = True
return validation

return func
10 changes: 5 additions & 5 deletions volto_addon/tests/test_cutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ def test_pkg_src_files_generated(cutter_result, file_path: str):
[".github/workflows/i18n.yml", "github-workflow"],
[".github/workflows/storybook.yml", "github-workflow"],
[".github/workflows/unit.yml", "github-workflow"],
["package.json", "package.json"],
["packages/volto-addon/package.json", "package.json"],
["packages/volto-addon/tsconfig.json", "tsconfig.json"],
["package.json", "package"],
["packages/volto-addon/package.json", "package"],
["packages/volto-addon/tsconfig.json", "tsconfig"],
],
)
def test_pkg_src_files_generated(
cutter_result, validate_schema, file_path: str, schema_name: str
cutter_result, schema_validate_file, file_path: str, schema_name: str
):
path = cutter_result.project_path / file_path
assert validate_schema(path, schema_name)
assert schema_validate_file(path, schema_name)

0 comments on commit 8af7b0b

Please sign in to comment.