Skip to content

Commit

Permalink
test: add version deprecation fixture (deepset-ai#3851)
Browse files Browse the repository at this point in the history
* add fixture

* Update test/conftest.py

* remove +2 and add tests

* few typos

* more cases

* Update test/conftest.py
  • Loading branch information
ZanSara authored Jan 16, 2023
1 parent 3ffdb0a commit 9e457db
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
33 changes: 32 additions & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pathlib import Path
import os
import re
from functools import wraps

import requests_cache
import responses
Expand All @@ -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,
Expand Down Expand Up @@ -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 = {
Expand Down
76 changes: 75 additions & 1 deletion test/others/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 9e457db

Please sign in to comment.