From 9e457db2e96248e107c39ab78e5f8d6dbd75d755 Mon Sep 17 00:00:00 2001 From: ZanSara Date: Mon, 16 Jan 2023 15:36:14 +0100 Subject: [PATCH] test: add version deprecation fixture (#3851) * add fixture * Update test/conftest.py * remove +2 and add tests * few typos * more cases * Update test/conftest.py --- test/conftest.py | 33 ++++++++++++++++- test/others/test_utils.py | 76 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index 59541d651e..51d8a0ce06 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -12,6 +12,7 @@ from pathlib import Path import os import re +from functools import wraps import requests_cache import responses @@ -23,7 +24,7 @@ import pytest import requests -from haystack import Answer, BaseComponent +from haystack import Answer, BaseComponent, __version__ as haystack_version from haystack.document_stores import ( BaseDocumentStore, InMemoryDocumentStore, @@ -107,6 +108,36 @@ requests_cache.install_cache(urls_expire_after={"huggingface.co": timedelta(hours=1), "*": requests_cache.DO_NOT_CACHE}) +def fail_at_version(version_major, version_minor): + """ + Reminder to remove deprecated features. + + ```python + from ..conftest import fail_at_version + + @fail_at_version(1, 10) # Will fail once Haystack version is greater than or equal to 1.10 + def test_test(): + assert True + ``` + """ + + def decorator(function): + current_version = tuple(int(num) for num in haystack_version.split(".")[:2]) + + @wraps(function) + def wrapper(*args, **kwargs): + if current_version[0] > version_major or ( + current_version[0] == version_major and current_version[1] >= version_minor + ): + pytest.fail(reason=f"This feature is marked for removal in v{version_major}.{version_minor}") + return_value = function(*args, **kwargs) + return return_value + + return wrapper + + return decorator + + def pytest_collection_modifyitems(config, items): # add pytest markers for tests that are not explicitly marked but include some keywords name_to_markers = { diff --git a/test/others/test_utils.py b/test/others/test_utils.py index 36aeb91662..68449bcb20 100644 --- a/test/others/test_utils.py +++ b/test/others/test_utils.py @@ -8,6 +8,9 @@ import responses from responses import matchers +import _pytest +from ..conftest import fail_at_version, haystack_version + from haystack.errors import OpenAIRateLimitError from haystack.schema import Answer, Document, Span, Label from haystack.utils.deepsetcloud import DeepsetCloud, DeepsetCloudExperiments @@ -17,7 +20,15 @@ from haystack.utils.reflection import retry_with_exponential_backoff from haystack.utils.context_matching import calculate_context_similarity, match_context, match_contexts -from ..conftest import DC_API_ENDPOINT, DC_API_KEY, MOCK_DC, SAMPLES_PATH, deepset_cloud_fixture +from ..conftest import ( + DC_API_ENDPOINT, + DC_API_KEY, + MOCK_DC, + SAMPLES_PATH, + deepset_cloud_fixture, + fail_at_version, + haystack_version, +) TEST_CONTEXT = context = """Der Merkantilismus förderte Handel und Verkehr mit teils marktkonformen, teils dirigistischen Maßnahmen. An der Schwelle zum 19. Jahrhundert entstand ein neuer Typus des Nationalstaats, der die Säkularisation durchsetzte, @@ -38,6 +49,69 @@ """ +def test_deprecation_fixture(): + current_major, current_minor = tuple(int(num) for num in haystack_version.split(".")[:2]) + + @fail_at_version(0, 1) + def test_previous_major_and_minor(): + assert True + + with pytest.raises(_pytest.outcomes.Failed): + test_previous_major_and_minor() + + @fail_at_version(0, current_minor) + def test_previous_major_and_same_minor(): + assert True + + with pytest.raises(_pytest.outcomes.Failed): + test_previous_major_and_same_minor() + + @fail_at_version(0, 1000) + def test_previous_major_and_later_minor(): + assert True + + with pytest.raises(_pytest.outcomes.Failed): + test_previous_major_and_later_minor() + + @fail_at_version(current_major, 1) + def test_same_major_and_previous_minor(): + assert True + + with pytest.raises(_pytest.outcomes.Failed): + test_same_major_and_previous_minor() + + @fail_at_version(current_major, current_minor) + def test_same_version(): + assert True + + with pytest.raises(_pytest.outcomes.Failed): + test_same_version() + + @fail_at_version(current_major, 1000) + def test_same_major_and_later_minor(): + assert True + + test_same_major_and_later_minor() + + @fail_at_version(1000, 1) + def test_later_major_and_previous_minor(): + assert True + + test_later_major_and_previous_minor() + + @fail_at_version(1000, current_minor) + def test_later_major_and_same_minor(): + assert True + + test_later_major_and_previous_minor() + + @fail_at_version(1000, 1000) + def test_later_major_and_later_minor(): + assert True + + test_later_major_and_later_minor() + + def test_convert_files_to_docs(): documents = convert_files_to_docs( dir_path=(SAMPLES_PATH).absolute(), clean_func=clean_wiki_text, split_paragraphs=True