Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: crud rest #2525

Merged
merged 1 commit into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/integration/crud/flow.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
jtype: Flow
with:
return_results: True
restful: $RESTFUL
executors:
- name: indexer
uses:
Expand Down
96 changes: 80 additions & 16 deletions tests/integration/crud/test_crud.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,110 @@
import numpy as np
import os
import pytest
import requests

from jina import Flow
from jina import Flow, Document
from tests import random_docs

# noinspection PyUnresolvedReferences
from tests.integration.crud import CrudIndexer

PARAMS = {'top_k': 10}

def test_crud(tmpdir):

def rest_post(f, endpoint, documents):
data = [d.dict() for d in documents]
if endpoint == 'delete':
method = 'delete'
elif endpoint == 'update':
method = 'put'
else:
method = 'post'
response = getattr(requests, method)(
f'http://0.0.0.0:{f.port_expose}/{endpoint}',
json={'data': data, 'parameters': PARAMS},
)
if response.status_code != 200:
raise Exception(f'exception in status code {response.status_code}')

return response.json()


@pytest.mark.parametrize('rest', [False, True])
def test_crud(tmpdir, rest):
os.environ['RESTFUL'] = 'true' if rest else 'false'
os.environ['WORKSPACE'] = str(tmpdir)

with Flow.load_config('flow.yml') as f:
original_docs = list(random_docs(10, chunks_per_doc=0))
f.post(
on='/index',
inputs=original_docs,
)
if rest:
rest_post(f, 'index', original_docs)
else:
f.post(
on='/index',
inputs=original_docs,
)

with Flow.load_config('flow.yml') as f:
results = f.post(on='/search', inputs=random_docs(1), parameters={'top_k': 10})
assert len(results[0].docs[0].matches) == 10
inputs = list(random_docs(1))
if rest:
results = rest_post(f, 'search', inputs)
matches = results['data']['docs'][0]['matches']
else:
results = f.post(on='/search', inputs=inputs, parameters=PARAMS)
matches = results[0].docs[0].matches

assert len(matches) == 10

with Flow.load_config('flow.yml') as f:
f.post(on='/delete', inputs=random_docs(5, chunks_per_doc=0))
inputs = list(random_docs(5, chunks_per_doc=0))

if rest:
rest_post(f, 'delete', inputs)

else:
f.post(on='/delete', inputs=inputs)

with Flow.load_config('flow.yml') as f:
results = f.post(on='/search', inputs=random_docs(1), parameters={'top_k': 10})
assert len(results[0].docs[0].matches) == 5
inputs = list(random_docs(1))

if rest:
results = rest_post(f, 'search', inputs)
matches = results['data']['docs'][0]['matches']

else:
results = f.post(on='/search', inputs=inputs, parameters=PARAMS)
matches = results[0].docs[0].matches

assert len(matches) == 5

updated_docs = list(
random_docs(5, chunks_per_doc=5, start_id=5, text=b'hello again')
)

with Flow.load_config('flow.yml') as f:
f.post(on='/update', inputs=updated_docs)
if rest:
rest_post(f, 'update', updated_docs)
else:
f.post(on='/update', inputs=updated_docs)

with Flow.load_config('flow.yml') as f:
results = f.post(on='/search', inputs=random_docs(1), parameters={'top_k': 10})
assert len(results[0].docs[0].matches) == 5
matches = results[0].docs[0].matches
matches = sorted(matches, key=lambda match: match.id)
inputs = list(random_docs(1))
if rest:
results = rest_post(f, 'search', inputs)
matches = sorted(
results['data']['docs'][0]['matches'], key=lambda match: match['id']
)
else:
results = f.post(on='/search', inputs=inputs, parameters=PARAMS)
matches = sorted(results[0].docs[0].matches, key=lambda match: match.id)

assert len(matches) == 5

for match, updated_doc in zip(matches, updated_docs):
if isinstance(match, dict):
match = Document(match)

assert updated_doc.id == match.id
assert updated_doc.text == match.text
np.testing.assert_array_equal(updated_doc.embedding, match.embedding)
Expand Down