Skip to content

Commit

Permalink
Convert "yield" tests to parametrized tests
Browse files Browse the repository at this point in the history
pytest does not support yield tests anymore because they do not work in a system where test collection is separate from test running.
  • Loading branch information
millerdev committed Sep 7, 2024
1 parent 98e14a2 commit df6532c
Show file tree
Hide file tree
Showing 28 changed files with 934 additions and 984 deletions.
24 changes: 12 additions & 12 deletions corehq/apps/auditcare/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from contextlib import contextmanager
from unittest.mock import patch

import pytest
from django.conf import settings
from django.contrib.auth.models import User
from django.test import RequestFactory
Expand Down Expand Up @@ -123,18 +124,17 @@ def test_audit_view_should_not_save(self):
self.assertIsNone(event.id)


def test_get_domain():
def test(cfg):
request = make_request(cfg.path)
if "request_domain" in cfg:
request.domain = cfg.request_domain
eq(mod.get_domain(request), cfg.expect)

cfg = Config(expect="block")
yield test, cfg(path="/path", expect=None)
yield test, cfg(path="/a/block/path")
yield test, cfg(path="/path", request_domain="block")
yield test, cfg(path="/a/block/path", request_domain="xx")
@pytest.mark.parametrize("cfg", [
Config(expect=None, path="/path"),
Config(expect="block", path="/a/block/path"),
Config(expect="block", path="/path", request_domain="block"),
Config(expect="block", path="/a/block/path", request_domain="xx"),
])
def test_get_domain(cfg):
request = make_request(cfg.path)
if "request_domain" in cfg:
request.domain = cfg.request_domain
eq(mod.get_domain(request), cfg.expect)


def make_request(path="/path", session_key="abc", params=None, **headers):
Expand Down
209 changes: 93 additions & 116 deletions corehq/apps/case_search/tests/test_subcase_query_parser.py
Original file line number Diff line number Diff line change
@@ -1,125 +1,102 @@
import pytest
from eulxml.xpath import parse as parse_xpath
from testil import eq, assert_raises

from corehq.apps.case_search.exceptions import XPathFunctionException
from corehq.apps.case_search.xpath_functions.subcase_functions import _parse_normalize_subcase_query


def test_subcase_query_parsing():
def _check(query, expected):
node = parse_xpath(query)
result = _parse_normalize_subcase_query(node)
eq(result.as_tuple(), expected)
@pytest.mark.parametrize("query, expected", [
(
"subcase-exists('parent', @case_type='bob')",
("parent", "@case_type='bob'", ">", 0, False)
),
(
"subcase-exists('p', @case_type='bob' and prop='value')",
("p", "@case_type='bob' and prop='value'", ">", 0, False)
),
(
"subcase-count('p', prop=1) > 3",
("p", "prop=1", ">", 3, False)
),
(
"subcase-count('p', prop=1) >= 3",
("p", "prop=1", ">", 2, False)
),
(
"subcase-count('p', prop=1) < 3",
("p", "prop=1", ">", 2, True)
),
(
"subcase-count('p', prop=1) <= 3",
("p", "prop=1", ">", 3, True)
),
(
"subcase-count('p', prop=1) = 3",
("p", "prop=1", "=", 3, False)
),
(
"subcase-count('p', prop=1) = 0",
("p", "prop=1", ">", 0, True)
),
(
"subcase-count('p', prop=1) != 2",
("p", "prop=1", "=", 2, True)
),
(
"subcase-count('p') = 2",
("p", None, "=", 2, False)
),
(
"subcase-exists('p')",
("p", None, ">", 0, False)
),
])
def test_subcase_query_parsing(query, expected):
node = parse_xpath(query)
result = _parse_normalize_subcase_query(node)
eq(result.as_tuple(), expected)

