Skip to content

Commit

Permalink
assert_raises_regex -> assert_raises
Browse files Browse the repository at this point in the history
Also parameterize a few "yield" tests.
  • Loading branch information
millerdev committed Sep 7, 2024
1 parent 446773c commit 98e14a2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 47 deletions.
19 changes: 10 additions & 9 deletions corehq/apps/case_search/tests/test_xpath_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import re

import pytest
from eulxml.xpath import parse as parse_xpath
from nose.tools import assert_raises_regex
from nose.tools import assert_raises

from corehq.apps.case_search.exceptions import XPathFunctionException
from corehq.apps.case_search.filter_dsl import (
Expand Down Expand Up @@ -31,11 +34,9 @@
)]


def test_invalid():
def _check(xpath, error_msg):
with assert_raises_regex(XPathFunctionException, error_msg):
parsed = parse_xpath(xpath)
build_filter_from_ast(parsed, SearchFilterContext("mydomain"))

for xpath, expected in INVALID_TEST_CASES:
yield _check, xpath, expected
@pytest.mark.parametrize("xpath, error_msg", INVALID_TEST_CASES)
def test_invalid(xpath, error_msg):
regex = re.compile(re.escape(error_msg))
with assert_raises(XPathFunctionException, msg=regex):
parsed = parse_xpath(xpath)
build_filter_from_ast(parsed, SearchFilterContext("mydomain"))
67 changes: 33 additions & 34 deletions corehq/apps/es/tests/test_test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import re
from contextlib import nullcontext

import pytest
from django.test import SimpleTestCase
from nose.tools import assert_raises_regex
from testil import assert_raises
from unittest.mock import patch

Expand Down Expand Up @@ -121,17 +124,14 @@ def test_only_dog_index_exists(self):


@es_test_attr
def test_index_state_with_function_decorator():

@es_test(requires=[pigs_adapter])
def test_pig_index_exists():
assert_index_exists(pigs_adapter)
@es_test(requires=[pigs_adapter])
def test_pig_index_exists():
assert_index_exists(pigs_adapter)

def test_pig_index_does_not_exist():
assert_not_index_exists(pigs_adapter)

yield test_pig_index_exists,
yield test_pig_index_does_not_exist,
@es_test_attr
def test_pig_index_does_not_exist():
assert_not_index_exists(pigs_adapter)


def assert_index_exists(adapter):
Expand All @@ -148,47 +148,46 @@ def assert_not_index_exists(adapter):

@es_test_attr
def test_setup_class_expects_classmethod():
with assert_raises_regex(ValueError, "^'setup_class' expects a classmethod"):
with assert_raises(ValueError, msg=re.compile("^'setup_class' expects a classmethod")):
@es_test(requires=[pigs_adapter], setup_class=True)
class TestExpectsClassmethod:
def setUpClass(self):
pass


@pytest.mark.parametrize("args", [
(), # without type/mapping
("test_doc", {"_meta": {}}), # with type/mapping
])
@es_test
def test_temporary_index():
def test_temporary_index(args):
index = "test_index"
with temporary_index(index, *args):
assert manager.index_exists(index)
assert not manager.index_exists(index)

def test_temporary_index_with_args(*args):
with temporary_index(index, *args):
assert manager.index_exists(index)
assert not manager.index_exists(index)

yield test_temporary_index_with_args, # without type/mapping
yield test_temporary_index_with_args, "test_doc", {"_meta": {}} # with type/mapping

@pytest.mark.parametrize("has_index, type_, mapping", [
# test while index exists
(True, "test_doc", None), # no mapping
(True, None, {}), # no type
# test while index does not exist
(False, "test_doc", None), # no mapping
(False, None, {}), # no type
])
@es_test
def test_temporary_index_fails_with_invalid_args():
def test_temporary_index_fails_with_invalid_args(has_index, type_, mapping):
index = "test_index"
with (temporary_index(index) if has_index else nullcontext()):
if has_index:
assert manager.index_exists(index)
else:
assert not manager.index_exists(index)

def test_temporary_index_with_args(type_, mapping):
index_was_present = manager.index_exists(index)
with assert_raises(ValueError):
with temporary_index(index, type_, mapping):
pass
assert index_was_present == manager.index_exists(index), \
"unexpected index existence change"

no_mapping = ("test_doc", None)
no_type = (None, {})

with temporary_index(index):
# test while index exists
assert manager.index_exists(index)
yield test_temporary_index_with_args, *no_mapping
yield test_temporary_index_with_args, *no_type
# test while index does not exist
assert not manager.index_exists(index)
yield test_temporary_index_with_args, *no_mapping
yield test_temporary_index_with_args, *no_type
8 changes: 4 additions & 4 deletions corehq/motech/openmrs/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
import doctest
import json
import logging
import re
from contextlib import contextmanager

from django.test import SimpleTestCase

import pytz
from unittest.mock import patch
from nose.tools import assert_raises_regex
from nose.tools import assert_raises
from requests.exceptions import ConnectTimeout, ReadTimeout

from corehq.apps.groups.models import Group
Expand Down Expand Up @@ -193,16 +194,15 @@ def test_bad_data_type():
'property': 'data_proxima_consulta'
}
with get_importer(bad_column_mapping) as importer:
with assert_raises_regex(
ConfigurationError,
with assert_raises(ConfigurationError, msg=re.compile(re.escape(
'Errors importing from <OpenmrsImporter None admin@http://www.example.com/openmrs>:\n'
'Unable to deserialize value 1551564000000 '
'in column "data_proxima_consulta" '
'for case property "data_proxima_consulta". '
'OpenMRS data type is given as "omrs_datetime". '
'CommCare data type is given as "cc_date": '
"argument of type 'int' is not iterable"
):
))):
get_case_properties(patient, importer)


Expand Down

0 comments on commit 98e14a2

Please sign in to comment.