Skip to content

Commit

Permalink
Created inital test cases for SearchPanel (mozilla#3361)
Browse files Browse the repository at this point in the history
* Created test file

* Created initial test cases for SearchPanel component

* Added backend test cases, failing redux test case

* Linting fixes

* Seperated backend tests into different functions

* Removed print statements

* Formatted backend tests to use args and kwargs

---------

Co-authored-by: Harmit Goswami <[email protected]>
Co-authored-by: Harmit Goswami <[email protected]>
  • Loading branch information
3 people authored Sep 26, 2024
1 parent 7963779 commit 07117c7
Show file tree
Hide file tree
Showing 2 changed files with 423 additions and 0 deletions.
348 changes: 348 additions & 0 deletions pontoon/base/tests/managers/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,354 @@ def test_mgr_entity_filter_combined(admin, resource_a, locale_a, user_a):
)


@pytest.mark.django_db
def test_mgr_entity_option_match_case(admin, resource_a, locale_a, user_a):
entities = [
EntityFactory.create(
key="key %s" % i,
resource=resource_a,
string="TestEntity%s" % i,
)
for i in range(0, 2)
]

TranslationFactory.create(
locale=locale_a,
entity=entities[0],
rejected=True,
user=user_a,
)
TranslationFactory.create(
locale=locale_a,
entity=entities[1],
approved=True,
user=user_a,
)

args = [admin, resource_a.project, locale_a]
kwargs = {"search": "testentity", "author": user_a.email}

# Base case
assert list(
Entity.for_project_locale(
*args,
**kwargs,
)
) == [entities[i] for i in range(0, 2)]

# Test search_match_case
kwargs["search_match_case"] = True
assert (
list(
Entity.for_project_locale(
*args,
**kwargs,
)
)
== []
)


@pytest.mark.django_db
def test_mgr_entity_option_match_whole_word(admin, resource_a, locale_a, user_a):
entities = [
EntityFactory.create(
resource=resource_a,
string="TestEntity%s" % i,
)
for i in range(0, 2)
]

TranslationFactory.create(
locale=locale_a,
entity=entities[0],
rejected=True,
user=user_a,
)
TranslationFactory.create(
locale=locale_a,
entity=entities[1],
approved=True,
user=user_a,
)

args = [admin, resource_a.project, locale_a]
kwargs = {"search": "TestEntity", "author": user_a.email}

# Base case
assert list(
Entity.for_project_locale(
*args,
**kwargs,
)
) == [entities[i] for i in range(0, 2)]

kwargs["search_match_whole_word"] = True
# Test search_match_whole_word
assert (
list(
Entity.for_project_locale(
admin,
resource_a.project,
locale_a,
search="TestEntity",
search_match_whole_word=True,
author=user_a.email,
)
)
== []
)


@pytest.mark.django_db
def test_mgr_entity_option_identifiers(admin, resource_a, locale_a, user_a):
entities = [
EntityFactory.create(
key="key %s" % i,
resource=resource_a,
string="TestEntity%s" % i,
)
for i in range(0, 2)
]

TranslationFactory.create(
locale=locale_a,
entity=entities[0],
rejected=True,
user=user_a,
)
TranslationFactory.create(
locale=locale_a,
entity=entities[1],
approved=True,
user=user_a,
)

args = [admin, resource_a.project, locale_a]
kwargs = {"search": "key", "author": user_a.email}

# Base case
assert (
list(
Entity.for_project_locale(
*args,
**kwargs,
)
)
== []
)

kwargs["search_identifiers"] = True
# Test search_identifiers
assert list(
Entity.for_project_locale(
*args,
**kwargs,
)
) == [entities[i] for i in range(0, 2)]


@pytest.mark.django_db
def test_mgr_entity_option_rejected_translations(admin, resource_a, locale_a, user_a):
entities = [
EntityFactory.create(
resource=resource_a,
string="TestEntity%s" % i,
)
for i in range(0, 2)
]

