Skip to content

Commit c035a6c

Browse files
committed
Refactor unit test.
1 parent d06ca78 commit c035a6c

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

tests/queries_/models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,15 @@ class Writer(EmbeddedModel):
6565
name = models.CharField(max_length=10)
6666

6767

68+
class Location(EmbeddedModel):
69+
type = models.CharField(default="Point", max_length=50)
70+
coordinates = ArrayField(models.FloatField(), max_size=2)
71+
72+
6873
class Article(models.Model):
6974
headline = models.CharField(max_length=100)
7075
number = models.IntegerField()
7176
body = models.TextField()
72-
location = models.JSONField(null=True)
77+
location = EmbeddedModelField(Location, null=True)
7378
plot_embedding = ArrayField(models.FloatField(), size=3, null=True)
7479
writer = EmbeddedModelField(Writer, null=True)

tests/queries_/test_search.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,18 @@
2828
SearchWildcard,
2929
)
3030

31-
from .models import Article, Writer
31+
from .models import Article, Location, Writer
32+
33+
34+
def wait_until_index_ready(collection, index_name, timeout: float = 30, interval: float = 0.5):
35+
start = monotonic()
36+
while monotonic() - start < timeout:
37+
indexes = list(collection.list_search_indexes())
38+
for idx in indexes:
39+
if idx["name"] == index_name and idx["status"] == "READY":
40+
return True
41+
sleep(interval)
42+
raise TimeoutError(f"Index {index_name} not ready after {timeout} seconds")
3243

3344

3445
def _delayed_assertion(timeout: float = 120, interval: float = 0.5):
@@ -76,6 +87,7 @@ def create_search_index(cls, model, index_name, definition, type="search"):
7687
collection = cls._get_collection(model)
7788
idx = SearchIndexModel(definition=definition, name=index_name, type=type)
7889
collection.create_search_index(idx)
90+
wait_until_index_ready(collection, index_name)
7991

8092
def drop_index():
8193
collection.drop_search_index(index_name)
@@ -479,10 +491,13 @@ def setUpClass(cls):
479491

480492
def setUp(self):
481493
self.article = Article.objects.create(
482-
headline="any", number=1, body="", location={"type": "Point", "coordinates": [40, 5]}
494+
headline="any", number=1, body="", location=Location(type="Point", coordinates=[40, 5])
483495
)
484496
Article.objects.create(
485-
headline="any", number=2, body="", location={"type": "Point", "coordinates": [400, 50]}
497+
headline="any",
498+
number=2,
499+
body="",
500+
location=Location(type="Point", coordinates=[400, 50]),
486501
)
487502

488503
def test_search_geo_shape(self):
@@ -524,10 +539,13 @@ def setUpClass(cls):
524539

525540
def setUp(self):
526541
self.article = Article.objects.create(
527-
headline="geo", number=2, body="", location={"type": "Point", "coordinates": [40, 5]}
542+
headline="geo", number=2, body="", location=Location(type="Point", coordinates=[40, 5])
528543
)
529544
Article.objects.create(
530-
headline="geo2", number=3, body="", location={"type": "Point", "coordinates": [-40, -5]}
545+
headline="geo2",
546+
number=3,
547+
body="",
548+
location=Location(type="Point", coordinates=[-40, -5]),
531549
)
532550

533551
def test_search_geo_within(self):
@@ -598,7 +616,6 @@ def test_search_more_like_this(self):
598616
{"headline": self.article1.headline, "body": self.article1.body},
599617
{"headline": self.article2.headline, "body": self.article2.body},
600618
]
601-
like_docs = [{"body": "NASA launches new satellite to explore the galaxy"}]
602619
qs = Article.objects.annotate(score=SearchMoreLikeThis(documents=like_docs)).order_by(
603620
"score"
604621
)

0 commit comments

Comments
 (0)