Skip to content
Snippets Groups Projects
Commit 99504a12 authored by Bastien Abadie's avatar Bastien Abadie
Browse files

Cache YAML payloads in tests

parent 6fc5b19f
No related branches found
No related tags found
1 merge request!59Cache YAML payloads in tests
Pipeline #78247 passed
# -*- coding: utf-8 -*-
import hashlib
import json
import os
import sys
from pathlib import Path
import pytest
import yaml
from arkindex.mock import MockApiClient
from arkindex_worker.git import GitHelper, GitlabHelper
......@@ -12,9 +14,37 @@ from arkindex_worker.worker import ElementsWorker
FIXTURES_DIR = Path(__file__).resolve().parent / "data"
__yaml_cache = {}
@pytest.fixture
def cache_yaml(monkeypatch):
"""
Cache all calls to yaml.safe_load in order to speedup
every test cases that load the OpenAPI schema
"""
# Keep a reference towards the original function
_original_yaml_load = yaml.safe_load
def _cached_yaml_load(yaml_payload):
# Create a unique cache key for direct YAML strings
# and file descriptors
if isinstance(yaml_payload, str):
key = hashlib.md5(yaml_payload.encode("utf-8")).hexdigest()
else:
key = yaml_payload.name
# Cache result
if key not in __yaml_cache:
__yaml_cache[key] = _original_yaml_load(yaml_payload)
return __yaml_cache[key]
monkeypatch.setattr(yaml, "safe_load", _cached_yaml_load)
@pytest.fixture(autouse=True)
def setup_api(responses, monkeypatch):
def setup_api(responses, monkeypatch, cache_yaml):
# Always use the environment variable first
schema_url = os.environ.get("ARKINDEX_API_SCHEMA_URL")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment