|
| 1 | +from fastapi import status |
| 2 | +from fastapi.testclient import TestClient |
| 3 | + |
| 4 | +from ragna.deploy import Config |
| 5 | +from ragna.deploy._api import app |
| 6 | + |
| 7 | +from .utils import authenticate |
| 8 | + |
| 9 | + |
| 10 | +def test_batch_sequential_upload_equivalence(tmp_local_root): |
| 11 | + "Check that uploading documents sequentially and in batch gives the same result" |
| 12 | + config = Config(local_root=tmp_local_root) |
| 13 | + |
| 14 | + document_root = config.local_root / "documents" |
| 15 | + document_root.mkdir() |
| 16 | + document_path1 = document_root / "test1.txt" |
| 17 | + with open(document_path1, "w") as file: |
| 18 | + file.write("!\n") |
| 19 | + document_path2 = document_root / "test2.txt" |
| 20 | + with open(document_path2, "w") as file: |
| 21 | + file.write("?\n") |
| 22 | + |
| 23 | + with TestClient( |
| 24 | + app(config=Config(), ignore_unavailable_components=False) |
| 25 | + ) as client: |
| 26 | + authenticate(client) |
| 27 | + |
| 28 | + document1_upload = ( |
| 29 | + client.post("/document", json={"name": document_path1.name}) |
| 30 | + .raise_for_status() |
| 31 | + .json() |
| 32 | + ) |
| 33 | + document2_upload = ( |
| 34 | + client.post("/document", json={"name": document_path2.name}) |
| 35 | + .raise_for_status() |
| 36 | + .json() |
| 37 | + ) |
| 38 | + |
| 39 | + documents_upload = ( |
| 40 | + client.post( |
| 41 | + "/documents", json={"names": [document_path1.name, document_path2.name]} |
| 42 | + ) |
| 43 | + .raise_for_status() |
| 44 | + .json() |
| 45 | + ) |
| 46 | + |
| 47 | + assert ( |
| 48 | + document1_upload["parameters"]["url"] |
| 49 | + == documents_upload[0]["parameters"]["url"] |
| 50 | + ) |
| 51 | + assert ( |
| 52 | + document2_upload["parameters"]["url"] |
| 53 | + == documents_upload[1]["parameters"]["url"] |
| 54 | + ) |
| 55 | + |
| 56 | + assert ( |
| 57 | + document1_upload["document"]["name"] |
| 58 | + == documents_upload[0]["document"]["name"] |
| 59 | + ) |
| 60 | + assert ( |
| 61 | + document2_upload["document"]["name"] |
| 62 | + == documents_upload[1]["document"]["name"] |
| 63 | + ) |
| 64 | + |
| 65 | + # assuming that if test passes for first document it will also pass for the other |
| 66 | + with open(document_path1, "rb") as file: |
| 67 | + response_sequential_upload1 = client.request( |
| 68 | + document1_upload["parameters"]["method"], |
| 69 | + document1_upload["parameters"]["url"], |
| 70 | + data=document1_upload["parameters"]["data"], |
| 71 | + files={"file": file}, |
| 72 | + ) |
| 73 | + response_batch_upload1 = client.request( |
| 74 | + documents_upload[0]["parameters"]["method"], |
| 75 | + documents_upload[0]["parameters"]["url"], |
| 76 | + data=documents_upload[0]["parameters"]["data"], |
| 77 | + files={"file": file}, |
| 78 | + ) |
| 79 | + |
| 80 | + assert response_sequential_upload1.status_code == status.HTTP_200_OK |
| 81 | + assert response_batch_upload1.status_code == status.HTTP_200_OK |
| 82 | + |
| 83 | + assert ( |
| 84 | + response_sequential_upload1.json()["name"] |
| 85 | + == response_batch_upload1.json()["name"] |
| 86 | + ) |
0 commit comments