Skip to content

Commit faab09d

Browse files
authored
Update test_search_engine.py
1 parent 2919e71 commit faab09d

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

tests/test_search_engine.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from search_engine.search_engine import preprocess, search, get_inverted_index
2+
from search_engine.search_engine import search, get_inverted_index
33

44
docs = [
55
{'id': 1, 'text': 'hello world'},
@@ -30,7 +30,8 @@ def test_search_single_word_found(self, docs, word, res):
3030
"""
3131
Test the search function to ensure it finds a single word correctly.
3232
"""
33-
assert search(docs, word) == res, f"Expected {res}, but got {search(docs, word)}"
33+
result = search(docs, word)
34+
assert result == res, f"Expected {res}, but got {result}"
3435

3536
@pytest.mark.parametrize(
3637
'docs, word, res',
@@ -44,7 +45,9 @@ def test_search_absent_word_not_found(self, docs, word, res):
4445
"""
4546
Test that the search function returns an empty list for absent words.
4647
"""
47-
assert search(docs, word) == res, f"Expected [], but got {search(docs, word)}"
48+
assert search(docs, word) == res, f"Expected [], but got {
49+
search(docs, word)
50+
}"
4851

4952
@pytest.mark.parametrize(
5053
'docs, word, res',
@@ -56,32 +59,42 @@ def test_search_absent_word_not_found(self, docs, word, res):
5659
)
5760
def test_search_part_of_word_not_found(self, docs, word, res):
5861
"""
59-
Test that the search function does not return results for partial words.
62+
Test that the search function does not return
63+
results for partial words.
6064
"""
61-
assert search(docs, word) == res, f"Expected [], but got {search(docs, word)}"
65+
assert search(docs, word) == res, f"Expected [], but got {
66+
search(docs, word)
67+
}"
6268

6369
@pytest.mark.parametrize('word', ['', ' ', '\t', '\n'])
6470
def test_search_empty_word(self, word):
6571
"""
66-
Test that the search function returns an empty list for empty or whitespace-only queries.
72+
Test that the search function returns an empty list for empty
73+
or whitespace-only queries.
6774
"""
68-
assert search(docs, word) == [], f'Expected [], but got {search(docs, word)}'
75+
assert search(docs, word) == [], f'Expected [], but got {
76+
search(docs, word)
77+
}'
6978

7079

7180
def test_search_word_with_punctuation(self):
7281
"""
7382
Test that the search word can be with puntcuation
7483
"""
75-
doc1 = {'id': 'doc1', 'text': "I can't shoot straight unless I've had a pint!"}
84+
doc1 = {
85+
'id': 'doc1',
86+
'text': "I can't shoot straight unless I've had a pint!"
87+
}
7688
docs = [doc1]
7789

7890
assert search(docs, 'pint') == ['doc1']
7991
assert search(docs, 'pint!') == ['doc1']
8092

8193
def test_search_ranging(self):
8294
"""
83-
Test that the search function returns results sorted by the frequency
84-
of the search word's occurrence in the document text.
95+
Test that the search function returns results
96+
sorted by the frequency of the search word's occurrence
97+
in the document text.
8598
"""
8699
doc1 = "I can't shoot straight unless I've had a pint!"
87100
doc2 = "Don't shoot shoot shoot that thing at me."
@@ -98,13 +111,16 @@ def test_search_ranging(self):
98111

99112
def test_search_multiple_words(self):
100113
"""
101-
Test that the search function returns documents containing all the words
102-
in the search query, and sorts them based on the frequency of the words' occurrence.
114+
Test that the search function returns documents
115+
containing all the words in the search query, and
116+
sorts them based on the frequency of the words' occurrence.
103117
104118
The test ensures the following:
105119
- The search can handle multiple words (i.e., 'shoot at me').
106-
- Documents are returned in the order of their relevance (based on word frequency).
107-
- Words in the search query are treated independently (i.e., not as a single phrase).
120+
- Documents are returned in the order of their relevance
121+
(based on word frequency).
122+
- Words in the search query are treated independently
123+
(i.e., not as a single phrase).
108124
"""
109125
doc1 = "I can't shoot straight unless I've had a pint!"
110126
doc2 = "Don't shoot shoot shoot that thing at me."
@@ -131,4 +147,4 @@ def test_get_inverted_index(self):
131147
'too': ['doc2']
132148
}
133149

134-
assert get_inverted_index(docs) == index
150+
assert get_inverted_index(docs) == index

0 commit comments

Comments
 (0)