Skip to content

Commit e213f6f

Browse files
committed
Fix content-type and add tests
1 parent 1da6c8b commit e213f6f

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

meilisearch/_httprequests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ def send_request(
2626
self.headers['Content-Type'] = content_type
2727
try:
2828
request_path = self.config.url + '/' + path
29-
if not content_type or content_type == 'application/json':
29+
if isinstance(body, bytes):
3030
request = http_method(
3131
request_path,
3232
timeout=self.config.timeout,
3333
headers=self.headers,
34-
data=json.dumps(body) if body else "null"
34+
data=body
3535
)
3636
else:
3737
request = http_method(
3838
request_path,
3939
timeout=self.config.timeout,
4040
headers=self.headers,
41-
data=body
41+
data=json.dumps(body) if body else "null"
4242
)
4343
return self.__validate(request)
4444

meilisearch/index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ def __settings_url_for(self, sub_route: str) -> str:
12411241
def _build_url(
12421242
self,
12431243
primary_key: Optional[str] = None,
1244-
):
1244+
) -> str:
12451245
if primary_key is None:
12461246
return f'{self.config.paths.index}/{self.uid}/{self.config.paths.document}'
12471247
primary_key = urllib.parse.urlencode({'primaryKey': primary_key})

meilisearch/tests/conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ def small_movies():
3838
with open('./datasets/small_movies.json', 'r', encoding="utf8") as movie_file:
3939
yield json.loads(movie_file.read())
4040

41+
@fixture(scope='session')
42+
def small_movies_json_file():
43+
"""
44+
Runs once per session. Provides the content of small_movies.json from read.
45+
"""
46+
with open('./datasets/small_movies.json', 'r') as movie_json_file:
47+
return movie_json_file.read().encode('utf-8')
48+
49+
@fixture(scope='session')
50+
def songs_csv():
51+
"""
52+
Runs once per session. Provides the content of songs.csv from read..
53+
"""
54+
with open('./datasets/songs.csv', 'r') as song_csv_file:
55+
return song_csv_file.read().encode('utf-8')
56+
57+
@fixture(scope='session')
58+
def songs_ndjson():
59+
"""
60+
Runs once per session. Provides the content of songs.ndjson from read..
61+
"""
62+
with open('./datasets/songs.ndjson', 'r') as song_ndjson_file:
63+
return song_ndjson_file.read().encode('utf-8')
64+
4165
@fixture(scope='function')
4266
def empty_index(client):
4367
def index_maker(index_name=common.INDEX_UID):

meilisearch/tests/index/test_index_document_meilisearch.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,36 @@ def test_delete_all_documents(index_with_documents):
144144
response = index.get_documents()
145145
assert isinstance(response, list)
146146
assert response == []
147+
148+
def test_add_documents_csv(empty_index, songs_csv):
149+
"""Tests adding new documents to a clean index."""
150+
index = empty_index()
151+
response = index.add_documents_csv(songs_csv)
152+
assert isinstance(response, dict)
153+
assert 'updateId' in response
154+
update = index.wait_for_pending_update(response['updateId'])
155+
assert update['status'] == 'processed'
156+
assert update['type']['number'] != 0
157+
assert index.get_primary_key() == 'id'
158+
159+
def test_add_documents_json(empty_index, small_movies_json_file):
160+
"""Tests adding new documents to a clean index."""
161+
index = empty_index()
162+
response = index.add_documents_json(small_movies_json_file)
163+
assert isinstance(response, dict)
164+
assert 'updateId' in response
165+
update = index.wait_for_pending_update(response['updateId'])
166+
assert update['status'] == 'processed'
167+
assert update['type']['number'] != 0
168+
assert index.get_primary_key() == 'id'
169+
170+
def test_add_documents_ndjson(empty_index, songs_ndjson):
171+
"""Tests adding new documents to a clean index."""
172+
index = empty_index()
173+
response = index.add_documents_ndjson(songs_ndjson)
174+
assert isinstance(response, dict)
175+
assert 'updateId' in response
176+
update = index.wait_for_pending_update(response['updateId'])
177+
assert update['status'] == 'processed'
178+
assert update['type']['number'] != 0
179+
assert index.get_primary_key() == 'id'

0 commit comments

Comments
 (0)