-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix merge conflict with latest from dev/7.6.x, re #10804
- Loading branch information
Showing
3 changed files
with
69 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,21 +16,25 @@ | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
""" | ||
|
||
import json | ||
import time | ||
from django.test.utils import captured_stdout | ||
import uuid | ||
|
||
from tests.base_test import ArchesTestCase | ||
from tests.utils.search_test_utils import sync_es | ||
from arches.app.search.search_engine_factory import SearchEngineFactory | ||
from arches.app.models import models | ||
from arches.app.models.resource import Resource | ||
from arches.app.models.tile import Tile | ||
from arches.app.search.elasticsearch_dsl_builder import ( | ||
Bool, | ||
Match, | ||
Query, | ||
Nested, | ||
Terms, | ||
GeoShape, | ||
Range, | ||
) | ||
from arches.app.search.search_engine_factory import SearchEngineFactory | ||
from arches.app.utils.i18n import LanguageSynchronizer | ||
from arches.app.views.search import search_terms | ||
from django.http import HttpRequest | ||
from django.contrib.auth.models import User | ||
from django.test.utils import captured_stdout | ||
from tests.base_test import ArchesTestCase | ||
from tests.utils.search_test_utils import sync_es | ||
|
||
# these tests can be run from the command line via | ||
# python manage.py test tests.search.search_tests --settings="tests.test_settings" | ||
|
@@ -39,12 +43,27 @@ | |
class SearchTests(ArchesTestCase): | ||
@classmethod | ||
def tearDownClass(cls): | ||
models.GraphModel.objects.filter( | ||
pk="d291a445-fa5f-11e6-afa8-14109fd34195" | ||
).delete() | ||
User.objects.filter(username="Tester").delete() | ||
Resource.objects.filter(pk="745f5e4a-d645-4c50-bafc-c677ea95f060").delete() | ||
se = SearchEngineFactory().create() | ||
with captured_stdout(): | ||
se.delete_index(index="test") | ||
se.delete_index(index="bulk") | ||
super().tearDownClass() | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
LanguageSynchronizer.synchronize_settings_with_db() | ||
User.objects.create_user( | ||
username="Tester", email="[email protected]", password="test12345!" | ||
) | ||
cls.loadOntology() | ||
cls.ensure_resource_test_model_loaded() | ||
|
||
def test_delete_by_query(self): | ||
""" | ||
Test deleting documents by query in Elasticsearch | ||
|
@@ -64,7 +83,6 @@ def test_delete_by_query(self): | |
query = Query(se, start=0, limit=100) | ||
match = Match(field="type", query="altLabel") | ||
query.add_query(match) | ||
|
||
query.delete(index="test", refresh=True) | ||
|
||
self.assertEqual(se.count(index="test"), 10) | ||
|
@@ -93,7 +111,7 @@ def test_bulk_add_documents(self): | |
) | ||
) | ||
|
||
ret = se.bulk_index(documents, refresh=True) | ||
se.bulk_index(documents, refresh=True) | ||
count_after = se.count(index="test") | ||
self.assertEqual(count_after - count_before, 10) | ||
|
||
|
@@ -109,3 +127,42 @@ def test_bulk_indexer(self): | |
|
||
count_after = se.count(index="bulk") | ||
self.assertEqual(count_after, 1001) | ||
|
||
def test_search_terms(self): | ||
""" | ||
Test finding a resource by a term | ||
""" | ||
|
||
nodeid = "c9b37b7c-17b3-11eb-a708-acde48001122" | ||
tileid = "bebffbea-daf6-414e-80c2-530ec88d2705" | ||
resourceinstanceid = "745f5e4a-d645-4c50-bafc-c677ea95f060" | ||
resource = Resource(uuid.UUID(resourceinstanceid)) | ||
user = User.objects.get(username="Tester") | ||
resource.graph_id = "c9b37a14-17b3-11eb-a708-acde48001122" | ||
resource.save(user=user, transaction_id=uuid.uuid4()) | ||
tile_data = {} | ||
tile_data[nodeid] = { | ||
"en": {"value": "Etiwanda Avenue Street Trees", "direction": "ltr"} | ||
} | ||
new_tile = Tile( | ||
tileid=uuid.UUID(tileid), | ||
resourceinstance_id=resourceinstanceid, | ||
data=tile_data, | ||
nodegroup_id=nodeid, | ||
) | ||
new_tile.save() | ||
time.sleep(1) # wait a moment for ES to finish indexing | ||
request = HttpRequest() | ||
request.method = "GET" | ||
request.GET.__setitem__("lang", "en") | ||
request.GET.__setitem__("q", "Etiwanda") | ||
request.LANGUAGE_CODE = "en" | ||
request.user = user | ||
response = search_terms(request) | ||
result = {} | ||
try: | ||
result = json.loads(response.content) | ||
except json.decoder.JSONDecodeError: | ||
print("Failed to parse search result") | ||
self.assertTrue("terms" in result and len(result["terms"]) == 1) |