Skip to content

Commit 577e09b

Browse files
committed
centralized tests
1 parent 03c9068 commit 577e09b

File tree

4 files changed

+46
-118
lines changed

4 files changed

+46
-118
lines changed

modelsearch/tests/test_backends.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,49 @@ def test_autocomplete_not_affected_by_stemming(self):
436436
],
437437
)
438438

439+
def test_related_field_search_returns_parent_model(self):
440+
"""
441+
Ensure that searching on a related field (authors__name)
442+
returns the parent model instance, and fails if the related field
443+
is not indexed.
444+
"""
445+
446+
# Create author
447+
author = models.Author.objects.create(name="Guido van Rossum")
448+
449+
# Create book linked to that author
450+
book = models.Book.objects.create(
451+
title="Python Internals",
452+
publication_date="1999-05-01",
453+
number_of_pages=333,
454+
)
455+
book.authors.add(author)
456+
457+
# Rebuild index with related fields
458+
self.backend.add(book)
459+
460+
# ---- Test 1: Search by related field should return book ----
461+
results = self.backend.search("Guido", models.Book, fields=["authors__name"])
462+
results_list = list(results)
463+
self.assertIn(book, results_list)
464+
465+
# ---- Test 2: Searching unrelated string should NOT return book ----
466+
results = self.backend.search(
467+
"Nonexistent Author", models.Book, fields=["authors__name"]
468+
)
469+
results_list = list(results)
470+
self.assertNotIn(book, results_list)
471+
472+
# ---- Test 3: Searching by book title still works ----
473+
results = self.backend.search("Python", models.Book, fields=["title"])
474+
results_list = list(results)
475+
self.assertIn(book, results_list)
476+
477+
# ---- Test 4: CRITICAL - Proof that fields parameter is RESPECTED ----
478+
results = self.backend.search("Python", models.Book, fields=["authors__name"])
479+
results_list = list(results)
480+
self.assertNotIn(book, results_list)
481+
439482
def test_autocomplete_hyphenated_term(self):
440483
book = models.Book.objects.create(
441484
title="Poseidon-1234ABC",

modelsearch/tests/test_mysql_backend.py

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -212,47 +212,7 @@ def test_search_and_match_none(self):
212212

213213
@unittest.skip("mysql backend dosen't support this yet")
214214
def test_related_field_search_returns_parent_model(self):
215-
"""
216-
Ensure that searching on a related field (authors__name)
217-
returns the parent model instance, and fails if the related field
218-
is not indexed.
219-
"""
220-
221-
# Create author
222-
author = models.Author.objects.create(name="Guido van Rossum")
223-
224-
# Create book linked to that author
225-
book = models.Book.objects.create(
226-
title="Python Internals",
227-
publication_date="1999-05-01",
228-
number_of_pages=333,
229-
)
230-
book.authors.add(author)
231-
232-
# Rebuild index with related fields
233-
self.backend.add(book)
234-
235-
# ---- Test 1: Search by related field should return book ----
236-
results = self.backend.search("Guido", models.Book, fields=["authors__name"])
237-
results_list = list(results)
238-
self.assertIn(book, results_list)
239-
240-
# ---- Test 2: Searching unrelated string should NOT return book ----
241-
results = self.backend.search(
242-
"Nonexistent Author", models.Book, fields=["authors__name"]
243-
)
244-
results_list = list(results)
245-
self.assertNotIn(book, results_list)
246-
247-
# ---- Test 3: Searching by book title still works ----
248-
results = self.backend.search("Python", models.Book, fields=["title"])
249-
results_list = list(results)
250-
self.assertIn(book, results_list)
251-
252-
# ---- Test 4: CRITICAL - Proof that fields parameter is RESPECTED ----
253-
results = self.backend.search("Python", models.Book, fields=["authors__name"])
254-
results_list = list(results)
255-
self.assertNotIn(book, results_list)
215+
return super().test_related_field_search_returns_parent_model()
256216

257217
def test_reset_indexes(self):
258218
"""

modelsearch/tests/test_postgres_backend.py

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -203,47 +203,7 @@ def test_reset_indexes(self):
203203
self.assertEqual(results.count(), 0)
204204

205205
def test_related_field_search_returns_parent_model(self):
206-
"""
207-
Ensure that searching on a related field (authors__name)
208-
returns the parent model instance, and fails if the related field
209-
is not indexed.
210-
"""
211-
212-
# Create author
213-
author = models.Author.objects.create(name="Guido van Rossum")
214-
215-
# Create book linked to that author
216-
book = models.Book.objects.create(
217-
title="Python Internals",
218-
publication_date="1999-05-01",
219-
number_of_pages=333,
220-
)
221-
book.authors.add(author)
222-
223-
# Rebuild index with related fields
224-
self.backend.add(book)
225-
226-
# ---- Test 1: Search by related field should return book ----
227-
results = self.backend.search("Guido", models.Book, fields=["authors__name"])
228-
results_list = list(results)
229-
self.assertIn(book, results_list)
230-
231-
# ---- Test 2: Searching unrelated string should NOT return book ----
232-
results = self.backend.search(
233-
"Nonexistent Author", models.Book, fields=["authors__name"]
234-
)
235-
results_list = list(results)
236-
self.assertNotIn(book, results_list)
237-
238-
# ---- Test 3: Searching by book title still works ----
239-
results = self.backend.search("Python", models.Book, fields=["title"])
240-
results_list = list(results)
241-
self.assertIn(book, results_list)
242-
243-
# ---- Test 4: CRITICAL - Proof that fields parameter is RESPECTED ----
244-
results = self.backend.search("Python", models.Book, fields=["authors__name"])
245-
results_list = list(results)
246-
self.assertNotIn(book, results_list)
206+
return super().test_related_field_search_returns_parent_model()
247207

248208
def test_get_search_field_for_related_fields(self):
249209
"""

modelsearch/tests/test_sqlite_backend.py

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -85,42 +85,7 @@ def test_search_and_match_all(self):
8585

8686
@unittest.skip("Sqlite isn't working for this test case.")
8787
def test_related_field_search_returns_parent_model(self):
88-
"""
89-
Ensure that searching on a related field (authors__name)
90-
returns the parent model instance, and fails if the related field
91-
is not indexed.
92-
"""
93-
94-
# Create author
95-
author = models.Author.objects.create(name="Guido van Rossum")
96-
97-
# Create book linked to that author
98-
book = models.Book.objects.create(
99-
title="Python Internals",
100-
publication_date="1999-05-01",
101-
number_of_pages=333,
102-
)
103-
book.authors.add(author)
104-
105-
# Rebuild index with related fields
106-
self.backend.add(book)
107-
108-
# ---- Test 1: Search by related field should return book ----
109-
results = self.backend.search("Guido", models.Book, fields=["authors__name"])
110-
results_list = list(results)
111-
self.assertIn(book, results_list)
112-
113-
# ---- Test 2: Searching unrelated string should NOT return book ----
114-
results = self.backend.search(
115-
"Nonexistent Author", models.Book, fields=["authors__name"]
116-
)
117-
results_list = list(results)
118-
self.assertNotIn(book, results_list)
119-
120-
# ---- Test 3: Searching by book title still works ----
121-
results = self.backend.search("Python", models.Book, fields=["title"])
122-
results_list = list(results)
123-
self.assertIn(book, results_list)
88+
return super().test_related_field_search_returns_parent_model()
12489

12590
def test_reset_indexes(self):
12691
"""

0 commit comments

Comments
 (0)