yield from [
(
_check,
"subcase-exists('parent', @case_type='bob')",
("parent", "@case_type='bob'", ">", 0, False)
),
(
_check,
"subcase-exists('p', @case_type='bob' and prop='value')",
("p", "@case_type='bob' and prop='value'", ">", 0, False)
),
(
_check,
"subcase-count('p', prop=1) > 3",
("p", "prop=1", ">", 3, False)
),
(
_check,
"subcase-count('p', prop=1) >= 3",
("p", "prop=1", ">", 2, False)
),
(
_check,
"subcase-count('p', prop=1) < 3",
("p", "prop=1", ">", 2, True)
),
(
_check,
"subcase-count('p', prop=1) <= 3",
("p", "prop=1", ">", 3, True)
),
(
_check,
"subcase-count('p', prop=1) = 3",
("p", "prop=1", "=", 3, False)
),
(
_check,
"subcase-count('p', prop=1) = 0",
("p", "prop=1", ">", 0, True)
),
(
_check,
"subcase-count('p', prop=1) != 2",
("p", "prop=1", "=", 2, True)
),
(
_check,
"subcase-count('p') = 2",
("p", None, "=", 2, False)
),
(
_check,
"subcase-exists('p')",
("p", None, ">", 0, False)
),
]


def test_subcase_query_parsing_validations():
def _check(query, msg):
node = parse_xpath(query)
with assert_raises(XPathFunctionException, msg=msg):
_parse_normalize_subcase_query(node)

yield from [
(
_check,
"subcase-exists()",
"'subcase-exists' expects one or two arguments"
),
(
_check,
"subcase-count() > 1",
"'subcase-count' expects one or two arguments"
),
(
_check,
"subcase-count()",
"XPath incorrectly formatted. Expected 'subcase-exists'"
),
(
_check,
"subcase-count('p', name = 'bob') > -1",
"'subcase-count' must be compared to a positive integer"
),
(
_check,
"subcase-count('p', name = 'bob') + 1",
"Unsupported operator for use with 'subcase-count': +"
),
(
_check,
"subcase-count('p', name = 'bob') > 'bob'",
"'subcase-count' must be compared to a positive integer"
),
(
_check,
"subcase-count('p', name = 'bob') > date('2020-01-01')",
"'subcase-count' must be compared to a positive integer"
),
(
_check,
"subcase-count(parent) = 1",
"'subcase-count' error. Index identifier must be a string"
),
(
_check,
"subcase-exists(3)",
"'subcase-exists' error. Index identifier must be a string"
),
]
@pytest.mark.parametrize("query, msg", [
(
"subcase-exists()",
"'subcase-exists' expects one or two arguments"
),
(
"subcase-count() > 1",
"'subcase-count' expects one or two arguments"
),
(
"subcase-count()",
"XPath incorrectly formatted. Expected 'subcase-exists'"
),
(
"subcase-count('p', name = 'bob') > -1",
"'subcase-count' must be compared to a positive integer"
),
(
"subcase-count('p', name = 'bob') + 1",
"Unsupported operator for use with 'subcase-count': +"
),
(
"subcase-count('p', name = 'bob') > 'bob'",
"'subcase-count' must be compared to a positive integer"
),
(
"subcase-count('p', name = 'bob') > date('2020-01-01')",
"'subcase-count' must be compared to a positive integer"
),
(
"subcase-count(parent) = 1",
"'subcase-count' error. Index identifier must be a string"
),
(
"subcase-exists(3)",
"'subcase-exists' error. Index identifier must be a string"
),
])
def test_subcase_query_parsing_validations(query, msg):
node = parse_xpath(query)
with assert_raises(XPathFunctionException, msg=msg):
_parse_normalize_subcase_query(node)
109 changes: 50 additions & 59 deletions corehq/apps/case_search/tests/test_value_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.test import TestCase

import pytest
from eulxml.xpath import parse as parse_xpath
from freezegun import freeze_time
from testil import assert_raises, eq
Expand Down Expand Up @@ -63,62 +64,52 @@ def test_date_today(self):
eq(result, '2021-08-02')


def test_date_add():
def _do_test(expression, expected):
node = parse_xpath(expression)
result = date_add(node, SearchFilterContext("domain"))
eq(result, expected)

