Skip to content

Commit

Permalink
Merge pull request #35103 from dimagi/dm/pytest-prep
Browse files Browse the repository at this point in the history
Replace nose attrs and yield tests to a use `pytest.mark`
  • Loading branch information
millerdev authored Sep 9, 2024
2 parents 7d46150 + df6532c commit 99b2821
Show file tree
Hide file tree
Showing 41 changed files with 1,087 additions and 1,174 deletions.
41 changes: 21 additions & 20 deletions corehq/apps/app_manager/tests/test_app_strings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from collections import OrderedDict

import pytest
from django.test import TestCase

import commcare_translations
Expand Down Expand Up @@ -61,28 +62,28 @@ def test_non_empty_only():
eq(non_empty_things, {"all_of_the_things": [None, 0, "", [], {}]})


class AppManagerTranslationsTest(TestCase, SuiteMixin):
root = os.path.dirname(__file__)
@pytest.mark.parametrize("input, expected_output", [
('hello', '<value>hello</value>'),
('abc < def > abc', '<value>abc &lt; def &gt; abc</value>'),
("bee's knees", "<value>bee's knees</value>"),
('unfortunate <xml expression', '<value>unfortunate &lt;xml expression</value>'),
('क्लिक', '<value>क्लिक</value>'),
('&#39', '<value>&amp;#39</value>'),
('question1 is <output value="/data/question1" vellum:value="#form/question1"/> !',
'<value>question1 is &lt;output value="/data/question1" '
'vellum:value="#form/question1"/&gt; !</value>'),
('Here is a ref <output value="/data/no_media"/> with some "trailing" text & that\'s some bad < xml.',
'<value>Here is a ref &lt;output value="/data/no_media"/&gt; with some "trailing" text &amp; that\'s'
' some bad &lt; xml.</value>')
])
def test_escape_output_value(input, expected_output):
escaped_input = BulkAppTranslationFormUpdater.escape_output_value(input)
escaped_value = etree.tostring(escaped_input, encoding='utf-8').decode('utf-8')
assert escaped_value == expected_output

def test_escape_output_value(self):
test_cases = [
('hello', '<value>hello</value>'),
('abc < def > abc', '<value>abc &lt; def &gt; abc</value>'),
("bee's knees", "<value>bee's knees</value>"),
('unfortunate <xml expression', '<value>unfortunate &lt;xml expression</value>'),
('क्लिक', '<value>क्लिक</value>'),
('&#39', '<value>&amp;#39</value>'),
('question1 is <output value="/data/question1" vellum:value="#form/question1"/> !',
'<value>question1 is &lt;output value="/data/question1" '
'vellum:value="#form/question1"/&gt; !</value>'),
('Here is a ref <output value="/data/no_media"/> with some "trailing" text & that\'s some bad < xml.',
'<value>Here is a ref &lt;output value="/data/no_media"/&gt; with some "trailing" text &amp; that\'s'
' some bad &lt; xml.</value>')

]
for input, expected_output in test_cases:
escaped_input = BulkAppTranslationFormUpdater.escape_output_value(input)
self.assertEqual(expected_output, etree.tostring(escaped_input, encoding='utf-8').decode('utf-8'))
class AppManagerTranslationsTest(TestCase, SuiteMixin):
root = os.path.dirname(__file__)

def test_language_names(self):
factory = AppFactory(build_version='2.40.0')
Expand Down
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)
Loading

0 comments on commit 99b2821

Please sign in to comment.