|
| 1 | +# This file is part of wger Workout Manager. |
| 2 | +# |
| 3 | +# wger Workout Manager is free software: you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU Affero General Public License as published by |
| 5 | +# the Free Software Foundation, either version 3 of the License, or |
| 6 | +# (at your option) any later version. |
| 7 | +# |
| 8 | +# wger Workout Manager is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +# GNU General Public License for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU Affero General Public License |
| 14 | +# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +# Third Party |
| 17 | +from rest_framework import status |
| 18 | + |
| 19 | +# wger |
| 20 | +from wger.core.tests.api_base_test import ApiBaseTestCase |
| 21 | +from wger.core.tests.base_testcase import BaseTestCase |
| 22 | + |
| 23 | + |
| 24 | +class ExerciseInfoFilterApiTestCase(BaseTestCase, ApiBaseTestCase): |
| 25 | + url = '/api/v2/exerciseinfo/' |
| 26 | + |
| 27 | + def setUp(self): |
| 28 | + super().setUp() |
| 29 | + self.init_media_root() |
| 30 | + |
| 31 | + def _results(self, response): |
| 32 | + if isinstance(response.data, dict) and 'results' in response.data: |
| 33 | + return response.data['results'] |
| 34 | + return response.data |
| 35 | + |
| 36 | + def _has_translation_name(self, item, expected_name: str) -> bool: |
| 37 | + for t in item.get('translations', []): |
| 38 | + if t.get('name') == expected_name: |
| 39 | + return True |
| 40 | + return False |
| 41 | + |
| 42 | + def test_basic_search_logged_out(self): |
| 43 | + """ |
| 44 | + Logged-out users can search via name__search and language__code |
| 45 | + """ |
| 46 | + response = self.client.get(self.url + '?name__search=exercise&language__code=en') |
| 47 | + results = self._results(response) |
| 48 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 49 | + self.assertEqual(len(results), 4) |
| 50 | + ids = {item['id'] for item in results} |
| 51 | + self.assertIn(1, ids) |
| 52 | + item1 = next(item for item in results if item['id'] == 1) |
| 53 | + self.assertTrue(self._has_translation_name(item1, 'An exercise')) |
| 54 | + |
| 55 | + def test_basic_search_logged_in(self): |
| 56 | + """ |
| 57 | + Logged-in users get the same results |
| 58 | + """ |
| 59 | + self.authenticate('test') |
| 60 | + response = self.client.get(self.url + '?name__search=exercise&language__code=en') |
| 61 | + results = self._results(response) |
| 62 | + |
| 63 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 64 | + self.assertEqual(len(results), 4) |
| 65 | + ids = {item['id'] for item in results} |
| 66 | + self.assertIn(1, ids) |
| 67 | + item1 = next(item for item in results if item['id'] == 1) |
| 68 | + self.assertTrue(self._has_translation_name(item1, 'An exercise')) |
| 69 | + |
| 70 | + def test_search_language_code_en_no_results(self): |
| 71 | + """ |
| 72 | + A DE-only exercise name should not be found when searching in English |
| 73 | + """ |
| 74 | + response = self.client.get(self.url + '?name__search=Weitere&language__code=en') |
| 75 | + results = self._results(response) |
| 76 | + |
| 77 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 78 | + self.assertEqual(len(results), 0) |
| 79 | + |
| 80 | + def test_search_language_code_de(self): |
| 81 | + """ |
| 82 | + A DE-only exercise should be found when searching in German |
| 83 | + """ |
| 84 | + response = self.client.get(self.url + '?name__search=Weitere&language__code=de') |
| 85 | + results = self._results(response) |
| 86 | + |
| 87 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 88 | + self.assertEqual(len(results), 1) |
| 89 | + self.assertEqual(results[0]['id'], 4) |
| 90 | + |
| 91 | + def test_search_several_language_codes(self): |
| 92 | + """ |
| 93 | + Passing different language codes works correctly |
| 94 | + """ |
| 95 | + response = self.client.get(self.url + '?name__search=demo&language__code=en,de') |
| 96 | + results = self._results(response) |
| 97 | + |
| 98 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 99 | + self.assertEqual(len(results), 4) |
| 100 | + |
| 101 | + def test_search_unknown_language_codes(self): |
| 102 | + """ |
| 103 | + Unknown language codes are ignored |
| 104 | + """ |
| 105 | + response = self.client.get(self.url + '?name__search=demo&language__code=en,de,zz') |
| 106 | + results = self._results(response) |
| 107 | + |
| 108 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 109 | + self.assertEqual(len(results), 4) |
| 110 | + |
| 111 | + def test_search_all_languages(self): |
| 112 | + """ |
| 113 | + Disable all language filters when language__code is omitted |
| 114 | + """ |
| 115 | + response = self.client.get(self.url + '?name__search=demo') |
| 116 | + results = self._results(response) |
| 117 | + |
| 118 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 119 | + self.assertEqual(len(results), 4) |
| 120 | + |
| 121 | + def test_search_matches_alias(self): |
| 122 | + """ |
| 123 | + Alias terms should also match |
| 124 | + """ |
| 125 | + response = self.client.get(self.url + '?name__search=different&language__code=en') |
| 126 | + results = self._results(response) |
| 127 | + |
| 128 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 129 | + self.assertEqual(len(results), 1) |
0 commit comments