test_rerank.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import pytest
  2. from utils import *
  3. server = ServerPreset.jina_reranker_tiny()
  4. @pytest.fixture(scope="module", autouse=True)
  5. def create_server():
  6. global server
  7. server = ServerPreset.jina_reranker_tiny()
  8. def test_rerank():
  9. global server
  10. server.start()
  11. res = server.make_request("POST", "/rerank", data={
  12. "query": "Machine learning is",
  13. "documents": [
  14. "A machine is a physical system that uses power to apply forces and control movement to perform an action. The term is commonly applied to artificial devices, such as those employing engines or motors, but also to natural biological macromolecules, such as molecular machines.",
  15. "Learning is the process of acquiring new understanding, knowledge, behaviors, skills, values, attitudes, and preferences. The ability to learn is possessed by humans, non-human animals, and some machines; there is also evidence for some kind of learning in certain plants.",
  16. "Machine learning is a field of study in artificial intelligence concerned with the development and study of statistical algorithms that can learn from data and generalize to unseen data, and thus perform tasks without explicit instructions.",
  17. "Paris, capitale de la France, est une grande ville européenne et un centre mondial de l'art, de la mode, de la gastronomie et de la culture. Son paysage urbain du XIXe siècle est traversé par de larges boulevards et la Seine."
  18. ]
  19. })
  20. assert res.status_code == 200
  21. assert len(res.body["results"]) == 4
  22. most_relevant = res.body["results"][0]
  23. least_relevant = res.body["results"][0]
  24. for doc in res.body["results"]:
  25. if doc["relevance_score"] > most_relevant["relevance_score"]:
  26. most_relevant = doc
  27. if doc["relevance_score"] < least_relevant["relevance_score"]:
  28. least_relevant = doc
  29. assert most_relevant["relevance_score"] > least_relevant["relevance_score"]
  30. assert most_relevant["index"] == 2
  31. assert least_relevant["index"] == 3
  32. @pytest.mark.parametrize("documents", [
  33. [],
  34. None,
  35. 123,
  36. [1, 2, 3],
  37. ])
  38. def test_invalid_rerank_req(documents):
  39. global server
  40. server.start()
  41. res = server.make_request("POST", "/rerank", data={
  42. "query": "Machine learning is",
  43. "documents": documents,
  44. })
  45. assert res.status_code == 400
  46. assert "error" in res.body