-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from i-dot-ai/feature/REDBOX-73-unit-test-cor…
…e-api Feature/redbox 73 unit test core api
- Loading branch information
Showing
9 changed files
with
226 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import os | ||
from typing import TypeVar, Generator | ||
|
||
import pytest | ||
from elasticsearch import Elasticsearch | ||
|
||
from fastapi.testclient import TestClient | ||
from redbox.models import File | ||
from core_api.src.app import app as application, env | ||
from redbox.storage import ElasticsearchStorageHandler | ||
|
||
T = TypeVar("T") | ||
|
||
YieldFixture = Generator[T, None, None] | ||
|
||
|
||
@pytest.fixture | ||
def s3_client(): | ||
yield env.s3_client() | ||
|
||
|
||
@pytest.fixture | ||
def es_client() -> YieldFixture[Elasticsearch]: | ||
yield env.elasticsearch_client() | ||
|
||
|
||
@pytest.fixture | ||
def app_client(): | ||
yield TestClient(application) | ||
|
||
|
||
@pytest.fixture | ||
def elasticsearch_storage_handler(es_client): | ||
yield ElasticsearchStorageHandler(es_client=es_client, root_index="redbox-data") | ||
|
||
|
||
@pytest.fixture | ||
def file(s3_client, file_pdf_path, bucket) -> YieldFixture[File]: | ||
""" | ||
TODO: this is a cut and paste of core_api:create_upload_file | ||
When we come to test core_api we should think about | ||
the relationship between core_api and the ingest app | ||
""" | ||
file_name = os.path.basename(file_pdf_path) | ||
file_type = file_name.split(".")[-1] | ||
|
||
with open(file_pdf_path, "rb") as f: | ||
s3_client.put_object( | ||
Bucket=bucket, | ||
Body=f.read(), | ||
Key=file_name, | ||
Tagging=f"file_type={file_type}", | ||
) | ||
|
||
authenticated_s3_url = s3_client.generate_presigned_url( | ||
"get_object", | ||
Params={"Bucket": env.bucket_name, "Key": file_name}, | ||
ExpiresIn=3600, | ||
) | ||
|
||
# Strip off the query string (we don't need the keys) | ||
simple_s3_url = authenticated_s3_url.split("?")[0] | ||
file_record = File( | ||
name=file_name, | ||
path=simple_s3_url, | ||
type=file_type, | ||
creator_user_uuid="dev", | ||
storage_kind=env.object_store, | ||
) | ||
|
||
yield file_record | ||
|
||
|
||
@pytest.fixture | ||
def stored_file(elasticsearch_storage_handler, file) -> YieldFixture[File]: | ||
elasticsearch_storage_handler.write_item(file) | ||
yield file | ||
|
||
|
||
@pytest.fixture | ||
def bucket(s3_client): | ||
buckets = s3_client.list_buckets() | ||
if not any(bucket["Name"] == env.bucket_name for bucket in buckets["Buckets"]): | ||
s3_client.create_bucket(Bucket=env.bucket_name) | ||
yield env.bucket_name | ||
|
||
|
||
@pytest.fixture | ||
def file_pdf_path() -> YieldFixture[str]: | ||
path = os.path.join( | ||
os.path.dirname(os.path.abspath(__file__)), | ||
"..", "..", "tests", | ||
"data", | ||
"pdf", | ||
"Cabinet Office - Wikipedia.pdf", | ||
) | ||
yield path | ||
|
||
|
||
@pytest.fixture | ||
def rabbitmq_connection(): | ||
connection = env.blocking_connection() | ||
yield connection | ||
connection.close() | ||
|
||
|
||
@pytest.fixture | ||
def rabbitmq_channel(rabbitmq_connection): | ||
channel = rabbitmq_connection.channel() | ||
yield channel | ||
channel.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from threading import Event | ||
|
||
import pytest | ||
from elasticsearch import NotFoundError | ||
|
||
from core_api.src.app import env | ||
|
||
|
||
def test_get_health(app_client): | ||
""" | ||
Given that the app is running | ||
When I call /health | ||
I Expect to see the docs | ||
""" | ||
response = app_client.get("/health") | ||
assert response.status_code == 200 | ||
|
||
|
||
def test_post_file_upload(s3_client, app_client, elasticsearch_storage_handler, bucket, file_pdf_path): | ||
""" | ||
Given a new file | ||
When I POST it to /file | ||
I Expect to see it persisted in s3 and elastic-search | ||
""" | ||
with open(file_pdf_path, "rb") as f: | ||
response = app_client.post("/file", files={"file": ("filename", f, "pdf")}) | ||
assert response.status_code == 200 | ||
assert s3_client.get_object(Bucket=bucket, Key=file_pdf_path.split("/")[-1]) | ||
json_response = response.json() | ||
assert elasticsearch_storage_handler.read_item( | ||
item_uuid=json_response["uuid"], model_type=json_response["model_type"] | ||
) | ||
|
||
|
||
def test_get_file(app_client, stored_file): | ||
""" | ||
Given a previously saved file | ||
When I GET it from /file/uuid | ||
I Expect to receive it | ||
""" | ||
|
||
response = app_client.get(f"/file/{stored_file.uuid}") | ||
assert response.status_code == 200 | ||
|
||
|
||
def test_delete_file(s3_client, app_client, elasticsearch_storage_handler, bucket, stored_file): | ||
""" | ||
Given a previously saved file | ||
When I DELETE it to /file | ||
I Expect to see it removed from s3 and elastic-search | ||
""" | ||
# check assets exist | ||
assert s3_client.get_object(Bucket=bucket, Key=stored_file.name) | ||
assert elasticsearch_storage_handler.read_item(item_uuid=stored_file.uuid, model_type="file") | ||
|
||
response = app_client.delete(f"/file/{stored_file.uuid}") | ||
assert response.status_code == 200 | ||
|
||
# check assets dont exist | ||
with pytest.raises(Exception): | ||
s3_client.get_object(Bucket=bucket, Key=stored_file.name) | ||
|
||
with pytest.raises(NotFoundError): | ||
elasticsearch_storage_handler.read_item(item_uuid=stored_file.uuid, model_type="file") | ||
|
||
|
||
def test_ingest_file(app_client, rabbitmq_channel, stored_file): | ||
""" | ||
Given a previously saved file | ||
When I POST to /file/uuid/ingest | ||
I Expect to see a message on the ingest-queue, THIS IS NOT WORKING | ||
""" | ||
message_consumed = Event() | ||
|
||
def callback(ch, method, properties, body): | ||
message_consumed.set() | ||
ch.basic_ack(delivery_tag=method.delivery_tag) | ||
|
||
rabbitmq_channel.basic_consume(queue=env.ingest_queue_name, on_message_callback=callback) | ||
|
||
response = app_client.post(f"/file/{stored_file.uuid}/ingest/") | ||
assert response.status_code == 200 | ||
|
||
# TODO: fix this! | ||
# start_time = time.time() | ||
# while not message_consumed.is_set() and time.time() - start_time < 10: | ||
# time.sleep(0.1) | ||
# | ||
# assert message_consumed.is_set() | ||
# rabbitmq_channel.basic_cancel(consumer_tag=None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.