test_cases = [
("date-add('2020-02-29', 'years', 1)", "2021-02-28"),
("date-add('2020-02-29', 'years', 1.0)", "2021-02-28"),
("date-add('2022-01-01', 'months', 1)", "2022-02-01"),
("date-add('2022-01-01', 'months', '3')", "2022-04-01"),
("date-add('2020-04-30', 'months', -2)", "2020-02-29"),
("date-add('2020-03-31', 'weeks', 4)", "2020-04-28"),
("date-add('2020-02-01', 'weeks', 1.5)", "2020-02-11"),
("date-add('2022-01-01', 'days', -1)", "2021-12-31"),
("date-add('2022-01-01', 'days', 1.5)", "2022-01-02"),
("date-add('2022-01-01', 'days', '5')", "2022-01-06"),
("date-add(0, 'days', '5')", "1970-01-06"),
("date-add(365, 'years', '5')", "1976-01-01"),
("date-add(date('2021-01-01'), 'years', '5')", "2026-01-01"),
("date-add(date(5), 'months', '1')", "1970-02-06"),
]

for expression, expected in test_cases:
yield _do_test, expression, expected


def test_date_add_errors():
def _do_test(expression):
node = parse_xpath(expression)
with assert_raises(XPathFunctionException):
date_add(node, SearchFilterContext("domain"))

test_cases = [
# bad date
"date-add('2020-02-31', 'years', 1)",
"date-add(1.5, 'years', 1)",

# non-integer quantity
"date-add('2020-02-01', 'years', 1.5)",
"date-add('2020-02-01', 'months', 1.5)",

# non-numeric quantity
"date-add('2020-02-01', 'months', 'five')",

# bad interval names
"date-add('2020-02-01', 'year', 1)",
"date-add('2020-02-01', 'month', 1)",
"date-add('2020-02-01', 'week', 1)",
"date-add('2020-02-01', 'day', 1)",

# missing args
"date-add('2020-02-01', 'days')",
"date-add('2020-02-01')",
"date-add()",
]

for expression in test_cases:
yield _do_test, expression
@pytest.mark.parametrize("expression, expected", [
("date-add('2020-02-29', 'years', 1)", "2021-02-28"),
("date-add('2020-02-29', 'years', 1.0)", "2021-02-28"),
("date-add('2022-01-01', 'months', 1)", "2022-02-01"),
("date-add('2022-01-01', 'months', '3')", "2022-04-01"),
("date-add('2020-04-30', 'months', -2)", "2020-02-29"),
("date-add('2020-03-31', 'weeks', 4)", "2020-04-28"),
("date-add('2020-02-01', 'weeks', 1.5)", "2020-02-11"),
("date-add('2022-01-01', 'days', -1)", "2021-12-31"),
("date-add('2022-01-01', 'days', 1.5)", "2022-01-02"),
("date-add('2022-01-01', 'days', '5')", "2022-01-06"),
("date-add(0, 'days', '5')", "1970-01-06"),
("date-add(365, 'years', '5')", "1976-01-01"),
("date-add(date('2021-01-01'), 'years', '5')", "2026-01-01"),
("date-add(date(5), 'months', '1')", "1970-02-06"),
])
def test_date_add(expression, expected):
node = parse_xpath(expression)
result = date_add(node, SearchFilterContext("domain"))
eq(result, expected)


@pytest.mark.parametrize("expression", [
# bad date
"date-add('2020-02-31', 'years', 1)",
"date-add(1.5, 'years', 1)",
# non-integer quantity
"date-add('2020-02-01', 'years', 1.5)",
"date-add('2020-02-01', 'months', 1.5)",
# non-numeric quantity
"date-add('2020-02-01', 'months', 'five')",
# bad interval names
"date-add('2020-02-01', 'year', 1)",
"date-add('2020-02-01', 'month', 1)",
"date-add('2020-02-01', 'week', 1)",
"date-add('2020-02-01', 'day', 1)",
# missing args
"date-add('2020-02-01', 'days')",
"date-add('2020-02-01')",
"date-add()",
])
def test_date_add_errors(expression):
node = parse_xpath(expression)
with assert_raises(XPathFunctionException):
date_add(node, SearchFilterContext("domain"))
Loading

0 comments on commit df6532c

Please sign in to comment.