TranslationFactory.create(
locale=locale_a,
entity=entities[0],
rejected=True,
string="TestString",
user=user_a,
)
TranslationFactory.create(
locale=locale_a,
entity=entities[1],
approved=True,
string="TestString",
user=user_a,
)

args = [admin, resource_a.project, locale_a]
kwargs = {"search": "TestString", "author": user_a.email}

# Base case
assert list(
Entity.for_project_locale(
*args,
**kwargs,
)
) == [entities[1]]

kwargs["search_rejected_translations"] = True
# Test search_rejected_translations
assert list(
Entity.for_project_locale(
*args,
**kwargs,
)
) == [entities[i] for i in range(0, 2)]


@pytest.mark.django_db
def test_mgr_entity_option_exclude_source_strings(admin, resource_a, locale_a, user_a):
entities = [
EntityFactory.create(
resource=resource_a,
string="TestEntity%s" % i,
)
for i in range(0, 2)
]

TranslationFactory.create(
locale=locale_a,
entity=entities[0],
rejected=True,
user=user_a,
)
TranslationFactory.create(
locale=locale_a,
entity=entities[1],
approved=True,
user=user_a,
)

args = [admin, resource_a.project, locale_a]
kwargs = {"search": "TestEntity", "author": user_a.email}

# Base case
assert list(
Entity.for_project_locale(
*args,
**kwargs,
)
) == [entities[i] for i in range(0, 2)]

kwargs["search_exclude_source_strings"] = True
# Test search_exclude_source_strings
assert (
list(
Entity.for_project_locale(
*args,
**kwargs,
)
)
== []
)


@pytest.mark.django_db
def test_mgr_entity_option_combined(admin, resource_a, locale_a, user_a):
"""
Test combinations of filters
"""

entities = [
EntityFactory.create(
key="key %s" % i,
resource=resource_a,
string="TestEntity%s" % i,
)
for i in range(0, 2)
]

TranslationFactory.create(
locale=locale_a,
entity=entities[0],
rejected=True,
string="Translation 0",
user=user_a,
)
TranslationFactory.create(
locale=locale_a,
entity=entities[1],
approved=True,
string="Translation 1",
user=user_a,
)

args = [admin, resource_a.project, locale_a]
kwargs = {}

# Base case
kwargs = {
"search": "",
"author": user_a.email,
}
assert list(
Entity.for_project_locale(
*args,
**kwargs,
)
) == [entities[i] for i in range(0, 2)]

# Test exclude_source_strings with identifiers
kwargs = {
"search": "key",
"search_exclude_source_strings": True,
"search_identifiers": True,
"author": user_a.email,
}
assert list(
Entity.for_project_locale(
*args,
**kwargs,
)
) == [entities[i] for i in range(0, 2)]

# Test identifiers with match_whole_word
kwargs = {
"search": "key",
"search_match_whole_word": True,
"search_identifiers": True,
"author": user_a.email,
}
assert list(
Entity.for_project_locale(
*args,
**kwargs,
)
) == [entities[i] for i in range(0, 2)]

# Test match_case with match_whole_word
kwargs = {
"search": "translation",
"search_match_case": True,
"search_match_whole_word": True,
"author": user_a.email,
}
assert (
list(
Entity.for_project_locale(
*args,
**kwargs,
)
)
== []
)

# Test all options at once
kwargs = {
"search": "Translation",
"search_match_case": True,
"search_match_whole_word": True,
"search_identifiers": True,
"search_rejected_translations": True,
"search_exclude_source_strings": True,
"author": user_a.email,
}
assert list(
Entity.for_project_locale(
*args,
**kwargs,
)
) == [entities[i] for i in range(0, 2)]


@pytest.mark.django_db
def test_mgr_entity_search_invalid_query(entity_test_search):
"""
Expand Down
Loading

0 comments on commit 07117c7

Please sign in to comment.